From f57c2fec7933502a0d75633566b1f06484c0ddb3 Mon Sep 17 00:00:00 2001 From: otsmr Date: Tue, 11 Nov 2025 00:42:57 +0100 Subject: [PATCH] improve logging --- lib/src/database/daos/messages.dao.dart | 3 +- .../signal/connect_pre_key_store.dart | 2 +- lib/src/services/api.service.dart | 17 +++---- .../api/client2client/messages.c2c.dart | 44 ++++++++++++------- .../api/mediafiles/upload.service.dart | 2 +- lib/src/services/api/messages.dart | 2 +- lib/src/services/api/server_messages.dart | 25 ++++++----- .../mediafiles/compression.service.dart | 4 +- .../notifications/pushkeys.notifications.dart | 2 +- .../twonly_safe/common.twonly_safe.dart | 2 +- .../last_message_time.dart | 12 ++--- 11 files changed, 63 insertions(+), 52 deletions(-) diff --git a/lib/src/database/daos/messages.dao.dart b/lib/src/database/daos/messages.dao.dart index 3576abe..97bd5b7 100644 --- a/lib/src/database/daos/messages.dao.dart +++ b/lib/src/database/daos/messages.dao.dart @@ -119,11 +119,10 @@ class MessagesDao extends DatabaseAccessor with _$MessagesDaoMixin { ..where( (m) => m.groupId.equals(group.groupId) & - // m.messageId.equals(lastMessage.messageId).not() & (m.mediaStored.equals(true) & m.isDeletedFromSender.equals(true) | m.mediaStored.equals(false)) & - (m.openedByAll.isSmallerThanValue(deletionTime) | + (m.openedAt.isSmallerThanValue(deletionTime) | (m.isDeletedFromSender.equals(true) & m.createdAt.isSmallerThanValue(deletionTime))), )) diff --git a/lib/src/database/signal/connect_pre_key_store.dart b/lib/src/database/signal/connect_pre_key_store.dart index 61846c3..aa8ccd9 100644 --- a/lib/src/database/signal/connect_pre_key_store.dart +++ b/lib/src/database/signal/connect_pre_key_store.dart @@ -20,7 +20,7 @@ class ConnectPreKeyStore extends PreKeyStore { .get(); if (preKeyRecord.isEmpty) { throw InvalidKeyIdException( - '[PREKEY] No such preKey record! - $preKeyId', + '[PREKEY] No such preKey record!', ); } final preKey = preKeyRecord.first.preKey; diff --git a/lib/src/services/api.service.dart b/lib/src/services/api.service.dart index 1611db1..09370ab 100644 --- a/lib/src/services/api.service.dart +++ b/lib/src/services/api.service.dart @@ -78,12 +78,7 @@ class ApiService { await _channel!.ready; Log.info('websocket connected to $apiUrl'); return true; - } on WebSocketChannelException catch (e) { - if (!e.message - .toString() - .contains('No address associated with hostname')) { - Log.error('could not connect to api got: $e'); - } + } catch (_) { return false; } } @@ -311,17 +306,17 @@ class ApiService { final res = asResult(await _waitForResponse(seq)); if (res.isError) { - Log.error('got error from server: ${res.error}'); + Log.warn('Got error from server: ${res.error}'); if (res.error == ErrorCode.AppVersionOutdated) { globalCallbackAppIsOutdated(); - Log.error('App Version is OUTDATED.'); + Log.warn('App Version is OUTDATED.'); appIsOutdated = true; await close(() {}); return Result.error(ErrorCode.InternalError); } if (res.error == ErrorCode.NewDeviceRegistered) { globalCallbackNewDeviceRegistered(); - Log.error( + Log.warn( 'Device is disabled, as a newer device restore twonly Backup.', ); appIsOutdated = true; @@ -336,13 +331,13 @@ class ApiService { // this will send the request one more time. return sendRequestSync(request, authenticated: false); } else { - Log.error('session is not authenticated'); + Log.warn('Session is not authenticated'); return Result.error(ErrorCode.InternalError); } } } if (res.error == ErrorCode.UserIdNotFound && contactId != null) { - Log.error('Contact deleted their account $contactId.'); + Log.warn('Contact deleted their account $contactId.'); final contact = await twonlyDB.contactsDao .getContactByUserId(contactId) .getSingleOrNull(); diff --git a/lib/src/services/api/client2client/messages.c2c.dart b/lib/src/services/api/client2client/messages.c2c.dart index ff87010..8a50ac6 100644 --- a/lib/src/services/api/client2client/messages.c2c.dart +++ b/lib/src/services/api/client2client/messages.c2c.dart @@ -13,33 +13,45 @@ Future handleMessageUpdate( Log.info( 'Opened message $targetMessageId', ); - await twonlyDB.messagesDao.handleMessageOpened( - contactId, - targetMessageId, - fromTimestamp(messageUpdate.timestamp), - ); + try { + await twonlyDB.messagesDao.handleMessageOpened( + contactId, + targetMessageId, + fromTimestamp(messageUpdate.timestamp), + ); + } catch (e) { + Log.warn(e); + } } case EncryptedContent_MessageUpdate_Type.DELETE: if (!await isSender(contactId, messageUpdate.senderMessageId)) { return; } Log.info('Delete message ${messageUpdate.senderMessageId}'); - await twonlyDB.messagesDao.handleMessageDeletion( - contactId, - messageUpdate.senderMessageId, - fromTimestamp(messageUpdate.timestamp), - ); + try { + await twonlyDB.messagesDao.handleMessageDeletion( + contactId, + messageUpdate.senderMessageId, + fromTimestamp(messageUpdate.timestamp), + ); + } catch (e) { + Log.warn(e); + } case EncryptedContent_MessageUpdate_Type.EDIT_TEXT: if (!await isSender(contactId, messageUpdate.senderMessageId)) { return; } Log.info('Edit message ${messageUpdate.senderMessageId}'); - await twonlyDB.messagesDao.handleTextEdit( - contactId, - messageUpdate.senderMessageId, - messageUpdate.text, - fromTimestamp(messageUpdate.timestamp), - ); + try { + await twonlyDB.messagesDao.handleTextEdit( + contactId, + messageUpdate.senderMessageId, + messageUpdate.text, + fromTimestamp(messageUpdate.timestamp), + ); + } catch (e) { + Log.warn(e); + } } } diff --git a/lib/src/services/api/mediafiles/upload.service.dart b/lib/src/services/api/mediafiles/upload.service.dart index 9712f16..b3a87c6 100644 --- a/lib/src/services/api/mediafiles/upload.service.dart +++ b/lib/src/services/api/mediafiles/upload.service.dart @@ -44,7 +44,7 @@ Future finishStartedPreprocessing() async { } await startBackgroundMediaUpload(service); } catch (e) { - Log.error(e); + Log.warn(e); } } } diff --git a/lib/src/services/api/messages.dart b/lib/src/services/api/messages.dart index 4a8cb43..e210413 100644 --- a/lib/src/services/api/messages.dart +++ b/lib/src/services/api/messages.dart @@ -113,7 +113,7 @@ Future<(Uint8List, Uint8List?)?> tryToSendCompleteMessage({ ); if (resp.isError) { - Log.error('Could not transmit message $receiptId got ${resp.error}.'); + Log.warn('Could not transmit message got ${resp.error}.'); if (resp.error == ErrorCode.UserIdNotFound) { await twonlyDB.receiptsDao.deleteReceipt(receiptId); await twonlyDB.contactsDao.updateContact( diff --git a/lib/src/services/api/server_messages.dart b/lib/src/services/api/server_messages.dart index 58c37f8..b4b2ddd 100644 --- a/lib/src/services/api/server_messages.dart +++ b/lib/src/services/api/server_messages.dart @@ -1,5 +1,4 @@ import 'dart:async'; - import 'package:drift/drift.dart'; import 'package:hashlib/random.dart'; import 'package:mutex/mutex.dart'; @@ -63,7 +62,7 @@ Future handleClient2ClientMessage(int fromUserId, Uint8List body) async { await protectReceiptCheck.protect(() async { if (await twonlyDB.receiptsDao.isDuplicated(receiptId)) { - Log.error('Got duplicated message from the server. Ignoring it.'); + Log.warn('Got duplicated message from the server.'); return; } await twonlyDB.receiptsDao.gotReceipt(receiptId); @@ -135,19 +134,23 @@ Future handleClient2ClientMessage(int fromUserId, Uint8List body) async { ..receiptId = receiptId ..type = Message_Type.PLAINTEXT_CONTENT ..plaintextContent = responsePlaintextContent; - Log.error('Sending decryption error ($receiptId)'); + Log.error('Sending decryption error'); } else { response = Message()..type = Message_Type.SENDER_DELIVERY_RECEIPT; } - await twonlyDB.receiptsDao.insertReceipt( - ReceiptsCompanion( - receiptId: Value(receiptId), - contactId: Value(fromUserId), - message: Value(response.writeToBuffer()), - contactWillSendsReceipt: const Value(false), - ), - ); + try { + await twonlyDB.receiptsDao.insertReceipt( + ReceiptsCompanion( + receiptId: Value(receiptId), + contactId: Value(fromUserId), + message: Value(response.writeToBuffer()), + contactWillSendsReceipt: const Value(false), + ), + ); + } catch (e) { + Log.warn(e); + } await tryToSendCompleteMessage(receiptId: receiptId); } case Message_Type.TEST_NOTIFICATION: diff --git a/lib/src/services/mediafiles/compression.service.dart b/lib/src/services/mediafiles/compression.service.dart index e1da17c..45a5699 100644 --- a/lib/src/services/mediafiles/compression.service.dart +++ b/lib/src/services/mediafiles/compression.service.dart @@ -27,7 +27,7 @@ Future compressImage( if (compressedBytes == null) { throw Exception( - 'Could not compress media file: $sourceFile. Sending original file.', + 'Could not compress media file: Sending original file.', ); } @@ -50,7 +50,7 @@ Future compressImage( await destinationFile.writeAsBytes(compressedBytes); } catch (e) { - Log.error('$e'); + Log.warn('$e'); sourceFile.copySync(destinationFile.path); } diff --git a/lib/src/services/notifications/pushkeys.notifications.dart b/lib/src/services/notifications/pushkeys.notifications.dart index 613db6a..cc5ce19 100644 --- a/lib/src/services/notifications/pushkeys.notifications.dart +++ b/lib/src/services/notifications/pushkeys.notifications.dart @@ -325,7 +325,7 @@ Future encryptPushNotification( content.kind != PushKind.testNotification) { // this will be enforced after every app uses this system... :/ // return null; - Log.error('Using insecure key as the receiver does not send a push key!'); + Log.warn('Using insecure key as the receiver does not send a push key!'); await sendCipherText( toUserId, diff --git a/lib/src/services/twonly_safe/common.twonly_safe.dart b/lib/src/services/twonly_safe/common.twonly_safe.dart index 530328a..a8b3c4c 100644 --- a/lib/src/services/twonly_safe/common.twonly_safe.dart +++ b/lib/src/services/twonly_safe/common.twonly_safe.dart @@ -37,7 +37,7 @@ Future removeTwonlySafeFromServer() async { ); Log.info('Download deleted with: ${response.statusCode}'); } catch (e) { - Log.error('Could not connect to the server.'); + Log.error('Could not connect upload the backup.'); } } } diff --git a/lib/src/views/chats/chat_list_components/last_message_time.dart b/lib/src/views/chats/chat_list_components/last_message_time.dart index 6b82d30..609cc24 100644 --- a/lib/src/views/chats/chat_list_components/last_message_time.dart +++ b/lib/src/views/chats/chat_list_components/last_message_time.dart @@ -35,11 +35,13 @@ class _LastMessageTimeState extends State { lastMessageInSeconds = DateTime.now().difference(widget.dateTime!).inSeconds; } - setState(() { - if (lastMessageInSeconds < 0) { - lastMessageInSeconds = 0; - } - }); + if (mounted) { + setState(() { + if (lastMessageInSeconds < 0) { + lastMessageInSeconds = 0; + } + }); + } }); }