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,31 +212,40 @@ class MessagesDao extends DatabaseAccessor<TwonlyDB> with _$MessagesDaoMixin {
);
}
Future<void> handleMessageOpened(
Future<void> handleMessagesOpened(
int contactId,
String messageId,
List<String> 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<void> handleMessageAckByServer(

View file

@ -9,19 +9,17 @@ Future<void> 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)) {

View file

@ -375,15 +375,18 @@ Future<void> 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);
}

View file

@ -128,12 +128,6 @@ class _ChatMessagesViewState extends State<ChatMessagesView> {
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);
});