chore_: add ChatPreviewFilterType

This commit is contained in:
frank 2024-11-21 16:50:12 +08:00
parent 502d4f3734
commit 42023d34b6
No known key found for this signature in database
GPG Key ID: B56FA1FC264D28FD
2 changed files with 19 additions and 5 deletions

View File

@ -12,6 +12,14 @@ import (
"github.com/status-im/status-go/protocol/transport" "github.com/status-im/status-go/protocol/transport"
) )
type ChatPreviewFilterType int
const (
ChatPreviewFilterTypeAll ChatPreviewFilterType = iota
ChatPreviewFilterTypeCommunity
ChatPreviewFilterTypeNonCommunity
)
func (m *Messenger) getOneToOneAndNextClock(contact *Contact) (*Chat, uint64, error) { func (m *Messenger) getOneToOneAndNextClock(contact *Contact) (*Chat, uint64, error) {
chat, ok := m.allChats.Load(contact.ID) chat, ok := m.allChats.Load(contact.ID)
if !ok { if !ok {
@ -50,13 +58,19 @@ func (m *Messenger) Chats() []*Chat {
// When onlyCommunityChats is nil, returns all chats // When onlyCommunityChats is nil, returns all chats
// When onlyCommunityChats is true, only returns community chats // When onlyCommunityChats is true, only returns community chats
// When onlyCommunityChats is false, returns all non-community chats // When onlyCommunityChats is false, returns all non-community chats
func (m *Messenger) ChatsPreview(onlyCommunityChats *bool) []*ChatPreview { func (m *Messenger) ChatsPreview(filterPointer *ChatPreviewFilterType) []*ChatPreview {
var chats []*ChatPreview var chats []*ChatPreview
filter := ChatPreviewFilterTypeAll
if filterPointer != nil {
filter = *filterPointer
}
m.allChats.Range(func(chatID string, chat *Chat) (shouldContinue bool) { m.allChats.Range(func(chatID string, chat *Chat) (shouldContinue bool) {
// Skip if chat doesn't match the filter // Skip if chat doesn't match the filter
isCommunityChat := chat.ChatType == ChatTypeCommunityChat isCommunityChat := chat.ChatType == ChatTypeCommunityChat
if onlyCommunityChats != nil && isCommunityChat != *onlyCommunityChats { if filter == ChatPreviewFilterTypeCommunity && !isCommunityChat {
return true
}
if filter == ChatPreviewFilterTypeNonCommunity && isCommunityChat {
return true return true
} }
if chat.Active || chat.Muted { if chat.Active || chat.Muted {

View File

@ -291,8 +291,8 @@ func (api *PublicAPI) Chats(parent context.Context) []*protocol.Chat {
return api.service.messenger.Chats() return api.service.messenger.Chats()
} }
func (api *PublicAPI) ChatsPreview(parent context.Context, onlyCommunityChats *bool) []*protocol.ChatPreview { func (api *PublicAPI) ChatsPreview(parent context.Context, filterType *protocol.ChatPreviewFilterType) []*protocol.ChatPreview {
return api.service.messenger.ChatsPreview(onlyCommunityChats) return api.service.messenger.ChatsPreview(filterType)
} }
func (api *PublicAPI) Chat(parent context.Context, chatID string) *protocol.Chat { func (api *PublicAPI) Chat(parent context.Context, chatID string) *protocol.Chat {