@@ -6,6 +6,8 @@ import 'package:stream_chat/src/core/models/event.dart';
6
6
import 'package:stream_chat/src/core/models/filter.dart' ;
7
7
import 'package:stream_chat/src/core/models/member.dart' ;
8
8
import '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' ;
9
11
import 'package:stream_chat/src/core/models/reaction.dart' ;
10
12
import 'package:stream_chat/src/core/models/read.dart' ;
11
13
import 'package:stream_chat/src/core/models/user.dart' ;
@@ -169,6 +171,12 @@ abstract class ChatPersistenceClient {
169
171
/// Updates all the channels using the new [channels] data.
170
172
Future <void > updateChannels (List <ChannelModel > channels);
171
173
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
+
172
180
/// Updates all the members of a particular channle [cid]
173
181
/// with the new [members] data
174
182
Future <void > updateMembers (String cid, List <Member > members) =>
@@ -194,12 +202,18 @@ abstract class ChatPersistenceClient {
194
202
/// Updates the pinned message reactions data with the new [reactions] data
195
203
Future <void > updatePinnedMessageReactions (List <Reaction > reactions);
196
204
205
+ /// Updates the poll votes data with the new [pollVotes] data
206
+ Future <void > updatePollVotes (List <PollVote > pollVotes);
207
+
197
208
/// Deletes all the reactions by [messageIds]
198
209
Future <void > deleteReactionsByMessageId (List <String > messageIds);
199
210
200
211
/// Deletes all the pinned messages reactions by [messageIds]
201
212
Future <void > deletePinnedMessageReactionsByMessageId (List <String > messageIds);
202
213
214
+ /// Deletes all the poll votes by [pollIds]
215
+ Future <void > deletePollVotesByPollIds (List <String > pollIds);
216
+
203
217
/// Deletes all the members by channel [cids]
204
218
Future <void > deleteMembersByCids (List <String > cids);
205
219
@@ -245,50 +259,64 @@ abstract class ChatPersistenceClient {
245
259
final reactions = < Reaction > [];
246
260
final pinnedReactions = < Reaction > [];
247
261
262
+ final polls = < Poll > [];
263
+ final pollVotes = < PollVote > [];
264
+ final pollVotesToDelete = < String > [];
265
+
248
266
for (final state in channelStates) {
249
267
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 (
259
277
(it) => ! it.attachments.any (
260
278
(it) => it.uploadState != const UploadState .success (),
261
279
),
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);
292
320
}
293
321
294
322
// Removing old members and reactions data as they may have
@@ -297,12 +325,14 @@ abstract class ChatPersistenceClient {
297
325
deleteMembersByCids (membersToDelete),
298
326
deleteReactionsByMessageId (reactionsToDelete),
299
327
deletePinnedMessageReactionsByMessageId (pinnedReactionsToDelete),
328
+ deletePollVotesByPollIds (pollVotesToDelete),
300
329
]);
301
330
302
331
// Updating first as does not depend on any other table.
303
332
await Future .wait ([
304
333
updateUsers (users.toList (growable: false )),
305
334
updateChannels (channels.toList (growable: false )),
335
+ updatePolls (polls.toList (growable: false )),
306
336
]);
307
337
308
338
// All has a foreign key relation with channels table.
@@ -315,10 +345,9 @@ abstract class ChatPersistenceClient {
315
345
316
346
// Both has a foreign key relation with messages, pinnedMessages table.
317
347
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),
322
351
]);
323
352
}
324
353
@@ -330,4 +359,15 @@ abstract class ChatPersistenceClient {
330
359
if (latest != null ) ...latest.where ((r) => r.userId != null ),
331
360
];
332
361
}
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
+ }
333
373
}
0 commit comments