From a3822d4941b7f67bfe8cac0b24166f6d4d3168eb Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Fri, 13 Dec 2019 10:35:12 -0300 Subject: [PATCH] [FIX] Messages being sent but showing as temp status (#1469) --- app/lib/methods/sendMessage.js | 55 ++++++++++++++++++++-------------- app/views/RoomView/index.js | 4 --- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/app/lib/methods/sendMessage.js b/app/lib/methods/sendMessage.js index 7b9e341ec..0d9fe5973 100644 --- a/app/lib/methods/sendMessage.js +++ b/app/lib/methods/sendMessage.js @@ -5,6 +5,36 @@ import database from '../database'; import log from '../../utils/log'; import random from '../../utils/random'; +const changeMessageStatus = async(id, tmid, status) => { + const db = database.active; + const msgCollection = db.collections.get('messages'); + const threadMessagesCollection = db.collections.get('thread_messages'); + const successBatch = []; + const messageRecord = await msgCollection.find(id); + successBatch.push( + messageRecord.prepareUpdate((m) => { + m.status = status; + }) + ); + + if (tmid) { + const threadMessageRecord = await threadMessagesCollection.find(id); + successBatch.push( + threadMessageRecord.prepareUpdate((tm) => { + tm.status = status; + }) + ); + } + + try { + await db.action(async() => { + await db.batch(...successBatch); + }); + } catch (error) { + // Do nothing + } +}; + export async function sendMessageCall(message) { const { id: _id, subscription: { id: rid }, msg, tmid @@ -17,30 +47,9 @@ export async function sendMessageCall(message) { _id, rid, msg, tmid } }); + await changeMessageStatus(_id, tmid, messagesStatus.SENT); } catch (e) { - const db = database.active; - const msgCollection = db.collections.get('messages'); - const threadMessagesCollection = db.collections.get('thread_messages'); - const errorBatch = []; - const messageRecord = await msgCollection.find(_id); - errorBatch.push( - messageRecord.prepareUpdate((m) => { - m.status = messagesStatus.ERROR; - }) - ); - - if (tmid) { - const threadMessageRecord = await threadMessagesCollection.find(_id); - errorBatch.push( - threadMessageRecord.prepareUpdate((tm) => { - tm.status = messagesStatus.ERROR; - }) - ); - } - - await db.action(async() => { - await db.batch(...errorBatch); - }); + await changeMessageStatus(_id, tmid, messagesStatus.ERROR); } } diff --git a/app/views/RoomView/index.js b/app/views/RoomView/index.js index 351da5a54..d13f58338 100644 --- a/app/views/RoomView/index.js +++ b/app/views/RoomView/index.js @@ -250,16 +250,12 @@ class RoomView extends React.Component { if (appState === 'foreground' && appState !== prevProps.appState && this.rid) { this.onForegroundInteraction = InteractionManager.runAfterInteractions(() => { - this.init(); // Fire List.init() just to keep observables working if (this.list && this.list.current) { this.list.current.init(); } }); } - if (appState === 'background' && appState !== prevProps.appState) { - this.unsubscribe(); - } } async componentWillUnmount() {