Line data Source code
1 : import 'dart:convert';
2 : import 'dart:math';
3 :
4 : import 'package:cwtch/config.dart';
5 : import 'package:cwtch/models/remoteserver.dart';
6 : import 'package:cwtch/models/search.dart';
7 : import 'package:flutter/widgets.dart';
8 : import 'package:provider/provider.dart';
9 : import 'package:scrollable_positioned_list/scrollable_positioned_list.dart';
10 :
11 : import '../main.dart';
12 : import '../themes/opaque.dart';
13 : import '../views/contactsview.dart';
14 : import 'contact.dart';
15 : import 'contactlist.dart';
16 : import 'filedownloadprogress.dart';
17 : import 'profileservers.dart';
18 : import 'search.dart';
19 :
20 : class ProfileInfoState extends ChangeNotifier {
21 : ProfileServerListState _servers = ProfileServerListState();
22 : ContactListState _contacts = ContactListState();
23 : SearchState _searchState = SearchState();
24 : final String onion;
25 : String _nickname = "";
26 : String _privateName = "";
27 : String _imagePath = "";
28 : String _defaultImagePath = "";
29 : int _unreadMessages = 0;
30 : bool _online = false;
31 : Map<String, FileDownloadProgress> _downloads = Map<String, FileDownloadProgress>();
32 : Map<String, int> _downloadTriggers = Map<String, int>();
33 : ItemScrollController contactListScrollController = new ItemScrollController();
34 : // assume profiles are encrypted...this will be set to false
35 : // in the constructor if the profile is encrypted with the defacto password.
36 : bool _encrypted = true;
37 :
38 : bool _autostart = true;
39 : bool _enabled = false;
40 : bool _appearOffline = false;
41 : bool _appearOfflineAtStartup = false;
42 :
43 0 : ProfileInfoState({
44 : required this.onion,
45 : nickname = "",
46 : privateName = "",
47 : imagePath = "",
48 : defaultImagePath = "",
49 : unreadMessages = 0,
50 : contactsJson = "",
51 : serversJson = "",
52 : online = false,
53 : autostart = true,
54 : encrypted = true,
55 : appearOffline = false,
56 : String,
57 : }) {
58 0 : this._nickname = nickname;
59 0 : this._privateName = privateName;
60 0 : this._imagePath = imagePath;
61 0 : this._defaultImagePath = defaultImagePath;
62 0 : this._unreadMessages = unreadMessages;
63 0 : this._online = online;
64 0 : this._enabled = _enabled;
65 0 : this._autostart = autostart;
66 : if (autostart) {
67 0 : this._enabled = true;
68 : }
69 0 : this._appearOffline = appearOffline;
70 0 : this._appearOfflineAtStartup = appearOffline;
71 0 : this._encrypted = encrypted;
72 :
73 0 : _contacts.connectServers(this._servers);
74 :
75 0 : if (contactsJson != null && contactsJson != "" && contactsJson != "null") {
76 0 : this.replaceServers(serversJson);
77 :
78 0 : List<dynamic> contacts = jsonDecode(contactsJson);
79 0 : this._contacts.addAll(contacts.map((contact) {
80 0 : this._unreadMessages += contact["numUnread"] as int;
81 0 : return ContactInfoState(this.onion, contact["identifier"], contact["onion"],
82 0 : nickname: contact["name"],
83 0 : localNickname: contact["attributes"]?["local.profile.name"] ?? "", // contact may not have a local name
84 0 : status: contact["status"],
85 0 : imagePath: contact["picture"],
86 0 : defaultImagePath: contact["isGroup"] ? contact["picture"] : contact["defaultPicture"],
87 0 : accepted: contact["accepted"],
88 0 : blocked: contact["blocked"],
89 0 : savePeerHistory: contact["saveConversationHistory"],
90 0 : numMessages: contact["numMessages"],
91 0 : numUnread: contact["numUnread"],
92 0 : isGroup: contact["isGroup"],
93 0 : server: contact["groupServer"],
94 0 : archived: contact["isArchived"] == true,
95 0 : lastMessageTime: DateTime.fromMillisecondsSinceEpoch(1000 * int.parse(contact["lastMsgTime"])),
96 0 : pinned: contact["attributes"]?["local.profile.pinned"] == "true",
97 0 : notificationPolicy: contact["notificationPolicy"] ?? "ConversationNotificationPolicy.Default");
98 : }));
99 :
100 : // dummy set to invoke sort-on-load
101 0 : if (this._contacts.num > 0) {
102 0 : this._contacts.updateLastMessageReceivedTime(this._contacts.contacts.first.identifier, this._contacts.contacts.first.lastMessageReceivedTime);
103 : }
104 : }
105 : }
106 :
107 : // Parse out the server list json into our server info state struct...
108 0 : void replaceServers(String serversJson) {
109 0 : if (serversJson != "" && serversJson != "null") {
110 0 : List<dynamic> servers = jsonDecode(serversJson);
111 0 : this._servers.replace(servers.map((server) {
112 : // TODO Keys...
113 0 : var preSyncStartTime = DateTime.tryParse(server["syncProgress"]["startTime"]);
114 0 : var lastMessageTime = DateTime.tryParse(server["syncProgress"]["lastMessageTime"]);
115 0 : return RemoteServerInfoState(server["onion"], server["identifier"], server["description"], server["status"], lastPreSyncMessageTime: preSyncStartTime, mostRecentMessageTime: lastMessageTime);
116 : }));
117 :
118 0 : this._contacts.contacts.forEach((contact) {
119 0 : if (contact.isGroup) {
120 0 : _servers.addGroup(contact);
121 : }
122 : });
123 :
124 0 : notifyListeners();
125 : }
126 : }
127 :
128 : //
129 0 : void updateServerStatusCache(String server, String status) {
130 0 : this._servers.updateServerState(server, status);
131 0 : notifyListeners();
132 : }
133 :
134 : // Getters and Setters for Online Status
135 0 : bool get isOnline => this._online;
136 :
137 0 : set isOnline(bool newValue) {
138 0 : this._online = newValue;
139 0 : notifyListeners();
140 : }
141 :
142 : // Check encrypted status for profile info screen
143 0 : bool get isEncrypted => this._encrypted;
144 0 : set isEncrypted(bool newValue) {
145 0 : this._encrypted = newValue;
146 0 : notifyListeners();
147 : }
148 :
149 0 : String get nickname => this._nickname;
150 :
151 0 : set nickname(String newValue) {
152 0 : this._nickname = newValue;
153 0 : notifyListeners();
154 : }
155 :
156 0 : String get imagePath => this._imagePath;
157 :
158 0 : set imagePath(String newVal) {
159 0 : this._imagePath = newVal;
160 0 : notifyListeners();
161 : }
162 :
163 0 : bool get enabled => this._enabled;
164 :
165 0 : set enabled(bool newVal) {
166 0 : this._enabled = newVal;
167 0 : notifyListeners();
168 : }
169 :
170 0 : bool get autostart => this._autostart;
171 0 : set autostart(bool newVal) {
172 0 : this._autostart = newVal;
173 0 : notifyListeners();
174 : }
175 :
176 0 : bool get appearOfflineAtStartup => this._appearOfflineAtStartup;
177 0 : set appearOfflineAtStartup(bool newVal) {
178 0 : this._appearOfflineAtStartup = newVal;
179 0 : notifyListeners();
180 : }
181 :
182 0 : bool get appearOffline => this._appearOffline;
183 0 : set appearOffline(bool newVal) {
184 0 : this._appearOffline = newVal;
185 0 : notifyListeners();
186 : }
187 :
188 0 : String get defaultImagePath => this._defaultImagePath;
189 :
190 0 : set defaultImagePath(String newVal) {
191 0 : this._defaultImagePath = newVal;
192 0 : notifyListeners();
193 : }
194 :
195 0 : int get unreadMessages => this._unreadMessages;
196 :
197 0 : set unreadMessages(int newVal) {
198 0 : this._unreadMessages = newVal;
199 0 : notifyListeners();
200 : }
201 :
202 0 : void recountUnread() {
203 0 : this._unreadMessages = _contacts.contacts.fold(0, (i, c) => i + c.unreadMessages);
204 : }
205 :
206 : // Remove a contact from a list. Currently only used when rejecting a group invitation.
207 : // Eventually will also be used for other removals.
208 0 : void removeContact(String handle) {
209 0 : this.contactList.removeContactByHandle(handle);
210 0 : notifyListeners();
211 : }
212 :
213 0 : ContactListState get contactList => this._contacts;
214 :
215 0 : ProfileServerListState get serverList => this._servers;
216 :
217 0 : SearchState get searchState => this._searchState;
218 :
219 0 : List<ContactInfoState> filteredList() {
220 0 : if (!this._searchState.isFiltered) return this._contacts.contacts;
221 0 : return this._contacts.contacts.where((ContactInfoState c) => c.onion.toLowerCase().startsWith(this._searchState.filter) || (c.nickname.toLowerCase().contains(this._searchState.filter))).toList();
222 : }
223 :
224 0 : @override
225 : void dispose() {
226 0 : super.dispose();
227 : }
228 :
229 0 : void updateFrom(String onion, String name, String picture, String contactsJson, String serverJson, bool online) {
230 0 : this._nickname = name;
231 0 : this._imagePath = picture;
232 0 : this._online = online;
233 0 : this._unreadMessages = 0;
234 0 : this.replaceServers(serverJson);
235 :
236 0 : if (contactsJson != "" && contactsJson != "null") {
237 0 : List<dynamic> contacts = jsonDecode(contactsJson);
238 0 : contacts.forEach((contact) {
239 0 : var profileContact = this._contacts.getContact(contact["identifier"]);
240 0 : this._unreadMessages += contact["numUnread"] as int;
241 : if (profileContact != null) {
242 0 : profileContact.status = contact["status"];
243 :
244 0 : var newCount = contact["numMessages"] as int;
245 0 : if (newCount != profileContact.totalMessages) {
246 0 : if (newCount < profileContact.totalMessages) {
247 : // on Android, when sharing a file the UI may be briefly unloaded for the
248 : // OS to display the file management/selection screen. Afterwards a
249 : // call to ReconnectCwtchForeground will be made which will refresh all values (including count of numMessages)
250 : // **at the same time** the foreground will increment .totalMessages and send a new message to the backend.
251 : // This will result in a negative number of messages being calculated here, and an incorrect totalMessage count.
252 : // This bug is exacerbated in debug mode, and when multiple files are sent in succession. Both cases result in multiple ReconnectCwtchForeground
253 : // events that have the potential to conflict with currentMessageCounts.
254 : // Note that *if* a new message came in at the same time, we would be unable to distinguish this case - as such this is specific instance of a more general problem
255 : // TODO: A true-fix to this bug is to implement a syncing step in the foreground where totalMessages and inFlightMessages can be distinguished
256 : // This requires a change to the backend to confirm submission of an inFlightMessage, which will be implemented in #664
257 0 : EnvironmentConfig.debugLog("Conflicting message counts: $newCount ${profileContact.totalMessages}");
258 0 : newCount = max(newCount, profileContact.totalMessages);
259 : }
260 0 : profileContact.messageCache.addFrontIndexGap(newCount - profileContact.totalMessages);
261 : }
262 0 : profileContact.totalMessages = newCount;
263 0 : profileContact.unreadMessages = contact["numUnread"];
264 0 : profileContact.lastMessageReceivedTime = DateTime.fromMillisecondsSinceEpoch(1000 * int.parse(contact["lastMsgTime"]));
265 : } else {
266 0 : this._contacts.add(ContactInfoState(
267 0 : this.onion,
268 0 : contact["identifier"],
269 0 : contact["onion"],
270 0 : nickname: contact["name"],
271 0 : defaultImagePath: contact["defaultPicture"],
272 0 : status: contact["status"],
273 0 : imagePath: contact["picture"],
274 0 : accepted: contact["accepted"],
275 0 : blocked: contact["blocked"],
276 0 : savePeerHistory: contact["saveConversationHistory"],
277 0 : numMessages: contact["numMessages"],
278 0 : numUnread: contact["numUnread"],
279 0 : isGroup: contact["isGroup"],
280 0 : server: contact["groupServer"],
281 0 : lastMessageTime: DateTime.fromMillisecondsSinceEpoch(1000 * int.parse(contact["lastMsgTime"])),
282 0 : notificationPolicy: contact["notificationPolicy"] ?? "ConversationNotificationPolicy.Default",
283 : ));
284 : }
285 : });
286 : }
287 0 : resortContacts();
288 : }
289 :
290 0 : void newMessage(
291 : int identifier, int messageID, DateTime timestamp, String senderHandle, String senderImage, bool isAuto, String data, String contenthash, bool selectedProfile, bool selectedConversation) {
292 : if (!selectedProfile) {
293 0 : unreadMessages++;
294 : }
295 :
296 0 : contactList.newMessage(identifier, messageID, timestamp, senderHandle, senderImage, isAuto, data, contenthash, selectedConversation);
297 0 : notifyListeners();
298 : }
299 :
300 0 : void resortContacts() {
301 0 : _contacts.resort();
302 0 : notifyListeners();
303 : }
304 :
305 0 : void downloadInit(String fileKey, int numChunks) {
306 0 : this._downloads[fileKey] = FileDownloadProgress(numChunks, DateTime.now());
307 0 : notifyListeners();
308 : }
309 :
310 0 : void downloadUpdate(String fileKey, int progress, int numChunks) {
311 0 : if (!downloadActive(fileKey)) {
312 0 : this._downloads[fileKey] = FileDownloadProgress(numChunks, DateTime.now());
313 0 : if (progress < 0) {
314 0 : this._downloads[fileKey]!.interrupted = true;
315 : }
316 : } else {
317 0 : if (this._downloads[fileKey]!.interrupted) {
318 0 : this._downloads[fileKey]!.interrupted = false;
319 : }
320 0 : this._downloads[fileKey]!.chunksDownloaded = progress;
321 0 : this._downloads[fileKey]!.chunksTotal = numChunks;
322 0 : this._downloads[fileKey]!.markUpdate();
323 : }
324 0 : notifyListeners();
325 : }
326 :
327 0 : void downloadMarkManifest(String fileKey) {
328 0 : if (!downloadActive(fileKey)) {
329 0 : this._downloads[fileKey] = FileDownloadProgress(1, DateTime.now());
330 : }
331 0 : this._downloads[fileKey]!.gotManifest = true;
332 0 : this._downloads[fileKey]!.markUpdate();
333 0 : notifyListeners();
334 : }
335 :
336 0 : void downloadMarkFinished(String fileKey, String finalPath) {
337 0 : if (!downloadActive(fileKey)) {
338 : // happens as a result of a CheckDownloadStatus call,
339 : // invoked from a historical (timeline) download message
340 : // so setting numChunks correctly shouldn't matter
341 0 : this.downloadInit(fileKey, 1);
342 : }
343 :
344 : // Update the contact with a custom profile image if we are
345 : // waiting for one...
346 0 : if (this._downloadTriggers.containsKey(fileKey)) {
347 0 : int identifier = this._downloadTriggers[fileKey]!;
348 0 : this.contactList.getContact(identifier)!.imagePath = finalPath;
349 0 : notifyListeners();
350 : }
351 :
352 : // only update if different
353 0 : if (!this._downloads[fileKey]!.complete) {
354 0 : this._downloads[fileKey]!.timeEnd = DateTime.now();
355 0 : this._downloads[fileKey]!.downloadedTo = finalPath;
356 0 : this._downloads[fileKey]!.complete = true;
357 0 : this._downloads[fileKey]!.markUpdate();
358 0 : notifyListeners();
359 : }
360 : }
361 :
362 0 : bool downloadKnown(String fileKey) {
363 0 : return this._downloads.containsKey(fileKey);
364 : }
365 :
366 0 : bool downloadActive(String fileKey) {
367 0 : return this._downloads.containsKey(fileKey) && !this._downloads[fileKey]!.interrupted;
368 : }
369 :
370 0 : bool downloadGotManifest(String fileKey) {
371 0 : return this._downloads.containsKey(fileKey) && this._downloads[fileKey]!.gotManifest;
372 : }
373 :
374 0 : bool downloadComplete(String fileKey) {
375 0 : return this._downloads.containsKey(fileKey) && this._downloads[fileKey]!.complete;
376 : }
377 :
378 0 : bool downloadInterrupted(String fileKey) {
379 0 : if (this._downloads.containsKey(fileKey)) {
380 0 : if (this._downloads[fileKey]!.interrupted) {
381 : return true;
382 : }
383 : }
384 : return false;
385 : }
386 :
387 0 : void downloadMarkResumed(String fileKey) {
388 0 : if (this._downloads.containsKey(fileKey)) {
389 0 : this._downloads[fileKey]!.interrupted = false;
390 0 : this._downloads[fileKey]!.requested = DateTime.now();
391 0 : this._downloads[fileKey]!.markUpdate();
392 0 : notifyListeners();
393 : }
394 : }
395 :
396 0 : double downloadProgress(String fileKey) {
397 0 : return this._downloads.containsKey(fileKey) ? this._downloads[fileKey]!.progress() : 0.0;
398 : }
399 :
400 : // used for loading interrupted download info; use downloadMarkFinished for successful downloads
401 0 : void downloadSetPath(String fileKey, String path) {
402 0 : if (this._downloads.containsKey(fileKey)) {
403 0 : this._downloads[fileKey]!.downloadedTo = path;
404 0 : notifyListeners();
405 : }
406 : }
407 :
408 : // set the download path for the sender
409 0 : void downloadSetPathForSender(String fileKey, String path) {
410 : // we may trigger this event for auto-downloaded receivers too,
411 : // as such we don't assume anything else about the file...other than that
412 : // it exists.
413 0 : if (!this._downloads.containsKey(fileKey)) {
414 : // this will be overwritten by download update if the file is being downloaded
415 0 : this._downloads[fileKey] = FileDownloadProgress(1, DateTime.now());
416 : }
417 0 : this._downloads[fileKey]!.downloadedTo = path;
418 0 : notifyListeners();
419 : }
420 :
421 0 : String? downloadFinalPath(String fileKey) {
422 0 : return this._downloads.containsKey(fileKey) ? this._downloads[fileKey]!.downloadedTo : null;
423 : }
424 :
425 0 : String downloadSpeed(String fileKey) {
426 0 : if (!downloadActive(fileKey) || this._downloads[fileKey]!.chunksDownloaded == 0) {
427 : return "0 B/s";
428 : }
429 0 : var bytes = this._downloads[fileKey]!.chunksDownloaded * 4096;
430 0 : var seconds = (this._downloads[fileKey]!.timeEnd ?? DateTime.now()).difference(this._downloads[fileKey]!.timeStart!).inSeconds;
431 0 : if (seconds == 0) {
432 : return "0 B/s";
433 : }
434 0 : return prettyBytes((bytes / seconds).round()) + "/s";
435 : }
436 :
437 0 : void waitForDownloadComplete(int identifier, String fileKey) {
438 0 : _downloadTriggers[fileKey] = identifier;
439 0 : notifyListeners();
440 : }
441 :
442 0 : int cacheMemUsage() {
443 0 : return _contacts.cacheMemUsage();
444 : }
445 :
446 0 : void downloadReset(String fileKey) {
447 0 : this._downloads.remove(fileKey);
448 0 : notifyListeners();
449 : }
450 :
451 0 : String getPrivateName() {
452 0 : return _privateName;
453 : }
454 :
455 0 : void setPrivateName(String pn) {
456 0 : _privateName = pn;
457 0 : notifyListeners();
458 : }
459 :
460 : // Profile Attributes. Can be set in Profile Edit View...
461 : List<String?> attributes = [null, null, null];
462 0 : void setAttribute(int i, String? value) {
463 0 : this.attributes[i] = value;
464 0 : notifyListeners();
465 : }
466 :
467 : ProfileStatusMenu availabilityStatus = ProfileStatusMenu.available;
468 0 : void setAvailabilityStatus(String status) {
469 : switch (status) {
470 0 : case "available":
471 0 : availabilityStatus = ProfileStatusMenu.available;
472 : break;
473 0 : case "busy":
474 0 : availabilityStatus = ProfileStatusMenu.busy;
475 : break;
476 0 : case "away":
477 0 : availabilityStatus = ProfileStatusMenu.away;
478 : break;
479 : default:
480 : ProfileStatusMenu.available;
481 : }
482 0 : notifyListeners();
483 : }
484 :
485 0 : Color getBorderColor(OpaqueThemeType theme) {
486 0 : switch (this.availabilityStatus) {
487 0 : case ProfileStatusMenu.available:
488 0 : return theme.portraitOnlineBorderColor;
489 0 : case ProfileStatusMenu.away:
490 0 : return theme.portraitOnlineAwayColor;
491 0 : case ProfileStatusMenu.busy:
492 0 : return theme.portraitOnlineBusyColor;
493 : default:
494 0 : throw UnimplementedError("not a valid status");
495 : }
496 : }
497 :
498 : // during deactivation it is possible that the event bus is cleaned up prior to statuses being updated
499 : // this method nicely cleans up our current state so that the UI functions as expected.
500 : // FIXME: Cwtch should be sending these events prior to shutting down the engine...
501 0 : void deactivatePeerEngine(BuildContext context) {
502 0 : Provider.of<FlwtchState>(context, listen: false).cwtch.DeactivatePeerEngine(onion);
503 0 : this.contactList.contacts.forEach((element) {
504 0 : element.status = "Disconnected";
505 : // reset retry time to allow for instant reconnection...
506 0 : element.lastRetryTime = element.loaded;
507 : });
508 0 : this.serverList.servers.forEach((element) {
509 0 : element.status = "Disconnected";
510 : });
511 : }
512 : }
|