refactor: channels design to allow attachment ownership

This commit is contained in:
Mohamed Marrouchi 2025-01-15 15:46:50 +01:00
parent e859cf1930
commit 39213e8b0b
11 changed files with 184 additions and 53 deletions

View File

@ -149,13 +149,13 @@ export class Attachment extends AttachmentStub {
@Schema({ timestamps: true }) @Schema({ timestamps: true })
export class UserAttachmentFull extends AttachmentStub { export class UserAttachmentFull extends AttachmentStub {
@Type(() => User) @Type(() => User)
owner: User | null; owner: User | undefined;
} }
@Schema({ timestamps: true }) @Schema({ timestamps: true })
export class SubscriberAttachmentFull extends AttachmentStub { export class SubscriberAttachmentFull extends AttachmentStub {
@Type(() => Subscriber) @Type(() => Subscriber)
owner: Subscriber | null; owner: Subscriber | undefined;
} }
export type AttachmentDocument = THydratedDocument<Attachment>; export type AttachmentDocument = THydratedDocument<Attachment>;

View File

@ -30,6 +30,7 @@ import { BaseService } from '@/utils/generics/base-service';
import { AttachmentMetadataDto } from '../dto/attachment.dto'; import { AttachmentMetadataDto } from '../dto/attachment.dto';
import { AttachmentRepository } from '../repositories/attachment.repository'; import { AttachmentRepository } from '../repositories/attachment.repository';
import { Attachment } from '../schemas/attachment.schema'; import { Attachment } from '../schemas/attachment.schema';
import { TAttachmentContext } from '../types';
import { import {
fileExists, fileExists,
generateUniqueFilename, generateUniqueFilename,
@ -156,6 +157,18 @@ export class AttachmentService extends BaseService<Attachment> {
} }
} }
/**
* Get the attachment root directory given the context
*
* @param context The attachment context
* @returns The root directory path
*/
getRootDirByContext(context: TAttachmentContext) {
return context === 'subscriber_avatar' || context === 'user_avatar'
? config.parameters.avatarDir
: config.parameters.uploadDir;
}
/** /**
* Uploads files to the server. If a storage plugin is configured it uploads files accordingly. * Uploads files to the server. If a storage plugin is configured it uploads files accordingly.
* Otherwise, uploads files to the local directory. * Otherwise, uploads files to the local directory.
@ -168,16 +181,12 @@ export class AttachmentService extends BaseService<Attachment> {
async store( async store(
file: Buffer | Stream | Readable | Express.Multer.File, file: Buffer | Stream | Readable | Express.Multer.File,
metadata: AttachmentMetadataDto, metadata: AttachmentMetadataDto,
rootDir = config.parameters.uploadDir,
): Promise<Attachment | undefined> { ): Promise<Attachment | undefined> {
if (this.getStoragePlugin()) { if (this.getStoragePlugin()) {
const storedDto = await this.getStoragePlugin()?.store?.( const storedDto = await this.getStoragePlugin()?.store?.(file, metadata);
file,
metadata,
rootDir,
);
return storedDto ? await this.create(storedDto) : undefined; return storedDto ? await this.create(storedDto) : undefined;
} else { } else {
const rootDir = this.getRootDirByContext(metadata.context);
const uniqueFilename = generateUniqueFilename(metadata.name); const uniqueFilename = generateUniqueFilename(metadata.name);
const filePath = resolve(join(rootDir, sanitizeFilename(uniqueFilename))); const filePath = resolve(join(rootDir, sanitizeFilename(uniqueFilename)));
@ -225,10 +234,7 @@ export class AttachmentService extends BaseService<Attachment> {
* @param rootDir - The root directory where attachment shoud be located. * @param rootDir - The root directory where attachment shoud be located.
* @returns A promise that resolves to a StreamableFile representing the downloaded attachment. * @returns A promise that resolves to a StreamableFile representing the downloaded attachment.
*/ */
async download( async download(attachment: Attachment): Promise<StreamableFile> {
attachment: Attachment,
rootDir = config.parameters.uploadDir,
): Promise<StreamableFile> {
if (this.getStoragePlugin()) { if (this.getStoragePlugin()) {
const streamableFile = const streamableFile =
await this.getStoragePlugin()?.download(attachment); await this.getStoragePlugin()?.download(attachment);
@ -238,6 +244,7 @@ export class AttachmentService extends BaseService<Attachment> {
return streamableFile; return streamableFile;
} else { } else {
const rootDir = this.getRootDirByContext(attachment.context);
const path = resolve(join(rootDir, attachment.location)); const path = resolve(join(rootDir, attachment.location));
if (!fileExists(path)) { if (!fileExists(path)) {

View File

@ -6,6 +6,8 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/ */
import { Readable, Stream } from 'stream';
/** /**
* Defines the types of owners for an attachment, * Defines the types of owners for an attachment,
* indicating whether the file belongs to a User or a Subscriber. * indicating whether the file belongs to a User or a Subscriber.
@ -31,3 +33,25 @@ export enum AttachmentContext {
} }
export type TAttachmentContext = `${AttachmentContext}`; export type TAttachmentContext = `${AttachmentContext}`;
export class AttachmentFile {
/**
* File original file name
*/
file: Buffer | Stream | Readable | Express.Multer.File;
/**
* File original file name
*/
name?: string;
/**
* File size in bytes
*/
size: number;
/**
* File MIME type
*/
type: string;
}

View File

@ -6,6 +6,7 @@
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). * 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/ */
import { Attachment } from '@/attachment/schemas/attachment.schema';
import { Subscriber } from '@/chat/schemas/subscriber.schema'; import { Subscriber } from '@/chat/schemas/subscriber.schema';
import { AttachmentPayload } from '@/chat/schemas/types/attachment'; import { AttachmentPayload } from '@/chat/schemas/types/attachment';
import { SubscriberChannelData } from '@/chat/schemas/types/channel'; import { SubscriberChannelData } from '@/chat/schemas/types/channel';
@ -29,19 +30,24 @@ export default abstract class EventWrapper<
eventType: StdEventType; eventType: StdEventType;
messageType?: IncomingMessageType; messageType?: IncomingMessageType;
raw: E; raw: E;
attachments?: Attachment[];
}, },
E, E,
N extends ChannelName = ChannelName, N extends ChannelName = ChannelName,
C extends ChannelHandler = ChannelHandler<N>, C extends ChannelHandler = ChannelHandler<N>,
S = SubscriberChannelDict[N], S = SubscriberChannelDict[N],
> { > {
_adapter: A = { raw: {}, eventType: StdEventType.unknown } as A; _adapter: A = {
raw: {},
eventType: StdEventType.unknown,
attachments: undefined,
} as A;
_handler: C; _handler: C;
channelAttrs: S; channelAttrs: S;
_profile!: Subscriber; subscriber!: Subscriber;
_nlp!: NLU.ParseEntities; _nlp!: NLU.ParseEntities;
@ -177,7 +183,7 @@ export default abstract class EventWrapper<
* @returns event sender data * @returns event sender data
*/ */
getSender(): Subscriber { getSender(): Subscriber {
return this._profile; return this.subscriber;
} }
/** /**
@ -186,7 +192,7 @@ export default abstract class EventWrapper<
* @param profile - Sender data * @param profile - Sender data
*/ */
setSender(profile: Subscriber) { setSender(profile: Subscriber) {
this._profile = profile; this.subscriber = profile;
} }
/** /**
@ -194,9 +200,13 @@ export default abstract class EventWrapper<
* *
* Child class can perform operations such as storing files as attachments. * Child class can perform operations such as storing files as attachments.
*/ */
preprocess() { async preprocess() {
// Nothing ... if (
return Promise.resolve(); this._adapter.eventType === StdEventType.message &&
this._adapter.messageType === IncomingMessageType.attachments
) {
await this._handler.persistMessageAttachments(this);
}
} }
/** /**

View File

@ -17,12 +17,17 @@ import {
import { JwtService, JwtSignOptions } from '@nestjs/jwt'; import { JwtService, JwtSignOptions } from '@nestjs/jwt';
import { plainToClass } from 'class-transformer'; import { plainToClass } from 'class-transformer';
import { NextFunction, Request, Response } from 'express'; import { NextFunction, Request, Response } from 'express';
import mime from 'mime';
import { v4 as uuidv4 } from 'uuid';
import { Attachment } from '@/attachment/schemas/attachment.schema'; import { Attachment } from '@/attachment/schemas/attachment.schema';
import { AttachmentService } from '@/attachment/services/attachment.service'; import { AttachmentService } from '@/attachment/services/attachment.service';
import { AttachmentFile } from '@/attachment/types';
import { SubscriberCreateDto } from '@/chat/dto/subscriber.dto'; import { SubscriberCreateDto } from '@/chat/dto/subscriber.dto';
import { AttachmentRef } from '@/chat/schemas/types/attachment'; import { AttachmentRef } from '@/chat/schemas/types/attachment';
import { import {
IncomingMessageType,
StdEventType,
StdOutgoingEnvelope, StdOutgoingEnvelope,
StdOutgoingMessage, StdOutgoingMessage,
} from '@/chat/schemas/types/message'; } from '@/chat/schemas/types/message';
@ -50,7 +55,7 @@ export default abstract class ChannelHandler<
private readonly settings: ChannelSetting<N>[]; private readonly settings: ChannelSetting<N>[];
@Inject(AttachmentService) @Inject(AttachmentService)
protected readonly attachmentService: AttachmentService; public readonly attachmentService: AttachmentService;
@Inject(JwtService) @Inject(JwtService)
protected readonly jwtService: JwtService; protected readonly jwtService: JwtService;
@ -206,15 +211,62 @@ export default abstract class ChannelHandler<
): Promise<{ mid: string }>; ): Promise<{ mid: string }>;
/** /**
* Fetch the end user profile data * Calls the channel handler to fetch attachments and stores them
*
* @param event
* @returns An attachment array
*/
getMessageAttachments?(
event: EventWrapper<any, any, N>,
): Promise<AttachmentFile[]>;
/**
* Fetch the subscriber profile data
* @param event - The message event received * @param event - The message event received
* @returns {Promise<Subscriber>} - The channel's response, otherwise an error * @returns {Promise<Subscriber>} - The channel's response, otherwise an error
*/ */
abstract getUserData( getSubscriberAvatar?(
event: EventWrapper<any, any, N>,
): Promise<AttachmentFile | undefined>;
/**
* Fetch the subscriber profile data
*
* @param event - The message event received
* @returns {Promise<Subscriber>} - The channel's response, otherwise an error
*/
abstract getSubscriberData(
event: EventWrapper<any, any, N>, event: EventWrapper<any, any, N>,
): Promise<SubscriberCreateDto>; ): Promise<SubscriberCreateDto>;
/**
* Persist Message attachments
*
* @returns Resolves the promise once attachments are fetched and stored
*/
async persistMessageAttachments(event: EventWrapper<any, any, N>) {
if (
event._adapter.eventType === StdEventType.message &&
event._adapter.messageType === IncomingMessageType.attachments &&
this.getMessageAttachments
) {
const metadatas = await this.getMessageAttachments(event);
const subscriber = event.getSender();
event._adapter.attachments = await Promise.all(
metadatas.map(({ file, name, type, size }) => {
return this.attachmentService.store(file, {
name: `${name ? `${name}-` : ''}${uuidv4()}.${mime.extension(type)}`,
type,
size,
context: 'message_attachment',
ownerType: 'Subscriber',
owner: subscriber.id,
});
}),
);
}
}
/** /**
* Custom channel middleware * Custom channel middleware
* @param req * @param req

View File

@ -20,7 +20,6 @@ import {
import { CsrfCheck } from '@tekuconcept/nestjs-csrf'; import { CsrfCheck } from '@tekuconcept/nestjs-csrf';
import { AttachmentService } from '@/attachment/services/attachment.service'; import { AttachmentService } from '@/attachment/services/attachment.service';
import { config } from '@/config';
import { CsrfInterceptor } from '@/interceptors/csrf.interceptor'; import { CsrfInterceptor } from '@/interceptors/csrf.interceptor';
import { LoggerService } from '@/logger/logger.service'; import { LoggerService } from '@/logger/logger.service';
import { BaseController } from '@/utils/generics/base-controller'; import { BaseController } from '@/utils/generics/base-controller';
@ -159,10 +158,7 @@ export class SubscriberController extends BaseController<
throw new Error('User has no avatar'); throw new Error('User has no avatar');
} }
return await this.attachmentService.download( return await this.attachmentService.download(subscriber.avatar);
subscriber.avatar,
config.parameters.avatarDir,
);
} catch (err) { } catch (err) {
this.logger.verbose( this.logger.verbose(
'Subscriber has no avatar, generating initials avatar ...', 'Subscriber has no avatar, generating initials avatar ...',

View File

@ -8,7 +8,10 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { EventEmitter2, OnEvent } from '@nestjs/event-emitter'; import { EventEmitter2, OnEvent } from '@nestjs/event-emitter';
import mime from 'mime';
import { v4 as uuidv4 } from 'uuid';
import { AttachmentService } from '@/attachment/services/attachment.service';
import EventWrapper from '@/channel/lib/EventWrapper'; import EventWrapper from '@/channel/lib/EventWrapper';
import { config } from '@/config'; import { config } from '@/config';
import { HelperService } from '@/helper/helper.service'; import { HelperService } from '@/helper/helper.service';
@ -36,6 +39,7 @@ export class ChatService {
private readonly botService: BotService, private readonly botService: BotService,
private readonly websocketGateway: WebsocketGateway, private readonly websocketGateway: WebsocketGateway,
private readonly helperService: HelperService, private readonly helperService: HelperService,
private readonly attachmentService: AttachmentService,
) {} ) {}
/** /**
@ -248,7 +252,7 @@ export class ChatService {
}); });
if (!subscriber) { if (!subscriber) {
const subscriberData = await handler.getUserData(event); const subscriberData = await handler.getSubscriberData(event);
this.eventEmitter.emit('hook:stats:entry', 'new_users', 'New users'); this.eventEmitter.emit('hook:stats:entry', 'new_users', 'New users');
subscriberData.channel = event.getChannelData(); subscriberData.channel = event.getChannelData();
subscriber = await this.subscriberService.create(subscriberData); subscriber = await this.subscriberService.create(subscriberData);
@ -260,9 +264,47 @@ export class ChatService {
this.websocketGateway.broadcastSubscriberUpdate(subscriber); this.websocketGateway.broadcastSubscriberUpdate(subscriber);
// Retrieve and store the subscriber avatar
if (handler.getSubscriberAvatar) {
try {
const metadata = await handler.getSubscriberAvatar(event);
if (metadata) {
const { file, type, size } = metadata;
const extension = mime.extension(type);
const avatar = await this.attachmentService.store(file, {
name: `avatar-${uuidv4()}.${extension}`,
size,
type,
context: 'subscriber_avatar',
ownerType: 'Subscriber',
owner: subscriber.id,
});
if (avatar) {
subscriber = await this.subscriberService.updateOne(
subscriber.id,
{
avatar: avatar.id,
},
);
}
}
} catch (err) {
this.logger.error(
`Unable to retrieve avatar for subscriber ${subscriber.id}`,
err,
);
}
}
// Set the subscriber object
event.setSender(subscriber); event.setSender(subscriber);
await event.preprocess(); // Preprocess the event (persist attachments, ...)
if (event.preprocess) {
await event.preprocess();
}
// Trigger message received event // Trigger message received event
this.eventEmitter.emit('hook:chatbot:received', event); this.eventEmitter.emit('hook:chatbot:received', event);

View File

@ -70,7 +70,7 @@ export default abstract class BaseWebChannelHandler<
protected readonly eventEmitter: EventEmitter2, protected readonly eventEmitter: EventEmitter2,
protected readonly i18n: I18nService, protected readonly i18n: I18nService,
protected readonly subscriberService: SubscriberService, protected readonly subscriberService: SubscriberService,
protected readonly attachmentService: AttachmentService, public readonly attachmentService: AttachmentService,
protected readonly messageService: MessageService, protected readonly messageService: MessageService,
protected readonly menuService: MenuService, protected readonly menuService: MenuService,
protected readonly websocketGateway: WebsocketGateway, protected readonly websocketGateway: WebsocketGateway,
@ -1321,7 +1321,9 @@ export default abstract class BaseWebChannelHandler<
* *
* @returns The web's response, otherwise an error * @returns The web's response, otherwise an error
*/ */
async getUserData(event: WebEventWrapper<N>): Promise<SubscriberCreateDto> { async getSubscriberData(
event: WebEventWrapper<N>,
): Promise<SubscriberCreateDto> {
const sender = event.getSender(); const sender = event.getSender();
const { const {
id: _id, id: _id,

View File

@ -37,10 +37,7 @@ export abstract class BaseStoragePlugin extends BasePlugin {
/** @deprecated use store() instead */ /** @deprecated use store() instead */
uploadAvatar?(file: Express.Multer.File): Promise<any>; uploadAvatar?(file: Express.Multer.File): Promise<any>;
abstract download( abstract download(attachment: Attachment): Promise<StreamableFile>;
attachment: Attachment,
rootLocation?: string,
): Promise<StreamableFile>;
/** @deprecated use download() instead */ /** @deprecated use download() instead */
downloadProfilePic?(name: string): Promise<StreamableFile>; downloadProfilePic?(name: string): Promise<StreamableFile>;
@ -52,6 +49,5 @@ export abstract class BaseStoragePlugin extends BasePlugin {
store?( store?(
file: Buffer | Stream | Readable | Express.Multer.File, file: Buffer | Stream | Readable | Express.Multer.File,
metadata: AttachmentMetadataDto, metadata: AttachmentMetadataDto,
rootDir?: string,
): Promise<Attachment>; ): Promise<Attachment>;
} }

View File

@ -110,10 +110,7 @@ export class ReadOnlyUserController extends BaseController<
throw new Error('User has no avatar'); throw new Error('User has no avatar');
} }
return await this.attachmentService.download( return await this.attachmentService.download(user.avatar);
user.avatar,
config.parameters.avatarDir,
);
} catch (err) { } catch (err) {
this.logger.verbose( this.logger.verbose(
'User has no avatar, generating initials avatar ...', 'User has no avatar, generating initials avatar ...',
@ -293,18 +290,14 @@ export class ReadWriteUserController extends ReadOnlyUserController {
// Upload Avatar if provided // Upload Avatar if provided
const avatar = avatarFile const avatar = avatarFile
? await this.attachmentService.store( ? await this.attachmentService.store(avatarFile, {
avatarFile, name: avatarFile.originalname,
{ size: avatarFile.size,
name: avatarFile.originalname, type: avatarFile.mimetype,
size: avatarFile.size, context: 'user_avatar',
type: avatarFile.mimetype, ownerType: 'User',
context: 'user_avatar', owner: req.user.id,
ownerType: 'User', })
owner: req.user.id,
},
config.parameters.avatarDir,
)
: undefined; : undefined;
const result = await this.userService.updateOne( const result = await this.userService.updateOne(

View File

@ -0,0 +1,9 @@
/*
* Copyright © 2025 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
export type PartialExcept<T, K extends keyof T> = Partial<T> & Pick<T, K>;