diff --git a/src/api/chatting.controller.ts b/src/api/chatting.controller.ts index 561612af..caa8eeed 100644 --- a/src/api/chatting.controller.ts +++ b/src/api/chatting.controller.ts @@ -5,7 +5,6 @@ import { Post, Put, Query, - UnprocessableEntityException, UsePipes, ValidationPipe, } from '@nestjs/common'; @@ -45,20 +44,10 @@ import { WANumberExistResult, } from '../structures/chatting.dto'; import { WAMessage } from '../structures/responses.dto'; -import { isJidGroup } from '@waha/core/utils/jids'; - -function validateRequestMentions(request: MessageTextRequest) { - if (!isJidGroup(request.chatId)) { - throw new UnprocessableEntityException( - `"mentions":["all"] can be used only in group chats, not in '${request.chatId}'`, - ); - } - if (request.mentions.length > 1) { - throw new UnprocessableEntityException( - `"mentions":["all"] cannot be used with other mentions`, - ); - } -} +import { + mentionsAll, + validateRequestMentions, +} from '@waha/core/utils/mentions.all'; @ApiSecurity('api_key') @Controller('api') @@ -70,14 +59,9 @@ export class ChattingController { @ApiOperation({ summary: 'Send a text message' }) async sendText(@Body() request: MessageTextRequest): Promise { const whatsapp = await this.manager.getWorkingSession(request.session); - if (request.mentions && request.mentions.includes('all')) { + if (mentionsAll(request)) { validateRequestMentions(request); - const participants = await whatsapp.getGroupParticipants(request.chatId); - let mentions = participants.map((p) => p.id); - // Exclude my ids - const me = whatsapp.getSessionMeInfo(); - mentions = mentions.filter((id) => id !== me.id && id !== me.lid); - request.mentions = mentions; + request.mentions = await whatsapp.resolveMentionsAll(request.chatId); } return whatsapp.sendText(request); } @@ -90,6 +74,10 @@ export class ChattingController { }) async sendImage(@Body() request: MessageImageRequest) { const whatsapp = await this.manager.getWorkingSession(request.session); + if (mentionsAll(request)) { + validateRequestMentions(request); + request.mentions = await whatsapp.resolveMentionsAll(request.chatId); + } return whatsapp.sendImage(request); } @@ -101,6 +89,10 @@ export class ChattingController { }) async sendFile(@Body() request: MessageFileRequest) { const whatsapp = await this.manager.getWorkingSession(request.session); + if (mentionsAll(request)) { + validateRequestMentions(request); + request.mentions = await whatsapp.resolveMentionsAll(request.chatId); + } return whatsapp.sendFile(request); } @@ -123,6 +115,10 @@ export class ChattingController { }) async sendVideo(@Body() request: MessageVideoRequest) { const whatsapp = await this.manager.getWorkingSession(request.session); + if (mentionsAll(request)) { + validateRequestMentions(request); + request.mentions = await whatsapp.resolveMentionsAll(request.chatId); + } return whatsapp.sendVideo(request); } diff --git a/src/core/abc/session.abc.ts b/src/core/abc/session.abc.ts index 7408822d..c0426445 100644 --- a/src/core/abc/session.abc.ts +++ b/src/core/abc/session.abc.ts @@ -6,6 +6,7 @@ import { MessagesForRead } from '@waha/core/utils/convertors'; import { IgnoreJidConfig, isJidBroadcast, + isJidGroup, isJidNewsletter, isNullJid, JidFilter, @@ -1198,6 +1199,14 @@ export abstract class WhatsappSession { public fetch(url: string): Promise { return fetchBuffer(url); } + + public async resolveMentionsAll(chatId: string): Promise { + const participants = await this.getGroupParticipants(chatId); + let mentions = participants.map((p) => p.id); + // Exclude my ids + const me = this.getSessionMeInfo(); + return mentions.filter((id) => id !== me.id && id !== me.lid); + } } export function getGroupInviteLink(code: string) { diff --git a/src/core/utils/mentions.all.ts b/src/core/utils/mentions.all.ts new file mode 100644 index 00000000..486e070d --- /dev/null +++ b/src/core/utils/mentions.all.ts @@ -0,0 +1,24 @@ +import { isJidGroup } from '@waha/core/utils/jids'; +import { UnprocessableEntityException } from '@nestjs/common'; + +const ALL = 'all'; + +export function mentionsAll(request: { mentions?: string[] }) { + return request.mentions && request.mentions.includes(ALL); +} + +export function validateRequestMentions(request: { + chatId: string; + mentions?: string[]; +}) { + if (!isJidGroup(request.chatId)) { + throw new UnprocessableEntityException( + `"mentions":["all"] can be used only in group chats, not in '${request.chatId}'`, + ); + } + if (request.mentions.length > 1) { + throw new UnprocessableEntityException( + `"mentions":["all"] cannot be used with other mentions`, + ); + } +} diff --git a/src/structures/chatting.dto.ts b/src/structures/chatting.dto.ts index 6ad38ca9..dbd2b7cb 100644 --- a/src/structures/chatting.dto.ts +++ b/src/structures/chatting.dto.ts @@ -303,6 +303,9 @@ class FileRequest extends ChatRequest { export class MessageImageRequest extends FileRequest { caption?: string; + @ApiHideProperty() + mentions?: string[]; + @ReplyToProperty() reply_to?: string; } @@ -310,6 +313,9 @@ export class MessageImageRequest extends FileRequest { export class MessageFileRequest extends FileRequest { caption?: string; + @ApiHideProperty() + mentions?: string[]; + @ReplyToProperty() reply_to?: string; } @@ -343,6 +349,9 @@ export class MessageVideoRequest extends ChatRequest { caption?: string = 'Just watch at this!'; + @ApiHideProperty() + mentions?: string[]; + @ApiProperty({ description: 'The ID of the message to reply to - false_11111111111@c.us_AAAAAAAAAAAAAAAAAAAA',