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:
parent
975883407f
commit
e7fbc191f4
|
@ -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,36 +145,53 @@ func (api *API) GetChannelGroups(ctx context.Context) (map[string]ChannelGroup,
|
||||||
totalUnviewedMessageCount := 0
|
totalUnviewedMessageCount := 0
|
||||||
totalUnviewedMentionsCount := 0
|
totalUnviewedMentionsCount := 0
|
||||||
|
|
||||||
for _, chat := range channels {
|
if channelGroupID == "" || channelGroupID == pubKey {
|
||||||
if !chat.Active {
|
chats := make(map[string]*Chat)
|
||||||
continue
|
for _, chat := range channels {
|
||||||
|
if !chat.IsActivePersonalChat() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
totalUnviewedMessageCount += int(chat.UnviewedMessagesCount)
|
||||||
|
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{
|
||||||
|
Type: Personal,
|
||||||
|
Name: "",
|
||||||
|
Images: make(map[string]images.IdentityImage),
|
||||||
|
Color: "",
|
||||||
|
Chats: chats,
|
||||||
|
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{},
|
||||||
|
UnviewedMessagesCount: totalUnviewedMessageCount,
|
||||||
|
UnviewedMentionsCount: totalUnviewedMentionsCount,
|
||||||
}
|
}
|
||||||
totalUnviewedMessageCount += int(chat.UnviewedMessagesCount)
|
|
||||||
totalUnviewedMentionsCount += int(chat.UnviewedMentionsCount)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result[pubKey] = ChannelGroup{
|
if channelGroupID == pubKey {
|
||||||
Type: Personal,
|
// They asked for the personal channel group only, so we return now
|
||||||
Name: "",
|
return result, nil
|
||||||
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{},
|
|
||||||
UnviewedMessagesCount: totalUnviewedMessageCount,
|
|
||||||
UnviewedMentionsCount: totalUnviewedMentionsCount,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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,133 +232,42 @@ 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}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, cat := range community.Categories() {
|
||||||
|
chGrp.Categories[cat.CategoryId] = communities.CommunityCategory{
|
||||||
|
ID: cat.CategoryId,
|
||||||
|
Name: cat.Name,
|
||||||
|
Position: int(cat.Position),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, chat := range channels {
|
||||||
|
if chat.CommunityID == community.IDString() && chat.Active {
|
||||||
|
c, err := api.toAPIChat(chat, community, pubKey, true)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
chGrp.Chats[c.ID] = c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
result[community.IDString()] = chGrp
|
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) GetChatsByChannelGroupID(ctx context.Context, channelGroupID string) (*ChannelGroup, error) {
|
func (api *API) GetChannelGroups(ctx context.Context) (map[string]ChannelGroup, error) {
|
||||||
pubKey := types.EncodeHex(crypto.FromECDSAPub(api.s.messenger.IdentityPublicKey()))
|
return api.getChannelGroups(ctx, "")
|
||||||
|
}
|
||||||
|
|
||||||
if pubKey == channelGroupID {
|
func (api *API) GetChannelGroupByID(ctx context.Context, channelGroupID string) (map[string]ChannelGroup, error) {
|
||||||
result := &ChannelGroup{
|
return api.getChannelGroups(ctx, channelGroupID)
|
||||||
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() {
|
|
||||||
result.Categories[cat.CategoryId] = communities.CommunityCategory{
|
|
||||||
ID: cat.CategoryId,
|
|
||||||
Name: cat.Name,
|
|
||||||
Position: int(cat.Position),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
channels := api.s.messenger.Chats()
|
|
||||||
for _, chat := range channels {
|
|
||||||
if chat.CommunityID == community.IDString() && chat.Active {
|
|
||||||
c, err := api.toAPIChat(chat, community, pubKey, true)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
result.Chats[c.ID] = c
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
|
|
Loading…
Reference in New Issue