Skip to content

Commit

Permalink
[core] NOWEB - get contacts list for status from store if not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
devlikepro committed Nov 10, 2024
1 parent 65b61c0 commit 3865254
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
40 changes: 28 additions & 12 deletions src/core/engines/noweb/session.noweb.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
39 changes: 28 additions & 11 deletions src/structures/status.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,27 @@ import {

export const BROADCAST_ID = 'status@broadcast';

export class StatusRequest {
@ApiProperty({
description: 'Contact list to send the status to.',
example: ['[email protected]'],
})
contacts = ['[email protected]'];
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) },
Expand All @@ -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) },
Expand All @@ -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) },
Expand All @@ -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[];
}

0 comments on commit 3865254

Please sign in to comment.