From 5e485b6327278fcb899c65384d34e31d881eca7b Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Thu, 6 Jan 2022 16:54:25 -0500 Subject: [PATCH] Rework accept/block data flow to map to new storage; fix add group server list falls back to onion when no description --- LIBCWTCH-GO-MACOS.version | 2 +- LIBCWTCH-GO.version | 2 +- .../kotlin/im/cwtch/flwtch/FlwtchWorker.kt | 5 ++ lib/cwtch/cwtch.dart | 2 + lib/cwtch/cwtchNotifier.dart | 16 +++---- lib/cwtch/ffi.dart | 11 +++++ lib/cwtch/gomobile.dart | 10 +++- lib/model.dart | 46 +++++++++---------- lib/views/addcontactview.dart | 2 +- lib/views/peersettingsview.dart | 26 ++--------- lib/widgets/contactrow.dart | 4 +- 11 files changed, 65 insertions(+), 61 deletions(-) diff --git a/LIBCWTCH-GO-MACOS.version b/LIBCWTCH-GO-MACOS.version index 052cc374..ddc4b6d9 100644 --- a/LIBCWTCH-GO-MACOS.version +++ b/LIBCWTCH-GO-MACOS.version @@ -1 +1 @@ -2021-12-20-18-47-v1.5.2 \ No newline at end of file +2022-01-06-17-13-v1.5.3 \ No newline at end of file diff --git a/LIBCWTCH-GO.version b/LIBCWTCH-GO.version index 6da6d31b..55034f0f 100644 --- a/LIBCWTCH-GO.version +++ b/LIBCWTCH-GO.version @@ -1 +1 @@ -2021-12-20-23-47-v1.5.2 \ No newline at end of file +2022-01-06-22-13-v1.5.3 \ No newline at end of file diff --git a/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt b/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt index 19965a88..41862380 100644 --- a/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt +++ b/android/app/src/main/kotlin/im/cwtch/flwtch/FlwtchWorker.kt @@ -225,6 +225,11 @@ class FlwtchWorker(context: Context, parameters: WorkerParameters) : val conversation = a.getInt("conversation").toLong() Cwtch.blockContact(profile, conversation) } + "UnblockContact" -> { + val profile = (a.get("ProfileOnion") as? String) ?: "" + val conversation = a.getInt("conversation").toLong() + Cwtch.unblockContact(profile, conversation) + } "SendMessage" -> { val profile = (a.get("ProfileOnion") as? String) ?: "" val conversation = a.getInt("conversation").toLong() diff --git a/lib/cwtch/cwtch.dart b/lib/cwtch/cwtch.dart index 46e4c754..8577b2a5 100644 --- a/lib/cwtch/cwtch.dart +++ b/lib/cwtch/cwtch.dart @@ -32,6 +32,8 @@ abstract class Cwtch { void AcceptContact(String profileOnion, int contactHandle); // ignore: non_constant_identifier_names void BlockContact(String profileOnion, int contactHandle); + // ignore: non_constant_identifier_names + void UnblockContact(String profileOnion, int contactHandle); // ignore: non_constant_identifier_names Future GetMessage(String profile, int handle, int index); diff --git a/lib/cwtch/cwtchNotifier.dart b/lib/cwtch/cwtchNotifier.dart index 3c433904..5c37c30c 100644 --- a/lib/cwtch/cwtchNotifier.dart +++ b/lib/cwtch/cwtchNotifier.dart @@ -50,7 +50,8 @@ class CwtchNotifier { profileCN.add(data["Identity"], data["name"], data["picture"], data["ContactsJson"], data["ServerList"], data["Online"] == "true", data["tag"] != "v1-defaultPassword"); break; case "ContactCreated": - EnvironmentConfig.debugLog("NewServer $data"); + EnvironmentConfig.debugLog("ContactCreated $data"); + profileCN.getProfile(data["ProfileOnion"])?.contactList.add(ContactInfoState( data["ProfileOnion"], int.parse(data["ConversationID"]), @@ -58,7 +59,8 @@ class CwtchNotifier { nickname: data["nick"], status: data["status"], imagePath: data["picture"], - authorization: stringToContactAuthorization(data["authorization"]), + blocked: data["blocked"] == "true", + accepted: data["accepted"] == "true", savePeerHistory: data["saveConversationHistory"] == null ? "DeleteHistoryConfirmed" : data["saveConversationHistory"], numMessages: int.parse(data["numMessages"]), numUnread: int.parse(data["unread"]), @@ -94,7 +96,8 @@ class CwtchNotifier { } if (profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(int.parse(data["ConversationID"])) == null) { profileCN.getProfile(data["ProfileOnion"])?.contactList.add(ContactInfoState(data["ProfileOnion"], int.parse(data["ConversationID"]), data["GroupID"], - authorization: ContactAuthorization.approved, + blocked: data["blocked"] == "true", + accepted: data["accepted"] == "true", imagePath: data["PicturePath"], nickname: data["GroupName"], status: status, @@ -127,10 +130,6 @@ class CwtchNotifier { if (data["ConnectionState"] != null) { contact.status = data["ConnectionState"]; } - if (data["authorization"] != null) { - contact.authorization = stringToContactAuthorization(data["authorization"]); - } - // contact.[status/isBlocked] might change the list's sort order profileCN.getProfile(data["ProfileOnion"])?.contactList.resort(); } break; @@ -289,7 +288,8 @@ class CwtchNotifier { if (profileCN.getProfile(data["ProfileOnion"])?.contactList.findContact(groupInvite["GroupID"]) == null) { var identifier = int.parse(data["ConversationID"]); profileCN.getProfile(data["ProfileOnion"])?.contactList.add(ContactInfoState(data["ProfileOnion"], identifier, groupInvite["GroupID"], - authorization: ContactAuthorization.approved, + blocked: data["blocked"] == "true", + accepted: data["accepted"] == "true", imagePath: data["PicturePath"], nickname: groupInvite["GroupName"], server: groupInvite["ServerHost"], diff --git a/lib/cwtch/ffi.dart b/lib/cwtch/ffi.dart index ebd444cb..baafc04e 100644 --- a/lib/cwtch/ffi.dart +++ b/lib/cwtch/ffi.dart @@ -342,6 +342,17 @@ class CwtchFfi implements Cwtch { malloc.free(u1); } + @override + // ignore: non_constant_identifier_names + void UnblockContact(String profileOnion, int contactHandle) { + var unblockContact = library.lookup>("c_UnblockContact"); + // ignore: non_constant_identifier_names + final UnblockContact = unblockContact.asFunction(); + final u1 = profileOnion.toNativeUtf8(); + UnblockContact(u1, u1.length, contactHandle); + malloc.free(u1); + } + @override // ignore: non_constant_identifier_names void SendMessage(String profileOnion, int contactHandle, String message) { diff --git a/lib/cwtch/gomobile.dart b/lib/cwtch/gomobile.dart index 29bf082f..73d30bec 100644 --- a/lib/cwtch/gomobile.dart +++ b/lib/cwtch/gomobile.dart @@ -111,7 +111,7 @@ class CwtchGomobile implements Cwtch { @override // ignore: non_constant_identifier_names void AcceptContact(String profileOnion, int conversation) { - cwtchPlatform.invokeMethod("AcceptContact", {"ProfileOnion": profileOnion, "conversation": conversation}); + cwtchPlatform.invokeMethod("AcceptConversation", {"ProfileOnion": profileOnion, "conversation": conversation}); } @override @@ -120,6 +120,12 @@ class CwtchGomobile implements Cwtch { cwtchPlatform.invokeMethod("BlockContact", {"ProfileOnion": profileOnion, "conversation": conversation}); } + @override + // ignore: non_constant_identifier_names + void UnblockContact(String profileOnion, int conversation) { + cwtchPlatform.invokeMethod("UnblockContact", {"ProfileOnion": profileOnion, "conversation": conversation}); + } + @override // ignore: non_constant_identifier_names void SendMessage(String profileOnion, int conversation, String message) { @@ -207,7 +213,7 @@ class CwtchGomobile implements Cwtch { @override // ignore: non_constant_identifier_names void SetConversationAttribute(String profile, int conversation, String key, String val) { - cwtchPlatform.invokeMethod("SetContactAttribute", {"ProfileOnion": profile, "conversation": conversation, "Key": key, "Val": val}); + cwtchPlatform.invokeMethod("SetConversationAttribute", {"ProfileOnion": profile, "conversation": conversation, "Key": key, "Val": val}); } @override diff --git a/lib/model.dart b/lib/model.dart index c9915d2f..3aec14cb 100644 --- a/lib/model.dart +++ b/lib/model.dart @@ -286,7 +286,8 @@ class ProfileInfoState extends ChangeNotifier { nickname: contact["name"], status: contact["status"], imagePath: contact["picture"], - authorization: stringToContactAuthorization(contact["authorization"]), + accepted: contact["accepted"], + blocked: contact["blocked"], savePeerHistory: contact["saveConversationHistory"], numMessages: contact["numMessages"], numUnread: contact["numUnread"], @@ -382,10 +383,11 @@ class ProfileInfoState extends ChangeNotifier { if (contactsJson != null && contactsJson != "" && contactsJson != "null") { List contacts = jsonDecode(contactsJson); contacts.forEach((contact) { - var profileContact = this._contacts.getContact(contact["onion"]); + var profileContact = this._contacts.getContact(contact["identifier"]); if (profileContact != null) { profileContact.status = contact["status"]; profileContact.totalMessages = contact["numMessages"]; + profileContact.unreadMessages = contact["numUnread"]; profileContact.lastMessageTime = DateTime.fromMillisecondsSinceEpoch(1000 * int.parse(contact["lastMsgTime"])); } else { this._contacts.add(ContactInfoState( @@ -395,7 +397,8 @@ class ProfileInfoState extends ChangeNotifier { nickname: contact["name"], status: contact["status"], imagePath: contact["picture"], - authorization: stringToContactAuthorization(contact["authorization"]), + accepted: contact["accepted"], + blocked: contact["blocked"], savePeerHistory: contact["saveConversationHistory"], numMessages: contact["numMessages"], numUnread: contact["numUnread"], @@ -406,6 +409,7 @@ class ProfileInfoState extends ChangeNotifier { } }); } + this._contacts.resort(); } void downloadInit(String fileKey, int numChunks) { @@ -534,19 +538,6 @@ String prettyBytes(int bytes) { } } -enum ContactAuthorization { unknown, approved, blocked } - -ContactAuthorization stringToContactAuthorization(String authStr) { - switch (authStr) { - case "approved": - return ContactAuthorization.approved; - case "blocked": - return ContactAuthorization.blocked; - default: - return ContactAuthorization.unknown; - } -} - class MessageCache { final MessageMetadata metadata; final String wrapper; @@ -559,7 +550,8 @@ class ContactInfoState extends ChangeNotifier { final String onion; late String _nickname; - late ContactAuthorization _authorization; + late bool _accepted; + late bool _blocked; late String _status; late String _imagePath; late String _savePeerHistory; @@ -579,7 +571,8 @@ class ContactInfoState extends ChangeNotifier { ContactInfoState(this.profileOnion, this.identifier, this.onion, {nickname = "", isGroup = false, - authorization = ContactAuthorization.unknown, + accepted = false, + blocked = false, status = "", imagePath = "", savePeerHistory = "DeleteHistoryConfirmed", @@ -590,7 +583,8 @@ class ContactInfoState extends ChangeNotifier { archived = false}) { this._nickname = nickname; this._isGroup = isGroup; - this._authorization = authorization; + this._accepted = accepted; + this._blocked = blocked; this._status = status; this._imagePath = imagePath; this._totalMessages = numMessages; @@ -633,13 +627,17 @@ class ContactInfoState extends ChangeNotifier { notifyListeners(); } - bool get isBlocked => this._authorization == ContactAuthorization.blocked; + bool get isBlocked => this._blocked; - bool get isInvitation => this._authorization == ContactAuthorization.unknown; + bool get isInvitation => !this._blocked && !this._accepted; - ContactAuthorization get authorization => this._authorization; - set authorization(ContactAuthorization newAuth) { - this._authorization = newAuth; + set accepted(bool newVal) { + this._accepted = newVal; + notifyListeners(); + } + + set blocked(bool newVal) { + this._blocked = newVal; notifyListeners(); } diff --git a/lib/views/addcontactview.dart b/lib/views/addcontactview.dart index bb0a2fc1..0d920f6c 100644 --- a/lib/views/addcontactview.dart +++ b/lib/views/addcontactview.dart @@ -209,7 +209,7 @@ class _AddContactViewState extends State { return DropdownMenuItem( value: serverInfo.onion, child: Text( - serverInfo.description, + serverInfo.description.isNotEmpty ? serverInfo.description : serverInfo.onion, overflow: TextOverflow.ellipsis, ), ); diff --git a/lib/views/peersettingsview.dart b/lib/views/peersettingsview.dart index 57f42b40..8646a648 100644 --- a/lib/views/peersettingsview.dart +++ b/lib/views/peersettingsview.dart @@ -108,33 +108,15 @@ class _PeerSettingsViewState extends State { title: Text(AppLocalizations.of(context)!.blockBtn, style: TextStyle(color: settings.current().mainTextColor)), value: Provider.of(context).isBlocked, onChanged: (bool blocked) { - // Save local blocked status - if (blocked) { - Provider.of(context, listen: false).authorization = ContactAuthorization.blocked; - } else { - Provider.of(context, listen: false).authorization = ContactAuthorization.unknown; - } + Provider.of(context, listen: false).blocked = blocked; - // Save New peer authorization var profileOnion = Provider.of(context, listen: false).profileOnion; - - var onion = Provider.of(context, listen: false).onion; - Provider.of(context, listen: false).nickname = ctrlrNick.text; + var identifier = Provider.of(context, listen: false).identifier; if (blocked) { - final setPeerAttribute = { - "EventType": "UpdatePeerAuthorization", - "Data": {"RemotePeer": onion, "Authorization": "blocked"}, - }; - final setPeerAttributeJson = jsonEncode(setPeerAttribute); - Provider.of(context, listen: false).cwtch.SendProfileEvent(profileOnion, setPeerAttributeJson); + Provider.of(context, listen: false).cwtch.BlockContact(profileOnion, identifier); } else { - final setPeerAttribute = { - "EventType": "UpdatePeerAuthorization", - "Data": {"RemotePeer": onion, "Authorization": "authorized"}, - }; - final setPeerAttributeJson = jsonEncode(setPeerAttribute); - Provider.of(context, listen: false).cwtch.SendProfileEvent(profileOnion, setPeerAttributeJson); + Provider.of(context, listen: false).cwtch.UnblockContact(profileOnion, identifier); } }, activeTrackColor: settings.theme.defaultButtonColor, diff --git a/lib/widgets/contactrow.dart b/lib/widgets/contactrow.dart index 5872dddb..cdba90de 100644 --- a/lib/widgets/contactrow.dart +++ b/lib/widgets/contactrow.dart @@ -111,14 +111,14 @@ class _ContactRowState extends State { } void _btnApprove() { - // Update the UI - Provider.of(context, listen: false).authorization = ContactAuthorization.approved; + Provider.of(context, listen: false).accepted = true; Provider.of(context, listen: false) .cwtch .AcceptContact(Provider.of(context, listen: false).profileOnion, Provider.of(context, listen: false).identifier); } void _btnReject() { + Provider.of(context, listen: false).blocked = true; ContactInfoState contact = Provider.of(context, listen: false); if (contact.isGroup == true) { // FIXME This flow is incrorect. Groups never just show up on the contact list anymore