2024-11-17 13:30:02 +00:00
|
|
|
import 'dart:async';
|
2024-11-16 17:16:54 +00:00
|
|
|
import 'dart:math' as math;
|
|
|
|
|
2024-11-19 14:17:17 +00:00
|
|
|
import 'package:collection/collection.dart';
|
2024-11-18 15:59:08 +00:00
|
|
|
import 'package:dio/dio.dart';
|
2024-11-16 17:16:54 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:hive/hive.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
2024-11-17 16:55:39 +00:00
|
|
|
import 'package:surface/providers/sn_attachment.dart';
|
2024-11-16 17:16:54 +00:00
|
|
|
import 'package:surface/providers/sn_network.dart';
|
|
|
|
import 'package:surface/providers/user_directory.dart';
|
2024-11-17 13:30:02 +00:00
|
|
|
import 'package:surface/providers/websocket.dart';
|
2024-11-16 17:16:54 +00:00
|
|
|
import 'package:surface/types/chat.dart';
|
|
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
|
|
|
class ChatMessageController extends ChangeNotifier {
|
|
|
|
static const kChatMessageBoxPrefix = 'nex_chat_messages_';
|
|
|
|
static const kSingleBatchLoadLimit = 100;
|
|
|
|
|
|
|
|
late final SnNetworkProvider _sn;
|
|
|
|
late final UserDirectoryProvider _ud;
|
2024-11-17 13:30:02 +00:00
|
|
|
late final WebSocketProvider _ws;
|
2024-11-17 16:55:39 +00:00
|
|
|
late final SnAttachmentProvider _attach;
|
2024-11-17 13:30:02 +00:00
|
|
|
|
|
|
|
StreamSubscription? _wsSubscription;
|
2024-11-16 17:16:54 +00:00
|
|
|
|
|
|
|
ChatMessageController(BuildContext context) {
|
|
|
|
_sn = context.read<SnNetworkProvider>();
|
|
|
|
_ud = context.read<UserDirectoryProvider>();
|
2024-11-17 13:30:02 +00:00
|
|
|
_ws = context.read<WebSocketProvider>();
|
2024-11-17 16:55:39 +00:00
|
|
|
_attach = context.read<SnAttachmentProvider>();
|
2024-11-16 17:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool isPending = true;
|
|
|
|
bool isLoading = false;
|
|
|
|
|
|
|
|
int? messageTotal;
|
|
|
|
|
|
|
|
bool get isAllLoaded =>
|
|
|
|
messageTotal != null && messages.length >= messageTotal!;
|
|
|
|
|
|
|
|
String? _boxKey;
|
2024-11-17 13:30:02 +00:00
|
|
|
SnChannel? channel;
|
|
|
|
SnChannelMember? profile;
|
|
|
|
|
|
|
|
/// Messages are the all the messages that in the channel
|
|
|
|
final List<SnChatMessage> messages = List.empty(growable: true);
|
2024-11-16 17:16:54 +00:00
|
|
|
|
2024-11-17 13:30:02 +00:00
|
|
|
/// Unconfirmed messages are the messages that sent by client but did not receive the reply from websocket server.
|
|
|
|
/// Stored as a list of nonce to provide the loading state
|
|
|
|
final List<String> unconfirmedMessages = List.empty(growable: true);
|
2024-11-16 17:16:54 +00:00
|
|
|
|
|
|
|
Box<SnChatMessage>? get _box =>
|
|
|
|
(_boxKey == null || isPending) ? null : Hive.box<SnChatMessage>(_boxKey!);
|
|
|
|
|
2024-11-17 13:30:02 +00:00
|
|
|
Future<void> initialize(SnChannel chan) async {
|
|
|
|
channel = chan;
|
|
|
|
|
|
|
|
// Initialize local data
|
|
|
|
_boxKey = '$kChatMessageBoxPrefix${chan.id}';
|
2024-11-16 17:16:54 +00:00
|
|
|
await Hive.openBox<SnChatMessage>(_boxKey!);
|
2024-11-17 13:30:02 +00:00
|
|
|
|
|
|
|
// Fetch channel profile
|
|
|
|
final resp = await _sn.client.get(
|
|
|
|
'/cgi/im/channels/${chan.keyPath}/me',
|
|
|
|
);
|
|
|
|
profile = SnChannelMember.fromJson(
|
|
|
|
resp.data as Map<String, dynamic>,
|
|
|
|
);
|
|
|
|
|
|
|
|
_wsSubscription = _ws.stream.stream.listen((event) {
|
|
|
|
switch (event.method) {
|
|
|
|
case 'events.new':
|
|
|
|
final payload = SnChatMessage.fromJson(event.payload!);
|
|
|
|
_addMessage(payload);
|
|
|
|
break;
|
|
|
|
case 'status.typing':
|
|
|
|
if (event.payload?['channel_id'] != channel?.id) break;
|
|
|
|
final member = SnChannelMember.fromJson(event.payload!['member']);
|
|
|
|
if (member.id == profile?.id) break;
|
|
|
|
// TODO impl typing users
|
|
|
|
// if (!_typingUsers.any((x) => x.id == member.id)) {
|
|
|
|
// setState(() {
|
|
|
|
// _typingUsers.add(member);
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
// _typingInactiveTimer[member.id]?.cancel();
|
|
|
|
// _typingInactiveTimer[member.id] = Timer(
|
|
|
|
// const Duration(seconds: 3),
|
|
|
|
// () {
|
|
|
|
// setState(() {
|
|
|
|
// _typingUsers.removeWhere((x) => x.id == member.id);
|
|
|
|
// _typingInactiveTimer.remove(member.id);
|
|
|
|
// });
|
|
|
|
// },
|
|
|
|
// );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-11-16 17:16:54 +00:00
|
|
|
isPending = false;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _saveMessageToLocal(Iterable<SnChatMessage> messages) async {
|
|
|
|
if (_box == null) return;
|
|
|
|
await _box!.putAll({
|
|
|
|
for (final message in messages) message.id: message,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-11-17 13:30:02 +00:00
|
|
|
Future<void> _addUnconfirmedMessage(SnChatMessage message) async {
|
2024-11-18 14:33:03 +00:00
|
|
|
SnChatMessage? quoteEvent;
|
2024-11-18 15:04:36 +00:00
|
|
|
if (message.quoteEventId != null) {
|
|
|
|
quoteEvent = await getMessage(message.quoteEventId as int);
|
2024-11-18 14:33:03 +00:00
|
|
|
}
|
|
|
|
|
2024-11-17 16:55:39 +00:00
|
|
|
final attachmentRid = List<String>.from(
|
|
|
|
message.body['attachments']?.cast<String>() ?? [],
|
|
|
|
);
|
|
|
|
final attachments = await _attach.getMultiple(attachmentRid);
|
|
|
|
message = message.copyWith(
|
2024-11-18 14:33:03 +00:00
|
|
|
preload: SnChatMessagePreload(
|
|
|
|
quoteEvent: quoteEvent,
|
|
|
|
attachments: attachments,
|
|
|
|
),
|
2024-11-17 16:55:39 +00:00
|
|
|
);
|
|
|
|
|
2024-11-17 13:30:02 +00:00
|
|
|
messages.insert(0, message);
|
|
|
|
unconfirmedMessages.add(message.uuid);
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
|
2024-11-16 17:16:54 +00:00
|
|
|
Future<void> _addMessage(SnChatMessage message) async {
|
2024-11-18 14:33:03 +00:00
|
|
|
SnChatMessage? quoteEvent;
|
2024-11-18 15:04:36 +00:00
|
|
|
if (message.quoteEventId != null) {
|
|
|
|
quoteEvent = await getMessage(message.quoteEventId as int);
|
2024-11-18 14:33:03 +00:00
|
|
|
}
|
|
|
|
|
2024-11-17 16:55:39 +00:00
|
|
|
final attachmentRid = List<String>.from(
|
|
|
|
message.body['attachments']?.cast<String>() ?? [],
|
|
|
|
);
|
|
|
|
final attachments = await _attach.getMultiple(attachmentRid);
|
|
|
|
message = message.copyWith(
|
2024-11-18 14:33:03 +00:00
|
|
|
preload: SnChatMessagePreload(
|
|
|
|
quoteEvent: quoteEvent,
|
|
|
|
attachments: attachments,
|
|
|
|
),
|
2024-11-17 16:55:39 +00:00
|
|
|
);
|
|
|
|
|
2024-11-17 13:30:02 +00:00
|
|
|
final idx = messages.indexWhere((e) => e.uuid == message.uuid);
|
|
|
|
if (idx != -1) {
|
|
|
|
unconfirmedMessages.remove(message.uuid);
|
|
|
|
messages[idx] = message;
|
|
|
|
} else {
|
|
|
|
messages.insert(0, message);
|
|
|
|
}
|
|
|
|
await _applyMessage(message);
|
2024-11-16 17:16:54 +00:00
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
if (_box == null) return;
|
|
|
|
await _box!.put(message.id, message);
|
|
|
|
}
|
|
|
|
|
2024-11-17 13:30:02 +00:00
|
|
|
Future<void> _applyMessage(SnChatMessage message) async {
|
|
|
|
if (message.channelId != channel?.id) return;
|
|
|
|
|
|
|
|
switch (message.type) {
|
|
|
|
case 'messages.edit':
|
2024-11-18 15:59:08 +00:00
|
|
|
if (message.relatedEventId != null) {
|
|
|
|
final idx =
|
|
|
|
messages.indexWhere((x) => x.id == message.relatedEventId);
|
2024-11-17 13:30:02 +00:00
|
|
|
if (idx != -1) {
|
|
|
|
final newBody = message.body;
|
|
|
|
newBody.remove('related_event');
|
|
|
|
messages[idx] = messages[idx].copyWith(
|
|
|
|
body: newBody,
|
|
|
|
updatedAt: message.updatedAt,
|
|
|
|
);
|
2024-11-18 15:59:08 +00:00
|
|
|
if (_box!.containsKey(message.relatedEventId)) {
|
|
|
|
await _box!.put(message.relatedEventId, messages[idx]);
|
2024-11-17 13:30:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'messages.delete':
|
2024-11-18 15:59:08 +00:00
|
|
|
if (message.relatedEventId != null) {
|
|
|
|
messages.removeWhere((x) => x.id == message.relatedEventId);
|
|
|
|
if (_box!.containsKey(message.relatedEventId)) {
|
|
|
|
await _box!.delete(message.relatedEventId);
|
2024-11-17 13:30:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 17:16:54 +00:00
|
|
|
Future<void> sendMessage(
|
|
|
|
String type,
|
|
|
|
String content, {
|
|
|
|
int? quoteId,
|
|
|
|
int? relatedId,
|
|
|
|
List<String>? attachments,
|
2024-11-18 15:59:08 +00:00
|
|
|
SnChatMessage? editingMessage,
|
2024-11-16 17:16:54 +00:00
|
|
|
}) async {
|
2024-11-17 13:30:02 +00:00
|
|
|
if (channel == null) return;
|
2024-11-16 17:16:54 +00:00
|
|
|
const uuid = Uuid();
|
|
|
|
final nonce = uuid.v4();
|
2024-11-17 13:30:02 +00:00
|
|
|
final body = {
|
|
|
|
'text': content,
|
|
|
|
'algorithm': 'plain',
|
2024-11-18 14:33:03 +00:00
|
|
|
if (quoteId != null) 'quote_event': quoteId,
|
2024-11-18 15:59:08 +00:00
|
|
|
if (relatedId != null) 'related_event': relatedId,
|
2024-11-17 16:55:39 +00:00
|
|
|
if (attachments != null && attachments.isNotEmpty)
|
|
|
|
'attachments': attachments,
|
2024-11-17 13:30:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Mock the message locally
|
2024-11-28 15:35:25 +00:00
|
|
|
final createdAt = DateTime.now();
|
2024-11-17 13:30:02 +00:00
|
|
|
final message = SnChatMessage(
|
|
|
|
id: 0,
|
2024-11-28 15:35:25 +00:00
|
|
|
createdAt: createdAt,
|
|
|
|
updatedAt: createdAt,
|
2024-11-17 13:30:02 +00:00
|
|
|
deletedAt: null,
|
|
|
|
uuid: nonce,
|
|
|
|
body: body,
|
|
|
|
type: type,
|
|
|
|
channel: channel!,
|
|
|
|
channelId: channel!.id,
|
|
|
|
sender: profile!,
|
|
|
|
senderId: profile!.id,
|
2024-11-18 15:04:36 +00:00
|
|
|
quoteEventId: quoteId,
|
2024-11-18 15:59:08 +00:00
|
|
|
relatedEventId: relatedId,
|
2024-11-16 17:16:54 +00:00
|
|
|
);
|
2024-11-17 13:30:02 +00:00
|
|
|
_addUnconfirmedMessage(message);
|
|
|
|
|
|
|
|
// Send to server
|
|
|
|
try {
|
2024-11-18 15:59:08 +00:00
|
|
|
await _sn.client.request(
|
|
|
|
editingMessage != null
|
|
|
|
? '/cgi/im/channels/${channel!.keyPath}/messages/${editingMessage.id}'
|
|
|
|
: '/cgi/im/channels/${channel!.keyPath}/messages',
|
2024-11-17 13:30:02 +00:00
|
|
|
data: {
|
|
|
|
'type': type,
|
|
|
|
'uuid': nonce,
|
|
|
|
'body': body,
|
|
|
|
},
|
2024-11-18 15:59:08 +00:00
|
|
|
options: Options(
|
|
|
|
method: editingMessage != null ? 'PUT' : 'POST',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteMessage(SnChatMessage message) async {
|
|
|
|
if (message.channelId != channel?.id) return;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await _sn.client.delete(
|
|
|
|
'/cgi/im/channels/${channel!.keyPath}/messages/${message.id}',
|
2024-11-17 13:30:02 +00:00
|
|
|
);
|
2024-11-18 15:59:08 +00:00
|
|
|
messages.removeWhere((x) => x.id == message.id);
|
2024-11-17 13:30:02 +00:00
|
|
|
} catch (err) {
|
2024-11-18 15:59:08 +00:00
|
|
|
// ignore
|
2024-11-17 13:30:02 +00:00
|
|
|
}
|
2024-11-16 17:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check the local storage is up to date with the server.
|
|
|
|
/// If the local storage is not up to date, it will be updated.
|
|
|
|
Future<void> checkUpdate() async {
|
|
|
|
if (_box == null) return;
|
|
|
|
if (_box!.isEmpty) return;
|
|
|
|
|
|
|
|
isLoading = true;
|
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
try {
|
|
|
|
final resp = await _sn.client.get(
|
2024-11-17 13:30:02 +00:00
|
|
|
'/cgi/im/channels/${channel!.keyPath}/events/update',
|
2024-11-16 17:16:54 +00:00
|
|
|
queryParameters: {
|
|
|
|
'pivot': _box!.values.last.id,
|
|
|
|
},
|
|
|
|
);
|
2024-11-17 13:30:02 +00:00
|
|
|
if (resp.data['up_to_date'] == true) return;
|
2024-11-16 17:16:54 +00:00
|
|
|
// Only preload the first 100 messages to prevent first time check update cause load to server and waste local storage.
|
|
|
|
// FIXME If the local is missing more than 100 messages, it won't be fetched, this is a problem, we need to fix it.
|
|
|
|
final countToFetch = math.min(resp.data['count'] as int, 100);
|
|
|
|
|
|
|
|
for (int idx = 0; idx < countToFetch; idx += kSingleBatchLoadLimit) {
|
2024-11-19 14:17:17 +00:00
|
|
|
await getMessages(kSingleBatchLoadLimit, idx, forceRemote: true);
|
2024-11-16 17:16:54 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
rethrow;
|
|
|
|
} finally {
|
2024-11-17 13:30:02 +00:00
|
|
|
await loadMessages();
|
2024-11-16 17:16:54 +00:00
|
|
|
isLoading = false;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-18 14:33:03 +00:00
|
|
|
/// Get a single event from the current channel
|
|
|
|
/// If it was not found in local storage we will look it up in remote
|
|
|
|
Future<SnChatMessage?> getMessage(int id) async {
|
|
|
|
SnChatMessage? out;
|
|
|
|
if (_box != null && _box!.containsKey(id)) {
|
|
|
|
out = _box!.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out == null) {
|
|
|
|
try {
|
|
|
|
final resp = await _sn.client
|
|
|
|
.get('/cgi/im/channels/${channel!.keyPath}/events/$id');
|
|
|
|
out = SnChatMessage.fromJson(resp.data);
|
|
|
|
_saveMessageToLocal([out]);
|
|
|
|
} catch (_) {
|
|
|
|
// ignore, maybe not found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preload some related things if found
|
|
|
|
if (out != null) {
|
|
|
|
await _ud.listAccount([out.sender.accountId]);
|
|
|
|
|
|
|
|
final attachments = await _attach.getMultiple(
|
|
|
|
out.body['attachments']?.cast<String>() ?? [],
|
|
|
|
);
|
|
|
|
out = out.copyWith(
|
|
|
|
preload: SnChatMessagePreload(
|
|
|
|
attachments: attachments,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2024-11-16 17:16:54 +00:00
|
|
|
/// Get message from local storage first, then from the server.
|
|
|
|
/// Will not check local storage is up to date with the server.
|
|
|
|
/// If you need to do the sync, do the `checkUpdate` instead.
|
|
|
|
Future<List<SnChatMessage>> getMessages(
|
|
|
|
int take,
|
|
|
|
int offset, {
|
|
|
|
bool forceLocal = false,
|
2024-11-19 14:17:17 +00:00
|
|
|
bool forceRemote = false,
|
2024-11-16 17:16:54 +00:00
|
|
|
}) async {
|
2024-11-17 16:55:39 +00:00
|
|
|
late List<SnChatMessage> out;
|
2024-11-19 14:17:17 +00:00
|
|
|
if (_box != null &&
|
|
|
|
(_box!.length >= take + offset || forceLocal) &&
|
|
|
|
!forceRemote) {
|
|
|
|
out = _box!.keys
|
|
|
|
.toList()
|
|
|
|
.cast<int>()
|
|
|
|
.sorted((a, b) => b.compareTo(a))
|
|
|
|
.skip(offset)
|
|
|
|
.take(take)
|
|
|
|
.map((key) => _box!.get(key)!)
|
|
|
|
.toList();
|
2024-11-17 16:55:39 +00:00
|
|
|
} else {
|
|
|
|
final resp = await _sn.client.get(
|
|
|
|
'/cgi/im/channels/${channel!.keyPath}/events',
|
|
|
|
queryParameters: {
|
|
|
|
'take': take,
|
|
|
|
'offset': offset,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
messageTotal = resp.data['count'] as int?;
|
|
|
|
out = List<SnChatMessage>.from(
|
|
|
|
resp.data['data']?.map((e) => SnChatMessage.fromJson(e)) ?? [],
|
|
|
|
);
|
|
|
|
_saveMessageToLocal(out);
|
2024-11-16 17:16:54 +00:00
|
|
|
}
|
|
|
|
|
2024-11-17 16:55:39 +00:00
|
|
|
// Preload attachments
|
|
|
|
final attachmentRid = List<String>.from(
|
|
|
|
out.expand((e) => (e.body['attachments'] as List<dynamic>?) ?? []),
|
2024-11-16 17:16:54 +00:00
|
|
|
);
|
2024-11-17 16:55:39 +00:00
|
|
|
final attachments = await _attach.getMultiple(attachmentRid);
|
2024-11-18 14:33:03 +00:00
|
|
|
|
|
|
|
// Putting preload back to data
|
2024-11-18 13:38:15 +00:00
|
|
|
for (var i = 0; i < out.length; i++) {
|
2024-11-18 14:33:03 +00:00
|
|
|
// Preload related events (quoted)
|
|
|
|
SnChatMessage? quoteEvent;
|
2024-11-18 15:04:36 +00:00
|
|
|
if (out[i].quoteEventId != null) {
|
|
|
|
quoteEvent = await getMessage(out[i].quoteEventId as int);
|
2024-11-18 14:33:03 +00:00
|
|
|
}
|
|
|
|
|
2024-11-18 13:38:15 +00:00
|
|
|
out[i] = out[i].copyWith(
|
|
|
|
preload: SnChatMessagePreload(
|
2024-11-18 14:33:03 +00:00
|
|
|
quoteEvent: quoteEvent,
|
2024-11-18 13:38:15 +00:00
|
|
|
attachments: attachments
|
|
|
|
.where(
|
|
|
|
(ele) =>
|
|
|
|
out[i].body['attachments']?.contains(ele?.rid) ?? false,
|
|
|
|
)
|
|
|
|
.toList(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-11-16 17:16:54 +00:00
|
|
|
|
|
|
|
// Preload sender accounts
|
2024-11-18 15:59:08 +00:00
|
|
|
final accountId = out
|
|
|
|
.where((ele) => ele.sender.accountId >= 0)
|
|
|
|
.map((ele) => ele.sender.accountId)
|
|
|
|
.toSet();
|
|
|
|
await _ud.listAccount(accountId);
|
2024-11-16 17:16:54 +00:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The load messages method work as same as the `getMessages` method.
|
|
|
|
/// But it won't return the messages instead append them to the value that controller has.
|
|
|
|
/// At the same time, this method provide the `isLoading` state.
|
|
|
|
/// The `skip` parameter is no longer required since it will skip the messages count that already loaded.
|
|
|
|
Future<void> loadMessages({int take = 20}) async {
|
|
|
|
isLoading = true;
|
|
|
|
notifyListeners();
|
|
|
|
|
|
|
|
try {
|
|
|
|
final out = await getMessages(take, messages.length);
|
|
|
|
messages.addAll(out);
|
|
|
|
} catch (err) {
|
|
|
|
rethrow;
|
|
|
|
} finally {
|
|
|
|
isLoading = false;
|
|
|
|
notifyListeners();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-17 13:30:02 +00:00
|
|
|
@override
|
|
|
|
void dispose() {
|
2024-11-16 17:16:54 +00:00
|
|
|
_box?.close();
|
2024-11-17 13:30:02 +00:00
|
|
|
_wsSubscription?.cancel();
|
|
|
|
super.dispose();
|
2024-11-16 17:16:54 +00:00
|
|
|
}
|
|
|
|
}
|