2024-11-17 13:30:02 +00:00
|
|
|
import 'dart:async';
|
2024-11-16 17:16:54 +00:00
|
|
|
import 'dart:math' as math;
|
|
|
|
|
|
|
|
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 'calls.new':
|
|
|
|
final payload = SnChatMessage.fromJson(event.payload!);
|
|
|
|
if (payload.channel.id == channel?.id) {
|
|
|
|
// TODO impl call
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'calls.end':
|
|
|
|
final payload = SnChatMessage.fromJson(event.payload!);
|
|
|
|
if (payload.channel.id == channel?.id) {
|
|
|
|
// TODO impl call
|
|
|
|
}
|
|
|
|
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-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(
|
|
|
|
preload: SnChatMessagePreload(attachments: attachments),
|
|
|
|
);
|
|
|
|
|
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-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(
|
|
|
|
preload: SnChatMessagePreload(attachments: attachments),
|
|
|
|
);
|
|
|
|
|
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':
|
|
|
|
final body = message.body;
|
|
|
|
if (body['related_event'] != null) {
|
|
|
|
final idx = messages.indexWhere((x) => x.id == body['related_event']);
|
|
|
|
if (idx != -1) {
|
|
|
|
final newBody = message.body;
|
|
|
|
newBody.remove('related_event');
|
|
|
|
messages[idx] = messages[idx].copyWith(
|
|
|
|
body: newBody,
|
|
|
|
updatedAt: message.updatedAt,
|
|
|
|
);
|
|
|
|
if (_box!.containsKey(body['related_event'])) {
|
|
|
|
await _box!.put(body['related_event'], messages[idx]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'messages.delete':
|
|
|
|
final body = message.body;
|
|
|
|
if (body['related_event'] != null) {
|
|
|
|
messages.removeWhere((x) => x.id == body['related_event']);
|
|
|
|
if (_box!.containsKey(body['related_event'])) {
|
|
|
|
await _box!.delete(body['related_event']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 17:16:54 +00:00
|
|
|
Future<void> sendMessage(
|
|
|
|
String type,
|
|
|
|
String content, {
|
|
|
|
int? quoteId,
|
|
|
|
int? relatedId,
|
|
|
|
List<String>? attachments,
|
|
|
|
}) 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',
|
|
|
|
if (quoteId != null) 'quote_id': quoteId,
|
|
|
|
if (relatedId != null) 'related_id': 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
|
|
|
|
final message = SnChatMessage(
|
|
|
|
id: 0,
|
|
|
|
createdAt: DateTime.now(),
|
|
|
|
updatedAt: DateTime.now(),
|
|
|
|
deletedAt: null,
|
|
|
|
uuid: nonce,
|
|
|
|
body: body,
|
|
|
|
type: type,
|
|
|
|
channel: channel!,
|
|
|
|
channelId: channel!.id,
|
|
|
|
sender: profile!,
|
|
|
|
senderId: profile!.id,
|
2024-11-16 17:16:54 +00:00
|
|
|
);
|
2024-11-17 13:30:02 +00:00
|
|
|
_addUnconfirmedMessage(message);
|
|
|
|
|
|
|
|
// Send to server
|
|
|
|
try {
|
|
|
|
await _sn.client.post(
|
|
|
|
'/cgi/im/channels/${channel!.keyPath}/messages',
|
|
|
|
data: {
|
|
|
|
'type': type,
|
|
|
|
'uuid': nonce,
|
|
|
|
'body': body,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
print(err);
|
|
|
|
}
|
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) {
|
|
|
|
await getMessages(kSingleBatchLoadLimit, idx);
|
|
|
|
}
|
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
|
|
|
}) async {
|
2024-11-17 16:55:39 +00:00
|
|
|
late List<SnChatMessage> out;
|
|
|
|
if (_box != null && (_box!.length >= take + offset || forceLocal)) {
|
2024-11-18 13:38:15 +00:00
|
|
|
out = _box!.values.skip(offset).take(take).toList().reversed.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 13:38:15 +00:00
|
|
|
for (var i = 0; i < out.length; i++) {
|
|
|
|
out[i] = out[i].copyWith(
|
|
|
|
preload: SnChatMessagePreload(
|
|
|
|
attachments: attachments
|
|
|
|
.where(
|
|
|
|
(ele) =>
|
|
|
|
out[i].body['attachments']?.contains(ele?.rid) ?? false,
|
|
|
|
)
|
|
|
|
.toList(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-11-16 17:16:54 +00:00
|
|
|
|
|
|
|
// Preload sender accounts
|
|
|
|
await _ud.listAccount(out.map((ele) => ele.sender.accountId).toSet());
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|