diff --git a/lib/controllers/chat_events_controller.dart b/lib/controllers/chat_events_controller.dart index 709f55b..a78f311 100644 --- a/lib/controllers/chat_events_controller.dart +++ b/lib/controllers/chat_events_controller.dart @@ -17,14 +17,27 @@ class ChatEventController { String? scope; initialize() async { - database = await createHistoryDb(); + if (!PlatformInfo.isWeb) { + database = await createHistoryDb(); + } currentEvents.clear(); } Future getEvent(int id) async { - if(channel == null || scope == null) return null; + if (channel == null || scope == null) return null; - return await database.getEvent(id, channel!, scope: scope!); + 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!); + } } Future getEvents(Channel channel, String scope) async { @@ -34,42 +47,82 @@ class ChatEventController { syncLocal(channel); isLoading.value = true; - final result = await database.syncEvents( - channel, - scope: scope, - ); - totalEvents.value = result?.$2 ?? 0; - if (!await syncLocal(channel) && result != null) { - currentEvents.addAll(result.$1.map( - (x) => LocalEvent( - x.id, - x, - x.channelId, - x.createdAt, - ), - )); + if (PlatformInfo.isWeb) { + final result = await getRemoteEvents( + channel, + scope, + remainDepth: 3, + offset: 0, + ); + totalEvents.value = result?.$2 ?? 0; + if (result != null) { + currentEvents.addAll(result.$1.map( + (x) => LocalEvent( + x.id, + x, + x.channelId, + x.createdAt, + ), + )); + } + } else { + final result = await database.syncEvents( + channel, + scope: scope, + ); + totalEvents.value = result?.$2 ?? 0; + if (!await syncLocal(channel) && result != null) { + currentEvents.addAll(result.$1.map( + (x) => LocalEvent( + x.id, + x, + x.channelId, + x.createdAt, + ), + )); + } } isLoading.value = false; } Future loadEvents(Channel channel, String scope) async { isLoading.value = true; - final result = await database.syncEvents( - channel, - depth: 3, - scope: scope, - offset: currentEvents.length, - ); - totalEvents.value = result?.$2 ?? 0; - if (!await syncLocal(channel) && result != null) { - currentEvents.addAll(result.$1.map( - (x) => LocalEvent( - x.id, - x, - x.channelId, - x.createdAt, - ), - )); + if (PlatformInfo.isWeb) { + final result = await getRemoteEvents( + channel, + scope, + remainDepth: 3, + offset: currentEvents.length, + ); + totalEvents.value = result?.$2 ?? 0; + if (result != null) { + currentEvents.addAll(result.$1.map( + (x) => LocalEvent( + x.id, + x, + x.channelId, + x.createdAt, + ), + )); + } + } else { + final result = await database.syncEvents( + channel, + depth: 3, + scope: scope, + offset: currentEvents.length, + ); + totalEvents.value = result?.$2 ?? 0; + if (!await syncLocal(channel) && result != null) { + currentEvents.addAll(result.$1.map( + (x) => LocalEvent( + x.id, + x, + x.channelId, + x.createdAt, + ), + )); + } } isLoading.value = false; } @@ -85,7 +138,17 @@ class ChatEventController { } receiveEvent(Event remote) async { - final entry = await database.receiveEvent(remote); + LocalEvent entry; + if (PlatformInfo.isWeb) { + entry = LocalEvent( + remote.id, + remote, + remote.channelId, + remote.createdAt, + ); + } else { + entry = await database.receiveEvent(remote); + } if (remote.channelId != channel?.id) return; diff --git a/lib/providers/message/helper.dart b/lib/providers/message/helper.dart index af97d9b..9fe3d53 100644 --- a/lib/providers/message/helper.dart +++ b/lib/providers/message/helper.dart @@ -3,7 +3,6 @@ import 'package:get/get.dart'; import 'package:solian/models/channel.dart'; import 'package:solian/models/event.dart'; import 'package:solian/models/pagination.dart'; -import 'package:solian/platform.dart'; import 'package:solian/providers/auth.dart'; import 'package:solian/providers/message/events.dart'; @@ -17,6 +16,71 @@ Future createHistoryDb() async { .addMigrations([migration1to2]).build(); } +Future getRemoteEvent(int id, Channel channel, String scope) async { + final AuthProvider auth = Get.find(); + if (!await auth.isAuthorized) return null; + + final client = auth.configureClient('messaging'); + + final resp = await client.get( + '/api/channels/$scope/${channel.alias}/events/$id', + ); + + if (resp.statusCode == 404) { + return null; + } else if (resp.statusCode != 200) { + throw Exception(resp.bodyString); + } + + return Event.fromJson(resp.body); +} + +Future<(List, int)?> getRemoteEvents( + Channel channel, + String scope, { + required int remainDepth, + bool Function(List items)? onBrake, + take = 10, + offset = 0, + }) async { + if (remainDepth <= 0) { + return null; + } + + final AuthProvider auth = Get.find(); + if (!await auth.isAuthorized) return null; + + final client = auth.configureClient('messaging'); + + final resp = await client.get( + '/api/channels/$scope/${channel.alias}/events?take=$take&offset=$offset', + ); + + if (resp.statusCode != 200) { + throw Exception(resp.bodyString); + } + + 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( + channel, + scope, + remainDepth: remainDepth - 1, + take: take, + offset: offset + result.length, + )) + ?.$1 ?? + List.empty(); + + return ([...result, ...expandResult], response.count); +} + extension MessageHistoryHelper on MessageHistoryDb { Future receiveEvent(Event remote) async { final entry = LocalEvent( @@ -51,7 +115,7 @@ extension MessageHistoryHelper on MessageHistoryDb { final localRecord = await localEvents.findById(id); if (localRecord != null) return localRecord; - final remoteRecord = await _getRemoteEvent(id, channel, scope); + final remoteRecord = await getRemoteEvent(id, channel, scope); if (remoteRecord == null) return null; return await receiveEvent(remoteRecord); @@ -61,7 +125,7 @@ extension MessageHistoryHelper on MessageHistoryDb { {String scope = 'global', depth = 10, offset = 0}) async { final lastOne = await localEvents.findLastByChannel(channel.id); - final data = await _getRemoteEvents( + final data = await getRemoteEvents( channel, scope, remainDepth: depth, @@ -70,7 +134,7 @@ extension MessageHistoryHelper on MessageHistoryDb { return items.any((x) => x.id == lastOne?.id); }, ); - if (data != null && !PlatformInfo.isWeb) { + if (data != null) { await localEvents.insertBulk( data.$1 .map((x) => LocalEvent(x.id, x, x.channelId, x.createdAt)) @@ -81,71 +145,6 @@ extension MessageHistoryHelper on MessageHistoryDb { return data; } - Future _getRemoteEvent(int id, Channel channel, String scope) async { - final AuthProvider auth = Get.find(); - if (!await auth.isAuthorized) return null; - - final client = auth.configureClient('messaging'); - - final resp = await client.get( - '/api/channels/$scope/${channel.alias}/events/$id', - ); - - if (resp.statusCode == 404) { - return null; - } else if (resp.statusCode != 200) { - throw Exception(resp.bodyString); - } - - return Event.fromJson(resp.body); - } - - Future<(List, int)?> _getRemoteEvents( - Channel channel, - String scope, { - required int remainDepth, - bool Function(List items)? onBrake, - take = 10, - offset = 0, - }) async { - if (remainDepth <= 0) { - return null; - } - - final AuthProvider auth = Get.find(); - if (!await auth.isAuthorized) return null; - - final client = auth.configureClient('messaging'); - - final resp = await client.get( - '/api/channels/$scope/${channel.alias}/events?take=$take&offset=$offset', - ); - - if (resp.statusCode != 200) { - throw Exception(resp.bodyString); - } - - 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( - channel, - scope, - remainDepth: remainDepth - 1, - take: take, - offset: offset + result.length, - )) - ?.$1 ?? - List.empty(); - - return ([...result, ...expandResult], response.count); - } - Future> listMessages(Channel channel) async { return await localEvents.findAllByChannel(channel.id); }