Solian/lib/controllers/chat_events_controller.dart

179 lines
4.7 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-09-13 16:30:33 +00:00
import 'package:solian/providers/database/database.dart';
import 'package:solian/providers/database/services/messages.dart';
class ChatEventController {
2024-09-13 16:30:33 +00:00
late final MessagesFetchingProvider src;
2024-09-13 16:30:33 +00:00
final RxList<LocalMessageEventTableData> currentEvents =
RxList.empty(growable: true);
final RxInt totalEvents = 0.obs;
2024-09-15 02:55:27 +00:00
final RxBool isLoading = true.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-09-13 16:30:33 +00:00
src = Get.find();
currentEvents.clear();
}
2024-09-13 16:30:33 +00:00
Future<LocalMessageEventTableData?> getEvent(int id) async {
2024-06-27 20:54:03 +00:00
if (channel == null || scope == null) return null;
2024-09-13 16:30:33 +00:00
return await src.getEvent(id, channel!, scope: scope!);
2024-06-27 18:49:28 +00:00
}
2024-09-15 02:55:27 +00:00
Future<void> getInitialEvents(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) {
2024-09-13 16:30:33 +00:00
final result = await src.fetchRemoteEvents(
2024-06-27 20:54:03 +00:00
channel,
scope,
2024-09-15 02:55:27 +00:00
depth: 1,
2024-06-27 20:54:03 +00:00
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-09-13 16:30:33 +00:00
final entry = LocalMessageEventTableData(
id: x.id,
channelId: x.channelId,
createdAt: x.createdAt,
data: x,
);
2024-07-06 09:12:57 +00:00
insertEvent(entry);
applyEvent(entry);
2024-06-28 06:56:57 +00:00
}
2024-06-27 20:54:03 +00:00
}
} else {
2024-09-13 16:30:33 +00:00
final result = await src.pullRemoteEvents(
2024-06-27 20:54:03 +00:00
channel,
scope: scope,
2024-09-15 02:55:27 +00:00
depth: 1,
2024-06-27 20:54:03 +00:00
);
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) {
2024-09-13 16:30:33 +00:00
final result = await src.fetchRemoteEvents(
2024-06-27 20:54:03 +00:00
channel,
scope,
2024-09-15 02:55:27 +00:00
depth: 3,
2024-06-27 20:54:03 +00:00
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-09-13 16:30:33 +00:00
final entry = LocalMessageEventTableData(
id: x.id,
channelId: x.channelId,
createdAt: x.createdAt,
data: x,
);
2024-07-06 09:12:57 +00:00
currentEvents.add(entry);
applyEvent(entry);
2024-06-28 06:56:57 +00:00
}
2024-06-27 20:54:03 +00:00
}
} else {
2024-09-13 16:30:33 +00:00
final result = await src.pullRemoteEvents(
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-09-13 16:30:33 +00:00
final data = await src.listEvents(channel);
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-09-13 16:30:33 +00:00
LocalMessageEventTableData entry;
2024-06-27 20:54:03 +00:00
if (PlatformInfo.isWeb) {
2024-09-13 16:30:33 +00:00
entry = LocalMessageEventTableData(
id: remote.id,
channelId: remote.channelId,
createdAt: remote.createdAt,
data: remote,
2024-06-27 20:54:03 +00:00
);
} else {
2024-09-13 16:30:33 +00:00
entry = await src.receiveEvent(remote);
2024-06-27 20:54:03 +00:00
}
2024-09-15 02:55:27 +00:00
totalEvents.value++;
2024-07-06 09:12:57 +00:00
insertEvent(entry);
2024-06-28 06:56:57 +00:00
applyEvent(entry);
}
2024-09-13 16:30:33 +00:00
void insertEvent(LocalMessageEventTableData entry) {
if (entry.channelId != channel?.id) return;
2024-09-13 16:30:33 +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
}
2024-09-13 16:30:33 +00:00
void applyEvent(LocalMessageEventTableData entry) {
2024-07-06 09:12:57 +00:00
if (entry.channelId != channel?.id) return;
2024-09-13 16:30:33 +00:00
switch (entry.data!.type) {
case 'messages.edit':
2024-09-13 16:30:33 +00:00
final body = EventMessageBody.fromJson(entry.data!.body);
if (body.relatedEvent != null) {
final idx =
2024-09-13 16:30:33 +00:00
currentEvents.indexWhere((x) => x.data!.id == body.relatedEvent);
if (idx != -1) {
2024-09-13 16:30:33 +00:00
currentEvents[idx].data!.body = entry.data!.body;
currentEvents[idx].data!.updatedAt = entry.data!.updatedAt;
}
}
case 'messages.delete':
2024-09-13 16:30:33 +00:00
final body = EventMessageBody.fromJson(entry.data!.body);
if (body.relatedEvent != null) {
currentEvents.removeWhere((x) => x.id == body.relatedEvent);
}
}
}
2024-09-13 16:30:33 +00:00
Future<void> addPendingEvent(Event info) async {
currentEvents.insert(
0,
2024-09-13 16:30:33 +00:00
LocalMessageEventTableData(
id: info.id,
channelId: info.channelId,
createdAt: DateTime.now(),
data: info,
),
);
}
}