Basic event based rendering

This commit is contained in:
2024-06-28 00:59:11 +08:00
parent bbc9ea69f7
commit 78f9ad941b
11 changed files with 352 additions and 275 deletions

View File

@ -18,7 +18,7 @@ Future<MessageHistoryDb> createHistoryDb() async {
}
extension MessageHistoryHelper on MessageHistoryDb {
receiveEvent(Event remote) async {
Future<LocalEvent> receiveEvent(Event remote) async {
final entry = LocalEvent(
remote.id,
remote,
@ -46,6 +46,17 @@ extension MessageHistoryHelper on MessageHistoryDb {
return entry;
}
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);
@ -70,6 +81,25 @@ extension MessageHistoryHelper on MessageHistoryDb {
return data;
}
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, {