2024-06-23 04:29:07 +00:00
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:solian/models/channel.dart';
|
|
|
|
import 'package:solian/models/message.dart';
|
|
|
|
import 'package:solian/models/pagination.dart';
|
|
|
|
import 'package:solian/providers/auth.dart';
|
|
|
|
import 'package:solian/providers/message/history.dart';
|
|
|
|
|
|
|
|
Future<MessageHistoryDb> createHistoryDb() async {
|
|
|
|
return await $FloorMessageHistoryDb
|
|
|
|
.databaseBuilder('messaging_data.dart')
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
extension MessageHistoryHelper on MessageHistoryDb {
|
|
|
|
receiveMessage(Message remote) async {
|
2024-06-23 10:51:49 +00:00
|
|
|
final entry = LocalMessage(
|
2024-06-23 04:29:07 +00:00
|
|
|
remote.id,
|
|
|
|
remote,
|
|
|
|
remote.channelId,
|
2024-06-23 10:51:49 +00:00
|
|
|
);
|
|
|
|
await localMessages.insert(entry);
|
|
|
|
return entry;
|
2024-06-23 04:29:07 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 05:27:21 +00:00
|
|
|
replaceMessage(Message remote) async {
|
2024-06-23 10:51:49 +00:00
|
|
|
final entry = LocalMessage(
|
2024-06-23 05:27:21 +00:00
|
|
|
remote.id,
|
|
|
|
remote,
|
|
|
|
remote.channelId,
|
2024-06-23 10:51:49 +00:00
|
|
|
);
|
|
|
|
await localMessages.update(entry);
|
|
|
|
return entry;
|
2024-06-23 05:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
burnMessage(int id) async {
|
|
|
|
await localMessages.delete(id);
|
|
|
|
}
|
|
|
|
|
2024-06-23 11:02:41 +00:00
|
|
|
syncMessages(Channel channel, {String scope = 'global', breath = 10, offset = 0}) async {
|
2024-06-23 05:27:21 +00:00
|
|
|
final lastOne = await localMessages.findLastByChannel(channel.id);
|
|
|
|
|
|
|
|
final data = await _getRemoteMessages(
|
|
|
|
channel,
|
|
|
|
scope,
|
2024-06-23 11:02:41 +00:00
|
|
|
remainBreath: breath,
|
2024-06-23 10:03:46 +00:00
|
|
|
offset: offset,
|
2024-06-23 05:27:21 +00:00
|
|
|
onBrake: (items) {
|
|
|
|
return items.any((x) => x.id == lastOne?.id);
|
|
|
|
},
|
|
|
|
);
|
2024-06-23 10:51:49 +00:00
|
|
|
if (data != null) {
|
|
|
|
await localMessages.insertBulk(
|
|
|
|
data.$1.map((x) => LocalMessage(x.id, x, x.channelId)).toList(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return data?.$2 ?? 0;
|
2024-06-23 05:27:21 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 10:51:49 +00:00
|
|
|
Future<(List<Message>, int)?> _getRemoteMessages(
|
2024-06-23 05:27:21 +00:00
|
|
|
Channel channel,
|
|
|
|
String scope, {
|
|
|
|
required int remainBreath,
|
|
|
|
bool Function(List<Message> items)? onBrake,
|
|
|
|
take = 10,
|
|
|
|
offset = 0,
|
|
|
|
}) async {
|
|
|
|
if (remainBreath <= 0) {
|
2024-06-23 10:51:49 +00:00
|
|
|
return null;
|
2024-06-23 05:27:21 +00:00
|
|
|
}
|
|
|
|
|
2024-06-23 04:29:07 +00:00
|
|
|
final AuthProvider auth = Get.find();
|
2024-06-23 10:51:49 +00:00
|
|
|
if (!await auth.isAuthorized) return null;
|
2024-06-23 04:29:07 +00:00
|
|
|
|
|
|
|
final client = auth.configureClient('messaging');
|
|
|
|
|
2024-06-23 05:27:21 +00:00
|
|
|
final resp = await client.get(
|
|
|
|
'/api/channels/$scope/${channel.alias}/messages?take=$take&offset=$offset');
|
2024-06-23 04:29:07 +00:00
|
|
|
|
|
|
|
if (resp.statusCode != 200) {
|
|
|
|
throw Exception(resp.bodyString);
|
|
|
|
}
|
|
|
|
|
2024-06-23 05:27:21 +00:00
|
|
|
final PaginationResult response = PaginationResult.fromJson(resp.body);
|
|
|
|
final result =
|
|
|
|
response.data?.map((e) => Message.fromJson(e)).toList() ?? List.empty();
|
2024-06-23 04:29:07 +00:00
|
|
|
|
2024-06-23 05:27:21 +00:00
|
|
|
if (onBrake != null && onBrake(result)) {
|
2024-06-23 10:51:49 +00:00
|
|
|
return (result, response.count);
|
2024-06-23 04:29:07 +00:00
|
|
|
}
|
2024-06-23 05:27:21 +00:00
|
|
|
|
2024-06-23 10:51:49 +00:00
|
|
|
final expandResult = (await _getRemoteMessages(
|
|
|
|
channel,
|
|
|
|
scope,
|
|
|
|
remainBreath: remainBreath - 1,
|
|
|
|
take: take,
|
|
|
|
offset: offset + result.length,
|
|
|
|
))
|
|
|
|
?.$1 ??
|
|
|
|
List.empty();
|
|
|
|
|
|
|
|
return ([...result, ...expandResult], response.count);
|
2024-06-23 04:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<LocalMessage>> listMessages(Channel channel) async {
|
|
|
|
return await localMessages.findAllByChannel(channel.id);
|
|
|
|
}
|
|
|
|
}
|