From 058fba7e692cf3df33017f08cd530c7d240f014b Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Mon, 26 Feb 2024 18:26:49 -0800 Subject: [PATCH] Sync cache for acks Also remove defunct calls. --- .../kotlin/im/cwtch/flwtch/MainActivity.kt | 3 -- lib/cwtch/cwtch.dart | 2 -- lib/cwtch/cwtchNotifier.dart | 4 ++- lib/cwtch/ffi.dart | 8 ----- lib/cwtch/gomobile.dart | 6 ---- lib/models/message.dart | 35 +++++++++++++++++-- 6 files changed, 35 insertions(+), 23 deletions(-) diff --git a/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt b/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt index 7d19a003..5fc51764 100644 --- a/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt +++ b/android/app/src/main/kotlin/im/cwtch/flwtch/MainActivity.kt @@ -577,9 +577,6 @@ class MainActivity: FlutterActivity() { result.success(Cwtch.searchConversations(profile, pattern)) return } - "ReconnectCwtchForeground" -> { - Cwtch.reconnectCwtchForeground() - } "Shutdown" -> { Cwtch.shutdownCwtch(); } diff --git a/lib/cwtch/cwtch.dart b/lib/cwtch/cwtch.dart index c2c04881..c11945fc 100644 --- a/lib/cwtch/cwtch.dart +++ b/lib/cwtch/cwtch.dart @@ -11,8 +11,6 @@ abstract class Cwtch { // ignore: non_constant_identifier_names Future Start(); - // ignore: non_constant_identifier_names - Future ReconnectCwtchForeground(); Future getCwtchDir(); diff --git a/lib/cwtch/cwtchNotifier.dart b/lib/cwtch/cwtchNotifier.dart index b0c03958..7e9a3179 100644 --- a/lib/cwtch/cwtchNotifier.dart +++ b/lib/cwtch/cwtchNotifier.dart @@ -3,6 +3,7 @@ import 'package:cwtch/cwtch/cwtch.dart'; import 'package:cwtch/main.dart'; import 'package:cwtch/models/appstate.dart'; import 'package:cwtch/models/contact.dart'; +import 'package:cwtch/models/message.dart'; import 'package:cwtch/models/profilelist.dart'; import 'package:cwtch/models/remoteserver.dart'; import 'package:cwtch/models/servers.dart'; @@ -58,7 +59,7 @@ class CwtchNotifier { // EnvironmentConfig.debugLog("NewEvent $type $data"); switch (type) { case "CwtchStarted": - if (data["Reload"] == "true" && profileCN.num > 0 ) { + if (data["Reload"] == "true" && profileCN.num > 0) { // don't reload... // unless we have loaded no profiles...then there isnt a risk and this // might be a first time (e.g. new apk, existing service) @@ -66,6 +67,7 @@ class CwtchNotifier { EnvironmentConfig.debugLog("Reloading....${data}"); flwtchState.cwtch.LoadProfiles(DefaultPassword); } + appState.SetCwtchInit(); break; case "CwtchStartError": diff --git a/lib/cwtch/ffi.dart b/lib/cwtch/ffi.dart index 32d7ed69..62505007 100644 --- a/lib/cwtch/ffi.dart +++ b/lib/cwtch/ffi.dart @@ -270,14 +270,6 @@ class CwtchFfi implements Cwtch { return _cwtchDir; } - // ignore: non_constant_identifier_names - Future ReconnectCwtchForeground() async { - var reconnectCwtch = library.lookup>("c_ReconnectCwtchForeground"); - // ignore: non_constant_identifier_names - final ReconnectCwtchForeground = reconnectCwtch.asFunction(); - ReconnectCwtchForeground(); - } - // Called on object being disposed to (presumably on app close) to close the isolate that's listening to libcwtch-go events @override void dispose() { diff --git a/lib/cwtch/gomobile.dart b/lib/cwtch/gomobile.dart index e75b95fe..d3db98c0 100644 --- a/lib/cwtch/gomobile.dart +++ b/lib/cwtch/gomobile.dart @@ -86,12 +86,6 @@ class CwtchGomobile implements Cwtch { cwtchPlatform.invokeMethod("Start", {"appDir": _cwtchDir, "torPath": torPath}); } - @override - // ignore: non_constant_identifier_names - Future ReconnectCwtchForeground() async { - cwtchPlatform.invokeMethod("ReconnectCwtchForeground", {}); - } - // Handle libcwtch-go events (received via kotlin) and dispatch to the cwtchNotifier Future _handleAppbusEvent(MethodCall call) async { final String json = call.arguments; diff --git a/lib/models/message.dart b/lib/models/message.dart index 1557e16d..1e166ff6 100644 --- a/lib/models/message.dart +++ b/lib/models/message.dart @@ -64,6 +64,7 @@ Message compileOverlay(MessageInfo messageInfo) { abstract class CacheHandler { Future get(Cwtch cwtch, String profileOnion, int conversationIdentifier, MessageCache cache); + Future sync(Cwtch cwtch, String profileOnion, int conversationIdentifier, MessageCache cache); } class ByIndex implements CacheHandler { @@ -128,7 +129,7 @@ class ByIndex implements CacheHandler { List messagesWrapper = jsonDecode(msgs); for (; i < messagesWrapper.length; i++) { - var messageInfo = messageWrapperToInfo(profileOnion, conversationIdentifier, messagesWrapper[i]); + var messageInfo = MessageWrapperToInfo(profileOnion, conversationIdentifier, messagesWrapper[i]); cache.addIndexed(messageInfo, start + i); } } catch (e, stacktrace) { @@ -143,6 +144,13 @@ class ByIndex implements CacheHandler { void add(MessageCache cache, MessageInfo messageInfo) { cache.addIndexed(messageInfo, index); } + + @override + Future sync(Cwtch cwtch, String profileOnion, int conversationIdentifier, MessageCache cache) { + EnvironmentConfig.debugLog("performing a resync on message ${index}"); + fetchAndProcess(index, 1, cwtch, profileOnion, conversationIdentifier, cache); + return get(cwtch, profileOnion, conversationIdentifier, cache); + } } class ById implements CacheHandler { @@ -172,6 +180,11 @@ class ById implements CacheHandler { } return fetch(cwtch, profileOnion, conversationIdentifier, cache); } + + @override + Future sync(Cwtch cwtch, String profileOnion, int conversationIdentifier, MessageCache cache) { + return get(cwtch, profileOnion, conversationIdentifier, cache); + } } class ByContentHash implements CacheHandler { @@ -200,6 +213,11 @@ class ByContentHash implements CacheHandler { } return fetch(cwtch, profileOnion, conversationIdentifier, cache); } + + @override + Future sync(Cwtch cwtch, String profileOnion, int conversationIdentifier, MessageCache cache) { + return get(cwtch, profileOnion, conversationIdentifier, cache); + } } List getReplies(MessageCache cache, int messageIdentifier) { @@ -257,6 +275,16 @@ Future messageHandler(BuildContext context, String profileOnion, int co MessageInfo? messageInfo = await cacheHandler.get(cwtch, profileOnion, conversationIdentifier, cache); + if (messageInfo != null) { + if (messageInfo.metadata.ackd == false) { + if (messageInfo.metadata.lastChecked == null || messageInfo.metadata.lastChecked!.difference(DateTime.now()).abs().inSeconds > 30) { + messageInfo.metadata.lastChecked = DateTime.now(); + // NOTE: Only ByIndex lookups will trigger + messageInfo = await cacheHandler.sync(cwtch, profileOnion, conversationIdentifier, cache); + } + } + } + if (messageInfo != null) { return compileOverlay(messageInfo); } else { @@ -272,14 +300,14 @@ MessageInfo? messageJsonToInfo(String profileOnion, int conversationIdentifier, return null; } - return messageWrapperToInfo(profileOnion, conversationIdentifier, messageWrapper); + return MessageWrapperToInfo(profileOnion, conversationIdentifier, messageWrapper); } catch (e, stacktrace) { EnvironmentConfig.debugLog("message handler exception on parse message and cache: " + e.toString() + " " + stacktrace.toString()); return null; } } -MessageInfo messageWrapperToInfo(String profileOnion, int conversationIdentifier, dynamic messageWrapper) { +MessageInfo MessageWrapperToInfo(String profileOnion, int conversationIdentifier, dynamic messageWrapper) { // Construct the initial metadata var messageID = messageWrapper['ID']; var timestamp = DateTime.tryParse(messageWrapper['Timestamp'])!; @@ -312,6 +340,7 @@ class MessageMetadata extends ChangeNotifier { final String? signature; final String contenthash; + DateTime? lastChecked; dynamic get attributes => this._attributes;