LCOV - code coverage report
Current view: top level - lib/models - profile.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 257 0.0 %
Date: 2024-12-02 19:08:52 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             : 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 :     this._contacts.resort();
     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 downloadInit(String fileKey, int numChunks) {
     301           0 :     this._downloads[fileKey] = FileDownloadProgress(numChunks, DateTime.now());
     302           0 :     notifyListeners();
     303             :   }
     304             : 
     305           0 :   void downloadUpdate(String fileKey, int progress, int numChunks) {
     306           0 :     if (!downloadActive(fileKey)) {
     307           0 :       this._downloads[fileKey] = FileDownloadProgress(numChunks, DateTime.now());
     308           0 :       if (progress < 0) {
     309           0 :         this._downloads[fileKey]!.interrupted = true;
     310             :       }
     311             :     } else {
     312           0 :       if (this._downloads[fileKey]!.interrupted) {
     313           0 :         this._downloads[fileKey]!.interrupted = false;
     314             :       }
     315           0 :       this._downloads[fileKey]!.chunksDownloaded = progress;
     316           0 :       this._downloads[fileKey]!.chunksTotal = numChunks;
     317           0 :       this._downloads[fileKey]!.markUpdate();
     318             :     }
     319           0 :     notifyListeners();
     320             :   }
     321             : 
     322           0 :   void downloadMarkManifest(String fileKey) {
     323           0 :     if (!downloadActive(fileKey)) {
     324           0 :       this._downloads[fileKey] = FileDownloadProgress(1, DateTime.now());
     325             :     }
     326           0 :     this._downloads[fileKey]!.gotManifest = true;
     327           0 :     this._downloads[fileKey]!.markUpdate();
     328           0 :     notifyListeners();
     329             :   }
     330             : 
     331           0 :   void downloadMarkFinished(String fileKey, String finalPath) {
     332           0 :     if (!downloadActive(fileKey)) {
     333             :       // happens as a result of a CheckDownloadStatus call,
     334             :       // invoked from a historical (timeline) download message
     335             :       // so setting numChunks correctly shouldn't matter
     336           0 :       this.downloadInit(fileKey, 1);
     337             :     }
     338             : 
     339             :     // Update the contact with a custom profile image if we are
     340             :     // waiting for one...
     341           0 :     if (this._downloadTriggers.containsKey(fileKey)) {
     342           0 :       int identifier = this._downloadTriggers[fileKey]!;
     343           0 :       this.contactList.getContact(identifier)!.imagePath = finalPath;
     344           0 :       notifyListeners();
     345             :     }
     346             : 
     347             :     // only update if different
     348           0 :     if (!this._downloads[fileKey]!.complete) {
     349           0 :       this._downloads[fileKey]!.timeEnd = DateTime.now();
     350           0 :       this._downloads[fileKey]!.downloadedTo = finalPath;
     351           0 :       this._downloads[fileKey]!.complete = true;
     352           0 :       this._downloads[fileKey]!.markUpdate();
     353           0 :       notifyListeners();
     354             :     }
     355             :   }
     356             : 
     357           0 :   bool downloadKnown(String fileKey) {
     358           0 :     return this._downloads.containsKey(fileKey);
     359             :   }
     360             : 
     361           0 :   bool downloadActive(String fileKey) {
     362           0 :     return this._downloads.containsKey(fileKey) && !this._downloads[fileKey]!.interrupted;
     363             :   }
     364             : 
     365           0 :   bool downloadGotManifest(String fileKey) {
     366           0 :     return this._downloads.containsKey(fileKey) && this._downloads[fileKey]!.gotManifest;
     367             :   }
     368             : 
     369           0 :   bool downloadComplete(String fileKey) {
     370           0 :     return this._downloads.containsKey(fileKey) && this._downloads[fileKey]!.complete;
     371             :   }
     372             : 
     373           0 :   bool downloadInterrupted(String fileKey) {
     374           0 :     if (this._downloads.containsKey(fileKey)) {
     375           0 :       if (this._downloads[fileKey]!.interrupted) {
     376             :         return true;
     377             :       }
     378             :     }
     379             :     return false;
     380             :   }
     381             : 
     382           0 :   void downloadMarkResumed(String fileKey) {
     383           0 :     if (this._downloads.containsKey(fileKey)) {
     384           0 :       this._downloads[fileKey]!.interrupted = false;
     385           0 :       this._downloads[fileKey]!.requested = DateTime.now();
     386           0 :       this._downloads[fileKey]!.markUpdate();
     387           0 :       notifyListeners();
     388             :     }
     389             :   }
     390             : 
     391           0 :   double downloadProgress(String fileKey) {
     392           0 :     return this._downloads.containsKey(fileKey) ? this._downloads[fileKey]!.progress() : 0.0;
     393             :   }
     394             : 
     395             :   // used for loading interrupted download info; use downloadMarkFinished for successful downloads
     396           0 :   void downloadSetPath(String fileKey, String path) {
     397           0 :     if (this._downloads.containsKey(fileKey)) {
     398           0 :       this._downloads[fileKey]!.downloadedTo = path;
     399           0 :       notifyListeners();
     400             :     }
     401             :   }
     402             : 
     403             :   // set the download path for the sender
     404           0 :   void downloadSetPathForSender(String fileKey, String path) {
     405             :     // we may trigger this event for auto-downloaded receivers too,
     406             :     // as such we don't assume anything else about the file...other than that
     407             :     // it exists.
     408           0 :     if (!this._downloads.containsKey(fileKey)) {
     409             :       // this will be overwritten by download update if the file is being downloaded
     410           0 :       this._downloads[fileKey] = FileDownloadProgress(1, DateTime.now());
     411             :     }
     412           0 :     this._downloads[fileKey]!.downloadedTo = path;
     413           0 :     notifyListeners();
     414             :   }
     415             : 
     416           0 :   String? downloadFinalPath(String fileKey) {
     417           0 :     return this._downloads.containsKey(fileKey) ? this._downloads[fileKey]!.downloadedTo : null;
     418             :   }
     419             : 
     420           0 :   String downloadSpeed(String fileKey) {
     421           0 :     if (!downloadActive(fileKey) || this._downloads[fileKey]!.chunksDownloaded == 0) {
     422             :       return "0 B/s";
     423             :     }
     424           0 :     var bytes = this._downloads[fileKey]!.chunksDownloaded * 4096;
     425           0 :     var seconds = (this._downloads[fileKey]!.timeEnd ?? DateTime.now()).difference(this._downloads[fileKey]!.timeStart!).inSeconds;
     426           0 :     if (seconds == 0) {
     427             :       return "0 B/s";
     428             :     }
     429           0 :     return prettyBytes((bytes / seconds).round()) + "/s";
     430             :   }
     431             : 
     432           0 :   void waitForDownloadComplete(int identifier, String fileKey) {
     433           0 :     _downloadTriggers[fileKey] = identifier;
     434           0 :     notifyListeners();
     435             :   }
     436             : 
     437           0 :   int cacheMemUsage() {
     438           0 :     return _contacts.cacheMemUsage();
     439             :   }
     440             : 
     441           0 :   void downloadReset(String fileKey) {
     442           0 :     this._downloads.remove(fileKey);
     443           0 :     notifyListeners();
     444             :   }
     445             : 
     446           0 :   String getPrivateName() {
     447           0 :     return _privateName;
     448             :   }
     449             : 
     450           0 :   void setPrivateName(String pn) {
     451           0 :     _privateName = pn;
     452           0 :     notifyListeners();
     453             :   }
     454             : 
     455             :   // Profile Attributes. Can be set in Profile Edit View...
     456             :   List<String?> attributes = [null, null, null];
     457           0 :   void setAttribute(int i, String? value) {
     458           0 :     this.attributes[i] = value;
     459           0 :     notifyListeners();
     460             :   }
     461             : 
     462             :   ProfileStatusMenu availabilityStatus = ProfileStatusMenu.available;
     463           0 :   void setAvailabilityStatus(String status) {
     464             :     switch (status) {
     465           0 :       case "available":
     466           0 :         availabilityStatus = ProfileStatusMenu.available;
     467             :         break;
     468           0 :       case "busy":
     469           0 :         availabilityStatus = ProfileStatusMenu.busy;
     470             :         break;
     471           0 :       case "away":
     472           0 :         availabilityStatus = ProfileStatusMenu.away;
     473             :         break;
     474             :       default:
     475             :         ProfileStatusMenu.available;
     476             :     }
     477           0 :     notifyListeners();
     478             :   }
     479             : 
     480           0 :   Color getBorderColor(OpaqueThemeType theme) {
     481           0 :     switch (this.availabilityStatus) {
     482           0 :       case ProfileStatusMenu.available:
     483           0 :         return theme.portraitOnlineBorderColor;
     484           0 :       case ProfileStatusMenu.away:
     485           0 :         return theme.portraitOnlineAwayColor;
     486           0 :       case ProfileStatusMenu.busy:
     487           0 :         return theme.portraitOnlineBusyColor;
     488             :       default:
     489           0 :         throw UnimplementedError("not a valid status");
     490             :     }
     491             :   }
     492             : 
     493             :   // during deactivation it is possible that the event bus is cleaned up prior to statuses being updated
     494             :   // this method nicely cleans up our current state so that the UI functions as expected.
     495             :   // FIXME: Cwtch should be sending these events prior to shutting down the engine...
     496           0 :   void deactivatePeerEngine(BuildContext context) {
     497           0 :     Provider.of<FlwtchState>(context, listen: false).cwtch.DeactivatePeerEngine(onion);
     498           0 :     this.contactList.contacts.forEach((element) {
     499           0 :       element.status = "Disconnected";
     500             :       // reset retry time to allow for instant reconnection...
     501           0 :       element.lastRetryTime = element.loaded;
     502             :     });
     503           0 :     this.serverList.servers.forEach((element) {
     504           0 :       element.status = "Disconnected";
     505             :     });
     506             :   }
     507             : }

Generated by: LCOV version 1.14