Solian/lib/controllers/chat_events_controller.dart

176 lines
4.5 KiB
Dart
Raw Normal View History

import 'package:get/get.dart';
import 'package:solian/models/channel.dart';
import 'package:solian/models/event.dart';
import 'package:solian/platform.dart';
2024-07-06 09:12:57 +00:00
import 'package:solian/providers/message/adaptor.dart';
import 'package:solian/providers/message/events.dart';
class ChatEventController {
late final MessageHistoryDb database;
final RxList<LocalEvent> currentEvents = RxList.empty(growable: true);
final RxInt totalEvents = 0.obs;
final RxBool isLoading = false.obs;
Channel? channel;
2024-06-27 18:49:28 +00:00
String? scope;
2024-08-01 16:54:19 +00:00
Future<void> initialize() async {
2024-06-27 20:54:03 +00:00
if (!PlatformInfo.isWeb) {
database = await createHistoryDb();
}
currentEvents.clear();
}
2024-06-27 18:49:28 +00:00
Future<LocalEvent?> getEvent(int id) async {
2024-06-27 20:54:03 +00:00
if (channel == null || scope == null) return null;
2024-06-27 18:49:28 +00:00
2024-06-27 20:54:03 +00:00
if (PlatformInfo.isWeb) {
final remoteRecord = await getRemoteEvent(id, channel!, scope!);
if (remoteRecord == null) return null;
return LocalEvent(
remoteRecord.id,
remoteRecord,
remoteRecord.channelId,
remoteRecord.createdAt,
);
} else {
return await database.getEvent(id, channel!, scope: scope!);
}
2024-06-27 18:49:28 +00:00
}
Future<void> getEvents(Channel channel, String scope) async {
2024-06-27 18:49:28 +00:00
this.channel = channel;
this.scope = scope;
syncLocal(channel);
isLoading.value = true;
2024-06-27 20:54:03 +00:00
if (PlatformInfo.isWeb) {
final result = await getRemoteEvents(
channel,
scope,
remainDepth: 3,
offset: 0,
);
totalEvents.value = result?.$2 ?? 0;
if (result != null) {
2024-06-28 06:56:57 +00:00
for (final x in result.$1.reversed) {
2024-07-06 09:12:57 +00:00
final entry = LocalEvent(x.id, x, x.channelId, x.createdAt);
insertEvent(entry);
applyEvent(entry);
2024-06-28 06:56:57 +00:00
}
2024-06-27 20:54:03 +00:00
}
} else {
2024-07-06 09:12:57 +00:00
final result = await database.syncRemoteEvents(
2024-06-27 20:54:03 +00:00
channel,
scope: scope,
);
totalEvents.value = result?.$2 ?? 0;
2024-06-28 06:56:57 +00:00
await syncLocal(channel);
}
isLoading.value = false;
}
Future<void> loadEvents(Channel channel, String scope) async {
isLoading.value = true;
2024-06-27 20:54:03 +00:00
if (PlatformInfo.isWeb) {
final result = await getRemoteEvents(
channel,
scope,
remainDepth: 3,
offset: currentEvents.length,
);
if (result != null) {
2024-07-06 09:12:57 +00:00
totalEvents.value = result.$2;
2024-06-28 06:56:57 +00:00
for (final x in result.$1.reversed) {
2024-07-06 09:12:57 +00:00
final entry = LocalEvent(x.id, x, x.channelId, x.createdAt);
currentEvents.add(entry);
applyEvent(entry);
2024-06-28 06:56:57 +00:00
}
2024-06-27 20:54:03 +00:00
}
} else {
2024-07-06 09:12:57 +00:00
final result = await database.syncRemoteEvents(
2024-06-27 20:54:03 +00:00
channel,
depth: 3,
scope: scope,
offset: currentEvents.length,
);
totalEvents.value = result?.$2 ?? 0;
2024-06-28 06:56:57 +00:00
await syncLocal(channel);
}
isLoading.value = false;
}
Future<bool> syncLocal(Channel channel) async {
if (PlatformInfo.isWeb) return false;
2024-06-28 06:56:57 +00:00
final data = await database.localEvents.findAllByChannel(channel.id);
2024-07-06 09:12:57 +00:00
currentEvents.replaceRange(0, currentEvents.length, data);
2024-06-28 06:56:57 +00:00
for (final x in data.reversed) {
applyEvent(x);
}
return true;
}
receiveEvent(Event remote) async {
2024-06-27 20:54:03 +00:00
LocalEvent entry;
if (PlatformInfo.isWeb) {
entry = LocalEvent(
remote.id,
remote,
remote.channelId,
remote.createdAt,
);
} else {
entry = await database.receiveEvent(remote);
}
2024-07-06 09:12:57 +00:00
insertEvent(entry);
2024-06-28 06:56:57 +00:00
applyEvent(entry);
}
2024-07-06 09:12:57 +00:00
insertEvent(LocalEvent entry) {
2024-06-28 06:56:57 +00:00
final idx = currentEvents.indexWhere((x) => x.data.uuid == entry.data.uuid);
if (idx != -1) {
currentEvents[idx] = entry;
} else {
currentEvents.insert(0, entry);
}
2024-07-06 09:12:57 +00:00
}
applyEvent(LocalEvent entry) {
if (entry.channelId != channel?.id) return;
2024-06-28 06:56:57 +00:00
switch (entry.data.type) {
case 'messages.edit':
2024-06-28 06:56:57 +00:00
final body = EventMessageBody.fromJson(entry.data.body);
if (body.relatedEvent != null) {
final idx =
currentEvents.indexWhere((x) => x.data.id == body.relatedEvent);
if (idx != -1) {
2024-06-28 06:56:57 +00:00
currentEvents[idx].data.body = entry.data.body;
currentEvents[idx].data.updatedAt = entry.data.updatedAt;
}
}
case 'messages.delete':
2024-06-28 06:56:57 +00:00
final body = EventMessageBody.fromJson(entry.data.body);
if (body.relatedEvent != null) {
currentEvents.removeWhere((x) => x.id == body.relatedEvent);
}
}
}
addPendingEvent(Event info) async {
currentEvents.insert(
0,
LocalEvent(
info.id,
info,
info.channelId,
DateTime.now(),
),
);
}
}