2023-05-06 10:45:26 -04:00
|
|
|
|
import 'dart:async';
|
2023-05-01 11:34:34 -04:00
|
|
|
|
import 'dart:convert';
|
2023-05-03 11:51:18 -04:00
|
|
|
|
import 'dart:io';
|
2023-05-06 10:45:26 -04:00
|
|
|
|
import 'package:contacts_plus_plus/apis/friend_api.dart';
|
2023-05-03 14:03:46 -04:00
|
|
|
|
import 'package:contacts_plus_plus/apis/message_api.dart';
|
2023-05-06 12:34:14 -04:00
|
|
|
|
import 'package:contacts_plus_plus/apis/user_api.dart';
|
2023-05-05 06:45:00 -04:00
|
|
|
|
import 'package:contacts_plus_plus/clients/notification_client.dart';
|
2023-05-04 07:13:24 -04:00
|
|
|
|
import 'package:contacts_plus_plus/models/authentication_data.dart';
|
2023-05-04 13:04:33 -04:00
|
|
|
|
import 'package:contacts_plus_plus/models/friend.dart';
|
2023-05-06 10:45:26 -04:00
|
|
|
|
import 'package:flutter/widgets.dart';
|
2023-05-16 07:01:42 -04:00
|
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
2023-05-01 11:34:34 -04:00
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
|
|
2023-05-03 15:55:34 -04:00
|
|
|
|
import 'package:contacts_plus_plus/clients/api_client.dart';
|
2023-05-01 13:13:40 -04:00
|
|
|
|
import 'package:contacts_plus_plus/config.dart';
|
|
|
|
|
import 'package:contacts_plus_plus/models/message.dart';
|
2023-05-03 11:51:18 -04:00
|
|
|
|
import 'package:logging/logging.dart';
|
2023-05-04 07:13:24 -04:00
|
|
|
|
import 'package:workmanager/workmanager.dart';
|
2023-05-01 11:34:34 -04:00
|
|
|
|
|
|
|
|
|
enum EventType {
|
|
|
|
|
unknown,
|
|
|
|
|
message,
|
2023-05-05 09:05:06 -04:00
|
|
|
|
unknown1,
|
|
|
|
|
unknown2,
|
|
|
|
|
unknown3,
|
|
|
|
|
unknown4,
|
|
|
|
|
keepAlive,
|
|
|
|
|
error;
|
2023-05-01 11:34:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum EventTarget {
|
|
|
|
|
unknown,
|
|
|
|
|
messageSent,
|
2023-05-05 05:29:54 -04:00
|
|
|
|
receiveMessage,
|
2023-05-01 11:34:34 -04:00
|
|
|
|
messagesRead;
|
|
|
|
|
|
|
|
|
|
factory EventTarget.parse(String? text) {
|
|
|
|
|
if (text == null) return EventTarget.unknown;
|
|
|
|
|
return EventTarget.values.firstWhere((element) => element.name.toLowerCase() == text.toLowerCase(),
|
|
|
|
|
orElse: () => EventTarget.unknown,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 10:45:26 -04:00
|
|
|
|
class MessagingClient extends ChangeNotifier {
|
2023-05-01 11:34:34 -04:00
|
|
|
|
static const String eofChar = "";
|
|
|
|
|
static const String _negotiationPacket = "{\"protocol\":\"json\", \"version\":1}$eofChar";
|
2023-05-03 11:51:18 -04:00
|
|
|
|
static const List<int> _reconnectTimeoutsSeconds = [0, 5, 10, 20, 60];
|
2023-05-04 07:13:24 -04:00
|
|
|
|
static const String taskName = "periodic-unread-check";
|
2023-05-16 07:01:42 -04:00
|
|
|
|
static const Duration _autoRefreshDuration = Duration(seconds: 10);
|
|
|
|
|
static const Duration _unreadSafeguardDuration = Duration(seconds: 120);
|
|
|
|
|
static const String _messageBoxKey = "message-box";
|
|
|
|
|
static const String _lastUpdateKey = "__last-update-time";
|
2023-05-02 04:04:54 -04:00
|
|
|
|
final ApiClient _apiClient;
|
2023-05-06 10:45:26 -04:00
|
|
|
|
final List<Friend> _sortedFriendsCache = []; // Keep a sorted copy so as to not have to sort during build()
|
2023-05-02 04:04:54 -04:00
|
|
|
|
final Map<String, MessageCache> _messageCache = {};
|
2023-05-05 06:40:19 -04:00
|
|
|
|
final Map<String, List<Message>> _unreads = {};
|
2023-05-03 11:51:18 -04:00
|
|
|
|
final Logger _logger = Logger("NeosHub");
|
2023-05-04 07:13:24 -04:00
|
|
|
|
final Workmanager _workmanager = Workmanager();
|
2023-05-05 05:29:54 -04:00
|
|
|
|
final NotificationClient _notificationClient;
|
2023-05-06 11:47:36 -04:00
|
|
|
|
Friend? selectedFriend;
|
2023-05-06 12:34:14 -04:00
|
|
|
|
Timer? _notifyOnlineTimer;
|
2023-05-06 10:45:26 -04:00
|
|
|
|
Timer? _autoRefresh;
|
|
|
|
|
Timer? _refreshTimeout;
|
2023-05-16 07:01:42 -04:00
|
|
|
|
Timer? _unreadSafeguard;
|
2023-05-05 09:05:06 -04:00
|
|
|
|
int _attempts = 0;
|
2023-05-03 11:51:18 -04:00
|
|
|
|
WebSocket? _wsChannel;
|
|
|
|
|
bool _isConnecting = false;
|
2023-05-06 10:57:08 -04:00
|
|
|
|
String? _initStatus;
|
2023-05-06 10:45:26 -04:00
|
|
|
|
|
2023-05-06 10:57:08 -04:00
|
|
|
|
String? get initStatus => _initStatus;
|
2023-05-01 11:34:34 -04:00
|
|
|
|
|
2023-05-06 12:34:14 -04:00
|
|
|
|
bool get websocketConnected => _wsChannel != null;
|
|
|
|
|
|
2023-05-05 05:29:54 -04:00
|
|
|
|
MessagingClient({required ApiClient apiClient, required NotificationClient notificationClient})
|
|
|
|
|
: _apiClient = apiClient, _notificationClient = notificationClient {
|
2023-05-16 07:01:42 -04:00
|
|
|
|
Hive.openBox(_messageBoxKey).then((box) async {
|
|
|
|
|
box.delete(_lastUpdateKey);
|
|
|
|
|
await refreshFriendsListWithErrorHandler();
|
|
|
|
|
await _refreshUnreads();
|
|
|
|
|
});
|
2023-05-06 12:34:14 -04:00
|
|
|
|
startWebsocket();
|
|
|
|
|
_notifyOnlineTimer = Timer.periodic(const Duration(seconds: 60), (timer) async {
|
|
|
|
|
// We should probably let the MessagingClient handle the entire state of USerStatus instead of mirroring like this
|
|
|
|
|
// but I don't feel like implementing that right now.
|
|
|
|
|
UserApi.setStatus(apiClient, status: await UserApi.getUserStatus(apiClient, userId: apiClient.userId));
|
|
|
|
|
});
|
2023-05-01 11:34:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 10:45:26 -04:00
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_autoRefresh?.cancel();
|
|
|
|
|
_refreshTimeout?.cancel();
|
2023-05-06 12:34:14 -04:00
|
|
|
|
_notifyOnlineTimer?.cancel();
|
2023-05-06 10:45:26 -04:00
|
|
|
|
_wsChannel?.close();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-03 14:03:46 -04:00
|
|
|
|
void _sendData(data) {
|
|
|
|
|
if (_wsChannel == null) throw "Neos Hub is not connected";
|
|
|
|
|
_wsChannel!.add(jsonEncode(data)+eofChar);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 10:57:08 -04:00
|
|
|
|
void resetStatus() {
|
|
|
|
|
_initStatus = null;
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 07:01:42 -04:00
|
|
|
|
Future<void> _refreshUnreads() async {
|
|
|
|
|
_unreadSafeguard?.cancel();
|
|
|
|
|
try {
|
|
|
|
|
final unreadMessages = await MessageApi.getUserMessages(_apiClient, unreadOnly: true);
|
|
|
|
|
updateAllUnreads(unreadMessages.toList());
|
|
|
|
|
} catch (_) {}
|
|
|
|
|
_unreadSafeguard = Timer(_unreadSafeguardDuration, _refreshUnreads);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> refreshFriendsListWithErrorHandler () async {
|
2023-05-06 10:45:26 -04:00
|
|
|
|
try {
|
|
|
|
|
await refreshFriendsList();
|
|
|
|
|
} catch (e) {
|
2023-05-06 10:57:08 -04:00
|
|
|
|
_initStatus = "$e";
|
|
|
|
|
notifyListeners();
|
2023-05-06 10:45:26 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> refreshFriendsList() async {
|
2023-05-16 07:01:42 -04:00
|
|
|
|
DateTime? lastUpdateUtc = Hive.box(_messageBoxKey).get(_lastUpdateKey);
|
2023-05-06 10:45:26 -04:00
|
|
|
|
_autoRefresh?.cancel();
|
|
|
|
|
_autoRefresh = Timer(_autoRefreshDuration, () => refreshFriendsList());
|
|
|
|
|
|
2023-05-16 07:01:42 -04:00
|
|
|
|
final friends = await FriendApi.getFriendsList(_apiClient, lastStatusUpdate: lastUpdateUtc);
|
2023-05-04 13:04:33 -04:00
|
|
|
|
for (final friend in friends) {
|
2023-05-16 07:01:42 -04:00
|
|
|
|
await _updateFriend(friend);
|
2023-05-04 13:04:33 -04:00
|
|
|
|
}
|
2023-05-16 07:01:42 -04:00
|
|
|
|
|
2023-05-06 12:34:14 -04:00
|
|
|
|
_initStatus = "";
|
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _sortFriendsCache() {
|
|
|
|
|
_sortedFriendsCache.sort((a, b) {
|
2023-05-06 10:45:26 -04:00
|
|
|
|
var aVal = friendHasUnreads(a) ? -3 : 0;
|
|
|
|
|
var bVal = friendHasUnreads(b) ? -3 : 0;
|
|
|
|
|
|
2023-05-06 13:01:15 -04:00
|
|
|
|
aVal -= a.latestMessageTime.compareTo(b.latestMessageTime);
|
2023-05-06 10:45:26 -04:00
|
|
|
|
aVal += a.userStatus.onlineStatus.compareTo(b.userStatus.onlineStatus) * 2;
|
|
|
|
|
return aVal.compareTo(bVal);
|
2023-05-06 12:34:14 -04:00
|
|
|
|
});
|
2023-05-04 13:04:33 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-05 06:40:19 -04:00
|
|
|
|
void updateAllUnreads(List<Message> messages) {
|
|
|
|
|
_unreads.clear();
|
|
|
|
|
for (final msg in messages) {
|
|
|
|
|
if (msg.senderId != _apiClient.userId) {
|
|
|
|
|
final value = _unreads[msg.senderId];
|
|
|
|
|
if (value == null) {
|
|
|
|
|
_unreads[msg.senderId] = [msg];
|
|
|
|
|
} else {
|
|
|
|
|
value.add(msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void addUnread(Message message) {
|
|
|
|
|
var messages = _unreads[message.senderId];
|
|
|
|
|
if (messages == null) {
|
|
|
|
|
messages = [message];
|
|
|
|
|
_unreads[message.senderId] = messages;
|
|
|
|
|
} else {
|
|
|
|
|
messages.add(message);
|
|
|
|
|
}
|
|
|
|
|
messages.sort();
|
2023-05-06 12:34:14 -04:00
|
|
|
|
_sortFriendsCache();
|
2023-05-05 06:40:19 -04:00
|
|
|
|
_notificationClient.showUnreadMessagesNotification(messages.reversed);
|
2023-05-06 10:45:26 -04:00
|
|
|
|
notifyListeners();
|
2023-05-05 06:40:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 13:01:15 -04:00
|
|
|
|
void clearUnreadsForUser(String userId) {
|
|
|
|
|
_unreads[userId]?.clear();
|
2023-05-06 10:45:26 -04:00
|
|
|
|
notifyListeners();
|
2023-05-05 06:40:19 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<Message> getUnreadsForFriend(Friend friend) => _unreads[friend.id] ?? [];
|
|
|
|
|
|
|
|
|
|
bool friendHasUnreads(Friend friend) => _unreads.containsKey(friend.id);
|
|
|
|
|
|
|
|
|
|
bool messageIsUnread(Message message) {
|
|
|
|
|
return _unreads[message.senderId]?.any((element) => element.id == message.id) ?? false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 07:01:42 -04:00
|
|
|
|
Friend? getAsFriend(String userId) => Friend.fromMapOrNull(Hive.box(_messageBoxKey).get(userId));
|
2023-05-04 13:04:33 -04:00
|
|
|
|
|
2023-05-06 10:45:26 -04:00
|
|
|
|
List<Friend> get cachedFriends => _sortedFriendsCache;
|
|
|
|
|
|
2023-05-06 11:47:36 -04:00
|
|
|
|
MessageCache _createUserMessageCache(String userId) => MessageCache(apiClient: _apiClient, userId: userId);
|
|
|
|
|
|
2023-05-16 09:57:44 -04:00
|
|
|
|
void deleteUserMessageCache(String userId) {
|
|
|
|
|
_messageCache.remove(userId);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 11:47:36 -04:00
|
|
|
|
Future<void> loadUserMessageCache(String userId) async {
|
|
|
|
|
final cache = getUserMessageCache(userId) ?? _createUserMessageCache(userId);
|
|
|
|
|
await cache.loadMessages();
|
|
|
|
|
_messageCache[userId] = cache;
|
|
|
|
|
notifyListeners();
|
2023-05-01 11:34:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 07:01:42 -04:00
|
|
|
|
Future<void> _updateFriend(Friend friend) async {
|
|
|
|
|
final box = Hive.box(_messageBoxKey);
|
|
|
|
|
box.put(friend.id, friend.toMap());
|
|
|
|
|
final lastStatusUpdate = box.get(_lastUpdateKey);
|
|
|
|
|
if (lastStatusUpdate == null || friend.userStatus.lastStatusChange.isAfter(lastStatusUpdate)) {
|
|
|
|
|
await box.put(_lastUpdateKey, friend.userStatus.lastStatusChange);
|
|
|
|
|
}
|
2023-05-06 13:45:54 -04:00
|
|
|
|
final sIndex = _sortedFriendsCache.indexWhere((element) => element.id == friend.id);
|
|
|
|
|
if (sIndex == -1) {
|
|
|
|
|
_sortedFriendsCache.add(friend);
|
|
|
|
|
} else {
|
|
|
|
|
_sortedFriendsCache[sIndex] = friend;
|
|
|
|
|
}
|
|
|
|
|
_sortFriendsCache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> updateFriendStatus(String userId) async {
|
|
|
|
|
final friend = getAsFriend(userId);
|
|
|
|
|
if (friend == null) return;
|
|
|
|
|
final newStatus = await UserApi.getUserStatus(_apiClient, userId: userId);
|
2023-05-16 07:01:42 -04:00
|
|
|
|
await _updateFriend(friend.copyWith(userStatus: newStatus));
|
2023-05-06 13:45:54 -04:00
|
|
|
|
notifyListeners();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 11:47:36 -04:00
|
|
|
|
MessageCache? getUserMessageCache(String userId) => _messageCache[userId];
|
|
|
|
|
|
2023-05-04 07:13:24 -04:00
|
|
|
|
static Future<void> backgroundCheckUnreads(Map<String, dynamic>? inputData) async {
|
|
|
|
|
if (inputData == null) return;
|
|
|
|
|
final auth = AuthenticationData.fromMap(inputData);
|
|
|
|
|
final unreads = await MessageApi.getUserMessages(ApiClient(authenticationData: auth), unreadOnly: true);
|
2023-05-03 14:03:46 -04:00
|
|
|
|
for (var message in unreads) {
|
|
|
|
|
throw UnimplementedError();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-04 07:13:24 -04:00
|
|
|
|
Future<void> _updateNotificationTask(int minuteInterval) async {
|
|
|
|
|
final auth = _apiClient.authenticationData;
|
|
|
|
|
if (!auth.isAuthenticated) throw "Unauthenticated";
|
|
|
|
|
await _workmanager.cancelByUniqueName(taskName);
|
|
|
|
|
_workmanager.registerPeriodicTask(
|
|
|
|
|
taskName,
|
|
|
|
|
taskName,
|
|
|
|
|
frequency: Duration(minutes: minuteInterval),
|
|
|
|
|
inputData: auth.toMap(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-05 09:05:06 -04:00
|
|
|
|
void _onDisconnected(error) async {
|
2023-05-06 12:34:14 -04:00
|
|
|
|
_wsChannel = null;
|
2023-05-03 11:51:18 -04:00
|
|
|
|
_logger.warning("Neos Hub connection died with error '$error', reconnecting...");
|
2023-05-06 12:34:14 -04:00
|
|
|
|
await startWebsocket();
|
2023-05-03 11:51:18 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-06 12:34:14 -04:00
|
|
|
|
Future<void> startWebsocket() async {
|
2023-05-02 04:04:54 -04:00
|
|
|
|
if (!_apiClient.isAuthenticated) {
|
2023-05-03 11:51:18 -04:00
|
|
|
|
_logger.info("Tried to connect to Neos Hub without authentication, this is probably fine for now.");
|
2023-05-01 11:34:34 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-05-03 11:51:18 -04:00
|
|
|
|
if (_isConnecting) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_isConnecting = true;
|
|
|
|
|
_wsChannel = await _tryConnect();
|
|
|
|
|
_isConnecting = false;
|
|
|
|
|
_logger.info("Connected to Neos Hub.");
|
|
|
|
|
_wsChannel!.done.then((error) => _onDisconnected(error));
|
|
|
|
|
_wsChannel!.listen(_handleEvent, onDone: () => _onDisconnected("Connection closed."), onError: _onDisconnected);
|
|
|
|
|
_wsChannel!.add(_negotiationPacket);
|
|
|
|
|
}
|
2023-05-01 11:34:34 -04:00
|
|
|
|
|
2023-05-03 11:51:18 -04:00
|
|
|
|
Future<WebSocket> _tryConnect() async {
|
|
|
|
|
while (true) {
|
|
|
|
|
try {
|
|
|
|
|
final http.Response response;
|
|
|
|
|
try {
|
|
|
|
|
response = await http.post(
|
|
|
|
|
Uri.parse("${Config.neosHubUrl}/negotiate"),
|
|
|
|
|
headers: _apiClient.authorizationHeader,
|
|
|
|
|
);
|
|
|
|
|
ApiClient.checkResponse(response);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
throw "Failed to acquire connection info from Neos API: $e";
|
|
|
|
|
}
|
|
|
|
|
final body = jsonDecode(response.body);
|
|
|
|
|
final url = (body["url"] as String?)?.replaceFirst("https://", "wss://");
|
|
|
|
|
final wsToken = body["accessToken"];
|
2023-05-01 11:34:34 -04:00
|
|
|
|
|
2023-05-03 11:51:18 -04:00
|
|
|
|
if (url == null || wsToken == null) {
|
|
|
|
|
throw "Invalid response from server.";
|
|
|
|
|
}
|
2023-05-05 09:05:06 -04:00
|
|
|
|
final ws = await WebSocket.connect("$url&access_token=$wsToken");
|
|
|
|
|
_attempts = 0;
|
|
|
|
|
return ws;
|
2023-05-03 11:51:18 -04:00
|
|
|
|
} catch (e) {
|
2023-05-05 09:05:06 -04:00
|
|
|
|
final timeout = _reconnectTimeoutsSeconds[_attempts.clamp(0, _reconnectTimeoutsSeconds.length - 1)];
|
2023-05-03 11:51:18 -04:00
|
|
|
|
_logger.severe(e);
|
|
|
|
|
_logger.severe("Retrying in $timeout seconds");
|
|
|
|
|
await Future.delayed(Duration(seconds: timeout));
|
2023-05-05 09:05:06 -04:00
|
|
|
|
_attempts++;
|
2023-05-03 11:51:18 -04:00
|
|
|
|
}
|
2023-05-01 11:34:34 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _handleEvent(event) {
|
|
|
|
|
final body = jsonDecode((event.toString().replaceAll(eofChar, "")));
|
|
|
|
|
final int rawType = body["type"] ?? 0;
|
|
|
|
|
if (rawType > EventType.values.length) {
|
2023-05-03 11:51:18 -04:00
|
|
|
|
_logger.info("Unhandled event type $rawType: $body");
|
2023-05-01 11:34:34 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
switch (EventType.values[rawType]) {
|
2023-05-05 09:05:06 -04:00
|
|
|
|
case EventType.unknown1:
|
|
|
|
|
case EventType.unknown2:
|
|
|
|
|
case EventType.unknown3:
|
|
|
|
|
case EventType.unknown4:
|
2023-05-01 11:34:34 -04:00
|
|
|
|
case EventType.unknown:
|
2023-05-05 09:05:06 -04:00
|
|
|
|
_logger.info("Received unknown event: $rawType: $body");
|
2023-05-01 11:34:34 -04:00
|
|
|
|
break;
|
|
|
|
|
case EventType.message:
|
2023-05-05 09:05:06 -04:00
|
|
|
|
_logger.info("Received message-event.");
|
2023-05-01 11:34:34 -04:00
|
|
|
|
_handleMessageEvent(body);
|
|
|
|
|
break;
|
2023-05-05 09:05:06 -04:00
|
|
|
|
case EventType.keepAlive:
|
|
|
|
|
_logger.info("Received keep-alive.");
|
|
|
|
|
break;
|
|
|
|
|
case EventType.error:
|
|
|
|
|
_logger.severe("Received error-event: ${body["error"]}");
|
|
|
|
|
// Should we trigger a manual reconnect here or just let the remote service close the connection?
|
|
|
|
|
break;
|
2023-05-01 11:34:34 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-02 04:04:54 -04:00
|
|
|
|
void _handleMessageEvent(body) async {
|
2023-05-01 11:34:34 -04:00
|
|
|
|
final target = EventTarget.parse(body["target"]);
|
|
|
|
|
final args = body["arguments"];
|
|
|
|
|
switch (target) {
|
|
|
|
|
case EventTarget.unknown:
|
2023-05-03 11:51:18 -04:00
|
|
|
|
_logger.info("Unknown event-target in message: $body");
|
2023-05-01 11:34:34 -04:00
|
|
|
|
return;
|
|
|
|
|
case EventTarget.messageSent:
|
|
|
|
|
final msg = args[0];
|
|
|
|
|
final message = Message.fromMap(msg, withState: MessageState.sent);
|
2023-05-06 11:47:36 -04:00
|
|
|
|
final cache = getUserMessageCache(message.recipientId) ?? _createUserMessageCache(message.recipientId);
|
2023-05-02 04:04:54 -04:00
|
|
|
|
cache.addMessage(message);
|
2023-05-06 11:47:36 -04:00
|
|
|
|
notifyListeners();
|
2023-05-01 11:34:34 -04:00
|
|
|
|
break;
|
2023-05-05 05:29:54 -04:00
|
|
|
|
case EventTarget.receiveMessage:
|
2023-05-01 11:34:34 -04:00
|
|
|
|
final msg = args[0];
|
|
|
|
|
final message = Message.fromMap(msg);
|
2023-05-06 11:47:36 -04:00
|
|
|
|
final cache = getUserMessageCache(message.senderId) ?? _createUserMessageCache(message.senderId);
|
2023-05-02 04:04:54 -04:00
|
|
|
|
cache.addMessage(message);
|
2023-05-06 11:47:36 -04:00
|
|
|
|
if (message.senderId != selectedFriend?.id) {
|
2023-05-05 06:40:19 -04:00
|
|
|
|
addUnread(message);
|
2023-05-06 13:45:54 -04:00
|
|
|
|
updateFriendStatus(message.senderId);
|
2023-05-05 05:29:54 -04:00
|
|
|
|
}
|
2023-05-06 11:47:36 -04:00
|
|
|
|
notifyListeners();
|
2023-05-01 11:34:34 -04:00
|
|
|
|
break;
|
|
|
|
|
case EventTarget.messagesRead:
|
|
|
|
|
final messageIds = args[0]["ids"] as List;
|
|
|
|
|
final recipientId = args[0]["recipientId"];
|
2023-05-06 11:47:36 -04:00
|
|
|
|
if (recipientId == null) break;
|
|
|
|
|
final cache = getUserMessageCache(recipientId);
|
|
|
|
|
if (cache == null) break;
|
2023-05-01 11:34:34 -04:00
|
|
|
|
for (var id in messageIds) {
|
2023-05-02 04:04:54 -04:00
|
|
|
|
cache.setMessageState(id, MessageState.read);
|
2023-05-01 11:34:34 -04:00
|
|
|
|
}
|
2023-05-06 11:47:36 -04:00
|
|
|
|
notifyListeners();
|
2023-05-01 11:34:34 -04:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-02 04:04:54 -04:00
|
|
|
|
void sendMessage(Message message) async {
|
2023-05-01 11:34:34 -04:00
|
|
|
|
final msgBody = message.toMap();
|
|
|
|
|
final data = {
|
|
|
|
|
"type": EventType.message.index,
|
|
|
|
|
"target": "SendMessage",
|
|
|
|
|
"arguments": [
|
|
|
|
|
msgBody
|
|
|
|
|
],
|
|
|
|
|
};
|
2023-05-03 14:03:46 -04:00
|
|
|
|
_sendData(data);
|
2023-05-06 11:47:36 -04:00
|
|
|
|
final cache = getUserMessageCache(message.recipientId) ?? _createUserMessageCache(message.recipientId);
|
2023-05-02 04:04:54 -04:00
|
|
|
|
cache.messages.add(message);
|
2023-05-06 11:47:36 -04:00
|
|
|
|
notifyListeners();
|
2023-05-01 11:34:34 -04:00
|
|
|
|
}
|
2023-05-03 14:03:46 -04:00
|
|
|
|
|
|
|
|
|
void markMessagesRead(MarkReadBatch batch) {
|
|
|
|
|
final msgBody = batch.toMap();
|
|
|
|
|
final data = {
|
|
|
|
|
"type": EventType.message.index,
|
|
|
|
|
"target": "MarkMessagesRead",
|
|
|
|
|
"arguments": [
|
|
|
|
|
msgBody
|
|
|
|
|
],
|
|
|
|
|
};
|
|
|
|
|
_sendData(data);
|
2023-05-06 13:01:15 -04:00
|
|
|
|
clearUnreadsForUser(batch.senderId);
|
2023-05-03 14:03:46 -04:00
|
|
|
|
}
|
2023-05-06 11:47:36 -04:00
|
|
|
|
}
|