Skip to content

Commit

Permalink
Merge pull request #2788 from ghayman/edit-delete-message-callbacks
Browse files Browse the repository at this point in the history
Edit / delete message callbacks
  • Loading branch information
orkestral authored Jul 23, 2024
2 parents 58cf1f7 + e756ed3 commit 11ada49
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 6 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,17 @@ client.onAnyMessage(message => {

// Listen to messages
client.onMessage(message => {
...
...
})

// Listen for messages that have been edited
client.onMessageEdit(message => {
...
})

// Listen for messages that have been deleted
client.onMessageDelete(message => {
...
})

// Listen to state changes
Expand Down
2 changes: 2 additions & 0 deletions src/api/helpers/exposed.enum.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export enum ExposedFn {
OnMessage = 'onMessage',
OnMessageEdit = 'onMessageEdit',
OnMessageDelete = 'onMessageDelete',
OnAnyMessage = 'onAnyMessage',
onAck = 'onAck',
onParticipantsChanged = 'onParticipantsChanged',
Expand Down
68 changes: 65 additions & 3 deletions src/api/layers/listener.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ declare global {
interface Window {
onMessage: any;
onAnyMessage: any;
onMessageEdit: any;
onMessageDelete: any;
onStateChange: any;
onIncomingCall: any;
onAck: any;
Expand Down Expand Up @@ -75,7 +77,7 @@ export class ListenerLayer extends ProfileLayer {
}
}

this.addMsg();
await this.addMsg();
await this.page
.evaluate(() => {
window.WAPI.onInterfaceChange((e: any) => {
Expand Down Expand Up @@ -104,6 +106,12 @@ export class ListenerLayer extends ProfileLayer {
window.WAPI.onAck((e: any) => {
window.onAck(e);
});
window.WAPI.onMessageEdit((e: any) => {
window.onMessageEdit(e);
});
window.WAPI.onMessageDelete((e: any) => {
window.onMessageDelete(e);
});
window.WAPI.onPoll((e: any) => {
window.onPoll(e);
});
Expand All @@ -129,6 +137,25 @@ export class ListenerLayer extends ProfileLayer {
}
}
});

window.Store.Msg.on(
'change:body change:caption',
async (newMessage) => {
if (newMessage && newMessage.isNewMsg) {
const processMessageObj = await window.WAPI.processMessageObj(
newMessage,
true,
false
);

if (newMessage.type == 'revoked') {
window.onMessageDelete(processMessageObj);
} else {
window.onMessageEdit(processMessageObj);
}
}
}
);
// } catch { }
})
.catch(() => {});
Expand All @@ -150,8 +177,7 @@ export class ListenerLayer extends ProfileLayer {

/**
* @event Listens to all new messages
* @param to callback
* @fires Message
* @param fn
*/
public async onAnyMessage(fn: (message: Message) => void) {
this.listenerEmitter.on(ExposedFn.OnAnyMessage, (msg) => {
Expand All @@ -167,6 +193,42 @@ export class ListenerLayer extends ProfileLayer {
};
}

/**
* @event Listens for edited message
* @param fn
*/
public async onMessageEdit(fn: (message: Message) => void) {
this.listenerEmitter.on(ExposedFn.OnMessageEdit, (msg) => {
fn(msg);
});

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

/**
* @event Listens for deleted message
* @param fn
*/
public async onMessageDelete(fn: (message: Message) => void) {
this.listenerEmitter.on(ExposedFn.OnMessageDelete, (msg) => {
fn(msg);
});

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

/**
* @event Listens to messages received
* @returns Observable stream of messages
Expand Down
4 changes: 2 additions & 2 deletions src/config/puppeteer.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ const puppeteerConfig = {
'--password-store=basic',
'--use-mock-keychain',
'--enable-blink-features=IdleDetection',
'--export-tagged-pdf',
'about:blank'
'--export-tagged-pdf'
// 'about:blank'
]
};

Expand Down
2 changes: 2 additions & 0 deletions src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
interface WAPI {
addParticipant: (groupId: string, contactId: string | string[]) => boolean;
onAnyMessage: (callback: Function) => void;
onMessageEdit: (callback: Function) => void;
onMessageDelete: (callback: Function) => void;
archiveChat: (chatId: string, option: boolean) => boolean;
arrayBufferToBase64: (buffer: ArrayBuffer) => string;
blockContact: (messageId: string) => boolean;
Expand Down

0 comments on commit 11ada49

Please sign in to comment.