fix(llc): also save reactions while saving threads data in persistence

Signed-off-by: xsahil03x <[email protected]>
This commit is contained in:
Sahil Kumar
2022-03-24 20:51:11 +05:30
committed by xsahil03x
parent 2bde45ec1a
commit e4c6d8ccda
2 changed files with 31 additions and 15 deletions
@@ -2122,12 +2122,12 @@ class ChannelClientState {
final BehaviorSubject<Map<String, List<Message>>> _threadsController =
BehaviorSubject.seeded({});
set _threads(Map<String, List<Message>> v) {
_channel.client.chatPersistenceClient?.updateMessages(
set _threads(Map<String, List<Message>> threads) {
_threadsController.add(threads);
_channel.client.chatPersistenceClient?.updateChannelThreads(
_channel.cid!,
v.values.expand((v) => v).toList(),
threads,
);
_threadsController.add(v);
}
/// Channel related typing users last value.
@@ -197,6 +197,22 @@ abstract class ChatPersistenceClient {
/// Deletes all the members by channel [cids]
Future<void> deleteMembersByCids(List<String> cids);
/// Updates the channel [cid] threads data along with reactions and users.
Future<void> updateChannelThreads(
String cid,
Map<String, List<Message>> threads,
) async {
final messages = threads.values.expand((it) => it).toList();
final reactions = messages.expand(_expandReactions).toList();
final users = messages.map((it) => it.user).withNullifyer.toList();
await Future.wait([
updateMessages(cid, messages),
updateReactions(reactions),
updateUsers(users),
]);
}
/// Update the channel state data using [channelState]
Future<void> updateChannelState(ChannelState channelState) =>
updateChannelStates([channelState]);
@@ -239,17 +255,8 @@ abstract class ChatPersistenceClient {
channelWithMessages[cid] = messages;
channelWithPinnedMessages[cid] = pinnedMessages;
List<Reaction> expandReactions(Message message) {
final own = message.ownReactions;
final latest = message.latestReactions;
return [
if (own != null) ...own.where((r) => r.userId != null),
if (latest != null) ...latest.where((r) => r.userId != null),
];
}
reactions.addAll(messages.expand(expandReactions));
pinnedReactions.addAll(pinnedMessages.expand(expandReactions));
reactions.addAll(messages.expand(_expandReactions));
pinnedReactions.addAll(pinnedMessages.expand(_expandReactions));
users.addAll([
channel.createdBy,
@@ -292,4 +299,13 @@ abstract class ChatPersistenceClient {
),
]);
}
List<Reaction> _expandReactions(Message message) {
final own = message.ownReactions;
final latest = message.latestReactions;
return [
if (own != null) ...own.where((r) => r.userId != null),
if (latest != null) ...latest.where((r) => r.userId != null),
];
}
}