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, int contactId,
String messageId, List<String> messageIds,
DateTime timestamp, DateTime timestamp,
) async { ) async {
await into(messageActions).insertOnConflictUpdate( await batch((batch) async {
MessageActionsCompanion( for (final messageId in messageIds) {
messageId: Value(messageId), batch.insert(
contactId: Value(contactId), messageActions,
type: const Value(MessageActionType.openedAt), MessageActionsCompanion(
actionAt: Value(timestamp), messageId: Value(messageId),
), contactId: Value(contactId),
); type: const Value(MessageActionType.openedAt),
// Directly show as message opened as soon as one person has opened it actionAt: Value(timestamp),
final openedByAll = ),
await haveAllMembers(messageId, MessageActionType.openedAt) mode: InsertMode.insertOrReplace,
? clock.now() );
: null; }
await twonlyDB.messagesDao.updateMessageId(
messageId, for (final messageId in messageIds) {
MessagesCompanion( final isOpenedByAll =
openedAt: Value(clock.now()), await haveAllMembers(messageId, MessageActionType.openedAt);
openedByAll: Value(openedByAll), 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( Future<void> handleMessageAckByServer(

View file

@ -9,19 +9,17 @@ 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 ${messageUpdate.multipleTargetMessageIds}',
'Opened message $targetMessageId', );
try {
await twonlyDB.messagesDao.handleMessagesOpened(
contactId,
messageUpdate.multipleTargetMessageIds,
fromTimestamp(messageUpdate.timestamp),
); );
try { } catch (e) {
await twonlyDB.messagesDao.handleMessageOpened( Log.warn(e);
contactId,
targetMessageId,
fromTimestamp(messageUpdate.timestamp),
);
} catch (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)) {

View file

@ -375,15 +375,18 @@ Future<void> notifyContactAboutOpeningMessage(
), ),
blocking: false, blocking: false,
); );
for (final messageId in messageOtherIds) { await twonlyDB.batch((batch) {
await twonlyDB.messagesDao.updateMessageId( for (final messageId in messageOtherIds) {
messageId, batch.update(
MessagesCompanion( twonlyDB.messages,
openedAt: Value(actionAt), MessagesCompanion(
openedByAll: Value(actionAt), openedAt: 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);
}); });