Line data Source code
1 : import 'dart:convert';
2 : import 'package:cwtch/cwtch/cwtch.dart';
3 : import 'package:cwtch/main.dart';
4 : import 'package:cwtch/models/appstate.dart';
5 : import 'package:cwtch/models/contact.dart';
6 : import 'package:cwtch/models/groupmembers.dart';
7 : import 'package:cwtch/models/hybridgroups.dart';
8 : import 'package:cwtch/models/profilelist.dart';
9 : import 'package:cwtch/models/remoteserver.dart';
10 : import 'package:cwtch/models/search.dart';
11 : import 'package:cwtch/models/servers.dart';
12 : import 'package:cwtch/notification_manager.dart';
13 :
14 : import 'package:cwtch/torstatus.dart';
15 :
16 : import '../config.dart';
17 : import '../errorHandler.dart';
18 : import '../settings.dart';
19 :
20 : typedef SeenMessageCallback = Function(String, int, DateTime);
21 :
22 : // Class that handles libcwtch-go events (received either via ffi with an isolate or gomobile over a method channel from kotlin)
23 : // Takes Notifiers and triggers them on appropriate events
24 : class CwtchNotifier {
25 : late ProfileListState profileCN;
26 : late Settings settings;
27 : late ErrorHandler error;
28 : late TorStatus torStatus;
29 : late NotificationsManager notificationManager;
30 : late AppState appState;
31 : late ServerListState serverListState;
32 : late HybridGroupsListState groupListState;
33 : late FlwtchState flwtchState;
34 :
35 : String? notificationSimple;
36 : String? notificationConversationInfo;
37 :
38 : SeenMessageCallback? seenMessageCallback;
39 :
40 0 : CwtchNotifier(
41 : ProfileListState pcn,
42 : Settings settingsCN,
43 : ErrorHandler errorCN,
44 : TorStatus torStatusCN,
45 : NotificationsManager notificationManagerP,
46 : AppState appStateCN,
47 : ServerListState serverListStateCN,
48 : HybridGroupsListState groupListStateCN,
49 : FlwtchState flwtchStateCN,
50 : ) {
51 0 : profileCN = pcn;
52 0 : settings = settingsCN;
53 0 : error = errorCN;
54 0 : torStatus = torStatusCN;
55 0 : notificationManager = notificationManagerP;
56 0 : appState = appStateCN;
57 0 : serverListState = serverListStateCN;
58 0 : groupListState = groupListStateCN;
59 0 : flwtchState = flwtchStateCN;
60 : }
61 :
62 0 : void l10nInit(String notificationSimple, String notificationConversationInfo) {
63 0 : this.notificationSimple = notificationSimple;
64 0 : this.notificationConversationInfo = notificationConversationInfo;
65 : }
66 :
67 0 : void setMessageSeenCallback(SeenMessageCallback callback) {
68 0 : seenMessageCallback = callback;
69 : }
70 :
71 0 : void handleMessage(String type, dynamic data) {
72 : // EnvironmentConfig.debugLog("NewEvent $type $data");
73 : switch (type) {
74 0 : case "CwtchStarted":
75 0 : if (data["Reload"] == "true" && profileCN.num > 0) {
76 : // don't reload...
77 : // unless we have loaded no profiles...then there isnt a risk and this
78 : // might be a first time (e.g. new apk, existing service)
79 : } else {
80 0 : flwtchState.cwtch.LoadProfiles(DefaultPassword);
81 : }
82 :
83 0 : appState.SetCwtchInit();
84 : break;
85 0 : case "CwtchStartError":
86 0 : appState.SetAppError(data["Error"]);
87 : break;
88 0 : case "NewPeer":
89 0 : if (data["tag"] == "v1-managedGroup") break;
90 :
91 : // else if tag != v1-defaultPassword then it is either encrypted OR it is an unencrypted account created during pre-beta...
92 0 : profileCN.add(
93 0 : data["Identity"],
94 0 : data["name"],
95 0 : data["private-name"],
96 0 : data["picture"],
97 0 : data["defaultPicture"],
98 0 : data["ContactsJson"],
99 0 : data["ServerList"],
100 0 : data["Online"] == "true",
101 0 : data["autostart"] == "true",
102 0 : data["tag"] != "v1-defaultPassword",
103 0 : data["appearOffline"] == "true",
104 : );
105 :
106 : // Update Profile Attributes
107 0 : flwtchState.cwtch.GetProfileAttribute(data["Identity"], "profile.profile-attribute-1").then((value) => profileCN.getProfile(data["Identity"])?.setAttribute(0, value));
108 0 : flwtchState.cwtch.GetProfileAttribute(data["Identity"], "profile.profile-attribute-2").then((value) => profileCN.getProfile(data["Identity"])?.setAttribute(1, value));
109 0 : flwtchState.cwtch.GetProfileAttribute(data["Identity"], "profile.profile-attribute-3").then((value) => profileCN.getProfile(data["Identity"])?.setAttribute(2, value));
110 0 : flwtchState.cwtch.GetProfileAttribute(data["Identity"], "profile.profile-status").then((value) => profileCN.getProfile(data["Identity"])?.setAvailabilityStatus(value ?? ""));
111 :
112 0 : profileCN.getProfile(data["Identity"])?.contactList.contacts.forEach((contact) {
113 0 : flwtchState.cwtch.GetConversationAttribute(data["Identity"], contact.identifier, "public.profile.profile-attribute-1").then((value) => contact.setAttribute(0, value));
114 0 : flwtchState.cwtch.GetConversationAttribute(data["Identity"], contact.identifier, "public.profile.profile-attribute-2").then((value) => contact.setAttribute(1, value));
115 0 : flwtchState.cwtch.GetConversationAttribute(data["Identity"], contact.identifier, "public.profile.profile-attribute-3").then((value) => contact.setAttribute(2, value));
116 0 : flwtchState.cwtch.GetConversationAttribute(data["Identity"], contact.identifier, "public.profile.profile-status").then((value) => contact.setAvailabilityStatus(value ?? ""));
117 : });
118 :
119 : break;
120 0 : case "ContactCreated":
121 0 : profileCN
122 0 : .getProfile(data["ProfileOnion"])
123 0 : ?.contactList
124 0 : .add(
125 0 : ContactInfoState(
126 0 : data["ProfileOnion"],
127 0 : int.parse(data["ConversationID"]),
128 0 : data["RemotePeer"],
129 0 : nickname: data["nick"],
130 0 : status: data["status"],
131 0 : imagePath: data["picture"],
132 0 : defaultImagePath: data["defaultPicture"],
133 0 : blocked: data["blocked"] == "true",
134 0 : accepted: data["accepted"] == "true",
135 0 : savePeerHistory: data["saveConversationHistory"] == null ? "DeleteHistoryConfirmed" : data["saveConversationHistory"],
136 0 : numMessages: int.parse(data["numMessages"]),
137 0 : numUnread: int.parse(data["unread"]),
138 : isGroup: false, // by definition
139 : server: null,
140 : archived: false,
141 0 : lastMessageTime: DateTime.now(), //show at the top of the contact list even if no messages yet
142 0 : notificationPolicy: data["notificationPolicy"] ?? "ConversationNotificationPolicy.Default",
143 0 : isManaged: data["managed"] == "true",
144 : ),
145 : );
146 : break;
147 0 : case "NewServer":
148 0 : serverListState.add(data["Onion"], data["ServerBundle"], data["Running"] == "true", data["Description"], data["Autostart"] == "true", data["StorageType"] == "storage-password");
149 : break;
150 0 : case "ServerIntentUpdate":
151 0 : var server = serverListState.getServer(data["Identity"]);
152 : if (server != null) {
153 0 : server.setRunning(data["Intent"] == "running");
154 : }
155 : break;
156 0 : case "ServerStatsUpdate":
157 0 : EnvironmentConfig.debugLog("ServerStatsUpdate $data");
158 0 : var totalMessages = int.parse(data["TotalMessages"]);
159 0 : var connections = int.parse(data["Connections"]);
160 0 : serverListState.updateServerStats(data["Identity"], totalMessages, connections);
161 : break;
162 0 : case "GroupCreated":
163 : // Retrieve Server Status from Cache...
164 : String status = "";
165 0 : RemoteServerInfoState? serverInfoState = profileCN.getProfile(data["ProfileOnion"])?.serverList.getServer(data["GroupServer"]);
166 : if (serverInfoState != null) {
167 0 : status = serverInfoState.status;
168 : }
169 0 : if (profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(int.parse(data["ConversationID"])) == null) {
170 0 : profileCN
171 0 : .getProfile(data["ProfileOnion"])
172 0 : ?.contactList
173 0 : .add(
174 0 : ContactInfoState(
175 0 : data["ProfileOnion"],
176 0 : int.parse(data["ConversationID"]),
177 0 : data["GroupID"],
178 : blocked: false, // we created
179 : accepted: true, // we created
180 0 : imagePath: data["picture"],
181 0 : defaultImagePath: data["picture"],
182 0 : nickname: data["GroupName"],
183 : status: status,
184 0 : server: data["GroupServer"],
185 : isGroup: true,
186 0 : lastMessageTime: DateTime.now(),
187 0 : notificationPolicy: data["notificationPolicy"] ?? "ConversationNotificationPolicy.Default",
188 : ),
189 : );
190 :
191 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.updateLastMessageReceivedTime(int.parse(data["ConversationID"]), DateTime.now());
192 : }
193 : break;
194 0 : case "PeerDeleted":
195 0 : profileCN.delete(data["Identity"]);
196 : // todo standarize
197 0 : error.handleUpdate("deleteprofile.success");
198 : break;
199 0 : case "ServerDeleted":
200 0 : error.handleUpdate("deletedserver." + data["Status"]);
201 0 : if (data["Status"] == "success") {
202 0 : serverListState.delete(data["Identity"]);
203 : }
204 : break;
205 0 : case "DeleteContact":
206 0 : var identifier = int.parse(data["ConversationID"]);
207 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.removeContact(identifier);
208 : break;
209 0 : case "PeerStateChange":
210 0 : ContactInfoState? contact = profileCN.getProfile(data["ProfileOnion"])?.contactList.findContact(data["RemotePeer"]);
211 : if (contact != null) {
212 0 : if (data["ConnectionState"] != null) {
213 0 : contact.status = data["ConnectionState"];
214 : }
215 0 : profileCN.getProfile(data["ProfileOnion"])?.resortContacts();
216 : }
217 : break;
218 0 : case "NewMessageFromPeer":
219 0 : var identifier = int.parse(data["ConversationID"]);
220 0 : var messageID = int.parse(data["Index"]);
221 0 : var timestamp = DateTime.tryParse(data['TimestampReceived'])!;
222 0 : var senderHandle = data['RemotePeer'];
223 0 : var senderImage = data['picture'];
224 0 : var isAuto = data['Auto'] == "true";
225 0 : String contenthash = data['ContentHash'];
226 :
227 : try {
228 0 : dynamic message = jsonDecode(data["Data"]);
229 0 : var overlay = int.parse(message['o'].toString());
230 0 : if (overlay > 1024 && overlay & 0x07 != 0) {
231 : break;
232 : }
233 : } catch (e) {
234 : // malformed message...
235 : }
236 :
237 0 : var selectedProfile = appState.selectedProfile == data["ProfileOnion"];
238 0 : var selectedConversation = selectedProfile && appState.selectedConversation == identifier;
239 0 : profileCN
240 0 : .getProfile(data["ProfileOnion"])
241 0 : ?.newMessage(identifier, messageID, timestamp, senderHandle, senderImage, isAuto, data["Data"], contenthash, selectedProfile, selectedConversation, "");
242 :
243 : // Now perform the notification logic...
244 0 : var notification = data["notification"];
245 0 : if (selectedConversation && seenMessageCallback != null) {
246 0 : seenMessageCallback!(data["ProfileOnion"]!, identifier, DateTime.now().toUtc());
247 : }
248 :
249 0 : if (notification == "SimpleEvent") {
250 0 : notificationManager.notify(notificationSimple ?? "New Message", "", 0);
251 0 : } else if (notification == "ContactInfo") {
252 0 : var contact = profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier);
253 0 : notificationManager.notify((notificationConversationInfo ?? "New Message from %1").replaceFirst("%1", (contact?.nickname ?? senderHandle.toString())), data["ProfileOnion"], identifier);
254 : }
255 0 : appState.notifyProfileUnread();
256 : break;
257 0 : case "PeerAcknowledgement":
258 : // We don't use these anymore, IndexedAcknowledgement is more suited to the UI front end...
259 : break;
260 0 : case "IndexedAcknowledgement":
261 0 : var conversation = int.parse(data["ConversationID"]);
262 0 : var messageID = int.parse(data["Index"]);
263 :
264 : // We only ever see acks from authenticated peers.
265 : // If the contact is marked as offline then override this - can happen when the contact is removed from the front
266 : // end during syncing.
267 0 : if (profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(conversation)!.isOnline() == false) {
268 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(conversation)!.status = "Authenticated";
269 : }
270 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(conversation)!.ackCache(messageID);
271 :
272 : break;
273 0 : case "NewMessageFromGroup":
274 0 : var identifier = int.parse(data["ConversationID"]);
275 0 : if (data["ProfileOnion"] != data["RemotePeer"]) {
276 0 : var idx = int.parse(data["Index"]);
277 0 : var senderHandle = data['RemotePeer'];
278 0 : var senderImage = data['picture'];
279 0 : var timestampSent = DateTime.tryParse(data['TimestampSent'])!;
280 0 : var contact = profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier);
281 0 : var currentTotal = contact!.totalMessages;
282 0 : var isAuto = data['Auto'] == "true";
283 0 : String contenthash = data['ContentHash'];
284 0 : var selectedProfile = appState.selectedProfile == data["ProfileOnion"];
285 0 : var selectedConversation = selectedProfile && appState.selectedConversation == identifier;
286 0 : var notification = data["notification"];
287 0 : var signature = data["Signature"];
288 :
289 : // Only bother to do anything if we know about the group and the provided index is greater than our current total...
290 0 : if (idx >= currentTotal) {
291 : // TODO: There are 2 timestamps associated with a new group message - time sent and time received.
292 : // Sent refers to the time a profile alleges they sent a message
293 : // Received refers to the time we actually saw the message from the server
294 : // These can obviously be very different for legitimate reasons.
295 : // We also maintain a relative hash-link through PreviousMessageSignature which is the ground truth for
296 : // order.
297 : // In the future we will want to combine these 3 ordering mechanisms into a cohesive view of the timeline
298 : // For now we perform some minimal checks on the sent timestamp to use to provide a useful ordering for honest contacts
299 : // and ensure that malicious contacts in groups can only set this timestamp to a value within the range of `last seen message time`
300 : // and `local now`.
301 0 : profileCN
302 0 : .getProfile(data["ProfileOnion"])
303 0 : ?.newMessage(identifier, idx, timestampSent, senderHandle, senderImage, isAuto, data["Data"], contenthash, selectedProfile, selectedConversation, signature);
304 0 : if (selectedConversation && seenMessageCallback != null) {
305 0 : seenMessageCallback!(data["ProfileOnion"]!, identifier, DateTime.now().toUtc());
306 : }
307 :
308 0 : if (notification == "SimpleEvent") {
309 0 : notificationManager.notify(notificationSimple ?? "New Message", "", 0);
310 0 : } else if (notification == "ContactInfo") {
311 0 : var contact = profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier);
312 0 : notificationManager.notify((notificationConversationInfo ?? "New Message from %1").replaceFirst("%1", (contact?.nickname ?? senderHandle.toString())), data["ProfileOnion"], identifier);
313 : }
314 0 : appState.notifyProfileUnread();
315 : }
316 0 : RemoteServerInfoState? server = profileCN.getProfile(data["ProfileOnion"])?.serverList.getServer(contact.server ?? "");
317 0 : server?.updateSyncProgressFor(timestampSent);
318 : } else {
319 : // This is dealt with by IndexedAcknowledgment
320 0 : EnvironmentConfig.debugLog("new message from group from yourself - this should not happen");
321 : }
322 : break;
323 0 : case "IndexedFailure":
324 0 : var identifier = int.parse(data["ConversationID"]);
325 0 : var contact = profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier);
326 0 : var messageID = int.parse(data["Index"]);
327 0 : contact?.errCache(messageID);
328 : break;
329 0 : case "AppError":
330 0 : EnvironmentConfig.debugLog("New App Error: $data");
331 : // special case for delete error (todo: standardize cwtch errors)
332 0 : if (data["Error"] == "Password did not match") {
333 0 : error.handleUpdate("deleteprofile.error");
334 0 : } else if (data["Data"] != null) {
335 0 : error.handleUpdate(data["Data"]);
336 : }
337 : break;
338 0 : case "UpdateGlobalSettings":
339 0 : settings.handleUpdate(jsonDecode(data["Data"]));
340 0 : appState.settingsLoaded = true;
341 : break;
342 0 : case "UpdatedProfileAttribute":
343 0 : if (data["Key"] == "public.profile.name") {
344 0 : profileCN.getProfile(data["ProfileOnion"])?.nickname = data["Data"];
345 0 : } else if (data["Key"].toString().startsWith("local.filesharing.")) {
346 0 : if (data["Key"].toString().endsWith(".path")) {
347 : // local.conversation.filekey.path
348 0 : List<String> keyparts = data["Key"].toString().split(".");
349 0 : if (keyparts.length == 5) {
350 0 : String filekey = keyparts[2] + "." + keyparts[3];
351 0 : profileCN.getProfile(data["ProfileOnion"])?.downloadSetPathForSender(filekey, data["Data"]);
352 : }
353 : }
354 0 : } else if (data["Key"].toString().startsWith("local.profile.private-name")) {
355 0 : profileCN.getProfile(data["ProfileOnion"])?.setPrivateName(data["Data"]);
356 0 : } else if (data["Key"].toString().startsWith("public.profile.profile-attribute")) {
357 : // ignore these events...
358 0 : } else if (data["Key"].toString().startsWith("public.profile.profile-status")) {
359 0 : profileCN.getProfile(data["ProfileOnion"])?.setAvailabilityStatus(data["Data"]);
360 : } else {
361 0 : EnvironmentConfig.debugLog("unhandled set attribute event: ${data['Key']}");
362 : }
363 : break;
364 0 : case "NetworkError":
365 0 : var isOnline = data["Status"] == "Success";
366 0 : profileCN.getProfile(data["ProfileOnion"])?.isOnline = isOnline;
367 : break;
368 0 : case "ACNStatus":
369 0 : EnvironmentConfig.debugLog("acn status: $data");
370 0 : torStatus.handleUpdate(int.parse(data["Progress"]), data["Status"]);
371 : break;
372 0 : case "ACNVersion":
373 0 : EnvironmentConfig.debugLog("acn version: $data");
374 0 : torStatus.updateVersion(data["Data"]);
375 : break;
376 0 : case "UpdateServerInfo":
377 0 : EnvironmentConfig.debugLog("NewEvent UpdateServerInfo $type $data");
378 0 : profileCN.getProfile(data["ProfileOnion"])?.replaceServers(data["ServerList"]);
379 : break;
380 0 : case "TokenManagerInfo":
381 : try {
382 0 : List<dynamic> associatedGroups = jsonDecode(data["Data"]);
383 0 : int count = int.parse(data["ServerTokenCount"]);
384 0 : associatedGroups.forEach((identifier) {
385 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(int.parse(identifier.toString()))!.antispamTickets = count;
386 : });
387 0 : EnvironmentConfig.debugLog("update server token count for $associatedGroups, $count");
388 : } catch (e) {
389 : // No tokens in data...
390 : }
391 : break;
392 0 : case "NewGroup":
393 0 : String invite = data["GroupInvite"].toString();
394 0 : if (invite.startsWith("torv3")) {
395 0 : String inviteJson = new String.fromCharCodes(base64Decode(invite.substring(5)));
396 0 : dynamic groupInvite = jsonDecode(inviteJson);
397 :
398 : // Retrieve Server Status from Cache...
399 : String status = "";
400 0 : RemoteServerInfoState? serverInfoState = profileCN.getProfile(data["ProfileOnion"])!.serverList.getServer(groupInvite["ServerHost"]);
401 : if (serverInfoState != null) {
402 0 : status = serverInfoState.status;
403 : }
404 :
405 0 : if (profileCN.getProfile(data["ProfileOnion"])?.contactList.findContact(groupInvite["GroupID"]) == null) {
406 0 : var identifier = int.parse(data["ConversationID"]);
407 0 : profileCN
408 0 : .getProfile(data["ProfileOnion"])
409 0 : ?.contactList
410 0 : .add(
411 0 : ContactInfoState(
412 0 : data["ProfileOnion"],
413 : identifier,
414 0 : groupInvite["GroupID"],
415 : blocked: false, // NewGroup only issued on accepting invite
416 : accepted: true, // NewGroup only issued on accepting invite
417 0 : imagePath: data["picture"],
418 0 : nickname: groupInvite["GroupName"],
419 0 : server: groupInvite["ServerHost"],
420 : status: status,
421 : isGroup: true,
422 0 : lastMessageTime: DateTime.now(),
423 : ),
424 : );
425 :
426 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.updateLastMessageReceivedTime(identifier, DateTime.fromMillisecondsSinceEpoch(0));
427 : }
428 : // request a new server update...
429 : // NOTE: In the future this should also update the TokenManagerInfo
430 : // This is not currently communicated by ServerUpdateInfo (but it probably should be)
431 0 : flwtchState.cwtch.PublishServerUpdate(data["ProfileOnion"]);
432 : }
433 : break;
434 0 : case "ServerStateChange":
435 : // Update the Server Cache
436 0 : profileCN.getProfile(data["ProfileOnion"])?.updateServerStatusCache(data["GroupServer"], data["ConnectionState"]);
437 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.contacts.forEach((contact) {
438 0 : if (contact.isGroup == true && contact.server == data["GroupServer"]) {
439 0 : contact.status = data["ConnectionState"];
440 : }
441 : });
442 0 : profileCN.getProfile(data["ProfileOnion"])?.resortContacts();
443 : break;
444 0 : case "UpdatedConversationAttribute":
445 0 : if (data["Path"] == "profile.name") {
446 0 : if (data["Data"].toString().trim().length > 0) {
447 : // Update locally on the UI...
448 0 : if (profileCN.getProfile(data["ProfileOnion"])?.contactList.findContact(data["RemotePeer"]) != null) {
449 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.findContact(data["RemotePeer"])!.nickname = data["Data"];
450 : }
451 : }
452 0 : } else if (data['Path'] == "profile.custom-profile-image") {
453 0 : EnvironmentConfig.debugLog("received ret val of custom profile image: $data");
454 0 : String fileKey = data['Data'];
455 0 : var contact = profileCN.getProfile(data["ProfileOnion"])?.contactList.findContact(data["RemotePeer"]);
456 : if (contact != null) {
457 0 : EnvironmentConfig.debugLog("waiting for download from $contact");
458 0 : profileCN.getProfile(data["ProfileOnion"])?.waitForDownloadComplete(contact.identifier, fileKey);
459 : }
460 0 : } else if (data['Path'] == "profile.profile-attribute-1" || data['Path'] == "profile.profile-attribute-2" || data['Path'] == "profile.profile-attribute-3") {
461 0 : var contact = profileCN.getProfile(data["ProfileOnion"])?.contactList.findContact(data["RemotePeer"]);
462 : if (contact != null) {
463 0 : switch (data['Path']) {
464 0 : case "profile.profile-attribute-1":
465 0 : contact.setAttribute(0, data["Data"]);
466 : break;
467 0 : case "profile.profile-attribute-2":
468 0 : contact.setAttribute(1, data["Data"]);
469 : break;
470 0 : case "profile.profile-attribute-3":
471 0 : contact.setAttribute(2, data["Data"]);
472 : break;
473 : }
474 : }
475 0 : } else if (data['Path'] == "profile.profile-status") {
476 0 : var contact = profileCN.getProfile(data["ProfileOnion"])?.contactList.findContact(data["RemotePeer"]);
477 : if (contact != null) {
478 0 : contact.setAvailabilityStatus(data['Data']);
479 : }
480 0 : } else if (data['Path'] == 'conversation.shadowed') {
481 0 : var contact = profileCN.getProfile(data["ProfileOnion"])?.contactList.findContact(data["RemotePeer"]);
482 : if (contact != null) {
483 0 : contact.isShadowed = data['Data'] == "true";
484 : }
485 0 : } else if (data['Path'] == 'filesharing.custom-profile-image-path') {
486 : //ignore
487 : } else {
488 0 : EnvironmentConfig.debugLog("unhandled ret val event: ${data['Path']}");
489 : }
490 0 : var c = profileCN.getProfile(data["ProfileOnion"])?.contactList.findContact(data["RemotePeer"]);
491 0 : if (c != null && c.isManaged) {
492 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.findContact(data["RemotePeer"])?.isOpPending = false;
493 : }
494 : break;
495 0 : case "ManifestSizeReceived":
496 0 : if (profileCN.getProfile(data["ProfileOnion"]) == null) {
497 : break;
498 : }
499 0 : if (!profileCN.getProfile(data["ProfileOnion"])!.downloadActive(data["FileKey"])) {
500 0 : profileCN.getProfile(data["ProfileOnion"])?.downloadUpdate(data["FileKey"], 0, 1);
501 : }
502 : break;
503 0 : case "ManifestSaved":
504 0 : profileCN.getProfile(data["ProfileOnion"])?.downloadMarkManifest(data["FileKey"]);
505 : break;
506 0 : case "FileDownloadProgressUpdate":
507 0 : var progress = int.parse(data["Progress"]);
508 0 : profileCN.getProfile(data["ProfileOnion"])?.downloadUpdate(data["FileKey"], progress, int.parse(data["FileSizeInChunks"]));
509 : // progress == -1 is a "download was interrupted" message and should contain a path
510 0 : if (progress < 0) {
511 0 : profileCN.getProfile(data["ProfileOnion"])?.downloadSetPath(data["FileKey"], data["FilePath"]);
512 : }
513 : break;
514 0 : case "FileDownloaded":
515 0 : profileCN.getProfile(data["ProfileOnion"])?.downloadMarkFinished(data["FileKey"], data["FilePath"]);
516 : break;
517 0 : case "ImportingProfileEvent":
518 : break;
519 0 : case "StartingStorageMigration":
520 0 : appState.SetModalState(ModalState.storageMigration);
521 : break;
522 0 : case "DoneStorageMigration":
523 0 : appState.SetModalState(ModalState.none);
524 : break;
525 0 : case "BlodeuweddSummary":
526 0 : var identifier = int.parse(data["ConversationID"]);
527 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier)?.updateSummaryEvent(data["Summary"]);
528 : break;
529 0 : case "BlodeuweddTranslation":
530 0 : var identifier = int.parse(data["ConversationID"]);
531 0 : var mid = int.parse(data["Index"]);
532 0 : EnvironmentConfig.debugLog("received translation event: $identifier $mid $data");
533 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier)?.updateTranslationEvent(mid, data["Translation"]);
534 : break;
535 0 : case "ACNInfo":
536 0 : var key = data["Key"];
537 0 : var handle = data["Handle"];
538 0 : if (key == "circuit") {
539 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.findContact(handle)?.acnCircuit = data["Data"];
540 : }
541 : break;
542 0 : case "SearchResult":
543 0 : String searchID = data["SearchID"];
544 0 : var conversationIdentifier = int.parse(data["ConversationID"]);
545 0 : var messageIndex = int.parse(data["RowIndex"]);
546 0 : profileCN.getProfile(data["ProfileOnion"])?.searchState.handleSearchResult(searchID, conversationIdentifier, messageIndex);
547 : break;
548 0 : case "NewHybridGroup":
549 0 : groupListState.add(data["Onion"], data["GroupBundle"], data["Running"] == "true", data["Description"], data["Autostart"] == "true", data["StorageType"] == "storage-password");
550 : break;
551 0 : case "HybridGroupDeleted":
552 0 : error.handleUpdate("deletedhybridgroup." + data["Status"]);
553 0 : if (data["Status"] == "success") {
554 0 : groupListState.delete(data["Identity"]);
555 : }
556 : break;
557 0 : case "HybridGroupStatsUpdate":
558 0 : EnvironmentConfig.debugLog("HybridGroupStatsUpdate $data");
559 0 : var totalMessages = int.parse(data["TotalMessages"]);
560 0 : var connections = int.parse(data["Connections"]);
561 0 : groupListState.updateGroupStats(data["Identity"], totalMessages, connections);
562 : break;
563 0 : case "MemberList":
564 0 : dynamic message = jsonDecode(data["Data"]);
565 0 : var identifier = int.parse(data["ConversationID"]);
566 0 : List<GroupMember> members = [];
567 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier)?.members = message["m"]
568 0 : .map<String, GroupMember>(
569 0 : (k, v) => MapEntry<String, GroupMember>(
570 : k,
571 0 : GroupMember(
572 : k, //v["Name"],
573 0 : v["AC"].cast<String, dynamic>(),
574 : //v["AC"].map<String,bool>((k,v)=>MapEntry<String,bool>(k, v as bool)) as Map<String,bool>
575 : ),
576 : ),
577 : )
578 0 : .values
579 0 : .toList();
580 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier)?.modeLine = message["g"];
581 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier)?.modeMask = message["h"];
582 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier)?.isOpPending = false;
583 0 : profileCN.getProfile(data["ProfileOnion"])?.notifyListeners();
584 : break;
585 0 : case "MessageUpdated":
586 0 : var identifier = int.parse(data["ConversationID"]);
587 0 : var contact = profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier);
588 0 : var messageID = int.parse(data["Index"]);
589 0 : String? sig = data.containsKey("Signature") ? data["Signature"] : null;
590 0 : profileCN.getProfile(data["ProfileOnion"])?.contactList.getContact(identifier)?.isOpPending = false;
591 0 : contact?.updateMessage(messageID, data["Message"], sig, jsonDecode(data["Attributes"])); //.cast<String,String>());
592 : break;
593 : default:
594 0 : EnvironmentConfig.debugLog("unhandled event: $type");
595 : }
596 : }
597 : }
|