feat: minor updates to accomodate slack

This commit is contained in:
Mohamed Marrouchi 2024-12-16 12:10:45 +01:00
parent b8d8bfeaa6
commit df8904c46a
5 changed files with 49 additions and 16 deletions

View File

@ -96,13 +96,12 @@ export class AttachmentService extends BaseService<Attachment> {
* @param res - The response object from which the profile picture will be buffered or piped.
* @param filename - The filename
*/
async uploadProfilePic(res: fetch.Response, filename: string) {
async uploadProfilePic(data: Buffer | fetch.Response, filename: string) {
if (this.getStoragePlugin()) {
// Upload profile picture
const buffer = await res.buffer();
const picture = {
originalname: filename,
buffer,
buffer: Buffer.isBuffer(data) ? data : await data.buffer(),
} as Express.Multer.File;
try {
await this.getStoragePlugin().uploadAvatar(picture);
@ -122,12 +121,19 @@ export class AttachmentService extends BaseService<Attachment> {
} else {
// Save profile picture locally
const dirPath = path.join(config.parameters.avatarDir, filename);
try {
// Ensure the directory exists
await fs.promises.mkdir(config.parameters.avatarDir, {
recursive: true,
}); // Ensure the directory exists
const dest = fs.createWriteStream(dirPath);
res.body.pipe(dest);
});
if (Buffer.isBuffer(data)) {
await fs.promises.writeFile(dirPath, data);
} else {
const dest = fs.createWriteStream(dirPath);
data.body.pipe(dest);
}
this.logger.debug(
'Messenger Channel Handler : Profile picture fetched successfully',
);
@ -204,4 +210,22 @@ export class AttachmentService extends BaseService<Attachment> {
});
}
}
/**
* Downloads an attachment identified by the provided parameters as a Buffer.
*
* @param attachment - The attachment to download.
* @returns A promise that resolves to a Buffer representing the downloaded attachment.
*/
async readAsBuffer(attachment: Attachment): Promise<Buffer> {
if (this.getStoragePlugin()) {
return await this.getStoragePlugin().readAsBuffer(attachment);
} else {
if (!fileExists(attachment.location)) {
throw new NotFoundException('No file was found');
}
const filePath = join(config.parameters.uploadDir, attachment.location);
return await fs.promises.readFile(filePath); // Reads the file content as a Buffer
}
}
}

View File

@ -28,13 +28,17 @@ export interface ChannelEvent {}
// eslint-disable-next-line prettier/prettier
export default abstract class EventWrapper<
A,
A extends {
eventType: StdEventType;
messageType?: IncomingMessageType;
raw: E;
},
E,
N extends ChannelName = ChannelName,
C extends ChannelHandler = ChannelHandler<N>,
S = SubscriberChannelDict[N],
> {
_adapter: A = {} as A;
_adapter: A = { raw: {}, eventType: StdEventType.unknown } as A;
_handler: C;
@ -54,7 +58,7 @@ export default abstract class EventWrapper<
* @param event - The message event received
* @param channelAttrs - Channel's specific data
*/
constructor(handler: C, event: E, channelAttrs: S = {} as S) {
constructor(handler: C, event: A['raw'], channelAttrs: S = {} as S) {
this._handler = handler;
this._init(event);
this.channelAttrs = channelAttrs;
@ -66,7 +70,7 @@ export default abstract class EventWrapper<
handler: this._handler.getName(),
channelData: this.getChannelData(),
sender: this.getSender(),
recipient: this.getRecipientForeignId(),
// recipient: this.getRecipientForeignId(),
eventType: this.getEventType(),
messageType: this.getMessageType(),
payload: this.getPayload(),
@ -90,7 +94,7 @@ export default abstract class EventWrapper<
*- `_adapter.raw` : Sets a typed object of the event raw data
* @param event - The message event received from a given channel
*/
abstract _init(event: E): void;
abstract _init(event: A['raw']): void;
/**
* Retrieves the current channel handler.
@ -198,14 +202,18 @@ export default abstract class EventWrapper<
*
* @returns The type of event received (message, delivery, read, ...)
*/
abstract getEventType(): StdEventType;
getEventType(): StdEventType {
return this._adapter.eventType;
}
/**
* Identifies the type of the message received
*
* @return The type of message
*/
abstract getMessageType(): IncomingMessageType;
getMessageType(): IncomingMessageType | undefined {
return this._adapter.messageType;
}
/**
* Return payload whenever user clicks on a button/quick_reply or sends an attachment, false otherwise

View File

@ -212,7 +212,7 @@ export class ChatService {
mid: event.getId(),
recipient: recipient.id,
message: event.getMessage(),
delivery: false,
delivery: true,
read: false,
};

View File

@ -88,8 +88,7 @@ export class NlpSampleService extends BaseService<
await this.findOneOrCreate(record, record);
this.logger.debug('User message saved as a inbox sample !');
} catch (err) {
this.logger.error('Unable to add message as a new inbox sample!', err);
throw err;
this.logger.warn('Unable to add message as a new inbox sample!', err);
}
}
}

View File

@ -32,4 +32,6 @@ export abstract class BaseStoragePlugin extends BasePlugin {
abstract download(attachment: Attachment): Promise<StreamableFile>;
abstract downloadProfilePic(name: string): Promise<StreamableFile>;
readAsBuffer?(attachment: Attachment): Promise<Buffer>;
}