feat: send tctoken to profile update and presence subscribe (#2257)
* fix: improve message resend logic by adding checks for message IDs * Revert "fix: improve message resend logic by adding checks for message IDs" This reverts commit c03f9d8e6fc6cbfbb9d1f8f67c169700e704213d. * feat(tc-token): implement buildTcTokenFromJid utility and integrate into chats socket * fix(tc-token): ensure consistent return value when tcTokenBuffer is absent * fix(chats): update import path for buildTcTokenFromJid utility
This commit is contained in:
+12
-13
@@ -54,6 +54,7 @@ import {
|
|||||||
} from '../WABinary'
|
} from '../WABinary'
|
||||||
import { USyncQuery, USyncUser } from '../WAUSync'
|
import { USyncQuery, USyncUser } from '../WAUSync'
|
||||||
import { makeSocket } from './socket.js'
|
import { makeSocket } from './socket.js'
|
||||||
|
import { buildTcTokenFromJid } from '../Utils/tc-token-utils'
|
||||||
const MAX_SYNC_ATTEMPTS = 2
|
const MAX_SYNC_ATTEMPTS = 2
|
||||||
|
|
||||||
export const makeChatsSocket = (config: SocketConfig) => {
|
export const makeChatsSocket = (config: SocketConfig) => {
|
||||||
@@ -612,7 +613,10 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
* type = "image for the high res picture"
|
* type = "image for the high res picture"
|
||||||
*/
|
*/
|
||||||
const profilePictureUrl = async (jid: string, type: 'preview' | 'image' = 'preview', timeoutMs?: number) => {
|
const profilePictureUrl = async (jid: string, type: 'preview' | 'image' = 'preview', timeoutMs?: number) => {
|
||||||
// TOOD: Add support for tctoken, existingID, and newsletter + group options
|
const baseContent: BinaryNode[] = [{ tag: 'picture', attrs: { type, query: 'url' } }]
|
||||||
|
|
||||||
|
const tcTokenContent = await buildTcTokenFromJid({ authState, jid, baseContent })
|
||||||
|
|
||||||
jid = jidNormalizedUser(jid)
|
jid = jidNormalizedUser(jid)
|
||||||
const result = await query(
|
const result = await query(
|
||||||
{
|
{
|
||||||
@@ -623,7 +627,7 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
type: 'get',
|
type: 'get',
|
||||||
xmlns: 'w:profile:picture'
|
xmlns: 'w:profile:picture'
|
||||||
},
|
},
|
||||||
content: [{ tag: 'picture', attrs: { type, query: 'url' } }]
|
content: tcTokenContent
|
||||||
},
|
},
|
||||||
timeoutMs
|
timeoutMs
|
||||||
)
|
)
|
||||||
@@ -694,24 +698,19 @@ export const makeChatsSocket = (config: SocketConfig) => {
|
|||||||
* @param toJid the jid to subscribe to
|
* @param toJid the jid to subscribe to
|
||||||
* @param tcToken token for subscription, use if present
|
* @param tcToken token for subscription, use if present
|
||||||
*/
|
*/
|
||||||
const presenceSubscribe = (toJid: string, tcToken?: Buffer) =>
|
const presenceSubscribe = async (toJid: string) => {
|
||||||
sendNode({
|
const tcTokenContent = await buildTcTokenFromJid({ authState, jid: toJid })
|
||||||
|
|
||||||
|
return sendNode({
|
||||||
tag: 'presence',
|
tag: 'presence',
|
||||||
attrs: {
|
attrs: {
|
||||||
to: toJid,
|
to: toJid,
|
||||||
id: generateMessageTag(),
|
id: generateMessageTag(),
|
||||||
type: 'subscribe'
|
type: 'subscribe'
|
||||||
},
|
},
|
||||||
content: tcToken
|
content: tcTokenContent
|
||||||
? [
|
|
||||||
{
|
|
||||||
tag: 'tctoken',
|
|
||||||
attrs: {},
|
|
||||||
content: tcToken
|
|
||||||
}
|
|
||||||
]
|
|
||||||
: undefined
|
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const handlePresenceUpdate = ({ tag, attrs, content }: BinaryNode) => {
|
const handlePresenceUpdate = ({ tag, attrs, content }: BinaryNode) => {
|
||||||
let presence: PresenceData | undefined
|
let presence: PresenceData | undefined
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import type { AuthenticationCreds, SignalKeyStoreWithTransaction } from '../Types'
|
||||||
|
import type { BinaryNode } from '../WABinary'
|
||||||
|
|
||||||
|
type TcTokenParams = {
|
||||||
|
jid: string
|
||||||
|
baseContent?: BinaryNode[]
|
||||||
|
authState: {
|
||||||
|
keys: SignalKeyStoreWithTransaction
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function buildTcTokenFromJid({
|
||||||
|
authState,
|
||||||
|
jid,
|
||||||
|
baseContent = []
|
||||||
|
}: TcTokenParams): Promise<BinaryNode[] | undefined> {
|
||||||
|
try {
|
||||||
|
const tcTokenData = await authState.keys.get('tctoken', [jid])
|
||||||
|
|
||||||
|
const tcTokenBuffer = tcTokenData?.[jid]?.token
|
||||||
|
|
||||||
|
if (!tcTokenBuffer) return baseContent.length > 0 ? baseContent : undefined
|
||||||
|
|
||||||
|
baseContent.push({
|
||||||
|
tag: 'tctoken',
|
||||||
|
attrs: {},
|
||||||
|
content: tcTokenBuffer
|
||||||
|
})
|
||||||
|
|
||||||
|
return baseContent
|
||||||
|
} catch (error) {
|
||||||
|
return baseContent.length > 0 ? baseContent : undefined
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user