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