From 88c56b1371efcf9d21455ccf4ada9d8c1ae23300 Mon Sep 17 00:00:00 2001 From: Pedro Lopez Date: Sat, 1 Feb 2020 19:34:28 -0400 Subject: [PATCH] Return new message after sending --- src/Client.js | 9 ++++++--- src/structures/Chat.js | 6 +++++- src/structures/Message.js | 10 +++++----- src/util/Injected.js | 29 ++++++++++++++++++++++++++++- 4 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/Client.js b/src/Client.js index 9c80e82..1788b58 100644 --- a/src/Client.js +++ b/src/Client.js @@ -138,9 +138,12 @@ class Client extends EventEmitter { * @param {string} message */ async sendMessage(chatId, message) { - await this.pupPage.evaluate((chatId, message) => { - Store.SendMessage(Store.Chat.get(chatId), message); - }, chatId, message) + const newMessage = await this.pupPage.evaluate(async (chatId, message) => { + const msg = await WWebJS.sendMessage(Store.Chat.get(chatId), message); + return msg.serialize(); + }, chatId, message); + + return new Message(this, newMessage); } /** diff --git a/src/structures/Chat.js b/src/structures/Chat.js index ec5f430..54c2464 100644 --- a/src/structures/Chat.js +++ b/src/structures/Chat.js @@ -25,7 +25,11 @@ class Chat extends Base { return super._patch(data); } - sendMessage(message) { + /** + * Sends a message to this chat. + * @param {string} message + */ + async sendMessage(message) { return this.client.sendMessage(this.id._serialized, message); } } diff --git a/src/structures/Message.js b/src/structures/Message.js index b6249ab..7e045f2 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -48,18 +48,18 @@ class Message extends Base { chatId = this.from; } - return await this.client.pupPage.evaluate((chatId, quotedMessageId, message) => { + const newMessage = await this.client.pupPage.evaluate(async (chatId, quotedMessageId, message) => { let quotedMessage = Store.Msg.get(quotedMessageId); if(quotedMessage.canReply()) { const chat = Store.Chat.get(chatId); - chat.composeQuotedMsg = quotedMessage; - window.Store.SendMessage(chat, message, {quotedMsg: quotedMessage}); - chat.composeQuotedMsg = null; + const newMessage = await WWebJS.sendMessage(chat, message, quotedMessage.msgContextInfo(chat)); + return newMessage.serialize(); } else { throw new Error('This message cannot be replied to.'); } - }, chatId, this.id._serialized, message); + + return new Message(this.client, newMessage); } async downloadMedia() { diff --git a/src/util/Injected.js b/src/util/Injected.js index fc6eb77..afda1bc 100644 --- a/src/util/Injected.js +++ b/src/util/Injected.js @@ -8,14 +8,41 @@ exports.ExposeStore = (moduleRaidStr) => { window.mR = moduleRaid(); window.Store = window.mR.findModule("Chat")[1].default; window.Store.AppState = window.mR.findModule("STREAM")[0].default; + window.Store.Conn = window.mR.findModule("Conn")[0].default; window.Store.CryptoLib = window.mR.findModule("decryptE2EMedia")[0]; window.Store.genId = window.mR.findModule((module) => module.default && typeof module.default === 'function' && module.default.toString().match(/crypto/))[0].default; - window.Store.SendMessage = window.mR.findModule("sendTextMsgToChat")[0].sendTextMsgToChat; + window.Store.SendMessage = window.mR.findModule("addAndSendMsgToChat")[0]; + window.Store.MsgKey = window.mR.findModule((module) => module.default && module.default.fromString)[0].default; } exports.LoadUtils = () => { window.WWebJS = {}; + window.WWebJS.sendMessage = async (chat, content, options) => { + const newMsgId = new Store.MsgKey({ + from: Store.Conn.me, + to: chat.id, + id: Store.genId(), + }); + + const message = { + id: newMsgId, + ack: 0, + body: content, + from: Store.Conn.me, + to: chat.id, + local: true, + self: 'out', + t: parseInt(new Date().getTime() / 1000), + isNewMsg: true, + type: 'chat', + ...options + } + + await Store.SendMessage.addAndSendMsgToChat(chat, message); + return Store.Msg.get(newMsgId._serialized); + } + window.WWebJS.getChatModel = chat => { let res = chat.serialize(); res.isGroup = chat.isGroup;