From ee06584b322ec4c56d1dd7566e8f3bb31bd6bd6a Mon Sep 17 00:00:00 2001 From: Ifedapo Olarewaju Date: Sun, 3 Dec 2017 23:39:32 +0100 Subject: [PATCH] fix: messages to old groups creating new threads #22 --- browser/js/funcs.js | 9 +++++---- instagram.js | 11 ++++++++++- main.js | 8 +++++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/browser/js/funcs.js b/browser/js/funcs.js index 77c5a78..9a6333a 100644 --- a/browser/js/funcs.js +++ b/browser/js/funcs.js @@ -73,19 +73,20 @@ function markAsRead (id, li) { delete unreadChats[id]; } - li.classList.remove('notification'); // or whatever + li.classList.remove('notification'); } -function sendMessage (message, accounts, isNewChat) { +function sendMessage (message, accounts, chatId) { + const isNewChat = !chatId; var users = accounts.map((account) => account.id); - ipcRenderer.send('message', { message, isNewChat, users }) + ipcRenderer.send('message', { message, isNewChat, users, chatId }); } function submitMessage (chat_) { var input = document.querySelector(MSG_INPUT_SELECTOR); var message = input.value; if (message.trim()) { - sendMessage(message, chat_.accounts, !chat_.id); + sendMessage(message, chat_.accounts, chat_.id); input.value = ''; var div = renderMessage(message, 'outward'); var msgContainer = document.querySelector('.chat .messages'); diff --git a/instagram.js b/instagram.js index 75f944f..02c1609 100644 --- a/instagram.js +++ b/instagram.js @@ -44,12 +44,21 @@ exports.getChat = function (session, chatId) { }) } -exports.sendMessage = function (session, message, recipients) { +exports.sendNewChatMessage = function (session, message, recipients) { return new Promise((resolve, reject) => { Client.Thread.configureText(session, recipients, message).then(resolve).catch(reject) }) } +exports.sendMessage = function (session, message, chatId) { + return new Promise((resolve, reject) => { + Client.Thread.getById(session, chatId) + .then((thread) => { + thread.broadcastText(message).then(resolve).catch(reject) + }).catch(reject) + }) +} + exports.searchUsers = function (session, search) { return new Promise((resolve, reject) => { Client.Account.search(session, search).then(resolve).catch(reject) diff --git a/main.js b/main.js index 25e92e5..a592b0b 100644 --- a/main.js +++ b/main.js @@ -137,9 +137,11 @@ electron.ipcMain.on('getChatList', getChatList) electron.ipcMain.on('getChat', getChat) electron.ipcMain.on('message', (evt, data) => { - instagram.sendMessage(session, data.message, data.users).then((chat) => { - if (data.isNewChat) getChat(null, chat[0].id) - }) + if (data.isNewChat) { + instagram.sendNewChatMessage(session, data.message, data.users).then((chat) => getChat(null, chat[0].id)) + } else { + instagram.sendMessage(session, data.message, data.chatId) + } }) electron.ipcMain.on('searchUsers', (evt, search) => {