Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added onMessageReaction() listener and callback #2799

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading