fix: Review changes

This commit is contained in:
Deven Joshi
2021-04-16 14:11:46 +05:30
parent be9c841c8a
commit 72a0ca3110
3 changed files with 39 additions and 31 deletions
@@ -985,7 +985,7 @@ class Channel {
if (preferOffline && cid != null) { if (preferOffline && cid != null) {
final updatedState = final updatedState =
(await _client.chatPersistenceClient?.getChannelStateByCid( (await _client.chatPersistenceClient?.getChannelStateByCid(
cid, cid!,
messagePagination: messagesPagination, messagePagination: messagesPagination,
))!; ))!;
if (updatedState.messages.isNotEmpty) { if (updatedState.messages.isNotEmpty) {
@@ -1014,7 +1014,7 @@ class Channel {
rethrow; rethrow;
} }
return _client.chatPersistenceClient!.getChannelStateByCid( return _client.chatPersistenceClient!.getChannelStateByCid(
cid, cid!,
messagePagination: messagesPagination, messagePagination: messagesPagination,
); );
} }
@@ -1122,7 +1122,10 @@ class Channel {
if (clearHistory == true) { if (clearHistory == true) {
state!.truncate(); state!.truncate();
await _client.chatPersistenceClient?.deleteMessageByCid(_cid); final cid = _cid;
if (cid != null) {
await _client.chatPersistenceClient?.deleteMessageByCid(cid);
}
} }
return _client.decode(response.data, EmptyResponse.fromJson); return _client.decode(response.data, EmptyResponse.fromJson);
@@ -1252,12 +1255,12 @@ class ChannelClientState {
_startCleaningPinnedMessages(); _startCleaningPinnedMessages();
_channel._client.chatPersistenceClient _channel._client.chatPersistenceClient
?.getChannelThreads(_channel.cid) ?.getChannelThreads(_channel.cid!)
.then((threads) { .then((threads) {
_threads = threads; _threads = threads;
}).then((_) { }).then((_) {
_channel._client.chatPersistenceClient _channel._client.chatPersistenceClient
?.getChannelStateByCid(_channel.cid) ?.getChannelStateByCid(_channel.cid!)
.then((state) { .then((state) {
// Replacing the persistence state members with the latest // Replacing the persistence state members with the latest
// `channelState.members` as they may have changes over the time. // `channelState.members` as they may have changes over the time.
@@ -1734,7 +1737,7 @@ class ChannelClientState {
set _threads(Map<String?, List<Message>?> v) { set _threads(Map<String?, List<Message>?> v) {
_channel._client.chatPersistenceClient?.updateMessages( _channel._client.chatPersistenceClient?.updateMessages(
_channel.cid, _channel.cid!,
v.values.expand((v) => v!).toList(), v.values.expand((v) => v!).toList(),
); );
_threadsController.add(v); _threadsController.add(v);
+6 -2
View File
@@ -764,7 +764,7 @@ class StreamChatClient {
final updateData = _mapChannelStateToChannel(channels); final updateData = _mapChannelStateToChannel(channels);
await _chatPersistenceClient?.updateChannelQueries( await _chatPersistenceClient?.updateChannelQueries(
filter, filter ?? {},
channels.map((c) => c.channel!.cid).toList(), channels.map((c) => c.channel!.cid).toList(),
clearQueryCache: paginationParams.offset == 0, clearQueryCache: paginationParams.offset == 0,
); );
@@ -1434,7 +1434,11 @@ class ClientState {
void _listenChannelHidden() { void _listenChannelHidden() {
_subscriptions.add(_client.on(EventType.channelHidden).listen((event) { _subscriptions.add(_client.on(EventType.channelHidden).listen((event) {
_client.chatPersistenceClient?.deleteChannels([event.cid]); final cid = event.cid;
if (cid != null) {
_client.chatPersistenceClient?.deleteChannels([cid]);
}
if (channels != null) { if (channels != null) {
channels = channels?..removeWhere((cid, ch) => cid == event.cid); channels = channels?..removeWhere((cid, ch) => cid == event.cid);
} }
@@ -39,32 +39,32 @@ abstract class ChatPersistenceClient {
Future<List<String>> getChannelCids(); Future<List<String>> getChannelCids();
/// Get stored [ChannelModel]s by providing channel [cid] /// Get stored [ChannelModel]s by providing channel [cid]
Future<ChannelModel> getChannelByCid(String? cid); Future<ChannelModel> getChannelByCid(String cid);
/// Get stored channel [Member]s by providing channel [cid] /// Get stored channel [Member]s by providing channel [cid]
Future<List<Member>> getMembersByCid(String? cid); Future<List<Member>> getMembersByCid(String cid);
/// Get stored channel [Read]s by providing channel [cid] /// Get stored channel [Read]s by providing channel [cid]
Future<List<Read>> getReadsByCid(String? cid); Future<List<Read>> getReadsByCid(String cid);
/// Get stored [Message]s by providing channel [cid] /// Get stored [Message]s by providing channel [cid]
/// ///
/// Optionally, you can [messagePagination] /// Optionally, you can [messagePagination]
/// for filtering out messages /// for filtering out messages
Future<List<Message>> getMessagesByCid( Future<List<Message>> getMessagesByCid(
String? cid, { String cid, {
PaginationParams? messagePagination, PaginationParams? messagePagination,
}); });
/// Get stored pinned [Message]s by providing channel [cid] /// Get stored pinned [Message]s by providing channel [cid]
Future<List<Message>> getPinnedMessagesByCid( Future<List<Message>> getPinnedMessagesByCid(
String? cid, { String cid, {
PaginationParams? messagePagination, PaginationParams? messagePagination,
}); });
/// Get [ChannelState] data by providing channel [cid] /// Get [ChannelState] data by providing channel [cid]
Future<ChannelState> getChannelStateByCid( Future<ChannelState> getChannelStateByCid(
String? cid, { String cid, {
PaginationParams? messagePagination, PaginationParams? messagePagination,
PaginationParams? pinnedMessagePagination, PaginationParams? pinnedMessagePagination,
}) async { }) async {
@@ -99,8 +99,8 @@ abstract class ChatPersistenceClient {
/// If [clearQueryCache] is true before the insert /// If [clearQueryCache] is true before the insert
/// the list of matching rows will be deleted /// the list of matching rows will be deleted
Future<void> updateChannelQueries( Future<void> updateChannelQueries(
Map<String, dynamic>? filter, Map<String, dynamic> filter,
List<String?> cids, { List<String> cids, {
bool clearQueryCache = false, bool clearQueryCache = false,
}); });
@@ -119,46 +119,46 @@ abstract class ChatPersistenceClient {
Future<void> deletePinnedMessageByIds(List<String> messageIds); Future<void> deletePinnedMessageByIds(List<String> messageIds);
/// Remove a message by channel [cid] /// Remove a message by channel [cid]
Future<void> deleteMessageByCid(String? cid) => deleteMessageByCids([cid]); Future<void> deleteMessageByCid(String cid) => deleteMessageByCids([cid]);
/// Remove a pinned message by channel [cid] /// Remove a pinned message by channel [cid]
Future<void> deletePinnedMessageByCid(String cid) async => Future<void> deletePinnedMessageByCid(String cid) async =>
deletePinnedMessageByCids([cid]); deletePinnedMessageByCids([cid]);
/// Remove a message by message [cids] /// Remove a message by message [cids]
Future<void> deleteMessageByCids(List<String?> cids); Future<void> deleteMessageByCids(List<String> cids);
/// Remove a pinned message by message [cids] /// Remove a pinned message by message [cids]
Future<void> deletePinnedMessageByCids(List<String> cids); Future<void> deletePinnedMessageByCids(List<String> cids);
/// Remove a channel by [cid] /// Remove a channel by [cid]
Future<void> deleteChannels(List<String?> cids); Future<void> deleteChannels(List<String> cids);
/// Updates the message data of a particular channel [cid] with /// Updates the message data of a particular channel [cid] with
/// the new [messages] data /// the new [messages] data
Future<void> updateMessages(String? cid, List<Message> messages); Future<void> updateMessages(String cid, List<Message> messages);
/// Updates the pinned message data of a particular channel [cid] with /// Updates the pinned message data of a particular channel [cid] with
/// the new [messages] data /// the new [messages] data
Future<void> updatePinnedMessages(String? cid, List<Message> messages); Future<void> updatePinnedMessages(String cid, List<Message> messages);
/// Returns all the threads by parent message of a particular channel by /// Returns all the threads by parent message of a particular channel by
/// providing channel [cid] /// providing channel [cid]
Future<Map<String, List<Message>>> getChannelThreads(String? cid); Future<Map<String, List<Message>>> getChannelThreads(String cid);
/// Updates all the channels using the new [channels] data. /// Updates all the channels using the new [channels] data.
Future<void> updateChannels(List<ChannelModel?> channels); Future<void> updateChannels(List<ChannelModel> channels);
/// Updates all the members of a particular channle [cid] /// Updates all the members of a particular channle [cid]
/// with the new [members] data /// with the new [members] data
Future<void> updateMembers(String? cid, List<Member?> members); Future<void> updateMembers(String cid, List<Member> members);
/// Updates the read data of a particular channel [cid] with /// Updates the read data of a particular channel [cid] with
/// the new [reads] data /// the new [reads] data
Future<void> updateReads(String? cid, List<Read> reads); Future<void> updateReads(String cid, List<Read> reads);
/// Updates the users data with the new [users] data /// Updates the users data with the new [users] data
Future<void> updateUsers(List<User?> users); Future<void> updateUsers(List<User> users);
/// Updates the reactions data with the new [reactions] data /// Updates the reactions data with the new [reactions] data
Future<void> updateReactions(List<Reaction> reactions); Future<void> updateReactions(List<Reaction> reactions);
@@ -167,7 +167,7 @@ abstract class ChatPersistenceClient {
Future<void> deleteReactionsByMessageId(List<String> messageIds); Future<void> deleteReactionsByMessageId(List<String> messageIds);
/// Deletes all the members by channel [cids] /// Deletes all the members by channel [cids]
Future<void> deleteMembersByCids(List<String?> cids); Future<void> deleteMembersByCids(List<String> cids);
/// Update the channel state data using [channelState] /// Update the channel state data using [channelState]
Future<void> updateChannelState(ChannelState channelState) => Future<void> updateChannelState(ChannelState channelState) =>
@@ -189,8 +189,9 @@ abstract class ChatPersistenceClient {
deleteMembers, deleteMembers,
]); ]);
final channels = final channels = channelStates
channelStates.map((it) => it.channel).where((it) => it != null); .map((it) => it.channel)
.where((it) => it != null) as Iterable<ChannelModel>;
final reactions = channelStates.expand((it) => it.messages).expand((it) => [ final reactions = channelStates.expand((it) => it.messages).expand((it) => [
if (it.ownReactions != null) if (it.ownReactions != null)
@@ -199,7 +200,7 @@ abstract class ChatPersistenceClient {
...it.latestReactions!.where((r) => r.userId != null) ...it.latestReactions!.where((r) => r.userId != null)
]); ]);
final users = channelStates var users = channelStates
.map((cs) => [ .map((cs) => [
cs.channel?.createdBy, cs.channel?.createdBy,
...cs.messages ...cs.messages
@@ -215,7 +216,7 @@ abstract class ChatPersistenceClient {
...cs.members.map((m) => m.user), ...cs.members.map((m) => m.user),
]) ])
.expand((it) => it) .expand((it) => it)
.where((it) => it != null); .where((it) => it != null) as Iterable<User>;
final updateMessagesFuture = channelStates.map((it) { final updateMessagesFuture = channelStates.map((it) {
final cid = it.channel!.cid; final cid = it.channel!.cid;