diff --git a/data/lib/api/message/api_message_service.dart b/data/lib/api/message/api_message_service.dart index 985a41ec..b8703823 100644 --- a/data/lib/api/message/api_message_service.dart +++ b/data/lib/api/message/api_message_service.dart @@ -58,8 +58,7 @@ class ApiMessageService { .map((snapshot) => snapshot.docs.map((doc) => doc.data()).toList()); } - Stream> getLatestMessages(String threadId, {int limit = 20}) { - print('called in api message service file'); + Stream> streamLatestMessages(String threadId, {int limit = 20}) { return threadMessageRef(threadId) .orderBy("created_at", descending: true) .limit(limit) @@ -67,6 +66,14 @@ class ApiMessageService { .map((snapshot) => snapshot.docs.map((doc) => ApiThreadMessage.fromJson(doc.data() as Map)).toList()); } + Future> getLatestMessages(String threadId, {int limit = 20}) async { + final snapshot = await threadMessageRef(threadId) + .orderBy("created_at", descending: true) + .limit(limit) + .get(); + return snapshot.docs.map((doc) => ApiThreadMessage.fromJson(doc.data() as Map)).toList(); + } + Stream> getLatestMessagesMembers(ApiThread thread) async* { final List memberInfoList = []; for (String memberId in thread.member_ids) { @@ -130,12 +137,6 @@ class ApiMessageService { if (threads.isEmpty) return Stream.value([]); final futures = threads.map((thread) async { - final List messages = []; - - getLatestMessages(thread.id, limit: 20).listen((messageList) { - messages.addAll(messageList); - }); - final List memberInfoList = []; for (String memberId in thread.member_ids) { final user = await userService.getUser(memberId); @@ -147,7 +148,7 @@ class ApiMessageService { return ThreadInfo( thread: thread, - threadMessage: messages, + threadMessage: await getLatestMessages(thread.id, limit: 20), members: filteredMemberInfoList, ); }).toList(); diff --git a/data/lib/service/message_service.dart b/data/lib/service/message_service.dart index f2829ea1..5d5fa1ac 100644 --- a/data/lib/service/message_service.dart +++ b/data/lib/service/message_service.dart @@ -59,7 +59,7 @@ class MessageService { } Stream> getLatestMessages(String threadId, {int limit = 20}) { - return messageService.getLatestMessages(threadId, limit: limit); + return messageService.streamLatestMessages(threadId, limit: limit); } Stream> getLatestMessageMember(ApiThread thread) {