From 21dbc7e11234ae5c93abb89fc8251f1df2cdd461 Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Tue, 29 Nov 2022 18:55:02 -0300 Subject: [PATCH] Fix edit, delete, resend and delete after error state --- app/containers/MessageActions/index.tsx | 2 +- app/containers/MessageErrorActions.tsx | 14 ++++++++------ app/lib/methods/sendMessage.ts | 14 +++++++++----- app/views/RoomView/index.tsx | 3 +-- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/app/containers/MessageActions/index.tsx b/app/containers/MessageActions/index.tsx index c233fb5b3..6cdd23ffd 100644 --- a/app/containers/MessageActions/index.tsx +++ b/app/containers/MessageActions/index.tsx @@ -347,7 +347,7 @@ const MessageActions = React.memo( onPress: async () => { try { logEvent(events.ROOM_MSG_ACTION_DELETE); - await Services.deleteMessage(message.id, message.subscription ? message.subscription.id : ''); + await Services.deleteMessage(message.id, message.rid); } catch (e) { logEvent(events.ROOM_MSG_ACTION_DELETE_F); log(e); diff --git a/app/containers/MessageErrorActions.tsx b/app/containers/MessageErrorActions.tsx index 724f8aaae..38403e660 100644 --- a/app/containers/MessageErrorActions.tsx +++ b/app/containers/MessageErrorActions.tsx @@ -1,6 +1,8 @@ import { forwardRef, useImperativeHandle } from 'react'; import Model from '@nozbe/watermelondb/Model'; +import { getMessageById } from '../lib/database/services/Message'; +import { getThreadMessageById } from '../lib/database/services/ThreadMessage'; import database from '../lib/database'; import protectedFunction from '../lib/methods/helpers/protectedFunction'; import { useActionSheet } from './ActionSheet'; @@ -27,16 +29,16 @@ const MessageErrorActions = forwardRef( const msgCollection = db.get('messages'); const threadCollection = db.get('threads'); - // Delete the object (it can be Message or ThreadMessage instance) - deleteBatch.push(message.prepareDestroyPermanently()); + const msg = await getMessageById(message.id); + if (msg) { + deleteBatch.push(msg.prepareDestroyPermanently()); + } // If it's a thread, we find and delete the whole tree, if necessary if (tmid) { - try { - const msg = await msgCollection.find(message.id); + const msg = await getThreadMessageById(message.id); + if (msg) { deleteBatch.push(msg.prepareDestroyPermanently()); - } catch { - // Do nothing: message not found } try { diff --git a/app/lib/methods/sendMessage.ts b/app/lib/methods/sendMessage.ts index fe1678d18..7a81a4d10 100644 --- a/app/lib/methods/sendMessage.ts +++ b/app/lib/methods/sendMessage.ts @@ -1,6 +1,7 @@ import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord'; import { Model } from '@nozbe/watermelondb'; +import { getMessageById } from '../database/services/Message'; import database from '../database'; import log from './helpers/log'; import { random } from './helpers'; @@ -66,14 +67,17 @@ async function sendMessageCall(message: any) { export async function resendMessage(message: TMessageModel, tmid?: string) { const db = database.active; try { - await db.write(async () => { - await message.update(m => { - m.status = messagesStatus.TEMP; + const messageRecord = await getMessageById(message.id); + if (messageRecord) { + await db.write(async () => { + await messageRecord.update(m => { + m.status = messagesStatus.TEMP; + }); }); - }); + } const m = await Encryption.encryptMessage({ _id: message.id, - rid: message.subscription ? message.subscription.id : '', + rid: message.rid, msg: message.msg, ...(tmid && { tmid }) } as IMessage); diff --git a/app/views/RoomView/index.tsx b/app/views/RoomView/index.tsx index 865157ddf..cdb47f70b 100644 --- a/app/views/RoomView/index.tsx +++ b/app/views/RoomView/index.tsx @@ -795,8 +795,7 @@ class RoomView extends React.Component { const newMessage = { id: message.id, subscription: { - // @ts-ignore TODO: we can remove this after we merge a PR separating IMessage vs IMessageFromServer - id: message.subscription.id + id: message.rid }, msg: message?.attachments?.[0]?.description || message.msg } as TMessageModel;