mirror of
https://github.com/Iconica-Development/flutter_chat.git
synced 2025-05-19 10:53:51 +02:00
fix: add proper implementation for getAllUsersForChat in FirebaseUserRepository
This commit is contained in:
parent
f7f15ef750
commit
74823e4c1e
1 changed files with 19 additions and 15 deletions
|
@ -7,10 +7,13 @@ class FirebaseUserRepository implements UserRepositoryInterface {
|
||||||
FirebaseUserRepository({
|
FirebaseUserRepository({
|
||||||
FirebaseFirestore? firestore,
|
FirebaseFirestore? firestore,
|
||||||
String userCollection = "users",
|
String userCollection = "users",
|
||||||
|
String chatCollection = "chats",
|
||||||
}) : _userCollection = userCollection,
|
}) : _userCollection = userCollection,
|
||||||
|
_chatCollection = chatCollection,
|
||||||
_firestore = firestore ?? FirebaseFirestore.instance;
|
_firestore = firestore ?? FirebaseFirestore.instance;
|
||||||
final FirebaseFirestore _firestore;
|
final FirebaseFirestore _firestore;
|
||||||
final String _userCollection;
|
final String _userCollection;
|
||||||
|
final String _chatCollection;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Stream<List<UserModel>> getAllUsers() =>
|
Stream<List<UserModel>> getAllUsers() =>
|
||||||
|
@ -35,19 +38,20 @@ class FirebaseUserRepository implements UserRepositoryInterface {
|
||||||
);
|
);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Stream<List<UserModel>> getAllUsersForChat({required String chatId}) =>
|
Stream<List<UserModel>> getAllUsersForChat({required String chatId}) {
|
||||||
_firestore
|
var chatDocStream =
|
||||||
.collection(_userCollection)
|
_firestore.collection(_chatCollection).doc(chatId).snapshots();
|
||||||
.where("chats", arrayContains: chatId)
|
|
||||||
.snapshots()
|
return chatDocStream.asyncMap((snapshot) async {
|
||||||
.map(
|
if (!snapshot.exists) return [];
|
||||||
(querySnapshot) => querySnapshot.docs
|
|
||||||
.map(
|
var chatModel = ChatModel.fromMap(snapshot.id, snapshot.data()!);
|
||||||
(doc) => UserModel.fromMap(
|
var userIds = chatModel.users;
|
||||||
doc.id,
|
|
||||||
doc.data(),
|
var userStreams =
|
||||||
),
|
userIds.map((userId) => getUser(userId: userId)).toList();
|
||||||
)
|
|
||||||
.toList(),
|
return Future.wait(userStreams.map((s) => s.first));
|
||||||
);
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue