@@ -6,6 +6,8 @@ import 'package:stream_chat/src/core/models/event.dart';
66import 'package:stream_chat/src/core/models/filter.dart' ;
77import 'package:stream_chat/src/core/models/member.dart' ;
88import 'package:stream_chat/src/core/models/message.dart' ;
9+ import 'package:stream_chat/src/core/models/poll.dart' ;
10+ import 'package:stream_chat/src/core/models/poll_vote.dart' ;
911import 'package:stream_chat/src/core/models/reaction.dart' ;
1012import 'package:stream_chat/src/core/models/read.dart' ;
1113import 'package:stream_chat/src/core/models/user.dart' ;
@@ -169,6 +171,12 @@ abstract class ChatPersistenceClient {
169171 /// Updates all the channels using the new [channels] data.
170172 Future <void > updateChannels (List <ChannelModel > channels);
171173
174+ /// Updates all the polls using the new [polls] data.
175+ Future <void > updatePolls (List <Poll > polls);
176+
177+ /// Deletes all the polls by [pollIds] .
178+ Future <void > deletePollsByIds (List <String > pollIds);
179+
172180 /// Updates all the members of a particular channle [cid]
173181 /// with the new [members] data
174182 Future <void > updateMembers (String cid, List <Member > members) =>
@@ -194,12 +202,18 @@ abstract class ChatPersistenceClient {
194202 /// Updates the pinned message reactions data with the new [reactions] data
195203 Future <void > updatePinnedMessageReactions (List <Reaction > reactions);
196204
205+ /// Updates the poll votes data with the new [pollVotes] data
206+ Future <void > updatePollVotes (List <PollVote > pollVotes);
207+
197208 /// Deletes all the reactions by [messageIds]
198209 Future <void > deleteReactionsByMessageId (List <String > messageIds);
199210
200211 /// Deletes all the pinned messages reactions by [messageIds]
201212 Future <void > deletePinnedMessageReactionsByMessageId (List <String > messageIds);
202213
214+ /// Deletes all the poll votes by [pollIds]
215+ Future <void > deletePollVotesByPollIds (List <String > pollIds);
216+
203217 /// Deletes all the members by channel [cids]
204218 Future <void > deleteMembersByCids (List <String > cids);
205219
@@ -245,50 +259,64 @@ abstract class ChatPersistenceClient {
245259 final reactions = < Reaction > [];
246260 final pinnedReactions = < Reaction > [];
247261
262+ final polls = < Poll > [];
263+ final pollVotes = < PollVote > [];
264+ final pollVotesToDelete = < String > [];
265+
248266 for (final state in channelStates) {
249267 final channel = state.channel;
250- if ( channel != null ) {
251- channels. add (channel) ;
252-
253- final cid = channel.cid;
254- final reads = state.read ;
255- final members = state.members ;
256- final Iterable < Message > ? messages ;
257- if (CurrentPlatform .isWeb) {
258- messages = state.messages? .where (
268+ // Continue if channel is not available.
269+ if (channel == null ) continue ;
270+ channels. add (channel);
271+
272+ final cid = channel.cid ;
273+ final reads = state.read ;
274+ final members = state.members ;
275+ final messages = switch (CurrentPlatform .isWeb) {
276+ true => state.messages? .where (
259277 (it) => ! it.attachments.any (
260278 (it) => it.uploadState != const UploadState .success (),
261279 ),
262- );
263- } else {
264- messages = state.messages;
265- }
266- final pinnedMessages = state.pinnedMessages;
267-
268- // Preparing deletion data
269- membersToDelete.add (cid);
270- reactionsToDelete.addAll (state.messages? .map ((it) => it.id) ?? []);
271- pinnedReactionsToDelete
272- .addAll (state.pinnedMessages? .map ((it) => it.id) ?? []);
273-
274- // preparing addition data
275- channelWithReads[cid] = reads;
276- channelWithMembers[cid] = members;
277- channelWithMessages[cid] = messages? .toList ();
278- channelWithPinnedMessages[cid] = pinnedMessages;
279-
280- reactions.addAll (messages? .expand (_expandReactions) ?? []);
281- pinnedReactions.addAll (pinnedMessages? .expand (_expandReactions) ?? []);
282-
283- users.addAll ([
284- channel.createdBy,
285- ...messages? .map ((it) => it.user) ?? < User > [],
286- ...reads? .map ((it) => it.user) ?? < User > [],
287- ...members? .map ((it) => it.user) ?? < User > [],
288- ...reactions.map ((it) => it.user),
289- ...pinnedReactions.map ((it) => it.user),
290- ].withNullifyer);
291- }
280+ ),
281+ _ => state.messages,
282+ };
283+
284+ final pinnedMessages = state.pinnedMessages;
285+
286+ // Preparing deletion data
287+ membersToDelete.add (cid);
288+ reactionsToDelete.addAll (messages? .map ((it) => it.id) ?? []);
289+ pinnedReactionsToDelete.addAll (pinnedMessages? .map ((it) => it.id) ?? []);
290+
291+ // preparing addition data
292+ channelWithReads[cid] = reads;
293+ channelWithMembers[cid] = members;
294+ channelWithMessages[cid] = messages? .toList ();
295+ channelWithPinnedMessages[cid] = pinnedMessages;
296+
297+ reactions.addAll (messages? .expand (_expandReactions) ?? []);
298+ pinnedReactions.addAll (pinnedMessages? .expand (_expandReactions) ?? []);
299+
300+ polls.addAll ([
301+ ...? messages? .map ((it) => it.poll),
302+ ...? pinnedMessages? .map ((it) => it.poll),
303+ ].withNullifyer);
304+
305+ pollVotesToDelete.addAll (polls.map ((it) => it.id));
306+
307+ pollVotes.addAll (polls.expand (_expandPollVotes));
308+
309+ users.addAll ([
310+ channel.createdBy,
311+ ...? messages? .map ((it) => it.user),
312+ ...? pinnedMessages? .map ((it) => it.user),
313+ ...? reads? .map ((it) => it.user),
314+ ...? members? .map ((it) => it.user),
315+ ...reactions.map ((it) => it.user),
316+ ...pinnedReactions.map ((it) => it.user),
317+ ...polls.map ((it) => it.createdBy),
318+ ...pollVotes.map ((it) => it.user),
319+ ].withNullifyer);
292320 }
293321
294322 // Removing old members and reactions data as they may have
@@ -297,12 +325,14 @@ abstract class ChatPersistenceClient {
297325 deleteMembersByCids (membersToDelete),
298326 deleteReactionsByMessageId (reactionsToDelete),
299327 deletePinnedMessageReactionsByMessageId (pinnedReactionsToDelete),
328+ deletePollVotesByPollIds (pollVotesToDelete),
300329 ]);
301330
302331 // Updating first as does not depend on any other table.
303332 await Future .wait ([
304333 updateUsers (users.toList (growable: false )),
305334 updateChannels (channels.toList (growable: false )),
335+ updatePolls (polls.toList (growable: false )),
306336 ]);
307337
308338 // All has a foreign key relation with channels table.
@@ -315,10 +345,9 @@ abstract class ChatPersistenceClient {
315345
316346 // Both has a foreign key relation with messages, pinnedMessages table.
317347 await Future .wait ([
318- updateReactions (reactions.toList (growable: false )),
319- updatePinnedMessageReactions (
320- pinnedReactions.toList (growable: false ),
321- ),
348+ updateReactions (reactions),
349+ updatePinnedMessageReactions (pinnedReactions),
350+ updatePollVotes (pollVotes),
322351 ]);
323352 }
324353
@@ -330,4 +359,15 @@ abstract class ChatPersistenceClient {
330359 if (latest != null ) ...latest.where ((r) => r.userId != null ),
331360 ];
332361 }
362+
363+ List <PollVote > _expandPollVotes (Poll poll) {
364+ final latestAnswers = poll.latestAnswers;
365+ final latestVotes = poll.latestVotesByOption.values;
366+ final ownVotesAndAnswers = poll.ownVotesAndAnswers;
367+ return [
368+ ...latestAnswers,
369+ ...latestVotes.expand ((it) => it),
370+ ...ownVotesAndAnswers,
371+ ];
372+ }
333373}
0 commit comments