LCOV - code coverage report
Current view: top level - lib/models - profile.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 261 0.0 %
Date: 2024-09-24 21:50:54 Functions: 0 0 -

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

Generated by: LCOV version 1.14