2024-06-27 16:05:43 +00:00
|
|
|
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';
|
2024-06-27 16:05:43 +00:00
|
|
|
|
|
|
|
class ChatEventController {
|
2024-09-13 16:30:33 +00:00
|
|
|
late final MessagesFetchingProvider src;
|
2024-06-27 16:05:43 +00:00
|
|
|
|
2024-09-13 16:30:33 +00:00
|
|
|
final RxList<LocalMessageEventTableData> currentEvents =
|
|
|
|
RxList.empty(growable: true);
|
2024-06-27 16:05:43 +00:00
|
|
|
final RxInt totalEvents = 0.obs;
|
|
|
|
|
2024-09-15 02:55:27 +00:00
|
|
|
final RxBool isLoading = true.obs;
|
2024-06-27 16:05:43 +00:00
|
|
|
|
|
|
|
Channel? channel;
|
2024-06-27 18:49:28 +00:00
|
|
|
String? scope;
|
2024-06-27 16:05:43 +00:00
|
|
|
|
2024-08-01 16:54:19 +00:00
|
|
|
Future<void> initialize() async {
|
2024-09-13 16:30:33 +00:00
|
|
|
src = Get.find();
|
2024-06-27 16:05:43 +00:00
|
|
|
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;
|
|
|
|
|
2024-06-27 16:05:43 +00:00
|
|
|
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);
|
2024-06-27 16:05:43 +00:00
|
|
|
}
|
|
|
|
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);
|
2024-06-27 16:05:43 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2024-06-27 16:05:43 +00:00
|
|
|
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-06-27 16:05:43 +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) {
|
2024-08-02 10:09:07 +00:00
|
|
|
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,
|
|
|
|
);
|
2024-06-27 16:05:43 +00:00
|
|
|
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-06-27 16:05:43 +00:00
|
|
|
|
2024-09-13 16:30:33 +00:00
|
|
|
switch (entry.data!.type) {
|
2024-06-27 16:05:43 +00:00
|
|
|
case 'messages.edit':
|
2024-09-13 16:30:33 +00:00
|
|
|
final body = EventMessageBody.fromJson(entry.data!.body);
|
2024-06-27 16:05:43 +00:00
|
|
|
if (body.relatedEvent != null) {
|
|
|
|
final idx =
|
2024-09-13 16:30:33 +00:00
|
|
|
currentEvents.indexWhere((x) => x.data!.id == body.relatedEvent);
|
2024-06-27 16:05:43 +00:00
|
|
|
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;
|
2024-06-27 16:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case 'messages.delete':
|
2024-09-13 16:30:33 +00:00
|
|
|
final body = EventMessageBody.fromJson(entry.data!.body);
|
2024-06-27 16:05:43 +00:00
|
|
|
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 {
|
2024-06-27 16:05:43 +00:00
|
|
|
currentEvents.insert(
|
|
|
|
0,
|
2024-09-13 16:30:33 +00:00
|
|
|
LocalMessageEventTableData(
|
|
|
|
id: info.id,
|
|
|
|
channelId: info.channelId,
|
|
|
|
createdAt: DateTime.now(),
|
|
|
|
data: info,
|
2024-06-27 16:05:43 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|