[StreamChatPersistence] Fix ownReactions userId bug

Signed-off-by: Sahil Kumar <[email protected]>
This commit is contained in:
Sahil Kumar
2021-01-27 15:51:55 +05:30
parent e74a0d40ff
commit 3b9af05214
9 changed files with 29 additions and 19 deletions
+1 -1
View File
@@ -456,7 +456,7 @@ class Client {
wsConnectionStatus.value = ConnectionStatus.connecting; wsConnectionStatus.value = ConnectionStatus.connecting;
if (persistenceEnabled) { if (persistenceEnabled) {
await chatPersistenceClient.connect('db_${state.user.id}'); await chatPersistenceClient.connect(state.user.id);
} }
_ws = WebSocket( _ws = WebSocket(
@@ -11,7 +11,7 @@ import 'package:stream_chat/src/models/user.dart';
/// ///
abstract class ChatPersistenceClient { abstract class ChatPersistenceClient {
/// Creates a new connection to the client /// Creates a new connection to the client
Future<void> connect(String name); Future<void> connect(String userId);
/// Closes the client connection /// Closes the client connection
/// If [flush] is true, the data will also be deleted /// If [flush] is true, the data will also be deleted
@@ -36,9 +36,9 @@ class MessageDao extends DatabaseAccessor<MoorChatDatabase>
final userEntity = rows.readTable(users); final userEntity = rows.readTable(users);
final msgEntity = rows.readTable(messages); final msgEntity = rows.readTable(messages);
final latestReactions = await _db.reactionDao.getReactions(msgEntity.id); final latestReactions = await _db.reactionDao.getReactions(msgEntity.id);
final ownReactions = await _db.reactionDao.getOwnReactions( final ownReactions = await _db.reactionDao.getReactionsByUserId(
msgEntity.id, msgEntity.id,
userEntity.id, _db.userId,
); );
Message quotedMessage; Message quotedMessage;
if (msgEntity.quotedMessageId != null) { if (msgEntity.quotedMessageId != null) {
@@ -29,7 +29,7 @@ class ReactionDao extends DatabaseAccessor<MoorChatDatabase>
} }
/// ///
Future<List<Reaction>> getOwnReactions( Future<List<Reaction>> getReactionsByUserId(
String messageId, String messageId,
String userId, String userId,
) async { ) async {
@@ -10,12 +10,12 @@ import 'shared/shared_db.dart';
part 'moor_chat_database.g.dart'; part 'moor_chat_database.g.dart';
LazyDatabase _openConnection( LazyDatabase _openConnection(
String dbName, { String userId, {
logStatements = false, logStatements = false,
}) { }) {
return LazyDatabase(() async { return LazyDatabase(() async {
return await SharedDB.constructDatabase( return await SharedDB.constructDatabase(
dbName, userId,
logStatements: logStatements, logStatements: logStatements,
); );
}); });
@@ -44,19 +44,25 @@ LazyDatabase _openConnection(
class MoorChatDatabase extends _$MoorChatDatabase { class MoorChatDatabase extends _$MoorChatDatabase {
/// Instantiate a new database instance /// Instantiate a new database instance
MoorChatDatabase( MoorChatDatabase(
String dbName, { this._userId, {
logStatements = false, logStatements = false,
}) : super(_openConnection( }) : super(_openConnection(
dbName, _userId,
logStatements: logStatements, logStatements: logStatements,
)); ));
/// Instantiate a new database instance /// Instantiate a new database instance
MoorChatDatabase.connect( MoorChatDatabase.connect(
this._userId,
this._isolate, this._isolate,
DatabaseConnection connection, DatabaseConnection connection,
) : super.connect(connection); ) : super.connect(connection);
final String _userId;
/// User id to which the database is connected
String get userId => _userId;
MoorIsolate _isolate; MoorIsolate _isolate;
// you should bump this number whenever you change or add a table definition. // you should bump this number whenever you change or add a table definition.
@@ -12,9 +12,10 @@ import '../moor_chat_database.dart';
class SharedDB { class SharedDB {
static Future<VmDatabase> constructDatabase( static Future<VmDatabase> constructDatabase(
String dbName, { String userId, {
bool logStatements = false, bool logStatements = false,
}) async { }) async {
final dbName = 'db_$userId';
if (Platform.isIOS || Platform.isAndroid) { if (Platform.isIOS || Platform.isAndroid) {
final dir = await getApplicationDocumentsDirectory(); final dir = await getApplicationDocumentsDirectory();
final path = join(dir.path, '$dbName.sqlite'); final path = join(dir.path, '$dbName.sqlite');
@@ -62,15 +63,16 @@ class SharedDB {
} }
static Future<MoorChatDatabase> constructOfflineStorage( static Future<MoorChatDatabase> constructOfflineStorage(
String dbName, { String userId, {
bool logStatements = false, bool logStatements = false,
}) async { }) async {
final dbName = 'db_$userId';
final isolate = await _createMoorIsolate( final isolate = await _createMoorIsolate(
dbName, dbName,
logStatements: logStatements, logStatements: logStatements,
); );
final connection = await isolate.connect(); final connection = await isolate.connect();
return MoorChatDatabase.connect(isolate, connection); return MoorChatDatabase.connect(userId, isolate, connection);
} }
} }
@@ -3,14 +3,14 @@
class SharedDB { class SharedDB {
static constructDatabase( static constructDatabase(
String dbName, { String userId, {
bool logStatements = false, bool logStatements = false,
}) { }) {
throw 'Unsupported Platform'; throw 'Unsupported Platform';
} }
static constructOfflineStorage( static constructOfflineStorage(
String dbName, { String userId, {
bool logStatements = false, bool logStatements = false,
}) { }) {
throw 'Unsupported Platform'; throw 'Unsupported Platform';
@@ -6,16 +6,18 @@ import '../moor_chat_database.dart';
class SharedDB { class SharedDB {
static constructDatabase( static constructDatabase(
String dbName, { String userId, {
bool logStatements = false, bool logStatements = false,
}) async { }) async {
final dbName = 'db_$userId';
return WebDatabase(dbName, logStatements: logStatements); return WebDatabase(dbName, logStatements: logStatements);
} }
static Future<MoorChatDatabase> constructOfflineStorage( static Future<MoorChatDatabase> constructOfflineStorage(
String dbName, { String userId, {
bool logStatements = false, bool logStatements = false,
}) async { }) async {
final dbName = 'db_$userId';
return MoorChatDatabase(dbName, logStatements: logStatements); return MoorChatDatabase(dbName, logStatements: logStatements);
} }
} }
@@ -42,7 +42,7 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
} }
@override @override
Future<void> connect(String name) async { Future<void> connect(String userId) async {
if (_db != null) { if (_db != null) {
throw Exception( throw Exception(
'An instance of StreamChatDatabase is already connected.\n' 'An instance of StreamChatDatabase is already connected.\n'
@@ -52,11 +52,11 @@ class StreamChatPersistenceClient extends ChatPersistenceClient {
switch (_connectionMode) { switch (_connectionMode) {
case ConnectionMode.regular: case ConnectionMode.regular:
_logger.info('Connecting on a regular isolate'); _logger.info('Connecting on a regular isolate');
_db = MoorChatDatabase(name); _db = MoorChatDatabase(userId);
return; return;
case ConnectionMode.background: case ConnectionMode.background:
_logger.info('Connecting on background isolate'); _logger.info('Connecting on background isolate');
_db = await SharedDB.constructOfflineStorage(name); _db = await SharedDB.constructOfflineStorage(userId);
return; return;
} }
} }