Solian/lib/providers/message/helper.dart

153 lines
4.3 KiB
Dart
Raw Normal View History

import 'package:floor/floor.dart';
2024-06-23 04:29:07 +00:00
import 'package:get/get.dart';
import 'package:solian/models/channel.dart';
import 'package:solian/models/event.dart';
2024-06-23 04:29:07 +00:00
import 'package:solian/models/pagination.dart';
import 'package:solian/platform.dart';
2024-06-23 04:29:07 +00:00
import 'package:solian/providers/auth.dart';
import 'package:solian/providers/message/events.dart';
2024-06-23 04:29:07 +00:00
Future<MessageHistoryDb> createHistoryDb() async {
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')
.addMigrations([migration1to2]).build();
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 {
final entry = LocalEvent(
remote.id,
remote,
remote.channelId,
remote.createdAt,
);
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!);
}
}
return entry;
}
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;
final remoteRecord = await _getRemoteEvent(id, channel, scope);
if (remoteRecord == null) return null;
return await receiveEvent(remoteRecord);
}
Future<(List<Event>, int)?> syncEvents(Channel channel,
{String scope = 'global', depth = 10, offset = 0}) async {
final lastOne = await localEvents.findLastByChannel(channel.id);
final data = await _getRemoteEvents(
channel,
scope,
remainDepth: depth,
2024-06-23 10:03:46 +00:00
offset: offset,
onBrake: (items) {
return items.any((x) => x.id == lastOne?.id);
},
);
if (data != null && !PlatformInfo.isWeb) {
await localEvents.insertBulk(
data.$1
.map((x) => LocalEvent(x.id, x, x.channelId, x.createdAt))
.toList(),
);
}
return data;
}
2024-06-27 16:59:11 +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;
}
2024-06-23 04:29:07 +00:00
final AuthProvider auth = Get.find();
if (!await auth.isAuthorized) return null;
2024-06-23 04:29:07 +00:00
final client = auth.configureClient('messaging');
final resp = await client.get(
'/api/channels/$scope/${channel.alias}/events?take=$take&offset=$offset',
);
2024-06-23 04:29:07 +00:00
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();
2024-06-23 04:29:07 +00:00
if (onBrake != null && onBrake(result)) {
return (result, response.count);
2024-06-23 04:29:07 +00:00
}
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
}
Future<List<LocalEvent>> listMessages(Channel channel) async {
return await localEvents.findAllByChannel(channel.id);
2024-06-23 04:29:07 +00:00
}
}