getChatById method
- String chatId
Retrieves a chat by the given ID.
chatId
: The ID of the chat.
Implementation
@override
Future<ChatModel> getChatById(String chatId) async {
var currentUser = await _userService.getCurrentUser();
var chatCollection = await _db
.collection(_options.usersCollectionName)
.doc(currentUser?.id)
.collection(_options.userChatsCollectionName)
.doc(chatId)
.get();
if (chatCollection.exists && chatCollection.data()?['users'] != null) {
// ignore: avoid_dynamic_calls
var otherUser = chatCollection.data()?['users'].firstWhere(
(element) => element != currentUser?.id,
);
var user = await _userService.getUser(otherUser);
return PersonalChatModel(
id: chatId,
user: user!,
canBeDeleted: chatCollection.data()?['can_be_deleted'] ?? true,
);
} else {
var groupChatCollection = await _db
.collection(_options.chatsMetaDataCollectionName)
.doc(chatId)
.withConverter(
fromFirestore: (snapshot, _) =>
FirebaseChatDocument.fromJson(snapshot.data()!, snapshot.id),
toFirestore: (chat, _) => chat.toJson(),
)
.get();
var chat = groupChatCollection.data();
var users = <ChatUserModel>[];
for (var userId in chat?.users ?? []) {
var user = await _userService.getUser(userId);
if (user != null) {
users.add(user);
}
}
return GroupChatModel(
id: chat?.id ?? chatId,
title: chat?.title ?? '',
imageUrl: chat?.imageUrl ?? '',
users: users,
canBeDeleted: chat?.canBeDeleted ?? true,
);
}
}