From 3806525653c07c6484bcf045ec14af8742b849a8 Mon Sep 17 00:00:00 2001 From: otsmr Date: Sun, 1 Mar 2026 21:38:32 +0100 Subject: [PATCH] use sql match --- lib/src/database/daos/messages.dao.dart | 53 +++++++++++-------- .../api/client2client/messages.c2c.dart | 22 ++++---- lib/src/services/api/messages.dart | 21 ++++---- lib/src/views/chats/chat_messages.view.dart | 6 --- 4 files changed, 53 insertions(+), 49 deletions(-) diff --git a/lib/src/database/daos/messages.dao.dart b/lib/src/database/daos/messages.dao.dart index 71bf8d2..db0b2d6 100644 --- a/lib/src/database/daos/messages.dao.dart +++ b/lib/src/database/daos/messages.dao.dart @@ -212,31 +212,40 @@ class MessagesDao extends DatabaseAccessor with _$MessagesDaoMixin { ); } - Future handleMessageOpened( + Future handleMessagesOpened( int contactId, - String messageId, + List messageIds, DateTime timestamp, ) async { - await into(messageActions).insertOnConflictUpdate( - MessageActionsCompanion( - messageId: Value(messageId), - contactId: Value(contactId), - type: const Value(MessageActionType.openedAt), - actionAt: Value(timestamp), - ), - ); - // Directly show as message opened as soon as one person has opened it - final openedByAll = - await haveAllMembers(messageId, MessageActionType.openedAt) - ? clock.now() - : null; - await twonlyDB.messagesDao.updateMessageId( - messageId, - MessagesCompanion( - openedAt: Value(clock.now()), - openedByAll: Value(openedByAll), - ), - ); + await batch((batch) async { + for (final messageId in messageIds) { + batch.insert( + messageActions, + MessageActionsCompanion( + messageId: Value(messageId), + contactId: Value(contactId), + type: const Value(MessageActionType.openedAt), + actionAt: Value(timestamp), + ), + mode: InsertMode.insertOrReplace, + ); + } + + for (final messageId in messageIds) { + final isOpenedByAll = + await haveAllMembers(messageId, MessageActionType.openedAt); + final now = clock.now(); + + batch.update( + twonlyDB.messages, + MessagesCompanion( + openedAt: Value(now), + openedByAll: Value(isOpenedByAll ? now : null), + ), + where: (tbl) => tbl.messageId.equals(messageId), + ); + } + }); } Future handleMessageAckByServer( diff --git a/lib/src/services/api/client2client/messages.c2c.dart b/lib/src/services/api/client2client/messages.c2c.dart index 8a50ac6..e193ed3 100644 --- a/lib/src/services/api/client2client/messages.c2c.dart +++ b/lib/src/services/api/client2client/messages.c2c.dart @@ -9,19 +9,17 @@ Future handleMessageUpdate( ) async { switch (messageUpdate.type) { case EncryptedContent_MessageUpdate_Type.OPENED: - for (final targetMessageId in messageUpdate.multipleTargetMessageIds) { - Log.info( - 'Opened message $targetMessageId', + Log.info( + 'Opened message ${messageUpdate.multipleTargetMessageIds}', + ); + try { + await twonlyDB.messagesDao.handleMessagesOpened( + contactId, + messageUpdate.multipleTargetMessageIds, + fromTimestamp(messageUpdate.timestamp), ); - try { - await twonlyDB.messagesDao.handleMessageOpened( - contactId, - targetMessageId, - fromTimestamp(messageUpdate.timestamp), - ); - } catch (e) { - Log.warn(e); - } + } catch (e) { + Log.warn(e); } case EncryptedContent_MessageUpdate_Type.DELETE: if (!await isSender(contactId, messageUpdate.senderMessageId)) { diff --git a/lib/src/services/api/messages.dart b/lib/src/services/api/messages.dart index b4452eb..2e83f50 100644 --- a/lib/src/services/api/messages.dart +++ b/lib/src/services/api/messages.dart @@ -375,15 +375,18 @@ Future notifyContactAboutOpeningMessage( ), blocking: false, ); - for (final messageId in messageOtherIds) { - await twonlyDB.messagesDao.updateMessageId( - messageId, - MessagesCompanion( - openedAt: Value(actionAt), - openedByAll: Value(actionAt), - ), - ); - } + await twonlyDB.batch((batch) { + for (final messageId in messageOtherIds) { + batch.update( + twonlyDB.messages, + MessagesCompanion( + openedAt: Value(actionAt), + openedByAll: Value(actionAt), + ), + where: (tbl) => tbl.messageId.equals(messageId), + ); + } + }); await updateLastMessageId(contactId, biggestMessageId); } diff --git a/lib/src/views/chats/chat_messages.view.dart b/lib/src/views/chats/chat_messages.view.dart index 422f18f..df66b41 100644 --- a/lib/src/views/chats/chat_messages.view.dart +++ b/lib/src/views/chats/chat_messages.view.dart @@ -128,12 +128,6 @@ class _ChatMessagesViewState extends State { final msgStream = twonlyDB.messagesDao.watchByGroupId(group.groupId); messageSub = msgStream.listen((update) async { allMessages = update; - - /// In case a message is not open yet the message is updated, which will trigger this watch to be called again. - /// So as long as the Mutex is locked just return... - if (protectMessageUpdating.isLocked) { - // return; - } await protectMessageUpdating.protect(() async { await setMessages(update, groupActions); });