2024-09-16 11:50:49 +00:00
|
|
|
import 'dart:math' as math;
|
|
|
|
|
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';
|
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-09-16 11:50:49 +00:00
|
|
|
const firstTake = 20;
|
|
|
|
const furtherTake = 100;
|
|
|
|
|
2024-06-27 16:05:43 +00:00
|
|
|
isLoading.value = true;
|
2024-09-16 11:50:49 +00:00
|
|
|
await syncLocal(channel, take: firstTake);
|
|
|
|
isLoading.value = false;
|
|
|
|
|
|
|
|
// Take a small range of messages to check is local database up to date
|
|
|
|
var isUpToDate = true;
|
|
|
|
final result =
|
|
|
|
await src.pullRemoteEvents(channel, scope: scope, take: firstTake);
|
|
|
|
totalEvents.value = result?.$2 ?? 0;
|
|
|
|
if ((result?.$1.length ?? 0) > 0) {
|
|
|
|
final minId = result!.$1.map((x) => x.id).reduce(math.min);
|
|
|
|
isUpToDate = await src.getEventFromLocal(minId) != null;
|
|
|
|
}
|
|
|
|
syncLocal(channel, take: firstTake);
|
2024-09-15 10:25:04 +00:00
|
|
|
|
2024-09-16 11:50:49 +00:00
|
|
|
if (!isUpToDate) {
|
|
|
|
// Loading more content due to isn't up to date
|
|
|
|
final result =
|
|
|
|
await src.pullRemoteEvents(channel, scope: scope, take: furtherTake);
|
2024-06-27 20:54:03 +00:00
|
|
|
totalEvents.value = result?.$2 ?? 0;
|
2024-09-16 11:50:49 +00:00
|
|
|
syncLocal(channel, take: furtherTake);
|
|
|
|
}
|
2024-06-27 16:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> loadEvents(Channel channel, String scope) async {
|
2024-09-15 10:25:04 +00:00
|
|
|
const take = 20;
|
|
|
|
final offset = currentEvents.length;
|
|
|
|
|
2024-06-27 16:05:43 +00:00
|
|
|
isLoading.value = true;
|
2024-09-15 10:25:04 +00:00
|
|
|
await syncLocal(channel, take: take, offset: offset);
|
2024-09-16 11:50:49 +00:00
|
|
|
src
|
|
|
|
.pullRemoteEvents(channel, scope: scope, take: take, offset: offset)
|
|
|
|
.then((result) {
|
2024-06-27 20:54:03 +00:00
|
|
|
totalEvents.value = result?.$2 ?? 0;
|
2024-09-15 10:25:04 +00:00
|
|
|
syncLocal(channel, take: take, offset: offset);
|
|
|
|
});
|
2024-06-27 16:05:43 +00:00
|
|
|
isLoading.value = false;
|
|
|
|
}
|
|
|
|
|
2024-09-15 10:25:04 +00:00
|
|
|
Future<bool> syncLocal(Channel channel,
|
|
|
|
{required int take, int offset = 0}) async {
|
|
|
|
final data = await src.listEvents(channel, take: take, offset: offset);
|
2024-09-16 11:50:49 +00:00
|
|
|
if (currentEvents.length >= offset + take) {
|
|
|
|
currentEvents.replaceRange(offset, offset + take, data);
|
|
|
|
} else {
|
|
|
|
currentEvents.insertAll(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-09-15 10:25:04 +00:00
|
|
|
entry = await src.receiveEvent(remote);
|
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
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|