Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/api/controllers/chat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DeleteMessage,
getBase64FromMediaMessageDto,
MarkChatUnreadDto,
MarkMessageAsPlayedDto,
NumberDto,
PrivacySettingDto,
ProfileNameDto,
Expand All @@ -30,6 +31,10 @@ export class ChatController {
return await this.waMonitor.waInstances[instanceName].markMessageAsRead(data);
}

public async markMessageAsPlayed({ instanceName }: InstanceDto, data: MarkMessageAsPlayedDto) {
return await this.waMonitor.waInstances[instanceName].markMessageAsPlayed(data);
}

public async archiveChat({ instanceName }: InstanceDto, data: ArchiveChatDto) {
return await this.waMonitor.waInstances[instanceName].archiveChat(data);
}
Expand Down
4 changes: 4 additions & 0 deletions src/api/dto/chat.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export class ReadMessageDto {
readMessages: Key[];
}

export class MarkMessageAsPlayedDto {
playedMessages: Key[];
}

export class LastMessage {
key: Key;
messageTimestamp?: number;
Expand Down
19 changes: 19 additions & 0 deletions src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getBase64FromMediaMessageDto,
LastMessage,
MarkChatUnreadDto,
MarkMessageAsPlayedDto,
NumberBusiness,
OnWhatsAppDto,
PrivacySettingDto,
Expand Down Expand Up @@ -3688,6 +3689,24 @@ export class BaileysStartupService extends ChannelStartupService {
}
}

public async markMessageAsPlayed(data: MarkMessageAsPlayedDto) {
try {
const keys: proto.IMessageKey[] = [];
data.playedMessages.forEach((played) => {
if (isJidGroup(played.remoteJid) || isPnUser(played.remoteJid)) {
keys.push({ remoteJid: played.remoteJid, fromMe: played.fromMe, id: played.id });
}
});
// Baileys exposes sendReceipts(keys, type) where type='played' triggers the
// PLAYED ack (blue microphone). Used when an agent plays back an audio
// message received from a contact, mirroring the contact's view in WhatsApp.
await this.client.sendReceipts(keys, 'played');
return { message: 'Played messages', played: 'success' };
} catch (error) {
throw new InternalServerErrorException('Mark messages as played fail', error.toString());
}
}

public async getLastMessage(number: string) {
const where: any = { key: { remoteJid: number }, instanceId: this.instance.id };

Expand Down
12 changes: 12 additions & 0 deletions src/api/routes/chat.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
DeleteMessage,
getBase64FromMediaMessageDto,
MarkChatUnreadDto,
MarkMessageAsPlayedDto,
NumberDto,
PrivacySettingDto,
ProfileNameDto,
Expand All @@ -25,6 +26,7 @@ import {
contactValidateSchema,
deleteMessageSchema,
markChatUnreadSchema,
markMessageAsPlayedSchema,
messageUpSchema,
messageValidateSchema,
presenceSchema,
Expand Down Expand Up @@ -70,6 +72,16 @@ export class ChatRouter extends RouterBroker {

return res.status(HttpStatus.CREATED).json(response);
})
.post(this.routerPath('markMessageAsPlayed'), ...guards, async (req, res) => {
const response = await this.dataValidate<MarkMessageAsPlayedDto>({
request: req,
schema: markMessageAsPlayedSchema,
ClassRef: MarkMessageAsPlayedDto,
execute: (instance, data) => chatController.markMessageAsPlayed(instance, data),
});

return res.status(HttpStatus.CREATED).json(response);
})
.post(this.routerPath('archiveChat'), ...guards, async (req, res) => {
const response = await this.dataValidate<ArchiveChatDto>({
request: req,
Expand Down
22 changes: 22 additions & 0 deletions src/validate/chat.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,28 @@ export const readMessageSchema: JSONSchema7 = {
required: ['readMessages'],
};

export const markMessageAsPlayedSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
properties: {
playedMessages: {
type: 'array',
minItems: 1,
uniqueItems: true,
items: {
properties: {
id: { type: 'string' },
fromMe: { type: 'boolean', enum: [true, false] },
remoteJid: { type: 'string' },
},
required: ['id', 'fromMe', 'remoteJid'],
...isNotEmpty('id', 'remoteJid'),
},
},
},
required: ['playedMessages'],
};

export const archiveChatSchema: JSONSchema7 = {
$id: v4(),
type: 'object',
Expand Down