From 1b3497789c3853c2254a85ce2f1c4c483e2ba54c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B3nalan=20de=20Lima?= Date: Mon, 1 Mar 2021 19:39:36 -0300 Subject: [PATCH] fix: Undefined or null to object sendMessage --- src/lib/wapi/functions/send-message.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/lib/wapi/functions/send-message.js b/src/lib/wapi/functions/send-message.js index 619dfe090..7aa896d16 100644 --- a/src/lib/wapi/functions/send-message.js +++ b/src/lib/wapi/functions/send-message.js @@ -52,10 +52,28 @@ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNMMNMNMMMNMMNNMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMNNNNMMNNNMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM */ +async function isInsideTo(to) { + let err; + do { + try { + await new Promise((r) => setTimeout(r, 2000)); + let chatTo = await WAPI.getchatId(to); + if (typeof chatTo !== 'undefined') { + err = false; + return chatTo; + } else { + throw 1; + } + } catch (e) { + err = true; + } + } while (err); +} export async function sendMessage(to, content) { - const chat = !Store.Chat.get(to) - ? await WAPI.sendExist(to) - : await Store.Chat.get(to); + if (typeof content != 'string' || content.length === 0) { + return WAPI.scope(to, true, 404, 'It is necessary to write a text!'); + } + const chat = await WAPI.sendExist(to); const m = { type: 'sendtext', text: content }; if (chat && chat.status != 404) { const newMsgId = await window.WAPI.getNewMessageId(chat.id); @@ -75,7 +93,7 @@ export async function sendMessage(to, content) { var result = ( await Promise.all(window.Store.addAndSendMsgToChat(chat, message)) )[1]; - let To = await WAPI.getchatId(chat.id); + let To = await isInsideTo(chat.id); if (result === 'success' || result === 'OK') { let obj = WAPI.scope(To, false, result, content); Object.assign(obj, m);