2024-06-27 16:05:43 +00:00
|
|
|
import 'package:floor/floor.dart';
|
2024-06-23 04:29:07 +00:00
|
|
|
import 'package:get/get.dart';
|
|
|
|
import 'package:solian/models/channel.dart';
|
2024-06-27 16:05:43 +00:00
|
|
|
import 'package:solian/models/event.dart';
|
2024-06-23 04:29:07 +00:00
|
|
|
import 'package:solian/models/pagination.dart';
|
|
|
|
import 'package:solian/providers/auth.dart';
|
2024-06-27 16:05:43 +00:00
|
|
|
import 'package:solian/providers/message/events.dart';
|
2024-06-23 04:29:07 +00:00
|
|
|
|
|
|
|
Future<MessageHistoryDb> createHistoryDb() async {
|
2024-06-27 16:05:43 +00:00
|
|
|
final migration1to2 = Migration(1, 2, (database) async {
|
|
|
|
await database.execute('DROP TABLE IF EXISTS LocalMessage');
|
|
|
|
});
|
|
|
|
|
2024-06-23 04:29:07 +00:00
|
|
|
return await $FloorMessageHistoryDb
|
|
|
|
.databaseBuilder('messaging_data.dart')
|
2024-06-27 16:05:43 +00:00
|
|
|
.addMigrations([migration1to2]).build();
|
2024-06-23 04:29:07 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 20:54:03 +00:00
|
|
|
Future<Event?> getRemoteEvent(int id, Channel channel, String scope) async {
|
|
|
|
final AuthProvider auth = Get.find();
|
|
|
|
if (!await auth.isAuthorized) return null;
|
|
|
|
|
|
|
|
final client = auth.configureClient('messaging');
|
|
|
|
|
|
|
|
final resp = await client.get(
|
|
|
|
'/api/channels/$scope/${channel.alias}/events/$id',
|
|
|
|
);
|
|
|
|
|
|
|
|
if (resp.statusCode == 404) {
|
|
|
|
return null;
|
|
|
|
} else if (resp.statusCode != 200) {
|
|
|
|
throw Exception(resp.bodyString);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Event.fromJson(resp.body);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<(List<Event>, int)?> getRemoteEvents(
|
|
|
|
Channel channel,
|
|
|
|
String scope, {
|
|
|
|
required int remainDepth,
|
|
|
|
bool Function(List<Event> items)? onBrake,
|
|
|
|
take = 10,
|
|
|
|
offset = 0,
|
|
|
|
}) async {
|
|
|
|
if (remainDepth <= 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final AuthProvider auth = Get.find();
|
|
|
|
if (!await auth.isAuthorized) return null;
|
|
|
|
|
|
|
|
final client = auth.configureClient('messaging');
|
|
|
|
|
|
|
|
final resp = await client.get(
|
|
|
|
'/api/channels/$scope/${channel.alias}/events?take=$take&offset=$offset',
|
|
|
|
);
|
|
|
|
|
|
|
|
if (resp.statusCode != 200) {
|
|
|
|
throw Exception(resp.bodyString);
|
|
|
|
}
|
|
|
|
|
|
|
|
final PaginationResult response = PaginationResult.fromJson(resp.body);
|
|
|
|
final result =
|
|
|
|
response.data?.map((e) => Event.fromJson(e)).toList() ?? List.empty();
|
|
|
|
|
|
|
|
if (onBrake != null && onBrake(result)) {
|
|
|
|
return (result, response.count);
|
|
|
|
}
|
|
|
|
|
|
|
|
final expandResult = (await getRemoteEvents(
|
|
|
|
channel,
|
|
|
|
scope,
|
|
|
|
remainDepth: remainDepth - 1,
|
|
|
|
take: take,
|
|
|
|
offset: offset + result.length,
|
|
|
|
))
|
|
|
|
?.$1 ??
|
|
|
|
List.empty();
|
|
|
|
|
|
|
|
return ([...result, ...expandResult], response.count);
|
|
|
|
}
|
|
|
|
|
2024-06-23 04:29:07 +00:00
|
|
|
extension MessageHistoryHelper on MessageHistoryDb {
|
2024-06-27 16:59:11 +00:00
|
|
|
Future<LocalEvent> receiveEvent(Event remote) async {
|
2024-06-27 16:05:43 +00:00
|
|
|
final entry = LocalEvent(
|
2024-06-23 05:27:21 +00:00
|
|
|
remote.id,
|
|
|
|
remote,
|
|
|
|
remote.channelId,
|
2024-06-27 16:05:43 +00:00
|
|
|
remote.createdAt,
|
2024-06-23 10:51:49 +00:00
|
|
|
);
|
2024-06-27 16:05:43 +00:00
|
|
|
await localEvents.insert(entry);
|
|
|
|
switch (remote.type) {
|
|
|
|
case 'messages.edit':
|
|
|
|
final body = EventMessageBody.fromJson(remote.body);
|
|
|
|
if (body.relatedEvent != null) {
|
|
|
|
final target = await localEvents.findById(body.relatedEvent!);
|
|
|
|
if (target != null) {
|
|
|
|
target.data.body = remote.body;
|
|
|
|
target.data.updatedAt = remote.updatedAt;
|
|
|
|
await localEvents.update(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'messages.delete':
|
|
|
|
final body = EventMessageBody.fromJson(remote.body);
|
|
|
|
if (body.relatedEvent != null) {
|
|
|
|
await localEvents.delete(body.relatedEvent!);
|
|
|
|
}
|
|
|
|
}
|
2024-06-23 10:51:49 +00:00
|
|
|
return entry;
|
2024-06-23 05:27:21 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 16:59:11 +00:00
|
|
|
Future<LocalEvent?> getEvent(int id, Channel channel,
|
|
|
|
{String scope = 'global'}) async {
|
|
|
|
final localRecord = await localEvents.findById(id);
|
|
|
|
if (localRecord != null) return localRecord;
|
|
|
|
|
2024-06-27 20:54:03 +00:00
|
|
|
final remoteRecord = await getRemoteEvent(id, channel, scope);
|
2024-06-27 16:59:11 +00:00
|
|
|
if (remoteRecord == null) return null;
|
|
|
|
|
|
|
|
return await receiveEvent(remoteRecord);
|
|
|
|
}
|
|
|
|
|
2024-06-27 16:05:43 +00:00
|
|
|
Future<(List<Event>, int)?> syncEvents(Channel channel,
|
|
|
|
{String scope = 'global', depth = 10, offset = 0}) async {
|
|
|
|
final lastOne = await localEvents.findLastByChannel(channel.id);
|
2024-06-23 05:27:21 +00:00
|
|
|
|
2024-06-27 20:54:03 +00:00
|
|
|
final data = await getRemoteEvents(
|
2024-06-23 05:27:21 +00:00
|
|
|
channel,
|
|
|
|
scope,
|
2024-06-27 16:05:43 +00:00
|
|
|
remainDepth: depth,
|
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-27 20:54:03 +00:00
|
|
|
if (data != null) {
|
2024-06-27 16:05:43 +00:00
|
|
|
await localEvents.insertBulk(
|
|
|
|
data.$1
|
|
|
|
.map((x) => LocalEvent(x.id, x, x.channelId, x.createdAt))
|
|
|
|
.toList(),
|
2024-06-23 10:51:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-06-27 16:05:43 +00:00
|
|
|
return data;
|
2024-06-23 05:27:21 +00:00
|
|
|
}
|
|
|
|
|
2024-06-27 16:05:43 +00:00
|
|
|
Future<List<LocalEvent>> listMessages(Channel channel) async {
|
|
|
|
return await localEvents.findAllByChannel(channel.id);
|
2024-06-23 04:29:07 +00:00
|
|
|
}
|
|
|
|
}
|