Skip to content

Commit

Permalink
Merge pull request #2799 from ghayman/reaction-callback
Browse files Browse the repository at this point in the history
Added onMessageReaction() listener and callback
  • Loading branch information
ghayman authored Sep 4, 2024
2 parents 11ada49 + 79da152 commit 8d5ab50
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/api/helpers/exposed.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export enum ExposedFn {
OnMessage = 'onMessage',
OnMessageEdit = 'onMessageEdit',
OnMessageDelete = 'onMessageDelete',
OnMessageReaction = 'onMessageReaction',
OnAnyMessage = 'onAnyMessage',
onAck = 'onAck',
onParticipantsChanged = 'onParticipantsChanged',
Expand Down
40 changes: 38 additions & 2 deletions src/api/layers/listener.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Chat,
LiveLocation,
Message,
Reaction,
ParticipantEvent,
PicTumb,
ChatStatus
Expand All @@ -24,6 +25,7 @@ declare global {
onAnyMessage: any;
onMessageEdit: any;
onMessageDelete: any;
onMessageReaction: any;
onStateChange: any;
onIncomingCall: any;
onAck: any;
Expand Down Expand Up @@ -112,6 +114,9 @@ export class ListenerLayer extends ProfileLayer {
window.WAPI.onMessageDelete((e: any) => {
window.onMessageDelete(e);
});
window.WAPI.onMessageReaction((e: any) => {
window.onMessageReaction(e);
});
window.WAPI.onPoll((e: any) => {
window.onPoll(e);
});
Expand All @@ -123,7 +128,8 @@ export class ListenerLayer extends ProfileLayer {
this.page
.evaluate(() => {
let isHeroEqual = {};
// try {

// Install the new message listener (add event)
window.Store.Msg.on('add', async (newMessage) => {
if (!Object.is(isHeroEqual, newMessage)) {
isHeroEqual = newMessage;
Expand All @@ -138,6 +144,7 @@ export class ListenerLayer extends ProfileLayer {
}
});

// Install the changed message / deleted message listener (change:body change:caption events)
window.Store.Msg.on(
'change:body change:caption',
async (newMessage) => {
Expand All @@ -148,6 +155,7 @@ export class ListenerLayer extends ProfileLayer {
false
);

// Edit or Delete?
if (newMessage.type == 'revoked') {
window.onMessageDelete(processMessageObj);
} else {
Expand All @@ -156,7 +164,17 @@ export class ListenerLayer extends ProfileLayer {
}
}
);
// } catch { }

// Install the message reaction listener
// This is a strange one - seems like the way to do it is to override the WhatsApp WAWebAddonReactionTableMode.reactionTableMode.bulkUpsert function
const module = window.Store.Reaction.reactionTableMode;
const ogMethod = module.bulkUpsert;
module.bulkUpsert = ((...args) => {
if (args[0].length > 0) {
window.onMessageReaction(args[0][0]);
}
return ogMethod(...args);
}).bind(module);
})
.catch(() => {});
}
Expand Down Expand Up @@ -229,6 +247,24 @@ export class ListenerLayer extends ProfileLayer {
};
}

/**
* @event Listens for reactions to messages
* @param fn
*/
public async onMessageReaction(fn: (reaction: Reaction) => void) {
this.listenerEmitter.on(ExposedFn.OnMessageReaction, (reaction) => {
fn(reaction);
});

return {
dispose: () => {
this.listenerEmitter.off(ExposedFn.OnMessageReaction, (reaction) => {
fn(reaction);
});
}
};
}

/**
* @event Listens to messages received
* @returns Observable stream of messages
Expand Down
1 change: 1 addition & 0 deletions src/api/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { Contact } from './contact';
export { GroupMetadata } from './group-metadata';
export { Id } from './id';
export { Message } from './message';
export { Reaction } from './reaction';
export { ParticipantEvent } from './participant-event';
export { PartialMessage } from './partial-message';
export { Ack } from './ack';
Expand Down
25 changes: 25 additions & 0 deletions src/api/model/reaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export interface Reaction {
id: ReactionKey;
from: string;
to: string;
type: string;
t: number;
ack: number;
author: string;
notifyName: string;
invis: boolean;
count: number;
kind: string;
reactionParentKey: ReactionKey;
reactionText: string;
reactionTimestamp: number;
read: boolean;
}

export interface ReactionKey {
fromMe: boolean;
remote: string;
id: string;
participant: string;
_serialized: string;
}
4 changes: 4 additions & 0 deletions src/lib/wapi/store/store-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ export const storeObjects = [
id: 'Reactions',
conditions: (module) => (module.sendReactionToMsg ? module : null),
},
{
id: 'Reaction',
conditions: (module) => (module.reactionTableMode ? module : null),
},
{
id: 'CheckWid',
conditions: (module) => (module.validateWid ? module : null),
Expand Down
1 change: 1 addition & 0 deletions src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface WAPI {
onAnyMessage: (callback: Function) => void;
onMessageEdit: (callback: Function) => void;
onMessageDelete: (callback: Function) => void;
onMessageReaction: (callback: Function) => void;
archiveChat: (chatId: string, option: boolean) => boolean;
arrayBufferToBase64: (buffer: ArrayBuffer) => string;
blockContact: (messageId: string) => boolean;
Expand Down

0 comments on commit 8d5ab50

Please sign in to comment.