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';
|
2024-08-21 07:39:29 +00:00
|
|
|
import 'package:solian/exceptions/request.dart';
|
2024-06-23 04:29:07 +00:00
|
|
|
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-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(
|
2024-07-16 11:46:53 +00:00
|
|
|
'/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) {
|
2024-08-21 07:39:29 +00:00
|
|
|
throw RequestException(resp);
|
2024-06-27 20:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Event.fromJson(resp.body);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<(List<Event>, int)?> getRemoteEvents(
|
2024-08-21 07:39:29 +00:00
|
|
|
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(
|
2024-07-16 11:46:53 +00:00
|
|
|
'/channels/$scope/${channel.alias}/events?take=$take&offset=$offset',
|
2024-06-27 20:54:03 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (resp.statusCode != 200) {
|
2024-08-21 07:39:29 +00:00
|
|
|
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(
|
2024-08-21 07:39:29 +00:00
|
|
|
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 {
|
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-07-06 09:12:57 +00:00
|
|
|
Future<(List<Event>, int)?> syncRemoteEvents(Channel channel,
|
2024-06-27 16:05:43 +00:00
|
|
|
{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-07-06 09:12:57 +00:00
|
|
|
Future<List<LocalEvent>> listEvents(Channel channel) async {
|
2024-06-27 16:05:43 +00:00
|
|
|
return await localEvents.findAllByChannel(channel.id);
|
2024-06-23 04:29:07 +00:00
|
|
|
}
|
|
|
|
}
|