From 572d0f1dd60bc1c8e8ea7913d487e60a681485da Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Sun, 11 Dec 2022 15:48:26 +0530 Subject: [PATCH] feat: implement tc token handling TC token is required in certain cases to receive presence updates on chats --- src/Socket/chats.ts | 17 +++++++++++++++-- src/Socket/messages-recv.ts | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Socket/chats.ts b/src/Socket/chats.ts index 6056d13..ee34528 100644 --- a/src/Socket/chats.ts +++ b/src/Socket/chats.ts @@ -494,14 +494,27 @@ export const makeChatsSocket = (config: SocketConfig) => { } } - const presenceSubscribe = (toJid: string) => ( + /** + * @param toJid the jid to subscribe to + * @param tcToken token for subscription, use if present + */ + const presenceSubscribe = (toJid: string, tcToken?: Buffer) => ( sendNode({ tag: 'presence', attrs: { to: toJid, id: generateMessageTag(), type: 'subscribe' - } + }, + content: tcToken + ? [ + { + tag: 'tctoken', + attrs: { }, + content: tcToken + } + ] + : undefined }) ) diff --git a/src/Socket/messages-recv.ts b/src/Socket/messages-recv.ts index 75a95d1..7310b4d 100644 --- a/src/Socket/messages-recv.ts +++ b/src/Socket/messages-recv.ts @@ -268,6 +268,21 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => { const from = jidNormalizedUser(node.attrs.from) switch (nodeType) { + case 'privacy_token': + const tokenList = getBinaryNodeChildren(child, 'token') + for(const { attrs, content } of tokenList) { + const jid = attrs.jid + ev.emit('chats.update', [ + { + id: jid, + tcToken: content as Buffer + } + ]) + + logger.debug({ jid }, 'got privacy token update') + } + + break case 'w:gp2': handleGroupNotification(node.attrs.participant, child, result) break