diff --git a/src/core/engines/noweb/session.noweb.core.ts b/src/core/engines/noweb/session.noweb.core.ts index b94c1410..9f32c7db 100644 --- a/src/core/engines/noweb/session.noweb.core.ts +++ b/src/core/engines/noweb/session.noweb.core.ts @@ -1101,38 +1101,54 @@ export class WhatsappSessionNoWebCore extends WhatsappSession { /** * Status methods */ - public sendTextStatus(status: TextStatus) { + public async sendTextStatus(status: TextStatus) { const message = { text: status.text }; - const JIDs = status.contacts.map(toJID); - this.upsertMeInJIDs(JIDs); + const jids = await this.prepareJidsForStatus(status.contacts); const options = { backgroundColor: status.backgroundColor, font: status.font, - statusJidList: JIDs, + statusJidList: jids, }; - return this.sock.sendMessage(BROADCAST_ID, message, options); + return await this.sock.sendMessage(BROADCAST_ID, message, options); } - public deleteStatus(request: DeleteStatusRequest) { + protected async prepareJidsForStatus(contacts: string[]) { + let jids: string[]; + if (contacts?.length > 0) { + jids = contacts.map(toJID); + } else { + jids = await this.fetchMyContactsJids(); + } + this.upsertMeInJIDs(jids); + return jids; + } + + protected async fetchMyContactsJids() { + const contacts = await this.store.getContacts({}); + const jids = contacts.map((contact) => contact.id); + return jids.filter((jid) => jid.endsWith('@s.whatsapp.net')); + } + + public async deleteStatus(request: DeleteStatusRequest) { const messageId = request.id; const key = parseMessageIdSerialized(messageId, true); key.fromMe = true; key.remoteJid = BROADCAST_ID; - const JIDs = request.contacts.map(toJID); + const jids = await this.prepareJidsForStatus(request.contacts); const options = { - statusJidList: JIDs, + statusJidList: jids, }; - return this.sock.sendMessage(BROADCAST_ID, { delete: key }, options); + return await this.sock.sendMessage(BROADCAST_ID, { delete: key }, options); } - protected upsertMeInJIDs(JIDs: string[]) { + protected upsertMeInJIDs(jids: string[]) { if (!this.sock?.authState?.creds?.me) { return; } const myJID = jidNormalizedUser(this.sock.authState.creds.me.id); - if (!JIDs.includes(myJID)) { - JIDs.push(myJID); + if (!jids.includes(myJID)) { + jids.push(myJID); } } diff --git a/src/structures/status.dto.ts b/src/structures/status.dto.ts index 1e51578f..ea08259b 100644 --- a/src/structures/status.dto.ts +++ b/src/structures/status.dto.ts @@ -11,22 +11,27 @@ import { export const BROADCAST_ID = 'status@broadcast'; -export class StatusRequest { - @ApiProperty({ - description: 'Contact list to send the status to.', - example: ['55xxxxxxxxxxx@c.us'], - }) - contacts = ['55xxxxxxxxxxx@c.us']; +const ContactsProperty = ApiProperty({ + description: 'Contact list to send the status to.', + example: null, + required: false, +}); + +export interface StatusRequest { + contacts?: string[]; } -export class TextStatus extends StatusRequest { +export class TextStatus { text: string = 'Have a look! https://github.com/'; backgroundColor: string = '#38b42f'; font: number = 1; + + @ContactsProperty + contacts?: string[]; } @ApiExtraModels(RemoteFile, BinaryFile) -export class ImageStatus extends StatusRequest { +export class ImageStatus { @ApiProperty({ oneOf: [ { $ref: getSchemaPath(RemoteFile) }, @@ -36,10 +41,13 @@ export class ImageStatus extends StatusRequest { file: RemoteFile | BinaryFile; caption?: string; + + @ContactsProperty + contacts?: string[]; } @ApiExtraModels(VoiceRemoteFile, VoiceBinaryFile) -export class VoiceStatus extends StatusRequest { +export class VoiceStatus { @ApiProperty({ oneOf: [ { $ref: getSchemaPath(VoiceRemoteFile) }, @@ -49,10 +57,13 @@ export class VoiceStatus extends StatusRequest { file: VoiceRemoteFile | VoiceBinaryFile; backgroundColor: string = '#38b42f'; + + @ContactsProperty + contacts?: string[]; } @ApiExtraModels(VideoRemoteFile, VideoBinaryFile) -export class VideoStatus extends StatusRequest { +export class VideoStatus { @ApiProperty({ oneOf: [ { $ref: getSchemaPath(VideoRemoteFile) }, @@ -62,12 +73,18 @@ export class VideoStatus extends StatusRequest { file: VideoRemoteFile | VideoBinaryFile; caption?: string; + + @ContactsProperty + contacts?: string[]; } -export class DeleteStatusRequest extends StatusRequest { +export class DeleteStatusRequest { @ApiProperty({ description: 'status message id', example: 'AAAAAAAAAAAAAAAAA', }) id: string; + + @ContactsProperty + contacts?: string[]; }