use sql match
Some checks are pending
Flutter analyze & test / flutter_analyze_and_test (push) Waiting to run

This commit is contained in:
otsmr 2026-03-01 21:38:32 +01:00
parent cd00910e86
commit 3806525653
4 changed files with 53 additions and 49 deletions

View file

@ -212,32 +212,41 @@ class MessagesDao extends DatabaseAccessor<TwonlyDB> with _$MessagesDaoMixin {
); );
} }
Future<void> handleMessageOpened( Future<void> handleMessagesOpened(
int contactId, int contactId,
String messageId, List<String> messageIds,
DateTime timestamp, DateTime timestamp,
) async { ) async {
await into(messageActions).insertOnConflictUpdate( await batch((batch) async {
for (final messageId in messageIds) {
batch.insert(
messageActions,
MessageActionsCompanion( MessageActionsCompanion(
messageId: Value(messageId), messageId: Value(messageId),
contactId: Value(contactId), contactId: Value(contactId),
type: const Value(MessageActionType.openedAt), type: const Value(MessageActionType.openedAt),
actionAt: Value(timestamp), actionAt: Value(timestamp),
), ),
mode: InsertMode.insertOrReplace,
); );
// Directly show as message opened as soon as one person has opened it }
final openedByAll =
await haveAllMembers(messageId, MessageActionType.openedAt) for (final messageId in messageIds) {
? clock.now() final isOpenedByAll =
: null; await haveAllMembers(messageId, MessageActionType.openedAt);
await twonlyDB.messagesDao.updateMessageId( final now = clock.now();
messageId,
batch.update(
twonlyDB.messages,
MessagesCompanion( MessagesCompanion(
openedAt: Value(clock.now()), openedAt: Value(now),
openedByAll: Value(openedByAll), openedByAll: Value(isOpenedByAll ? now : null),
), ),
where: (tbl) => tbl.messageId.equals(messageId),
); );
} }
});
}
Future<void> handleMessageAckByServer( Future<void> handleMessageAckByServer(
int contactId, int contactId,

View file

@ -9,20 +9,18 @@ Future<void> handleMessageUpdate(
) async { ) async {
switch (messageUpdate.type) { switch (messageUpdate.type) {
case EncryptedContent_MessageUpdate_Type.OPENED: case EncryptedContent_MessageUpdate_Type.OPENED:
for (final targetMessageId in messageUpdate.multipleTargetMessageIds) {
Log.info( Log.info(
'Opened message $targetMessageId', 'Opened message ${messageUpdate.multipleTargetMessageIds}',
); );
try { try {
await twonlyDB.messagesDao.handleMessageOpened( await twonlyDB.messagesDao.handleMessagesOpened(
contactId, contactId,
targetMessageId, messageUpdate.multipleTargetMessageIds,
fromTimestamp(messageUpdate.timestamp), fromTimestamp(messageUpdate.timestamp),
); );
} catch (e) { } catch (e) {
Log.warn(e); Log.warn(e);
} }
}
case EncryptedContent_MessageUpdate_Type.DELETE: case EncryptedContent_MessageUpdate_Type.DELETE:
if (!await isSender(contactId, messageUpdate.senderMessageId)) { if (!await isSender(contactId, messageUpdate.senderMessageId)) {
return; return;

View file

@ -375,15 +375,18 @@ Future<void> notifyContactAboutOpeningMessage(
), ),
blocking: false, blocking: false,
); );
await twonlyDB.batch((batch) {
for (final messageId in messageOtherIds) { for (final messageId in messageOtherIds) {
await twonlyDB.messagesDao.updateMessageId( batch.update(
messageId, twonlyDB.messages,
MessagesCompanion( MessagesCompanion(
openedAt: Value(actionAt), openedAt: Value(actionAt),
openedByAll: Value(actionAt), openedByAll: Value(actionAt),
), ),
where: (tbl) => tbl.messageId.equals(messageId),
); );
} }
});
await updateLastMessageId(contactId, biggestMessageId); await updateLastMessageId(contactId, biggestMessageId);
} }

View file

@ -128,12 +128,6 @@ class _ChatMessagesViewState extends State<ChatMessagesView> {
final msgStream = twonlyDB.messagesDao.watchByGroupId(group.groupId); final msgStream = twonlyDB.messagesDao.watchByGroupId(group.groupId);
messageSub = msgStream.listen((update) async { messageSub = msgStream.listen((update) async {
allMessages = update; 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 protectMessageUpdating.protect(() async {
await setMessages(update, groupActions); await setMessages(update, groupActions);
}); });