2021-01-21 20:37:35 +00:00
|
|
|
import 'dart:convert';
|
2021-02-10 01:36:12 +00:00
|
|
|
|
2021-02-03 11:22:25 +00:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2021-05-19 21:39:52 +00:00
|
|
|
import 'package:cwtch/models/servers.dart';
|
|
|
|
import 'package:cwtch/widgets/messagebubble.dart';
|
2021-04-10 02:31:27 +00:00
|
|
|
import 'package:provider/provider.dart';
|
2021-01-06 23:09:58 +00:00
|
|
|
import 'dart:async';
|
2021-01-07 19:38:06 +00:00
|
|
|
import 'dart:collection';
|
2021-01-06 23:09:58 +00:00
|
|
|
|
2021-01-12 06:21:18 +00:00
|
|
|
import 'cwtch/cwtch.dart';
|
2021-04-10 02:31:27 +00:00
|
|
|
import 'main.dart';
|
2021-01-12 06:21:18 +00:00
|
|
|
|
2021-01-07 19:38:06 +00:00
|
|
|
////////////////////
|
|
|
|
/// UI State ///
|
|
|
|
////////////////////
|
2021-01-06 23:09:58 +00:00
|
|
|
|
2021-01-21 20:37:35 +00:00
|
|
|
class ChatMessage {
|
|
|
|
final int o;
|
|
|
|
final String d;
|
|
|
|
|
2021-05-25 00:11:39 +00:00
|
|
|
ChatMessage({required this.o, required this.d});
|
2021-01-21 20:37:35 +00:00
|
|
|
|
|
|
|
ChatMessage.fromJson(Map<String, dynamic> json)
|
|
|
|
: o = json['o'],
|
|
|
|
d = json['d'];
|
|
|
|
|
2021-03-10 17:40:14 +00:00
|
|
|
Map<String, dynamic> toJson() => {
|
2021-01-21 20:37:35 +00:00
|
|
|
'o': o,
|
|
|
|
'd': d,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-02-03 11:22:25 +00:00
|
|
|
///////////////////
|
|
|
|
/// Providers ///
|
|
|
|
///////////////////
|
|
|
|
|
|
|
|
class ProfileListState extends ChangeNotifier {
|
2021-03-12 12:31:21 +00:00
|
|
|
List<ProfileInfoState> _profiles = [];
|
|
|
|
int get num => _profiles.length;
|
2021-02-03 11:22:25 +00:00
|
|
|
|
2021-03-12 12:31:21 +00:00
|
|
|
void addAll(Iterable<ProfileInfoState> newProfiles) {
|
|
|
|
_profiles.addAll(newProfiles);
|
2021-02-03 11:22:25 +00:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:31:21 +00:00
|
|
|
void add(ProfileInfoState newProfile) {
|
|
|
|
_profiles.add(newProfile);
|
2021-02-03 11:22:25 +00:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-03-16 23:33:03 +00:00
|
|
|
List<ProfileInfoState> get profiles => _profiles.sublist(0); //todo: copy?? dont want caller able to bypass changenotifier
|
2021-03-12 12:31:21 +00:00
|
|
|
|
2021-05-28 21:48:55 +00:00
|
|
|
ProfileInfoState? getProfile(String onion) {
|
2021-03-12 12:31:21 +00:00
|
|
|
int idx = _profiles.indexWhere((element) => element.onion == onion);
|
2021-05-28 21:48:55 +00:00
|
|
|
return idx >= 0 ? _profiles[idx] : null;
|
2021-03-12 12:31:21 +00:00
|
|
|
}
|
2021-02-03 11:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ContactListState extends ChangeNotifier {
|
2021-03-12 12:31:21 +00:00
|
|
|
List<ContactInfoState> _contacts = [];
|
2021-05-25 00:11:39 +00:00
|
|
|
String _filter = "";
|
2021-03-12 12:31:21 +00:00
|
|
|
int get num => _contacts.length;
|
2021-05-19 01:17:50 +00:00
|
|
|
int get numFiltered => isFiltered ? filteredList().length : num;
|
2021-05-25 00:11:39 +00:00
|
|
|
bool get isFiltered => _filter != "";
|
2021-05-19 01:17:50 +00:00
|
|
|
String get filter => _filter;
|
|
|
|
set filter(String newVal) {
|
|
|
|
_filter = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<ContactInfoState> filteredList() {
|
|
|
|
if (!isFiltered) return contacts;
|
2021-05-26 00:02:34 +00:00
|
|
|
return _contacts.where((ContactInfoState c) => c.onion.contains(_filter) || (c.nickname.contains(_filter))).toList();
|
2021-05-19 01:17:50 +00:00
|
|
|
}
|
2021-03-12 12:31:21 +00:00
|
|
|
|
|
|
|
void addAll(Iterable<ContactInfoState> newContacts) {
|
|
|
|
_contacts.addAll(newContacts);
|
2021-02-03 11:22:25 +00:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:31:21 +00:00
|
|
|
void add(ContactInfoState newContact) {
|
|
|
|
_contacts.add(newContact);
|
2021-02-03 11:22:25 +00:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-05-19 01:17:50 +00:00
|
|
|
void resort() {
|
2021-03-16 23:33:03 +00:00
|
|
|
_contacts.sort((ContactInfoState a, ContactInfoState b) {
|
2021-05-19 01:17:50 +00:00
|
|
|
// return -1 = a first in list
|
|
|
|
// return 1 = b first in list
|
|
|
|
// blocked contacts last
|
|
|
|
if (a.isBlocked == true && b.isBlocked != true) return 1;
|
|
|
|
if (a.isBlocked != true && b.isBlocked == true) return -1;
|
|
|
|
// special sorting for contacts with no messages in either history
|
|
|
|
if (a.lastMessageTime.millisecondsSinceEpoch == 0 && b.lastMessageTime.millisecondsSinceEpoch == 0) {
|
|
|
|
// online contacts first
|
|
|
|
if (a.isOnline() && !b.isOnline()) return -1;
|
|
|
|
if (!a.isOnline() && b.isOnline()) return 1;
|
|
|
|
// finally resort to onion
|
|
|
|
return a.onion.toString().compareTo(b.onion.toString());
|
|
|
|
}
|
|
|
|
// finally... most recent history first
|
|
|
|
if (a.lastMessageTime.millisecondsSinceEpoch == 0) return 1;
|
|
|
|
if (b.lastMessageTime.millisecondsSinceEpoch == 0) return -1;
|
2021-04-13 20:59:25 +00:00
|
|
|
return b.lastMessageTime.compareTo(a.lastMessageTime);
|
2021-03-16 23:33:03 +00:00
|
|
|
});
|
2021-02-03 11:22:25 +00:00
|
|
|
//<todo> if(changed) {
|
|
|
|
notifyListeners();
|
|
|
|
//} </todo>
|
|
|
|
}
|
|
|
|
|
2021-05-19 01:17:50 +00:00
|
|
|
void updateLastMessageTime(String forOnion, DateTime newVal) {
|
|
|
|
var contact = getContact(forOnion);
|
|
|
|
if (contact == null) return;
|
|
|
|
|
|
|
|
contact.lastMessageTime = newVal;
|
|
|
|
resort();
|
|
|
|
}
|
|
|
|
|
2021-03-16 23:33:03 +00:00
|
|
|
List<ContactInfoState> get contacts => _contacts.sublist(0); //todo: copy?? dont want caller able to bypass changenotifier
|
2021-03-12 12:31:21 +00:00
|
|
|
|
2021-05-27 00:48:55 +00:00
|
|
|
ContactInfoState? getContact(String onion) {
|
2021-03-12 12:31:21 +00:00
|
|
|
int idx = _contacts.indexWhere((element) => element.onion == onion);
|
2021-05-27 00:48:55 +00:00
|
|
|
return idx >= 0 ? _contacts[idx] : null;
|
2021-03-12 12:31:21 +00:00
|
|
|
}
|
2021-05-28 09:14:21 +00:00
|
|
|
|
|
|
|
void removeContact(String onion) {
|
|
|
|
int idx = _contacts.indexWhere((element) => element.onion == onion);
|
|
|
|
if (idx >= 0) {
|
|
|
|
_contacts.removeAt(idx);
|
2021-05-28 21:48:55 +00:00
|
|
|
notifyListeners();
|
2021-05-28 09:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-03 11:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ProfileInfoState extends ChangeNotifier {
|
2021-03-12 12:31:21 +00:00
|
|
|
ContactListState _contacts = ContactListState();
|
2021-04-20 23:54:47 +00:00
|
|
|
ServerListState _servers = ServerListState();
|
2021-02-03 11:22:25 +00:00
|
|
|
final String onion;
|
|
|
|
String _nickname = "";
|
|
|
|
String _imagePath = "";
|
|
|
|
int _unreadMessages = 0;
|
2021-04-10 04:01:12 +00:00
|
|
|
bool _online = false;
|
2021-02-03 11:22:25 +00:00
|
|
|
|
2021-03-16 23:33:03 +00:00
|
|
|
ProfileInfoState({
|
2021-05-25 00:11:39 +00:00
|
|
|
required this.onion,
|
2021-03-16 23:33:03 +00:00
|
|
|
nickname = "",
|
|
|
|
imagePath = "",
|
|
|
|
unreadMessages = 0,
|
|
|
|
contactsJson = "",
|
2021-04-20 23:54:47 +00:00
|
|
|
serversJson = "",
|
2021-04-10 04:01:12 +00:00
|
|
|
online = false,
|
2021-03-16 23:33:03 +00:00
|
|
|
}) {
|
2021-02-03 11:22:25 +00:00
|
|
|
this._nickname = nickname;
|
|
|
|
this._imagePath = imagePath;
|
|
|
|
this._unreadMessages = unreadMessages;
|
2021-04-10 04:01:12 +00:00
|
|
|
this._online = online;
|
2021-03-12 12:31:21 +00:00
|
|
|
|
|
|
|
if (contactsJson != null && contactsJson != "" && contactsJson != "null") {
|
|
|
|
List<dynamic> contacts = jsonDecode(contactsJson);
|
2021-03-16 23:33:03 +00:00
|
|
|
this._contacts.addAll(contacts.map((contact) {
|
2021-04-28 22:26:49 +00:00
|
|
|
return ContactInfoState(this.onion, contact["onion"],
|
2021-03-17 22:40:33 +00:00
|
|
|
nickname: contact["name"],
|
|
|
|
status: contact["status"],
|
|
|
|
imagePath: contact["picture"],
|
|
|
|
isBlocked: contact["authorization"] == "blocked",
|
2021-04-08 05:07:01 +00:00
|
|
|
isInvitation: contact["authorization"] == "unknown",
|
2021-03-17 22:56:59 +00:00
|
|
|
savePeerHistory: contact["saveConversationHistory"],
|
|
|
|
numMessages: contact["numMessages"],
|
2021-04-13 00:04:51 +00:00
|
|
|
numUnread: contact["numUnread"],
|
2021-04-22 21:15:27 +00:00
|
|
|
isGroup: contact["isGroup"],
|
|
|
|
server: contact["groupServer"],
|
2021-04-13 20:59:25 +00:00
|
|
|
lastMessageTime: DateTime.fromMillisecondsSinceEpoch(1000 * int.parse(contact["lastMsgTime"])));
|
2021-03-16 23:33:03 +00:00
|
|
|
}));
|
2021-04-13 00:04:51 +00:00
|
|
|
|
|
|
|
// dummy set to invoke sort-on-load
|
|
|
|
if (this._contacts.num > 0) {
|
|
|
|
this._contacts.updateLastMessageTime(this._contacts._contacts.first.onion, this._contacts._contacts.first.lastMessageTime);
|
|
|
|
}
|
2021-03-12 12:31:21 +00:00
|
|
|
}
|
2021-04-20 23:54:47 +00:00
|
|
|
|
|
|
|
this.replaceServers(serversJson);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse out the server list json into our server info state struct...
|
|
|
|
void replaceServers(String serversJson) {
|
|
|
|
if (serversJson != null && serversJson != "" && serversJson != "null") {
|
|
|
|
print("got servers $serversJson");
|
|
|
|
List<dynamic> servers = jsonDecode(serversJson);
|
|
|
|
this._servers.replace(servers.map((server) {
|
|
|
|
// TODO Keys...
|
|
|
|
return ServerInfoState(onion: server["onion"], status: server["status"]);
|
|
|
|
}));
|
2021-06-01 19:33:19 +00:00
|
|
|
notifyListeners();
|
2021-04-20 23:54:47 +00:00
|
|
|
}
|
2021-02-03 11:22:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 19:33:19 +00:00
|
|
|
//
|
|
|
|
void updateServerStatusCache(String server, String status) {
|
|
|
|
this._servers.updateServerCache(server, status);
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-04-10 04:01:12 +00:00
|
|
|
// Getters and Setters for Online Status
|
|
|
|
bool get isOnline => this._online;
|
|
|
|
set isOnline(bool newValue) {
|
|
|
|
this._online = newValue;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-02-03 11:22:25 +00:00
|
|
|
String get nickname => this._nickname;
|
|
|
|
set nickname(String newValue) {
|
2021-02-23 02:55:10 +00:00
|
|
|
this._nickname = newValue;
|
2021-02-03 11:22:25 +00:00
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
String get imagePath => this._imagePath;
|
|
|
|
set imagePath(String newVal) {
|
|
|
|
this._imagePath = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
int get unreadMessages => this._unreadMessages;
|
|
|
|
set unreadMessages(int newVal) {
|
|
|
|
this._unreadMessages = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2021-02-11 01:03:02 +00:00
|
|
|
|
2021-04-28 22:26:49 +00:00
|
|
|
// Remove a contact from a list. Currently only used when rejecting a group invitation.
|
|
|
|
// Eventually will also be used for other removals.
|
|
|
|
void removeContact(String handle) {
|
|
|
|
int idx = this.contactList._contacts.indexWhere((element) => element.onion == handle);
|
|
|
|
this.contactList._contacts.removeAt(idx);
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:31:21 +00:00
|
|
|
ContactListState get contactList => this._contacts;
|
2021-04-20 23:54:47 +00:00
|
|
|
ServerListState get serverList => this._servers;
|
2021-03-12 12:31:21 +00:00
|
|
|
|
2021-02-11 01:03:02 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
print("profileinfostate.dispose()");
|
|
|
|
}
|
2021-02-03 11:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class ContactInfoState extends ChangeNotifier {
|
|
|
|
final String profileOnion;
|
|
|
|
final String onion;
|
2021-05-25 00:11:39 +00:00
|
|
|
late String _nickname;
|
|
|
|
|
|
|
|
late bool _isInvitation;
|
|
|
|
late bool _isBlocked;
|
|
|
|
late String _status;
|
|
|
|
late String _imagePath;
|
|
|
|
late String _savePeerHistory;
|
|
|
|
late int _unreadMessages = 0;
|
|
|
|
late int _totalMessages = 0;
|
|
|
|
late DateTime _lastMessageTime;
|
|
|
|
late Map<String, GlobalKey<MessageBubbleState>> keys;
|
2021-02-03 11:22:25 +00:00
|
|
|
|
2021-04-26 23:00:23 +00:00
|
|
|
// todo: a nicer way to model contacts, groups and other "entities"
|
2021-05-25 00:11:39 +00:00
|
|
|
late bool _isGroup;
|
2021-05-31 23:11:39 +00:00
|
|
|
String? _server;
|
2021-04-22 21:15:27 +00:00
|
|
|
|
2021-04-28 21:20:09 +00:00
|
|
|
ContactInfoState(
|
2021-03-24 23:35:24 +00:00
|
|
|
this.profileOnion,
|
2021-04-28 22:26:49 +00:00
|
|
|
this.onion, {
|
2021-03-24 23:35:24 +00:00
|
|
|
nickname = "",
|
|
|
|
isGroup = false,
|
|
|
|
isInvitation = false,
|
|
|
|
isBlocked = false,
|
|
|
|
status = "",
|
|
|
|
imagePath = "",
|
|
|
|
savePeerHistory = "DeleteHistoryConfirmed",
|
|
|
|
numMessages = 0,
|
|
|
|
numUnread = 0,
|
2021-05-03 18:47:54 +00:00
|
|
|
lastMessageTime,
|
2021-05-31 23:11:39 +00:00
|
|
|
server,
|
2021-03-24 23:35:24 +00:00
|
|
|
}) {
|
2021-02-03 11:22:25 +00:00
|
|
|
this._nickname = nickname;
|
|
|
|
this._isGroup = isGroup;
|
|
|
|
this._isInvitation = isInvitation;
|
|
|
|
this._isBlocked = isBlocked;
|
|
|
|
this._status = status;
|
|
|
|
this._imagePath = imagePath;
|
2021-03-17 22:54:41 +00:00
|
|
|
this._totalMessages = numMessages;
|
|
|
|
this._unreadMessages = numUnread;
|
2021-03-17 22:40:33 +00:00
|
|
|
this._savePeerHistory = savePeerHistory;
|
2021-05-25 00:11:39 +00:00
|
|
|
this._lastMessageTime = lastMessageTime == null ? DateTime.fromMillisecondsSinceEpoch(0) : lastMessageTime;
|
2021-04-22 21:15:27 +00:00
|
|
|
this._server = server;
|
2021-05-25 00:11:39 +00:00
|
|
|
keys = Map<String, GlobalKey<MessageBubbleState>>();
|
2021-02-03 11:22:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 00:11:39 +00:00
|
|
|
String get nickname => this._nickname;
|
2021-03-17 22:40:33 +00:00
|
|
|
|
2021-05-25 00:11:39 +00:00
|
|
|
String get savePeerHistory => this._savePeerHistory;
|
2021-03-17 22:40:33 +00:00
|
|
|
set savePeerHistory(String newVal) {
|
|
|
|
this._savePeerHistory = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-02-03 11:22:25 +00:00
|
|
|
set nickname(String newVal) {
|
|
|
|
this._nickname = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-05-25 00:11:39 +00:00
|
|
|
bool get isGroup => this._isGroup;
|
2021-03-23 22:42:10 +00:00
|
|
|
set isGroup(bool newVal) {
|
|
|
|
this._isGroup = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-05-25 00:11:39 +00:00
|
|
|
bool get isBlocked => this._isBlocked;
|
2021-03-17 21:48:11 +00:00
|
|
|
set isBlocked(bool newVal) {
|
|
|
|
this._isBlocked = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-05-25 00:11:39 +00:00
|
|
|
bool get isInvitation => this._isInvitation;
|
2021-02-10 01:36:12 +00:00
|
|
|
set isInvitation(bool newVal) {
|
|
|
|
this._isInvitation = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-05-25 00:11:39 +00:00
|
|
|
String get status => this._status;
|
2021-02-10 01:36:12 +00:00
|
|
|
set status(String newVal) {
|
|
|
|
this._status = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-05-25 00:11:39 +00:00
|
|
|
int get unreadMessages => this._unreadMessages;
|
2021-02-03 11:22:25 +00:00
|
|
|
set unreadMessages(int newVal) {
|
|
|
|
this._unreadMessages = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2021-03-12 12:31:21 +00:00
|
|
|
|
2021-05-25 00:11:39 +00:00
|
|
|
int get totalMessages => this._totalMessages;
|
2021-03-17 22:54:41 +00:00
|
|
|
set totalMessages(int newVal) {
|
|
|
|
this._totalMessages = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-05-25 00:11:39 +00:00
|
|
|
String get imagePath => this._imagePath;
|
2021-03-12 12:31:21 +00:00
|
|
|
set imagePath(String newVal) {
|
|
|
|
this._imagePath = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2021-04-13 00:04:51 +00:00
|
|
|
|
2021-05-25 00:11:39 +00:00
|
|
|
DateTime get lastMessageTime => this._lastMessageTime;
|
2021-04-13 00:04:51 +00:00
|
|
|
set lastMessageTime(DateTime newVal) {
|
|
|
|
this._lastMessageTime = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2021-04-22 21:15:27 +00:00
|
|
|
|
|
|
|
// we only allow callers to fetch the server
|
|
|
|
get server => this._server;
|
|
|
|
|
|
|
|
bool isOnline() {
|
2021-04-26 23:00:23 +00:00
|
|
|
if (this.isGroup == true) {
|
2021-04-22 21:15:27 +00:00
|
|
|
return this.status == "Synced";
|
|
|
|
} else {
|
|
|
|
return this.status == "Authenticated";
|
|
|
|
}
|
|
|
|
}
|
2021-05-03 02:38:43 +00:00
|
|
|
|
2021-05-05 22:02:31 +00:00
|
|
|
GlobalKey<MessageBubbleState> getMessageKey(String index) {
|
2021-05-03 02:38:43 +00:00
|
|
|
if (keys[index] == null) {
|
|
|
|
keys[index] = GlobalKey<MessageBubbleState>();
|
|
|
|
}
|
2021-05-25 00:11:39 +00:00
|
|
|
GlobalKey<MessageBubbleState> ret = keys[index]!;
|
|
|
|
return ret;
|
2021-05-03 02:38:43 +00:00
|
|
|
}
|
2021-02-03 11:22:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-10 02:31:27 +00:00
|
|
|
class MessageState extends ChangeNotifier {
|
|
|
|
final String profileOnion;
|
|
|
|
final String contactHandle;
|
|
|
|
final int messageIndex;
|
2021-05-25 00:11:39 +00:00
|
|
|
late String _message;
|
|
|
|
late int _overlay;
|
|
|
|
late String _inviteTarget;
|
|
|
|
late String _inviteNick;
|
|
|
|
late DateTime _timestamp;
|
|
|
|
late String _senderOnion;
|
2021-06-03 18:32:25 +00:00
|
|
|
String? _senderImage;
|
2021-05-25 00:11:39 +00:00
|
|
|
late String _signature = "";
|
|
|
|
late bool _ackd = false;
|
|
|
|
late bool _error = false;
|
|
|
|
late bool _loaded = false;
|
|
|
|
late bool _malformed = false;
|
2021-04-10 02:31:27 +00:00
|
|
|
|
|
|
|
MessageState({
|
2021-05-25 00:11:39 +00:00
|
|
|
required BuildContext context,
|
|
|
|
required this.profileOnion,
|
|
|
|
required this.contactHandle,
|
|
|
|
required this.messageIndex,
|
2021-04-10 02:31:27 +00:00
|
|
|
}) {
|
2021-05-12 22:39:24 +00:00
|
|
|
this._senderOnion = profileOnion;
|
2021-05-03 18:47:54 +00:00
|
|
|
tryLoad(context);
|
2021-04-10 02:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get message => this._message;
|
2021-05-11 04:34:04 +00:00
|
|
|
get overlay => this._overlay;
|
2021-04-10 02:31:27 +00:00
|
|
|
get timestamp => this._timestamp;
|
2021-05-25 00:11:39 +00:00
|
|
|
bool get ackd => this._ackd;
|
|
|
|
bool get error => this._error;
|
2021-06-03 18:32:25 +00:00
|
|
|
bool get malformed => this._malformed;
|
|
|
|
bool get loaded => this._loaded;
|
2021-04-14 02:33:21 +00:00
|
|
|
get senderOnion => this._senderOnion;
|
2021-05-03 18:47:54 +00:00
|
|
|
get senderImage => this._senderImage;
|
2021-05-05 22:02:31 +00:00
|
|
|
get signature => this._signature;
|
2021-05-11 04:34:04 +00:00
|
|
|
get isInvite => this.overlay == 100 || this.overlay == 101;
|
|
|
|
get inviteTarget => this._inviteTarget;
|
|
|
|
get inviteNick => this._inviteNick;
|
2021-04-10 02:31:27 +00:00
|
|
|
|
|
|
|
set ackd(bool newVal) {
|
|
|
|
this._ackd = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
2021-05-03 18:47:54 +00:00
|
|
|
|
2021-05-10 23:58:34 +00:00
|
|
|
set error(bool newVal) {
|
|
|
|
this._error = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-06-03 18:32:25 +00:00
|
|
|
set malformed(bool newVal) {
|
|
|
|
this._malformed = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
set loaded(bool newVal) {
|
|
|
|
this._loaded = newVal;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2021-05-03 18:47:54 +00:00
|
|
|
void tryLoad(BuildContext context) {
|
|
|
|
Provider.of<FlwtchState>(context, listen: false).cwtch.GetMessage(profileOnion, contactHandle, messageIndex).then((jsonMessage) {
|
2021-05-12 22:39:10 +00:00
|
|
|
try {
|
|
|
|
dynamic messageWrapper = jsonDecode(jsonMessage);
|
|
|
|
if (messageWrapper['Message'] == null || messageWrapper['Message'] == '' || messageWrapper['Message'] == '{}') {
|
|
|
|
this._senderOnion = profileOnion;
|
|
|
|
Future.delayed(const Duration(milliseconds: 2), () {
|
|
|
|
tryLoad(context);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dynamic message = jsonDecode(messageWrapper['Message']);
|
|
|
|
this._message = message['d'];
|
|
|
|
this._overlay = int.parse(message['o'].toString());
|
2021-05-25 00:11:39 +00:00
|
|
|
this._timestamp = DateTime.tryParse(messageWrapper['Timestamp'])!;
|
2021-05-12 22:39:10 +00:00
|
|
|
this._senderOnion = messageWrapper['PeerID'];
|
|
|
|
this._senderImage = messageWrapper['ContactImage'];
|
|
|
|
|
|
|
|
// If this is a group, store the signature
|
|
|
|
if (contactHandle.length == 32) {
|
|
|
|
this._signature = messageWrapper['Signature'];
|
|
|
|
}
|
2021-05-05 22:02:31 +00:00
|
|
|
|
2021-05-12 22:39:10 +00:00
|
|
|
// if this is an invite, get the contact handle
|
|
|
|
if (this.isInvite) {
|
|
|
|
if (message['d'].toString().length == 56) {
|
|
|
|
this._inviteTarget = message['d'];
|
|
|
|
var targetContact = Provider.of<ProfileInfoState>(context).contactList.getContact(this._inviteTarget);
|
|
|
|
this._inviteNick = targetContact == null ? message['d'] : targetContact.nickname;
|
|
|
|
} else {
|
|
|
|
var parts = message['d'].toString().split("||");
|
|
|
|
if (parts.length == 2) {
|
|
|
|
var jsonObj = jsonDecode(utf8.fuse(base64).decode(parts[1].substring(5)));
|
|
|
|
this._inviteTarget = jsonObj['GroupID'];
|
|
|
|
this._inviteNick = jsonObj['GroupName'];
|
|
|
|
}
|
2021-05-11 04:34:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 18:32:25 +00:00
|
|
|
this.loaded = true;
|
2021-05-10 23:58:34 +00:00
|
|
|
|
2021-05-12 22:39:10 +00:00
|
|
|
//update ackd and error last as they are changenotified
|
|
|
|
this.ackd = messageWrapper['Acknowledged'];
|
|
|
|
if (messageWrapper['Error'] != null) {
|
|
|
|
this.error = true;
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2021-06-03 18:32:25 +00:00
|
|
|
this._overlay = -1;
|
|
|
|
this.loaded = true;
|
|
|
|
this.malformed = true;
|
2021-05-10 23:58:34 +00:00
|
|
|
}
|
2021-05-03 18:47:54 +00:00
|
|
|
});
|
|
|
|
}
|
2021-04-10 02:31:27 +00:00
|
|
|
}
|