From 3b9af052143858a34ed7f7009c983ffe49093cdf Mon Sep 17 00:00:00 2001 From: Sahil Kumar Date: Wed, 27 Jan 2021 15:51:55 +0530 Subject: [PATCH] [StreamChatPersistence] Fix ownReactions userId bug Signed-off-by: Sahil Kumar --- packages/dart_client/lib/src/client.dart | 2 +- .../lib/src/db/chat_persistence_client.dart | 2 +- .../lib/src/dao/message_dao.dart | 4 ++-- .../lib/src/dao/reaction_dao.dart | 2 +- .../lib/src/db/moor_chat_database.dart | 14 ++++++++++---- .../lib/src/db/shared/native_db.dart | 8 +++++--- .../lib/src/db/shared/unsupported_db.dart | 4 ++-- .../lib/src/db/shared/web_db.dart | 6 ++++-- .../lib/src/stream_chat_persistence_client.dart | 6 +++--- 9 files changed, 29 insertions(+), 19 deletions(-) diff --git a/packages/dart_client/lib/src/client.dart b/packages/dart_client/lib/src/client.dart index 714acbd5..f2a6f0c0 100644 --- a/packages/dart_client/lib/src/client.dart +++ b/packages/dart_client/lib/src/client.dart @@ -456,7 +456,7 @@ class Client { wsConnectionStatus.value = ConnectionStatus.connecting; if (persistenceEnabled) { - await chatPersistenceClient.connect('db_${state.user.id}'); + await chatPersistenceClient.connect(state.user.id); } _ws = WebSocket( diff --git a/packages/dart_client/lib/src/db/chat_persistence_client.dart b/packages/dart_client/lib/src/db/chat_persistence_client.dart index c435df9e..c0cb68b2 100644 --- a/packages/dart_client/lib/src/db/chat_persistence_client.dart +++ b/packages/dart_client/lib/src/db/chat_persistence_client.dart @@ -11,7 +11,7 @@ import 'package:stream_chat/src/models/user.dart'; /// abstract class ChatPersistenceClient { /// Creates a new connection to the client - Future connect(String name); + Future connect(String userId); /// Closes the client connection /// If [flush] is true, the data will also be deleted diff --git a/packages/stream_chat_persistence/lib/src/dao/message_dao.dart b/packages/stream_chat_persistence/lib/src/dao/message_dao.dart index 320cb022..ee779845 100644 --- a/packages/stream_chat_persistence/lib/src/dao/message_dao.dart +++ b/packages/stream_chat_persistence/lib/src/dao/message_dao.dart @@ -36,9 +36,9 @@ class MessageDao extends DatabaseAccessor final userEntity = rows.readTable(users); final msgEntity = rows.readTable(messages); final latestReactions = await _db.reactionDao.getReactions(msgEntity.id); - final ownReactions = await _db.reactionDao.getOwnReactions( + final ownReactions = await _db.reactionDao.getReactionsByUserId( msgEntity.id, - userEntity.id, + _db.userId, ); Message quotedMessage; if (msgEntity.quotedMessageId != null) { diff --git a/packages/stream_chat_persistence/lib/src/dao/reaction_dao.dart b/packages/stream_chat_persistence/lib/src/dao/reaction_dao.dart index 8df23d44..dc6faefb 100644 --- a/packages/stream_chat_persistence/lib/src/dao/reaction_dao.dart +++ b/packages/stream_chat_persistence/lib/src/dao/reaction_dao.dart @@ -29,7 +29,7 @@ class ReactionDao extends DatabaseAccessor } /// - Future> getOwnReactions( + Future> getReactionsByUserId( String messageId, String userId, ) async { diff --git a/packages/stream_chat_persistence/lib/src/db/moor_chat_database.dart b/packages/stream_chat_persistence/lib/src/db/moor_chat_database.dart index e32c3e3c..691961a7 100644 --- a/packages/stream_chat_persistence/lib/src/db/moor_chat_database.dart +++ b/packages/stream_chat_persistence/lib/src/db/moor_chat_database.dart @@ -10,12 +10,12 @@ import 'shared/shared_db.dart'; part 'moor_chat_database.g.dart'; LazyDatabase _openConnection( - String dbName, { + String userId, { logStatements = false, }) { return LazyDatabase(() async { return await SharedDB.constructDatabase( - dbName, + userId, logStatements: logStatements, ); }); @@ -44,19 +44,25 @@ LazyDatabase _openConnection( class MoorChatDatabase extends _$MoorChatDatabase { /// Instantiate a new database instance MoorChatDatabase( - String dbName, { + this._userId, { logStatements = false, }) : super(_openConnection( - dbName, + _userId, logStatements: logStatements, )); /// Instantiate a new database instance MoorChatDatabase.connect( + this._userId, this._isolate, DatabaseConnection connection, ) : super.connect(connection); + final String _userId; + + /// User id to which the database is connected + String get userId => _userId; + MoorIsolate _isolate; // you should bump this number whenever you change or add a table definition. diff --git a/packages/stream_chat_persistence/lib/src/db/shared/native_db.dart b/packages/stream_chat_persistence/lib/src/db/shared/native_db.dart index 198b9010..90f19de6 100644 --- a/packages/stream_chat_persistence/lib/src/db/shared/native_db.dart +++ b/packages/stream_chat_persistence/lib/src/db/shared/native_db.dart @@ -12,9 +12,10 @@ import '../moor_chat_database.dart'; class SharedDB { static Future constructDatabase( - String dbName, { + String userId, { bool logStatements = false, }) async { + final dbName = 'db_$userId'; if (Platform.isIOS || Platform.isAndroid) { final dir = await getApplicationDocumentsDirectory(); final path = join(dir.path, '$dbName.sqlite'); @@ -62,15 +63,16 @@ class SharedDB { } static Future constructOfflineStorage( - String dbName, { + String userId, { bool logStatements = false, }) async { + final dbName = 'db_$userId'; final isolate = await _createMoorIsolate( dbName, logStatements: logStatements, ); final connection = await isolate.connect(); - return MoorChatDatabase.connect(isolate, connection); + return MoorChatDatabase.connect(userId, isolate, connection); } } diff --git a/packages/stream_chat_persistence/lib/src/db/shared/unsupported_db.dart b/packages/stream_chat_persistence/lib/src/db/shared/unsupported_db.dart index 647314eb..b014c833 100644 --- a/packages/stream_chat_persistence/lib/src/db/shared/unsupported_db.dart +++ b/packages/stream_chat_persistence/lib/src/db/shared/unsupported_db.dart @@ -3,14 +3,14 @@ class SharedDB { static constructDatabase( - String dbName, { + String userId, { bool logStatements = false, }) { throw 'Unsupported Platform'; } static constructOfflineStorage( - String dbName, { + String userId, { bool logStatements = false, }) { throw 'Unsupported Platform'; diff --git a/packages/stream_chat_persistence/lib/src/db/shared/web_db.dart b/packages/stream_chat_persistence/lib/src/db/shared/web_db.dart index 31b4f4b6..e30d08ea 100644 --- a/packages/stream_chat_persistence/lib/src/db/shared/web_db.dart +++ b/packages/stream_chat_persistence/lib/src/db/shared/web_db.dart @@ -6,16 +6,18 @@ import '../moor_chat_database.dart'; class SharedDB { static constructDatabase( - String dbName, { + String userId, { bool logStatements = false, }) async { + final dbName = 'db_$userId'; return WebDatabase(dbName, logStatements: logStatements); } static Future constructOfflineStorage( - String dbName, { + String userId, { bool logStatements = false, }) async { + final dbName = 'db_$userId'; return MoorChatDatabase(dbName, logStatements: logStatements); } } diff --git a/packages/stream_chat_persistence/lib/src/stream_chat_persistence_client.dart b/packages/stream_chat_persistence/lib/src/stream_chat_persistence_client.dart index 72536b93..cfcc5c68 100644 --- a/packages/stream_chat_persistence/lib/src/stream_chat_persistence_client.dart +++ b/packages/stream_chat_persistence/lib/src/stream_chat_persistence_client.dart @@ -42,7 +42,7 @@ class StreamChatPersistenceClient extends ChatPersistenceClient { } @override - Future connect(String name) async { + Future connect(String userId) async { if (_db != null) { throw Exception( 'An instance of StreamChatDatabase is already connected.\n' @@ -52,11 +52,11 @@ class StreamChatPersistenceClient extends ChatPersistenceClient { switch (_connectionMode) { case ConnectionMode.regular: _logger.info('Connecting on a regular isolate'); - _db = MoorChatDatabase(name); + _db = MoorChatDatabase(userId); return; case ConnectionMode.background: _logger.info('Connecting on background isolate'); - _db = await SharedDB.constructOfflineStorage(name); + _db = await SharedDB.constructOfflineStorage(userId); return; } }