fix: avatar update rate

This commit is contained in:
Mohamed Marrouchi 2025-05-12 19:12:13 +01:00
parent c56f176d85
commit 33f6483e2e
3 changed files with 30 additions and 24 deletions

View File

@ -254,6 +254,7 @@ export class ChatService {
let subscriber = await this.subscriberService.findOne({ let subscriber = await this.subscriberService.findOne({
foreign_id: foreignId, foreign_id: foreignId,
}); });
if (!subscriber) { if (!subscriber) {
const subscriberData = await handler.getSubscriberData(event); const subscriberData = await handler.getSubscriberData(event);
subscriberData.channel = event.getChannelData(); subscriberData.channel = event.getChannelData();
@ -262,19 +263,17 @@ export class ChatService {
if (!subscriber) { if (!subscriber) {
throw new Error('Unable to create a new subscriber'); throw new Error('Unable to create a new subscriber');
} }
}
event.setSender(subscriber);
// Exec lastvisit hook
this.eventEmitter.emit('hook:user:lastvisit', subscriber);
this.websocketGateway.broadcastSubscriberUpdate(subscriber);
// Retrieve and store the subscriber avatar // Retrieve and store the subscriber avatar
// @TODO: We need to handle the avatar update (based on the lastvisit?)
if (handler.getSubscriberAvatar) { if (handler.getSubscriberAvatar) {
try { try {
const file = await handler.getSubscriberAvatar(event); const file = await handler.getSubscriberAvatar(event);
if (file) { if (file) {
await this.subscriberService.storeAvatar(subscriber.id, file); subscriber = await this.subscriberService.storeAvatar(
subscriber.id,
file,
);
} }
} catch (err) { } catch (err) {
this.logger.error( this.logger.error(
@ -283,6 +282,12 @@ export class ChatService {
); );
} }
} }
}
event.setSender(subscriber);
// Exec lastvisit hook
this.eventEmitter.emit('hook:user:lastvisit', subscriber);
this.websocketGateway.broadcastSubscriberUpdate(subscriber);
// Set the subscriber object // Set the subscriber object
event.setSender(subscriber!); event.setSender(subscriber!);

View File

@ -178,11 +178,11 @@ describe('SubscriberService', () => {
const fakeAttachment = { id: '9'.repeat(24) } as Attachment; const fakeAttachment = { id: '9'.repeat(24) } as Attachment;
jest.spyOn(attachmentService, 'store').mockResolvedValue(fakeAttachment); jest.spyOn(attachmentService, 'store').mockResolvedValue(fakeAttachment);
const updateOneSpy = jest
.spyOn(subscriberService, 'updateOne')
.mockResolvedValue(allSubscribers[0]);
await subscriberService.storeAvatar(subscriber.id, avatarPayload); const result = await subscriberService.storeAvatar(
subscriber.id,
avatarPayload,
);
expect(attachmentService.store).toHaveBeenCalledTimes(1); expect(attachmentService.store).toHaveBeenCalledTimes(1);
expect(attachmentService.store).toHaveBeenCalledWith( expect(attachmentService.store).toHaveBeenCalledWith(
@ -198,9 +198,7 @@ describe('SubscriberService', () => {
}), }),
); );
expect(updateOneSpy).toHaveBeenCalledWith(subscriber.id, { expect(result.avatar).toBe(fakeAttachment.id);
avatar: fakeAttachment.id,
});
}); });
it('should propagate an error from AttachmentService and leave the subscriber unchanged', async () => { it('should propagate an error from AttachmentService and leave the subscriber unchanged', async () => {

View File

@ -156,7 +156,10 @@ export class SubscriberService extends BaseService<
* *
* @returns Resolves once the subscriber avatar is stored * @returns Resolves once the subscriber avatar is stored
*/ */
async storeAvatar(subscriberId: string, avatar: AttachmentFile) { async storeAvatar(
subscriberId: string,
avatar: AttachmentFile,
): Promise<Subscriber> {
const { file, type, size } = avatar; const { file, type, size } = avatar;
const extension = mime.extension(type); const extension = mime.extension(type);
@ -170,7 +173,7 @@ export class SubscriberService extends BaseService<
createdBy: subscriberId, createdBy: subscriberId,
}); });
await this.updateOne(subscriberId, { return await this.updateOne(subscriberId, {
avatar: attachment.id, avatar: attachment.id,
}); });
} }