Skip to content

Commit

Permalink
feat: Added msg_revoke (WPP.chat.on) event
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Oct 12, 2021
1 parent 799eee9 commit d6303bf
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
39 changes: 39 additions & 0 deletions src/chat/Chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,48 @@ export class Chat extends Emittery<ChatEventTypes> {
debugChat(Constants.COLLECTION_HAS_SYNCED);
});

this.registerEvents();

debugChat('initialized');
}

protected registerEvents() {
/**
* processMultipleMessages receive all msgs events before the screen processing,
* so for some events, like revoke, is called here and not in MsgStore,
* because the message is not in MsgStore
*/
const processMultipleMessages = MsgStore.processMultipleMessages;

MsgStore.processMultipleMessages = async (
chatId: Wid,
msgs: RawMessage[],
...args: any[]
) => {
// try...catch to avoid screen block
try {
for (const msg of msgs) {
if (!msg.isNewMsg) {
continue;
}

if (msg.type === 'protocol' && msg.subtype === 'revoke') {
this.emit('msg_revoke', {
author: msg.author,
from: msg.from!,
id: msg.id!,
refId: msg.protocolMessageKey!,
to: msg.to!,
});
}
}
} catch (error) {}

// Call the original method
return processMultipleMessages.call(MsgStore, chatId, msgs, ...args);
};
}

async find(chatId: string | Wid): Promise<ChatModel> {
const wid = assertWid(chatId);
return findChat(wid);
Expand Down
18 changes: 17 additions & 1 deletion src/chat/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,27 @@
* limitations under the License.
*/

import { ModelPropertiesContructor, MsgModel } from '../whatsapp';
import { ModelPropertiesContructor, MsgKey, MsgModel, Wid } from '../whatsapp';

export interface ChatEventTypes {
change: string;
idle: undefined;
msg_revoke: {
/**
* Author of message, only for groups
*/
author?: Wid;
from: Wid;
/**
* Message id of revoke event
*/
id: MsgKey;
/**
* Message id of revoked message
*/
refId: MsgKey;
to: Wid;
};
}

export interface GetMessagesOptions {
Expand Down
10 changes: 8 additions & 2 deletions src/whatsapp/collections/MsgCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
* limitations under the License.
*/

import { Wid } from '..';
import { exportModule } from '../exportModule';
import { MsgModel } from '../models';
import { ModelPropertiesContructor, MsgModel } from '../models';
import { CollectionCache } from './CollectionCache';

/**
Expand Down Expand Up @@ -43,7 +44,12 @@ export declare class MsgCollection extends CollectionCache<MsgModel> {
resyncReceipts(): any;
updateLastReceipt(e?: any, t?: any): any;
updateLastTransactionTime(e?: any, t?: any): any;
processMultipleMessages(e?: any, t?: any, r?: any, a?: any): any;
processMultipleMessages(
chatId: Wid,
msgs: ModelPropertiesContructor<MsgModel>[],
r?: any,
a?: any
): Promise<MsgModel[]>;
markAllAsStale(): any;
}

Expand Down
2 changes: 1 addition & 1 deletion src/whatsapp/models/MsgModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface Props {
notifyName?: any;
from?: Wid;
to?: Wid;
author?: any;
author?: Wid;
self?: string;
/**
* See {@link Constants}
Expand Down

0 comments on commit d6303bf

Please sign in to comment.