feat: add the access attribute

This commit is contained in:
Mohamed Marrouchi 2025-01-16 06:46:41 +01:00
parent 0b4a1085ec
commit 505cd247a1
13 changed files with 66 additions and 8 deletions

View File

@ -137,6 +137,7 @@ describe('AttachmentController', () => {
name: attachmentFile.originalname, name: attachmentFile.originalname,
location: expect.stringMatching(new RegExp(`^/${name}`)), location: expect.stringMatching(new RegExp(`^/${name}`)),
context: 'block_attachment', context: 'block_attachment',
access: 'public',
createdByRef: 'User', createdByRef: 'User',
createdBy: '9'.repeat(24), createdBy: '9'.repeat(24),
}); });

View File

@ -130,7 +130,7 @@ export class AttachmentController extends BaseController<Attachment> {
async uploadFile( async uploadFile(
@UploadedFiles() files: { file: Express.Multer.File[] }, @UploadedFiles() files: { file: Express.Multer.File[] },
@Req() req: Request, @Req() req: Request,
@Query() { context }: AttachmentContextParamDto, @Query() { context, access = 'public' }: AttachmentContextParamDto,
): Promise<Attachment[]> { ): Promise<Attachment[]> {
if (!files || !Array.isArray(files?.file) || files.file.length === 0) { if (!files || !Array.isArray(files?.file) || files.file.length === 0) {
throw new BadRequestException('No file was selected'); throw new BadRequestException('No file was selected');
@ -150,6 +150,7 @@ export class AttachmentController extends BaseController<Attachment> {
size: file.size, size: file.size,
type: file.mimetype, type: file.mimetype,
context, context,
access,
createdBy: userId, createdBy: userId,
createdByRef: 'User', createdByRef: 'User',
}); });

View File

@ -24,8 +24,10 @@ import { ObjectIdDto } from '@/utils/dto/object-id.dto';
import { IsObjectId } from '@/utils/validation-rules/is-object-id'; import { IsObjectId } from '@/utils/validation-rules/is-object-id';
import { import {
AttachmentAccess,
AttachmentContext, AttachmentContext,
AttachmentCreatedByRef, AttachmentCreatedByRef,
TAttachmentAccess,
TAttachmentContext, TAttachmentContext,
TAttachmentCreatedByRef, TAttachmentCreatedByRef,
} from '../types'; } from '../types';
@ -67,7 +69,7 @@ export class AttachmentMetadataDto {
/** /**
* Attachment context * Attachment context
*/ */
@ApiPropertyOptional({ @ApiProperty({
description: 'Attachment Context', description: 'Attachment Context',
enum: Object.values(AttachmentContext), enum: Object.values(AttachmentContext),
}) })
@ -79,7 +81,7 @@ export class AttachmentMetadataDto {
/** /**
* Attachment Owner Type * Attachment Owner Type
*/ */
@ApiPropertyOptional({ @ApiProperty({
description: 'Attachment Owner Type', description: 'Attachment Owner Type',
enum: Object.values(AttachmentCreatedByRef), enum: Object.values(AttachmentCreatedByRef),
}) })
@ -88,12 +90,24 @@ export class AttachmentMetadataDto {
@IsIn(Object.values(AttachmentCreatedByRef)) @IsIn(Object.values(AttachmentCreatedByRef))
createdByRef: TAttachmentCreatedByRef; createdByRef: TAttachmentCreatedByRef;
/**
* Attachment Access
*/
@ApiProperty({
description: 'Attachment Access',
enum: Object.values(AttachmentAccess),
})
@IsString()
@IsNotEmpty()
@IsIn(Object.values(AttachmentAccess))
access: TAttachmentAccess;
/** /**
* Attachment Owner : Subscriber or User ID * Attachment Owner : Subscriber or User ID
*/ */
@ApiPropertyOptional({ @ApiProperty({
description: 'Attachment Owner : Subscriber / User ID', description: 'Attachment Owner : Subscriber / User ID',
enum: Object.values(AttachmentContext), type: String,
}) })
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
@ -126,7 +140,7 @@ export class AttachmentDownloadDto extends ObjectIdDto {
} }
export class AttachmentContextParamDto { export class AttachmentContextParamDto {
@ApiPropertyOptional({ @ApiProperty({
description: 'Attachment Context', description: 'Attachment Context',
enum: Object.values(AttachmentContext), enum: Object.values(AttachmentContext),
}) })
@ -134,4 +148,13 @@ export class AttachmentContextParamDto {
@IsIn(Object.values(AttachmentContext)) @IsIn(Object.values(AttachmentContext))
@IsNotEmpty() @IsNotEmpty()
context: TAttachmentContext; context: TAttachmentContext;
@ApiPropertyOptional({
description: 'Attachment Access',
enum: Object.values(AttachmentAccess),
})
@IsString()
@IsIn(Object.values(AttachmentAccess))
@IsOptional()
access?: TAttachmentAccess;
} }

View File

@ -17,6 +17,7 @@ export const attachment: Attachment = {
location: location:
'/Screenshot from 2022-03-11 08-41-27-2a9799a8b6109c88fd9a7a690c1101934c.png', '/Screenshot from 2022-03-11 08-41-27-2a9799a8b6109c88fd9a7a690c1101934c.png',
context: 'block_attachment', context: 'block_attachment',
access: 'public',
id: '65940d115178607da65c82b6', id: '65940d115178607da65c82b6',
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
@ -47,6 +48,7 @@ export const attachments: Attachment[] = [
'/app/src/attachment/uploads/Screenshot from 2022-03-11 08-41-27-2a9799a8b6109c88fd9a7a690c1101934c.png', '/app/src/attachment/uploads/Screenshot from 2022-03-11 08-41-27-2a9799a8b6109c88fd9a7a690c1101934c.png',
channel: { ['some-channel']: {} }, channel: { ['some-channel']: {} },
context: 'block_attachment', context: 'block_attachment',
access: 'public',
id: '65940d115178607da65c82b7', id: '65940d115178607da65c82b7',
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
@ -61,6 +63,7 @@ export const attachments: Attachment[] = [
'/app/src/attachment/uploads/Screenshot from 2022-03-18 08-58-15-af61e7f71281f9fd3f1ad7ad10107741c.png', '/app/src/attachment/uploads/Screenshot from 2022-03-18 08-58-15-af61e7f71281f9fd3f1ad7ad10107741c.png',
channel: { ['some-channel']: {} }, channel: { ['some-channel']: {} },
context: 'block_attachment', context: 'block_attachment',
access: 'public',
id: '65940d115178607da65c82b8', id: '65940d115178607da65c82b8',
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),

View File

@ -24,8 +24,10 @@ import {
} from '@/utils/types/filter.types'; } from '@/utils/types/filter.types';
import { import {
AttachmentAccess,
AttachmentContext, AttachmentContext,
AttachmentCreatedByRef, AttachmentCreatedByRef,
TAttachmentAccess,
TAttachmentContext, TAttachmentContext,
TAttachmentCreatedByRef, TAttachmentCreatedByRef,
} from '../types'; } from '../types';
@ -100,6 +102,12 @@ export class AttachmentStub extends BaseSchema {
@Prop({ type: String, enum: Object.values(AttachmentContext) }) @Prop({ type: String, enum: Object.values(AttachmentContext) })
context: TAttachmentContext; context: TAttachmentContext;
/**
* Context of the attachment
*/
@Prop({ type: String, enum: Object.values(AttachmentAccess) })
access: TAttachmentAccess;
/** /**
* Optional property representing the URL of the attachment. * Optional property representing the URL of the attachment.
* *

View File

@ -34,6 +34,13 @@ export enum AttachmentContext {
export type TAttachmentContext = `${AttachmentContext}`; export type TAttachmentContext = `${AttachmentContext}`;
export enum AttachmentAccess {
Public = 'public',
Private = 'private',
}
export type TAttachmentAccess = `${AttachmentAccess}`;
export class AttachmentFile { export class AttachmentFile {
/** /**
* File original file name * File original file name

View File

@ -259,6 +259,7 @@ export default abstract class ChannelHandler<
type, type,
size, size,
context: 'message_attachment', context: 'message_attachment',
access: 'private',
createdByRef: 'Subscriber', createdByRef: 'Subscriber',
createdBy: subscriber.id, createdBy: subscriber.id,
}); });

View File

@ -89,6 +89,7 @@ const attachment: Attachment = {
}, },
}, },
context: 'block_attachment', context: 'block_attachment',
access: 'public',
createdByRef: 'User', createdByRef: 'User',
createdBy: null, createdBy: null,
createdAt: new Date(), createdAt: new Date(),

View File

@ -281,6 +281,7 @@ export class ChatService {
size, size,
type, type,
context: 'subscriber_avatar', context: 'subscriber_avatar',
access: 'private',
createdByRef: 'Subscriber', createdByRef: 'Subscriber',
createdBy: subscriber.id, createdBy: subscriber.id,
}); });

View File

@ -626,6 +626,7 @@ export default abstract class BaseWebChannelHandler<
size: Buffer.byteLength(data.file), size: Buffer.byteLength(data.file),
type: data.type, type: data.type,
context: 'message_attachment', context: 'message_attachment',
access: 'private',
createdByRef: 'Subscriber', createdByRef: 'Subscriber',
createdBy: req.session?.web?.profile?.id, createdBy: req.session?.web?.profile?.id,
}); });
@ -692,6 +693,7 @@ export default abstract class BaseWebChannelHandler<
size: file.size, size: file.size,
type: file.mimetype, type: file.mimetype,
context: 'message_attachment', context: 'message_attachment',
access: 'private',
createdByRef: 'Subscriber', createdByRef: 'Subscriber',
createdBy: req.session.web.profile?.id, createdBy: req.session.web.profile?.id,
}); });

View File

@ -80,6 +80,7 @@ const populateBlockAttachments = async ({ logger }: MigrationServices) => {
{ {
$set: { $set: {
context: AttachmentContext.BlockAttachment, context: AttachmentContext.BlockAttachment,
access: 'public',
createdByRef: AttachmentCreatedByRef.User, createdByRef: AttachmentCreatedByRef.User,
createdBy: user._id, createdBy: user._id,
}, },
@ -130,6 +131,7 @@ const populateSettingAttachments = async ({ logger }: MigrationServices) => {
{ {
$set: { $set: {
context: AttachmentContext.SettingAttachment, context: AttachmentContext.SettingAttachment,
access: 'public',
createdByRef: AttachmentCreatedByRef.User, createdByRef: AttachmentCreatedByRef.User,
createdBy: user._id, createdBy: user._id,
}, },
@ -168,6 +170,7 @@ const populateUserAvatars = async ({ logger }: MigrationServices) => {
{ {
$set: { $set: {
context: AttachmentContext.UserAvatar, context: AttachmentContext.UserAvatar,
access: 'private',
createdByRef: AttachmentCreatedByRef.User, createdByRef: AttachmentCreatedByRef.User,
createdBy: user._id, createdBy: user._id,
}, },
@ -229,6 +232,7 @@ const populateSubscriberAvatars = async ({ logger }: MigrationServices) => {
{ {
$set: { $set: {
context: AttachmentContext.SubscriberAvatar, context: AttachmentContext.SubscriberAvatar,
access: 'private',
createdByRef: AttachmentCreatedByRef.Subscriber, createdByRef: AttachmentCreatedByRef.Subscriber,
createdBy: subscriber._id, createdBy: subscriber._id,
}, },
@ -361,6 +365,7 @@ const undoPopulateAttachments = async ({ logger }: MigrationServices) => {
{ {
$unset: { $unset: {
context: '', context: '',
access: '',
createdByRef: '', createdByRef: '',
createdBy: '', createdBy: '',
}, },
@ -640,9 +645,10 @@ const migrateAndPopulateAttachmentMessages = async ({
await attachmentService.updateOne( await attachmentService.updateOne(
msg.message.attachment.payload.attachment_id as string, msg.message.attachment.payload.attachment_id as string,
{ {
context: 'message_attachment',
access: 'private',
createdByRef: msg.sender ? 'Subscriber' : 'User', createdByRef: msg.sender ? 'Subscriber' : 'User',
createdBy: msg.sender ? msg.sender : adminUser.id, createdBy: msg.sender ? msg.sender : adminUser.id,
context: 'message_attachment',
}, },
); );
// Rename `attachment_id` to `id` // Rename `attachment_id` to `id`
@ -672,9 +678,10 @@ const migrateAndPopulateAttachmentMessages = async ({
size: fileBuffer.length, size: fileBuffer.length,
type: response.headers['content-type'], type: response.headers['content-type'],
channel: {}, channel: {},
context: 'message_attachment',
access: msg.sender ? 'private' : 'public',
createdBy: msg.sender ? msg.sender : adminUser.id, createdBy: msg.sender ? msg.sender : adminUser.id,
createdByRef: msg.sender ? 'Subscriber' : 'User', createdByRef: msg.sender ? 'Subscriber' : 'User',
context: 'message_attachment',
}); });
if (attachment) { if (attachment) {

View File

@ -295,6 +295,7 @@ export class ReadWriteUserController extends ReadOnlyUserController {
size: avatarFile.size, size: avatarFile.size,
type: avatarFile.mimetype, type: avatarFile.mimetype,
context: 'user_avatar', context: 'user_avatar',
access: 'private',
createdByRef: 'User', createdByRef: 'User',
createdBy: req.user.id, createdBy: req.user.id,
}) })

View File

@ -23,6 +23,7 @@ export const attachmentFixtures: AttachmentCreateDto[] = [
}, },
}, },
context: 'content_attachment', context: 'content_attachment',
access: 'public',
createdByRef: 'User', createdByRef: 'User',
createdBy: '9'.repeat(24), createdBy: '9'.repeat(24),
}, },
@ -37,6 +38,7 @@ export const attachmentFixtures: AttachmentCreateDto[] = [
}, },
}, },
context: 'content_attachment', context: 'content_attachment',
access: 'public',
createdByRef: 'User', createdByRef: 'User',
createdBy: '9'.repeat(24), createdBy: '9'.repeat(24),
}, },