🐛 Fix web messaging module
This commit is contained in:
parent
693725d5ae
commit
793ad156e3
@ -17,14 +17,27 @@ class ChatEventController {
|
||||
String? scope;
|
||||
|
||||
initialize() async {
|
||||
database = await createHistoryDb();
|
||||
if (!PlatformInfo.isWeb) {
|
||||
database = await createHistoryDb();
|
||||
}
|
||||
currentEvents.clear();
|
||||
}
|
||||
|
||||
Future<LocalEvent?> 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<void> 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<void> 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;
|
||||
|
||||
|
@ -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<MessageHistoryDb> createHistoryDb() async {
|
||||
.addMigrations([migration1to2]).build();
|
||||
}
|
||||
|
||||
Future<Event?> 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<Event>, int)?> getRemoteEvents(
|
||||
Channel channel,
|
||||
String scope, {
|
||||
required int remainDepth,
|
||||
bool Function(List<Event> 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<LocalEvent> 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<Event?> _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<Event>, int)?> _getRemoteEvents(
|
||||
Channel channel,
|
||||
String scope, {
|
||||
required int remainDepth,
|
||||
bool Function(List<Event> 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<List<LocalEvent>> listMessages(Channel channel) async {
|
||||
return await localEvents.findAllByChannel(channel.id);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user