From e393f0f9a8b9f81611f94e7a5800af6c1ba9eb54 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Sat, 18 Dec 2021 16:44:31 +0530 Subject: [PATCH] chore: make APIs similar for legacy + MD --- Example/example-legacy.ts | 2 +- src/LegacySocket/auth.ts | 4 ++-- src/LegacySocket/chats.ts | 28 ++++++++++++++-------------- src/LegacySocket/groups.ts | 2 +- src/LegacySocket/messages.ts | 16 ++++++++++------ src/LegacySocket/socket.ts | 9 ++++----- src/Socket/groups.ts | 2 +- 7 files changed, 33 insertions(+), 30 deletions(-) diff --git a/Example/example-legacy.ts b/Example/example-legacy.ts index 06c6fd0..2660a80 100644 --- a/Example/example-legacy.ts +++ b/Example/example-legacy.ts @@ -22,7 +22,7 @@ const startSock = () => { await sock.sendPresenceUpdate('paused', jid) - await sock.sendWAMessage(jid, msg) + await sock.sendMessage(jid, msg) } sock.ev.on('messages.upsert', async m => { diff --git a/src/LegacySocket/auth.ts b/src/LegacySocket/auth.ts index fbae0b4..e8d87d1 100644 --- a/src/LegacySocket/auth.ts +++ b/src/LegacySocket/auth.ts @@ -64,7 +64,7 @@ const makeAuthSocket = (config: LegacySocketConfig) => { */ const logout = async() => { if(state.connection === 'open') { - await socket.sendMessage({ + await socket.sendNode({ json: ['admin', 'Conn', 'disconnect'], tag: 'goodbye' }) @@ -178,7 +178,7 @@ const makeAuthSocket = (config: LegacySocketConfig) => { return } logger.info('sending login request') - socket.sendMessage({ + socket.sendNode({ json, tag: loginTag }) diff --git a/src/LegacySocket/chats.ts b/src/LegacySocket/chats.ts index 67d8c00..e80ad62 100644 --- a/src/LegacySocket/chats.ts +++ b/src/LegacySocket/chats.ts @@ -12,14 +12,14 @@ const makeChatsSocket = (config: LegacySocketConfig) => { currentEpoch, setQuery, query, - sendMessage, + sendNode, getState } = sock const chatsDebounceTimeout = debouncedTimeout(10_000, () => sendChatsQuery(1)) const sendChatsQuery = (epoch: number) => ( - sendMessage({ + sendNode({ json: { tag: 'query', attrs: {type: 'chat', epoch: epoch.toString()} @@ -108,27 +108,27 @@ const makeChatsSocket = (config: LegacySocketConfig) => { if(connection !== 'open') return try { await Promise.all([ - sendMessage({ + sendNode({ json: { tag: 'query', attrs: {type: 'contacts', epoch: '1'} }, binaryTag: [ WAMetric.queryContact, WAFlag.ignore ] }), - sendMessage({ + sendNode({ json: { tag: 'query', attrs: {type: 'status', epoch: '1'} }, binaryTag: [ WAMetric.queryStatus, WAFlag.ignore ] }), - sendMessage({ + sendNode({ json: { tag: 'query', attrs: {type: 'quick_reply', epoch: '1'} }, binaryTag: [ WAMetric.queryQuickReply, WAFlag.ignore ] }), - sendMessage({ + sendNode({ json: { tag: 'query', attrs: {type: 'label', epoch: '1'} }, binaryTag: [ WAMetric.queryLabel, WAFlag.ignore ] }), - sendMessage({ + sendNode({ json: { tag: 'query', attrs: {type: 'emoji', epoch: '1'} }, binaryTag: [ WAMetric.queryEmoji, WAFlag.ignore ] }), - sendMessage({ + sendNode({ json: { tag: 'action', attrs: { type: 'set', epoch: '1' }, @@ -206,7 +206,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => { const update: Partial = { id: jidNormalizedUser(attrs.jid) } - if (attrs.type === 'false') update.unreadCount = -1 + if(attrs.type === 'false') update.unreadCount = -1 else update.unreadCount = 0 ev.emit('chats.update', [update]) @@ -277,7 +277,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => { * Modify a given chat (archive, pin etc.) * @param jid the ID of the person/group you are modifiying */ - modifyChat: async(jid: string, modification: ChatModification, chatInfo: Pick, index?: WAMessageKey) => { + chatModify: async( modification: ChatModification, jid: string, chatInfo: Pick, index?: WAMessageKey) => { let chatAttrs: BinaryNode['attrs'] = { jid: jid } let data: BinaryNode[] | undefined = undefined const stamp = unixTimestampSeconds() @@ -335,12 +335,12 @@ const makeChatsSocket = (config: LegacySocketConfig) => { * @param str phone number/jid you want to check for * @returns undefined if the number doesn't exists, otherwise the correctly formatted jid */ - isOnWhatsApp: async (str: string) => { + onWhatsApp: async(str: string) => { const { status, jid, biz } = await query({ json: ['query', 'exist', str], requiresPhoneConnection: false }) - if (status === 200) { + if(status === 200) { return { exists: true, jid: jidNormalizedUser(jid), @@ -354,7 +354,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => { * @param type your presence */ sendPresenceUpdate: ( type: WAPresence, jid: string | undefined) => ( - sendMessage({ + sendNode({ binaryTag: [WAMetric.presence, WAFlag[type]], // weird stuff WA does json: { tag: 'action', @@ -373,7 +373,7 @@ const makeChatsSocket = (config: LegacySocketConfig) => { * this returns nothing, you'll receive updates in chats.update event * */ presenceSubscribe: async (jid: string) => ( - sendMessage({ json: ['action', 'presence', 'subscribe', jid] }) + sendNode({ json: ['action', 'presence', 'subscribe', jid] }) ), /** Query the status of the person (see groupMetadata() for groups) */ getStatus: async(jid: string) => { diff --git a/src/LegacySocket/groups.ts b/src/LegacySocket/groups.ts index b4bba8b..053de2d 100644 --- a/src/LegacySocket/groups.ts +++ b/src/LegacySocket/groups.ts @@ -224,7 +224,7 @@ const makeGroupsSocket = (config: LegacySocketConfig) => { } return metadata }, - inviteCode: async(jid: string) => { + groupInviteCode: async(jid: string) => { const response = await sock.query({ json: ['query', 'inviteCode', jid], expect200: true, diff --git a/src/LegacySocket/messages.ts b/src/LegacySocket/messages.ts index 83d6f58..fbde9a7 100644 --- a/src/LegacySocket/messages.ts +++ b/src/LegacySocket/messages.ts @@ -249,7 +249,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => { } /** Relay (send) a WAMessage; more advanced functionality to send a built WA Message, you may want to stick with sendMessage() */ - const relayWAMessage = async(message: WAMessage, { waitForAck } = { waitForAck: true }) => { + const relayMessage = async(message: WAMessage, { waitForAck } = { waitForAck: true }) => { const json: BinaryNode = { tag: 'action', attrs: { epoch: currentEpoch().toString(), type: 'relay' }, @@ -388,13 +388,18 @@ const makeMessagesSocket = (config: LegacySocketConfig) => { return { ...sock, - relayWAMessage, + relayMessage, generateUrlInfo, messageInfo: async(jid: string, messageID: string) => { const { content }: BinaryNode = await query({ json: { tag: 'query', - attrs: {type: 'message_info', index: messageID, jid: jid, epoch: currentEpoch().toString()} + attrs: { + type: 'message_info', + index: messageID, + jid: jid, + epoch: currentEpoch().toString() + } }, binaryTag: [WAMetric.queryRead, WAFlag.ignore], expect200: true, @@ -419,7 +424,6 @@ const makeMessagesSocket = (config: LegacySocketConfig) => { return info }, downloadMediaMessage: async(message: WAMessage, type: 'buffer' | 'stream' = 'buffer') => { - const downloadMediaMessage = async () => { let mContent = extractMessageContent(message.message) if (!mContent) throw new Boom('No message present', { statusCode: 400, data: message }) @@ -486,7 +490,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => { messages: getBinaryNodeMessages(node) } }, - sendWAMessage: async( + sendMessage: async( jid: string, content: AnyMessageContent, options: MiscMessageGenerationOptions & { waitForAck?: boolean } = { waitForAck: true } @@ -526,7 +530,7 @@ const makeMessagesSocket = (config: LegacySocketConfig) => { } ) - await relayWAMessage(msg, { waitForAck: options.waitForAck }) + await relayMessage(msg, { waitForAck: options.waitForAck }) return msg } } diff --git a/src/LegacySocket/socket.ts b/src/LegacySocket/socket.ts index 5f7c3bf..2257c88 100644 --- a/src/LegacySocket/socket.ts +++ b/src/LegacySocket/socket.ts @@ -62,7 +62,7 @@ export const makeSocket = ({ * Send a message to the WA servers * @returns the tag attached in the message * */ - const sendMessage = async( + const sendNode = async( { json, binaryTag, tag, longTag }: SocketSendMessageOptions ) => { tag = tag || generateMessageTag(longTag) @@ -204,7 +204,7 @@ export const makeSocket = ({ } } /** checks for phone connection */ - const sendAdminTest = () => sendMessage({ json: ['admin', 'test'] }) + const sendAdminTest = () => sendNode({ json: ['admin', 'test'] }) /** * Wait for a message with a certain tag to be received * @param tag the message tag to await @@ -266,7 +266,7 @@ export const makeSocket = ({ tag = tag || generateMessageTag(longTag) const { promise, cancelToken } = waitForMessage(tag, requiresPhoneConnection, timeoutMs) try { - await sendMessage({ json, tag, binaryTag }) + await sendNode({ json, tag, binaryTag }) } catch(error) { cancelToken() // swallow error @@ -366,8 +366,7 @@ export const makeSocket = ({ ws, updateKeys: (info: { encKey: Buffer, macKey: Buffer }) => authInfo = info, waitForSocketOpen, - sendRawMessage, - sendMessage, + sendNode, generateMessageTag, waitForMessage, query, diff --git a/src/Socket/groups.ts b/src/Socket/groups.ts index 11a16a1..7a76fb6 100644 --- a/src/Socket/groups.ts +++ b/src/Socket/groups.ts @@ -124,7 +124,7 @@ export const makeGroupsSocket = (config: SocketConfig) => { const inviteNode = getBinaryNodeChild(result, 'invite') return inviteNode.attrs.code }, - groupRevokeInvite: async (jid: string) => { + groupRevokeInvite: async (jid: string) => { const result = await groupQuery(jid, 'set', [{ tag: 'invite', attrs: {} }]) const inviteNode = getBinaryNodeChild(result, 'invite') return inviteNode.attrs.code