[Persistence] Delete members, read data before adding new one

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-01-29 19:17:07 +05:30
parent ab9266bfeb
commit e85a4326d6
6 changed files with 61 additions and 12 deletions
@@ -129,6 +129,12 @@ abstract class ChatPersistenceClient {
///
Future<void> updateReactions(List<Reaction> reactions);
///
Future<void> deleteReactionsByMessageId(List<String> messageIds);
///
Future<void> deleteMembersByCids(List<String> cids);
///
Future<void> updateChannelState(ChannelState channelState) {
return updateChannelStates([channelState]);
@@ -181,13 +187,26 @@ abstract class ChatPersistenceClient {
return updateMembers(cid, members.toList(growable: false));
}).toList(growable: false);
final deleteReactions = deleteReactionsByMessageId(channelStates
.expand((it) => it.messages)
.map((m) => m.id)
.toList(growable: false));
final deleteMembers = deleteMembersByCids(
channelStates.map((it) => it.channel.cid).toList(growable: false),
);
await Future.wait([
deleteReactions,
deleteMembers,
]);
await Future.wait([
...updateMessagesFuture,
...updateReadsFuture,
...updateMembersFuture,
updateUsers(users.toList(growable: false)),
updateChannels(channels.toList(growable: false)),
updateReactions(reactions.toList(growable: false)),
updateUsers(users.toList(growable: false)),
]);
}
}
@@ -16,7 +16,7 @@ class MemberDao extends DatabaseAccessor<MoorChatDatabase>
/// Creates a new member dao instance
MemberDao(MoorChatDatabase db) : super(db);
/// Get all members where [members.channelCid] matches [cid]
/// Get all members where [Members.channelCid] matches [cid]
Future<List<Member>> getMembersByCid(String cid) async {
return (select(members).join([
leftOuterJoin(users, members.userId.equalsExp(users.id)),
@@ -40,4 +40,14 @@ class MemberDao extends DatabaseAccessor<MoorChatDatabase>
),
);
}
/// Deletes all the members whose [Members.channelCid] is present in [cids]
Future<void> deleteMemberByCids(List<String> cids) async {
return batch((it) {
it.deleteWhere<Members, MemberEntity>(
members,
(m) => m.channelCid.isIn(cids),
);
});
}
}
@@ -16,7 +16,7 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
final MoorChatDatabase _db;
/// Removes all the messages by matching [messages.id] in [messageIds]
/// Removes all the messages by matching [Messages.id] in [messageIds]
///
/// This will automatically delete the following linked records
/// 1. Message Reactions
@@ -24,7 +24,7 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
return (delete(messages)..where((tbl) => tbl.id.isIn(messageIds))).go();
}
/// Removes all the messages by matching [messages.channelCid] in [cids]
/// Removes all the messages by matching [Messages.channelCid] in [cids]
///
/// This will automatically delete the following linked records
/// 1. Message Reactions
@@ -52,7 +52,7 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
);
}
/// Returns a single message by matching the [messages.id] with [id]
/// Returns a single message by matching the [Messages.id] with [id]
Future<Message> getMessageById(String id) async {
return await (select(messages).join([
leftOuterJoin(users, messages.userId.equalsExp(users.id)),
@@ -63,7 +63,7 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
}
/// Returns all the messages of a particular thread by matching
/// [messages.channelCid] with [cid]
/// [Messages.channelCid] with [cid]
Future<List<Message>> getThreadMessages(String cid) async {
return Future.wait(await (select(messages).join([
leftOuterJoin(users, messages.userId.equalsExp(users.id)),
@@ -76,7 +76,7 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
}
/// Returns all the messages of a particular thread by matching
/// [messages.parentId] with [parentId]
/// [Messages.parentId] with [parentId]
Future<List<Message>> getThreadMessagesByParentId(
String parentId, {
PaginationParams options,
@@ -97,7 +97,7 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
}
/// Returns all the messages of a channel by matching
/// [messages.channelCid] with [parentId]
/// [Messages.channelCid] with [parentId]
Future<List<Message>> getMessagesByCid(
String cid, {
PaginationParams messagePagination,
@@ -15,7 +15,7 @@ class ReactionDao extends DatabaseAccessor<MoorChatDatabase>
ReactionDao(MoorChatDatabase db) : super(db);
/// Returns all the reactions of a particular message by matching
/// [reactions.messageId] with [messageId]
/// [Reactions.messageId] with [messageId]
Future<List<Reaction>> getReactions(String messageId) {
return (select(reactions).join([
leftOuterJoin(users, reactions.userId.equalsExp(users.id)),
@@ -31,8 +31,8 @@ class ReactionDao extends DatabaseAccessor<MoorChatDatabase>
/// Returns all the reactions of a particular message
/// added by a particular user by matching
/// [reactions.messageId] with [messageId] and
/// [reactions.userId] with [userId]
/// [Reactions.messageId] with [messageId] and
/// [Reactions.userId] with [userId]
Future<List<Reaction>> getReactionsByUserId(
String messageId,
String userId,
@@ -51,4 +51,14 @@ class ReactionDao extends DatabaseAccessor<MoorChatDatabase>
);
});
}
/// Deletes all the reactions whose [Reactions.messageId] is present in [messageIds]
Future<void> deleteReactionsByMessageIds(List<String> messageIds) {
return batch((it) {
it.deleteWhere<Reactions, ReactionEntity>(
reactions,
(r) => r.messageId.isIn(messageIds),
);
});
}
}
@@ -13,7 +13,7 @@ class ReadDao extends DatabaseAccessor<MoorChatDatabase> with _$ReadDaoMixin {
/// Creates a new read dao instance
ReadDao(MoorChatDatabase db) : super(db);
/// Get all reads where [reads.channelCid] matches [cid]
/// Get all reads where [Reads.channelCid] matches [cid]
Future<List<Read>> getReadsByCid(String cid) async {
return (select(reads).join([
leftOuterJoin(users, reads.userId.equalsExp(users.id)),
@@ -193,6 +193,16 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
return _db.userDao.updateUsers(users);
}
@override
Future<void> deleteReactionsByMessageId(List<String> messageIds) {
return _db.reactionDao.deleteReactionsByMessageIds(messageIds);
}
@override
Future<void> deleteMembersByCids(List<String> cids) {
return _db.memberDao.deleteMemberByCids(cids);
}
@override
Future<void> disconnect({bool flush = false}) async {
if (_db != null) {