From e2c2ad90810d4d8a2b9d8d13c755a1c58cee7595 Mon Sep 17 00:00:00 2001 From: frank Date: Sun, 17 Nov 2024 20:19:24 +0800 Subject: [PATCH] perf_: return chats preview with optional filter --- protocol/messenger_chats.go | 11 ++++++++++- services/ext/api.go | 4 ++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/protocol/messenger_chats.go b/protocol/messenger_chats.go index d51f4f1f5..28e38bc87 100644 --- a/protocol/messenger_chats.go +++ b/protocol/messenger_chats.go @@ -46,10 +46,19 @@ func (m *Messenger) Chats() []*Chat { return chats } -func (m *Messenger) ChatsPreview() []*ChatPreview { +// ChatsPreview returns a list of chat previews. +// When onlyCommunityChats is nil, returns all chats +// When onlyCommunityChats is true, only returns community chats +// When onlyCommunityChats is false, returns all non-community chats +func (m *Messenger) ChatsPreview(onlyCommunityChats *bool) []*ChatPreview { var chats []*ChatPreview m.allChats.Range(func(chatID string, chat *Chat) (shouldContinue bool) { + // Skip if chat doesn't match the filter + isCommunityChat := chat.ChatType == ChatTypeCommunityChat + if onlyCommunityChats != nil && isCommunityChat != *onlyCommunityChats { + return true + } if chat.Active || chat.Muted { chatPreview := &ChatPreview{ ID: chat.ID, diff --git a/services/ext/api.go b/services/ext/api.go index 1ce6c06f7..3bd748272 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -291,8 +291,8 @@ func (api *PublicAPI) Chats(parent context.Context) []*protocol.Chat { return api.service.messenger.Chats() } -func (api *PublicAPI) ChatsPreview(parent context.Context) []*protocol.ChatPreview { - return api.service.messenger.ChatsPreview() +func (api *PublicAPI) ChatsPreview(parent context.Context, onlyCommunityChats *bool) []*protocol.ChatPreview { + return api.service.messenger.ChatsPreview(onlyCommunityChats) } func (api *PublicAPI) Chat(parent context.Context, chatID string) *protocol.Chat {