Solian/lib/providers/message/adaptor.dart

174 lines
4.7 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/exceptions/request.dart';
2024-06-23 04:29:07 +00:00
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/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
}
2024-09-03 15:07:20 +00:00
Future<(List<Event>, int)?> getWhatsNewEvents(int pivot, {take = 10}) async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) return null;
final client = auth.configureClient('messaging');
final resp = await client.get(
'/whats-new?pivot=$pivot&take=$take',
);
if (resp.statusCode != 200) {
throw RequestException(resp);
}
final PaginationResult response = PaginationResult.fromJson(resp.body);
final result =
response.data?.map((e) => Event.fromJson(e)).toList() ?? List.empty();
return (result, response.count);
}
2024-06-27 20:54:03 +00:00
Future<Event?> getRemoteEvent(int id, Channel channel, String scope) async {
final AuthProvider auth = Get.find();
2024-07-24 17:18:47 +00:00
if (auth.isAuthorized.isFalse) return null;
2024-06-27 20:54:03 +00:00
final client = auth.configureClient('messaging');
final resp = await client.get(
'/channels/$scope/${channel.alias}/events/$id',
2024-06-27 20:54:03 +00:00
);
if (resp.statusCode == 404) {
return null;
} else if (resp.statusCode != 200) {
throw RequestException(resp);
2024-06-27 20:54:03 +00:00
}
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 {
2024-06-27 20:54:03 +00:00
if (remainDepth <= 0) {
return null;
}
final AuthProvider auth = Get.find();
2024-07-24 17:18:47 +00:00
if (auth.isAuthorized.isFalse) return null;
2024-06-27 20:54:03 +00:00
final client = auth.configureClient('messaging');
final resp = await client.get(
'/channels/$scope/${channel.alias}/events?take=$take&offset=$offset',
2024-06-27 20:54:03 +00:00
);
if (resp.statusCode != 200) {
throw RequestException(resp);
2024-06-27 20:54:03 +00:00
}
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 ??
2024-06-27 20:54:03 +00:00
List.empty();
return ([...result, ...expandResult], response.count);
}
2024-07-06 09:12:57 +00:00
extension MessageHistoryAdaptor 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;
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-07-06 09:12:57 +00:00
Future<(List<Event>, int)?> syncRemoteEvents(Channel channel,
{String scope = 'global', depth = 10, offset = 0}) async {
final lastOne = await localEvents.findLastByChannel(channel.id);
2024-06-27 20:54:03 +00:00
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);
},
);
2024-06-27 20:54:03 +00:00
if (data != null) {
await localEvents.insertBulk(
data.$1
.map((x) => LocalEvent(x.id, x, x.channelId, x.createdAt))
.toList(),
);
}
return data;
}
2024-07-06 09:12:57 +00:00
Future<List<LocalEvent>> listEvents(Channel channel) async {
return await localEvents.findAllByChannel(channel.id);
2024-06-23 04:29:07 +00:00
}
}