From bc3cb6c178211988c74f5b3203504d0c4c074c32 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Wed, 23 Dec 2020 09:31:00 +0530 Subject: [PATCH 1/6] Require phone connection in required places --- package.json | 2 +- src/WAConnection/0.Base.ts | 2 +- src/WAConnection/5.User.ts | 12 +++++++++--- src/WAConnection/7.MessagesExtra.ts | 9 +++++++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index d03a8a2..a84e9a6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@adiwajshing/baileys", - "version": "3.4.0", + "version": "3.4.1", "description": "WhatsApp Web API", "homepage": "https://github.com/adiwajshing/Baileys", "main": "lib/index.js", diff --git a/src/WAConnection/0.Base.ts b/src/WAConnection/0.Base.ts index e0aa4ed..8348cb4 100644 --- a/src/WAConnection/0.Base.ts +++ b/src/WAConnection/0.Base.ts @@ -195,7 +195,7 @@ export class WAConnection extends EventEmitter { /** Generic function for action, set queries */ async setQuery (nodes: WANode[], binaryTags: WATag = [WAMetric.group, WAFlag.ignore], tag?: string) { const json = ['action', {epoch: this.msgCount.toString(), type: 'set'}, nodes] - const result = await this.query({ json, binaryTags, tag, expect200: true }) as Promise<{status: number}> + const result = await this.query({ json, binaryTags, tag, expect200: true, requiresPhoneConnection: true }) as Promise<{status: number}> return result } /** diff --git a/src/WAConnection/5.User.ts b/src/WAConnection/5.User.ts index f69d18a..3248ac6 100644 --- a/src/WAConnection/5.User.ts +++ b/src/WAConnection/5.User.ts @@ -83,13 +83,13 @@ export class WAConnection extends Base { /** Get your contacts */ async getContacts() { const json = ['query', { epoch: this.msgCount.toString(), type: 'contacts' }, null] - const response = await this.query({ json, binaryTags: [6, WAFlag.ignore], expect200: true }) // this has to be an encrypted query + const response = await this.query({ json, binaryTags: [WAMetric.queryContact, WAFlag.ignore], expect200: true, requiresPhoneConnection: true }) // this has to be an encrypted query return response } /** Get the stories of your contacts */ async getStories() { const json = ['query', { epoch: this.msgCount.toString(), type: 'status' }, null] - const response = await this.query({json, binaryTags: [30, WAFlag.ignore], expect200: true }) as WANode + const response = await this.query({json, binaryTags: [WAMetric.queryStatus, WAFlag.ignore], expect200: true, requiresPhoneConnection: true }) as WANode if (Array.isArray(response[2])) { return response[2].map (row => ( { @@ -107,7 +107,13 @@ export class WAConnection extends Base { return this.query({ json, binaryTags: [5, WAFlag.ignore], expect200: true }) // this has to be an encrypted query } /** Query broadcast list info */ - async getBroadcastListInfo(jid: string) { return this.query({json: ['query', 'contact', jid], expect200: true }) as Promise } + async getBroadcastListInfo(jid: string) { + return this.query({ + json: ['query', 'contact', jid], + expect200: true, + requiresPhoneConnection: true + }) as Promise + } /** * Load chats in a paginated manner + gets the profile picture * @param before chats before the given cursor diff --git a/src/WAConnection/7.MessagesExtra.ts b/src/WAConnection/7.MessagesExtra.ts index 2939260..01825c4 100644 --- a/src/WAConnection/7.MessagesExtra.ts +++ b/src/WAConnection/7.MessagesExtra.ts @@ -19,7 +19,12 @@ export class WAConnection extends Base { @Mutex ((jid, messageID) => jid+messageID) async messageInfo (jid: string, messageID: string) { const query = ['query', {type: 'message_info', index: messageID, jid: jid, epoch: this.msgCount.toString()}, null] - const [,,response] = await this.query ({json: query, binaryTags: [WAMetric.queryRead, WAFlag.ignore], expect200: true}) + const [,,response] = await this.query ({ + json: query, + binaryTags: [WAMetric.queryRead, WAFlag.ignore], + expect200: true, + requiresPhoneConnection: true + }) const info: MessageInfo = {reads: [], deliveries: []} if (response) { @@ -87,7 +92,7 @@ export class WAConnection extends Base { }, null, ] - const response = await this.query({json, binaryTags: [WAMetric.queryMessages, WAFlag.ignore], expect200: false}) + const response = await this.query({json, binaryTags: [WAMetric.queryMessages, WAFlag.ignore], expect200: false, requiresPhoneConnection: true}) return (response[2] as WANode[])?.map(item => item[2] as WAMessage) || [] } /** From 5fb9e8abed1002a66ffdf945f1c34135fb6df06a Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Mon, 28 Dec 2020 11:44:02 +0530 Subject: [PATCH 2/6] Minor updates --- src/WAConnection/3.Connect.ts | 2 +- src/WAConnection/5.User.ts | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/WAConnection/3.Connect.ts b/src/WAConnection/3.Connect.ts index 62e8811..e71034f 100644 --- a/src/WAConnection/3.Connect.ts +++ b/src/WAConnection/3.Connect.ts @@ -215,7 +215,7 @@ export class WAConnection extends Base { if (this.state === 'open' && json[0] === 'Pong') { if (!json[1]) { - this.closeInternal(DisconnectReason.close) + this.unexpectedDisconnect(DisconnectReason.close) this.logger.info('Connection terminated by phone, closing...') return } diff --git a/src/WAConnection/5.User.ts b/src/WAConnection/5.User.ts index 3248ac6..46222df 100644 --- a/src/WAConnection/5.User.ts +++ b/src/WAConnection/5.User.ts @@ -172,9 +172,6 @@ export class WAConnection extends Base { */ @Mutex (jid => jid) async blockUser (jid: string, type: 'add' | 'remove' = 'add') { - jid.replace('@s.whatsapp.net', '@c.us') - - const tag = this.generateMessageTag() const json: WANode = [ 'block', { @@ -184,7 +181,7 @@ export class WAConnection extends Base { ['user', { jid }, null] ], ] - const result = await this.setQuery ([json], [WAMetric.block, WAFlag.ignore], tag) + const result = await this.setQuery ([json], [WAMetric.block, WAFlag.ignore]) if (result.status === 200) { if (type === 'add') { From 165451d752be03ce16aef0ec2fc1761b584701ac Mon Sep 17 00:00:00 2001 From: Dmitry Sadovsky Date: Mon, 28 Dec 2020 13:41:27 +0300 Subject: [PATCH 3/6] Added option for forcing message id --- src/WAConnection/6.MessagesSend.ts | 2 +- src/WAConnection/Constants.ts | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/WAConnection/6.MessagesSend.ts b/src/WAConnection/6.MessagesSend.ts index 94c1dd1..5a78834 100644 --- a/src/WAConnection/6.MessagesSend.ts +++ b/src/WAConnection/6.MessagesSend.ts @@ -260,7 +260,7 @@ export class WAConnection extends Base { key: { remoteJid: id, fromMe: true, - id: generateMessageID(), + id: options?.messageId || generateMessageID(), }, message: message, messageTimestamp: timestamp, diff --git a/src/WAConnection/Constants.ts b/src/WAConnection/Constants.ts index e5f82ba..e4f6a09 100644 --- a/src/WAConnection/Constants.ts +++ b/src/WAConnection/Constants.ts @@ -377,6 +377,8 @@ export interface MessageOptions { /** Should it send as a disappearing messages. * By default 'chat' -- which follows the setting of the chat */ sendEphemeral?: 'chat' | boolean + /** Force message id */ + messageId?: string } export interface WABroadcastListInfo { status: number @@ -474,4 +476,4 @@ export type BaileysEvent = 'received-pong' | 'credentials-updated' | 'connection-validated' | - 'blocklist-update' \ No newline at end of file + 'blocklist-update' From 471ba8c23f13b55a016838d200b356651dce88e8 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Tue, 29 Dec 2020 00:42:28 +0530 Subject: [PATCH 4/6] Add requiresPhoneConnection to sendMessage --- src/WAConnection/6.MessagesSend.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/WAConnection/6.MessagesSend.ts b/src/WAConnection/6.MessagesSend.ts index 94c1dd1..e6a2b77 100644 --- a/src/WAConnection/6.MessagesSend.ts +++ b/src/WAConnection/6.MessagesSend.ts @@ -280,7 +280,8 @@ export class WAConnection extends Base { json, binaryTags: [WAMetric.message, flag], tag: mID, - expect200: true + expect200: true, + requiresPhoneConnection: true }) .then(() => message.status = WA_MESSAGE_STATUS_TYPE.SERVER_ACK) @@ -304,7 +305,12 @@ export class WAConnection extends Base { if (!content) throw new BaileysError (`given message ${message.key.id} is not a media message`, message) const query = ['query',{type: 'media', index: message.key.id, owner: message.key.fromMe ? 'true' : 'false', jid: message.key.remoteJid, epoch: this.msgCount.toString()},null] - const response = await this.query ({json: query, binaryTags: [WAMetric.queryMedia, WAFlag.ignore], expect200: true}) + const response = await this.query ({ + json: query, + binaryTags: [WAMetric.queryMedia, WAFlag.ignore], + expect200: true, + requiresPhoneConnection: true + }) Object.keys (response[1]).forEach (key => content[key] = response[1][key]) // update message } /** From 3d6418e9d60e1642d577a08ddf53b6481f05f17a Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Tue, 29 Dec 2020 11:05:23 +0530 Subject: [PATCH 5/6] Update 7.MessagesExtra.ts --- src/WAConnection/7.MessagesExtra.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WAConnection/7.MessagesExtra.ts b/src/WAConnection/7.MessagesExtra.ts index 01825c4..2af1226 100644 --- a/src/WAConnection/7.MessagesExtra.ts +++ b/src/WAConnection/7.MessagesExtra.ts @@ -114,7 +114,7 @@ export class WAConnection extends Base { const chat = this.chats.get (jid) const hasCursor = cursor?.id && typeof cursor?.fromMe !== 'undefined' - const cursorValue = hasCursor && chat.messages.get (GET_MESSAGE_ID(cursor)) + const cursorValue = hasCursor && chat.messages?.get (GET_MESSAGE_ID(cursor)) let messages: WAMessage[] if (chat?.messages && mostRecentFirst && (!hasCursor || cursorValue)) { From 26b15b2b78280c596235c0ce4a4e87d2419c0629 Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Tue, 29 Dec 2020 11:09:21 +0530 Subject: [PATCH 6/6] Update 7.MessagesExtra.ts --- src/WAConnection/7.MessagesExtra.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WAConnection/7.MessagesExtra.ts b/src/WAConnection/7.MessagesExtra.ts index 2af1226..b287963 100644 --- a/src/WAConnection/7.MessagesExtra.ts +++ b/src/WAConnection/7.MessagesExtra.ts @@ -114,7 +114,7 @@ export class WAConnection extends Base { const chat = this.chats.get (jid) const hasCursor = cursor?.id && typeof cursor?.fromMe !== 'undefined' - const cursorValue = hasCursor && chat.messages?.get (GET_MESSAGE_ID(cursor)) + const cursorValue = hasCursor && chat?.messages.get (GET_MESSAGE_ID(cursor)) let messages: WAMessage[] if (chat?.messages && mostRecentFirst && (!hasCursor || cursorValue)) {