refactor(chat/api): re-add chats in the getChannelGroups api (#3439)

* refactor(chat/api): re-add chats in the getChannelGroups api

* feat(chat/api): add GetChannelGroupById api function
This commit is contained in:
Jonathan Rainville 2023-05-02 13:54:44 -04:00 committed by GitHub
parent 975883407f
commit e7fbc191f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 71 additions and 145 deletions

View File

@ -126,7 +126,7 @@ func unique(communities []*communities.Community) (result []*communities.Communi
return result return result
} }
func (api *API) GetChannelGroups(ctx context.Context) (map[string]ChannelGroup, error) { func (api *API) getChannelGroups(ctx context.Context, channelGroupID string) (map[string]ChannelGroup, error) {
joinedCommunities, err := api.s.messenger.JoinedCommunities() joinedCommunities, err := api.s.messenger.JoinedCommunities()
if err != nil { if err != nil {
return nil, err return nil, err
@ -145,12 +145,20 @@ func (api *API) GetChannelGroups(ctx context.Context) (map[string]ChannelGroup,
totalUnviewedMessageCount := 0 totalUnviewedMessageCount := 0
totalUnviewedMentionsCount := 0 totalUnviewedMentionsCount := 0
if channelGroupID == "" || channelGroupID == pubKey {
chats := make(map[string]*Chat)
for _, chat := range channels { for _, chat := range channels {
if !chat.Active { if !chat.IsActivePersonalChat() {
continue continue
} }
totalUnviewedMessageCount += int(chat.UnviewedMessagesCount) totalUnviewedMessageCount += int(chat.UnviewedMessagesCount)
totalUnviewedMentionsCount += int(chat.UnviewedMentionsCount) totalUnviewedMentionsCount += int(chat.UnviewedMentionsCount)
c, err := api.toAPIChat(chat, nil, pubKey, true)
if err != nil {
return nil, err
}
chats[chat.ID] = c
} }
result[pubKey] = ChannelGroup{ result[pubKey] = ChannelGroup{
@ -158,7 +166,7 @@ func (api *API) GetChannelGroups(ctx context.Context) (map[string]ChannelGroup,
Name: "", Name: "",
Images: make(map[string]images.IdentityImage), Images: make(map[string]images.IdentityImage),
Color: "", Color: "",
Chats: make(map[string]*Chat), Chats: chats,
Categories: make(map[string]communities.CommunityCategory), Categories: make(map[string]communities.CommunityCategory),
EnsName: "", // Not implemented yet in communities EnsName: "", // Not implemented yet in communities
Admin: true, Admin: true,
@ -173,8 +181,17 @@ func (api *API) GetChannelGroups(ctx context.Context) (map[string]ChannelGroup,
UnviewedMessagesCount: totalUnviewedMessageCount, UnviewedMessagesCount: totalUnviewedMessageCount,
UnviewedMentionsCount: totalUnviewedMentionsCount, UnviewedMentionsCount: totalUnviewedMentionsCount,
} }
}
if channelGroupID == pubKey {
// They asked for the personal channel group only, so we return now
return result, nil
}
for _, community := range unique(append(joinedCommunities, spectatedCommunities...)) { for _, community := range unique(append(joinedCommunities, spectatedCommunities...)) {
if channelGroupID != "" && channelGroupID != community.IDString() {
continue
}
totalUnviewedMessageCount = 0 totalUnviewedMessageCount = 0
totalUnviewedMentionsCount = 0 totalUnviewedMentionsCount = 0
@ -215,121 +232,14 @@ func (api *API) GetChannelGroups(ctx context.Context) (map[string]ChannelGroup,
chGrp.Images[t] = images.IdentityImage{Name: t, Payload: i.Payload} chGrp.Images[t] = images.IdentityImage{Name: t, Payload: i.Payload}
} }
result[community.IDString()] = chGrp
}
return result, nil
}
func (api *API) GetChatsByChannelGroupID(ctx context.Context, channelGroupID string) (*ChannelGroup, error) {
pubKey := types.EncodeHex(crypto.FromECDSAPub(api.s.messenger.IdentityPublicKey()))
if pubKey == channelGroupID {
result := &ChannelGroup{
Type: Personal,
Name: "",
Images: make(map[string]images.IdentityImage),
Color: "",
Chats: make(map[string]*Chat),
Categories: make(map[string]communities.CommunityCategory),
EnsName: "", // Not implemented yet in communities
Admin: true,
Verified: true,
Description: "",
IntroMessage: "",
OutroMessage: "",
Tags: []communities.CommunityTag{},
Permissions: &protobuf.CommunityPermissions{},
Muted: false,
CommunityTokensMetadata: []*protobuf.CommunityTokenMetadata{},
}
channels := api.s.messenger.Chats()
for _, chat := range channels {
if !chat.IsActivePersonalChat() {
continue
}
c, err := api.toAPIChat(chat, nil, pubKey, true)
if err != nil {
return nil, err
}
result.Chats[chat.ID] = c
}
return result, nil
}
joinedCommunities, err := api.s.messenger.JoinedCommunities()
if err != nil {
return nil, err
}
found := false
var community *communities.Community
for _, comm := range joinedCommunities {
if comm.IDString() == channelGroupID {
community = comm
found = true
break
}
}
if !found {
spectatedCommunities, err := api.s.messenger.SpectatedCommunities()
if err != nil {
return nil, err
}
for _, comm := range spectatedCommunities {
if comm.IDString() == channelGroupID {
community = comm
found = true
break
}
}
}
if !found {
return nil, ErrCommunityNotFound
}
result := &ChannelGroup{
Type: Community,
Name: community.Name(),
Color: community.Color(),
Images: make(map[string]images.IdentityImage),
Chats: make(map[string]*Chat),
Categories: make(map[string]communities.CommunityCategory),
Admin: community.IsAdmin(),
Verified: community.Verified(),
Description: community.DescriptionText(),
IntroMessage: community.IntroMessage(),
OutroMessage: community.OutroMessage(),
Tags: community.Tags(),
Permissions: community.Description().Permissions,
Members: community.Description().Members,
CanManageUsers: community.CanManageUsers(community.MemberIdentity()),
Muted: community.Muted(),
BanList: community.Description().BanList,
Encrypted: community.Encrypted(),
CommunityTokensMetadata: community.Description().CommunityTokensMetadata,
}
for t, i := range community.Images() {
result.Images[t] = images.IdentityImage{Name: t, Payload: i.Payload}
}
for _, cat := range community.Categories() { for _, cat := range community.Categories() {
result.Categories[cat.CategoryId] = communities.CommunityCategory{ chGrp.Categories[cat.CategoryId] = communities.CommunityCategory{
ID: cat.CategoryId, ID: cat.CategoryId,
Name: cat.Name, Name: cat.Name,
Position: int(cat.Position), Position: int(cat.Position),
} }
} }
channels := api.s.messenger.Chats()
for _, chat := range channels { for _, chat := range channels {
if chat.CommunityID == community.IDString() && chat.Active { if chat.CommunityID == community.IDString() && chat.Active {
c, err := api.toAPIChat(chat, community, pubKey, true) c, err := api.toAPIChat(chat, community, pubKey, true)
@ -337,13 +247,29 @@ func (api *API) GetChatsByChannelGroupID(ctx context.Context, channelGroupID str
return nil, err return nil, err
} }
result.Chats[c.ID] = c chGrp.Chats[c.ID] = c
}
}
result[community.IDString()] = chGrp
if channelGroupID == community.IDString() {
// We asked for this particular community, so we return now
return result, nil
} }
} }
return result, nil return result, nil
} }
func (api *API) GetChannelGroups(ctx context.Context) (map[string]ChannelGroup, error) {
return api.getChannelGroups(ctx, "")
}
func (api *API) GetChannelGroupByID(ctx context.Context, channelGroupID string) (map[string]ChannelGroup, error) {
return api.getChannelGroups(ctx, channelGroupID)
}
func (api *API) GetChat(ctx context.Context, communityID types.HexBytes, chatID string) (*Chat, error) { func (api *API) GetChat(ctx context.Context, communityID types.HexBytes, chatID string) (*Chat, error) {
pubKey := types.EncodeHex(crypto.FromECDSAPub(api.s.messenger.IdentityPublicKey())) pubKey := types.EncodeHex(crypto.FromECDSAPub(api.s.messenger.IdentityPublicKey()))
messengerChat, community, err := api.getChatAndCommunity(pubKey, communityID, chatID) messengerChat, community, err := api.getChatAndCommunity(pubKey, communityID, chatID)