2022-02-10 15:15:27 +00:00
|
|
|
package chat
|
2022-02-09 21:58:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2022-02-10 22:55:03 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2022-02-09 21:58:33 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/images"
|
|
|
|
"github.com/status-im/status-go/protocol"
|
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/communities"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2022-02-10 22:55:03 +00:00
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
2022-02-09 21:58:33 +00:00
|
|
|
v1protocol "github.com/status-im/status-go/protocol/v1"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-02-11 16:29:57 +00:00
|
|
|
ErrChatNotFound = errors.New("can't find chat")
|
2023-01-23 11:21:11 +00:00
|
|
|
ErrCommunityNotFound = errors.New("can't find community")
|
2022-02-11 16:29:57 +00:00
|
|
|
ErrCommunitiesNotSupported = errors.New("communities are not supported")
|
2022-07-12 07:48:58 +00:00
|
|
|
ErrChatTypeNotSupported = errors.New("chat type not supported")
|
2022-02-09 21:58:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ChannelGroupType string
|
|
|
|
|
|
|
|
const Personal ChannelGroupType = "personal"
|
|
|
|
const Community ChannelGroupType = "community"
|
|
|
|
|
2022-02-10 15:15:27 +00:00
|
|
|
type PinnedMessages struct {
|
2022-02-09 21:58:33 +00:00
|
|
|
Cursor string
|
|
|
|
PinnedMessages []*common.PinnedMessage
|
|
|
|
}
|
|
|
|
|
2022-02-10 15:15:27 +00:00
|
|
|
type Member struct {
|
2023-06-14 14:15:46 +00:00
|
|
|
// Community Role
|
|
|
|
Role protobuf.CommunityMember_Roles `json:"role,omitempty"`
|
2022-02-09 21:58:33 +00:00
|
|
|
// Joined indicates if the member has joined the group chat
|
|
|
|
Joined bool `json:"joined"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Chat struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
Emoji string `json:"emoji"`
|
|
|
|
Active bool `json:"active"`
|
|
|
|
ChatType protocol.ChatType `json:"chatType"`
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
LastClockValue uint64 `json:"lastClockValue"`
|
|
|
|
DeletedAtClockValue uint64 `json:"deletedAtClockValue"`
|
|
|
|
ReadMessagesAtClockValue uint64 `json:"readMessagesAtClockValue"`
|
|
|
|
UnviewedMessagesCount uint `json:"unviewedMessagesCount"`
|
|
|
|
UnviewedMentionsCount uint `json:"unviewedMentionsCount"`
|
|
|
|
LastMessage *common.Message `json:"lastMessage"`
|
2022-02-10 15:15:27 +00:00
|
|
|
Members map[string]Member `json:"members,omitempty"`
|
2022-02-09 21:58:33 +00:00
|
|
|
MembershipUpdates []v1protocol.MembershipUpdateEvent `json:"membershipUpdateEvents"`
|
|
|
|
Alias string `json:"alias,omitempty"`
|
|
|
|
Identicon string `json:"identicon"`
|
|
|
|
Muted bool `json:"muted"`
|
|
|
|
InvitationAdmin string `json:"invitationAdmin,omitempty"`
|
|
|
|
ReceivedInvitationAdmin string `json:"receivedInvitationAdmin,omitempty"`
|
|
|
|
Profile string `json:"profile,omitempty"`
|
2022-02-10 22:55:03 +00:00
|
|
|
CommunityID string `json:"communityId"`
|
2022-02-09 21:58:33 +00:00
|
|
|
CategoryID string `json:"categoryId"`
|
|
|
|
Position int32 `json:"position,omitempty"`
|
|
|
|
Permissions *protobuf.CommunityPermissions `json:"permissions,omitempty"`
|
|
|
|
Joined int64 `json:"joined,omitempty"`
|
|
|
|
SyncedTo uint32 `json:"syncedTo,omitempty"`
|
|
|
|
SyncedFrom uint32 `json:"syncedFrom,omitempty"`
|
2022-09-02 08:36:07 +00:00
|
|
|
FirstMessageTimestamp uint32 `json:"firstMessageTimestamp,omitempty"`
|
2022-02-09 21:58:33 +00:00
|
|
|
Highlight bool `json:"highlight,omitempty"`
|
2022-02-10 15:15:27 +00:00
|
|
|
PinnedMessages *PinnedMessages `json:"pinnedMessages,omitempty"`
|
2022-02-09 21:58:33 +00:00
|
|
|
CanPost bool `json:"canPost"`
|
2022-08-08 15:22:22 +00:00
|
|
|
Base64Image string `json:"image,omitempty"`
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ChannelGroup struct {
|
2023-02-20 11:57:33 +00:00
|
|
|
Type ChannelGroupType `json:"channelGroupType"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Images map[string]images.IdentityImage `json:"images"`
|
|
|
|
Color string `json:"color"`
|
|
|
|
Chats map[string]*Chat `json:"chats"`
|
|
|
|
Categories map[string]communities.CommunityCategory `json:"categories"`
|
|
|
|
EnsName string `json:"ensName"`
|
2023-06-14 14:15:46 +00:00
|
|
|
MemberRole protobuf.CommunityMember_Roles `json:"memberRole"`
|
2023-02-20 11:57:33 +00:00
|
|
|
Verified bool `json:"verified"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
IntroMessage string `json:"introMessage"`
|
|
|
|
OutroMessage string `json:"outroMessage"`
|
|
|
|
Tags []communities.CommunityTag `json:"tags"`
|
|
|
|
Permissions *protobuf.CommunityPermissions `json:"permissions"`
|
|
|
|
Members map[string]*protobuf.CommunityMember `json:"members"`
|
|
|
|
CanManageUsers bool `json:"canManageUsers"`
|
|
|
|
Muted bool `json:"muted"`
|
|
|
|
BanList []string `json:"banList"`
|
|
|
|
Encrypted bool `json:"encrypted"`
|
|
|
|
CommunityTokensMetadata []*protobuf.CommunityTokenMetadata `json:"communityTokensMetadata"`
|
2023-03-24 15:19:31 +00:00
|
|
|
UnviewedMessagesCount int `json:"unviewedMessagesCount"`
|
|
|
|
UnviewedMentionsCount int `json:"unviewedMentionsCount"`
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 15:15:27 +00:00
|
|
|
func NewAPI(service *Service) *API {
|
|
|
|
return &API{
|
|
|
|
s: service,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type API struct {
|
|
|
|
s *Service
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:57:39 +00:00
|
|
|
func unique(communities []*communities.Community) (result []*communities.Community) {
|
|
|
|
inResult := make(map[string]bool)
|
|
|
|
for _, community := range communities {
|
|
|
|
if _, ok := inResult[community.IDString()]; !ok {
|
|
|
|
inResult[community.IDString()] = true
|
|
|
|
result = append(result, community)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-05-02 17:54:44 +00:00
|
|
|
func (api *API) getChannelGroups(ctx context.Context, channelGroupID string) (map[string]ChannelGroup, error) {
|
2022-02-10 15:15:27 +00:00
|
|
|
joinedCommunities, err := api.s.messenger.JoinedCommunities()
|
2022-02-09 21:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-20 19:57:39 +00:00
|
|
|
spectatedCommunities, err := api.s.messenger.SpectatedCommunities()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-02-09 21:58:33 +00:00
|
|
|
|
2023-03-23 19:12:22 +00:00
|
|
|
pubKey := types.EncodeHex(crypto.FromECDSAPub(api.s.messenger.IdentityPublicKey()))
|
2023-03-14 20:12:47 +00:00
|
|
|
|
2023-03-23 19:12:22 +00:00
|
|
|
result := make(map[string]ChannelGroup)
|
2023-03-14 20:12:47 +00:00
|
|
|
|
2023-03-24 15:19:31 +00:00
|
|
|
// Get chats from cache to get unviewed messages counts
|
|
|
|
channels := api.s.messenger.Chats()
|
|
|
|
totalUnviewedMessageCount := 0
|
|
|
|
totalUnviewedMentionsCount := 0
|
|
|
|
|
2023-05-02 17:54:44 +00:00
|
|
|
if channelGroupID == "" || channelGroupID == pubKey {
|
|
|
|
chats := make(map[string]*Chat)
|
|
|
|
for _, chat := range channels {
|
|
|
|
if !chat.IsActivePersonalChat() {
|
|
|
|
continue
|
|
|
|
}
|
2023-06-16 18:10:04 +00:00
|
|
|
if !chat.Muted || chat.UnviewedMentionsCount > 0 {
|
|
|
|
totalUnviewedMessageCount += int(chat.UnviewedMessagesCount)
|
|
|
|
}
|
2023-05-02 17:54:44 +00:00
|
|
|
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
|
2023-06-14 14:15:46 +00:00
|
|
|
MemberRole: protobuf.CommunityMember_ROLE_OWNER,
|
2023-05-02 17:54:44 +00:00
|
|
|
Verified: true,
|
|
|
|
Description: "",
|
|
|
|
IntroMessage: "",
|
|
|
|
OutroMessage: "",
|
|
|
|
Tags: []communities.CommunityTag{},
|
|
|
|
Permissions: &protobuf.CommunityPermissions{},
|
|
|
|
Muted: false,
|
|
|
|
CommunityTokensMetadata: []*protobuf.CommunityTokenMetadata{},
|
|
|
|
UnviewedMessagesCount: totalUnviewedMessageCount,
|
|
|
|
UnviewedMentionsCount: totalUnviewedMentionsCount,
|
2023-03-24 15:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 17:54:44 +00:00
|
|
|
if channelGroupID == pubKey {
|
|
|
|
// They asked for the personal channel group only, so we return now
|
|
|
|
return result, nil
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 15:19:31 +00:00
|
|
|
for _, community := range unique(append(joinedCommunities, spectatedCommunities...)) {
|
2023-05-02 17:54:44 +00:00
|
|
|
if channelGroupID != "" && channelGroupID != community.IDString() {
|
|
|
|
continue
|
|
|
|
}
|
2023-03-24 15:19:31 +00:00
|
|
|
totalUnviewedMessageCount = 0
|
|
|
|
totalUnviewedMentionsCount = 0
|
2023-03-14 20:12:47 +00:00
|
|
|
|
2023-03-24 15:19:31 +00:00
|
|
|
for _, chat := range channels {
|
|
|
|
if chat.CommunityID != community.IDString() || !chat.Active {
|
|
|
|
continue
|
|
|
|
}
|
2023-06-16 18:10:04 +00:00
|
|
|
if !chat.Muted || chat.UnviewedMentionsCount > 0 {
|
|
|
|
totalUnviewedMessageCount += int(chat.UnviewedMessagesCount)
|
|
|
|
}
|
2023-03-24 15:19:31 +00:00
|
|
|
totalUnviewedMentionsCount += int(chat.UnviewedMentionsCount)
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chGrp := ChannelGroup{
|
2023-02-20 11:57:33 +00:00
|
|
|
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),
|
2023-06-14 14:15:46 +00:00
|
|
|
MemberRole: community.MemberRole(community.MemberIdentity()),
|
2023-02-20 11:57:33 +00:00
|
|
|
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,
|
2023-03-24 15:19:31 +00:00
|
|
|
UnviewedMessagesCount: totalUnviewedMessageCount,
|
|
|
|
UnviewedMentionsCount: totalUnviewedMentionsCount,
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 12:46:52 +00:00
|
|
|
for t, i := range community.Images() {
|
2022-02-09 21:58:33 +00:00
|
|
|
chGrp.Images[t] = images.IdentityImage{Name: t, Payload: i.Payload}
|
|
|
|
}
|
|
|
|
|
2023-05-02 17:54:44 +00:00
|
|
|
for _, cat := range community.Categories() {
|
|
|
|
chGrp.Categories[cat.CategoryId] = communities.CommunityCategory{
|
|
|
|
ID: cat.CategoryId,
|
|
|
|
Name: cat.Name,
|
|
|
|
Position: int(cat.Position),
|
2023-03-14 20:12:47 +00:00
|
|
|
}
|
2023-03-24 15:19:31 +00:00
|
|
|
}
|
|
|
|
|
2023-05-02 17:54:44 +00:00
|
|
|
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
|
|
|
|
}
|
2023-03-24 15:19:31 +00:00
|
|
|
|
2023-05-02 17:54:44 +00:00
|
|
|
chGrp.Chats[c.ID] = c
|
2023-03-24 15:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 17:54:44 +00:00
|
|
|
result[community.IDString()] = chGrp
|
2023-03-24 15:19:31 +00:00
|
|
|
|
2023-05-02 17:54:44 +00:00
|
|
|
if channelGroupID == community.IDString() {
|
|
|
|
// We asked for this particular community, so we return now
|
|
|
|
return result, nil
|
2023-03-24 15:19:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 17:54:44 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
2023-03-24 15:19:31 +00:00
|
|
|
|
2023-05-02 17:54:44 +00:00
|
|
|
func (api *API) GetChannelGroups(ctx context.Context) (map[string]ChannelGroup, error) {
|
|
|
|
return api.getChannelGroups(ctx, "")
|
|
|
|
}
|
2022-02-09 21:58:33 +00:00
|
|
|
|
2023-05-02 17:54:44 +00:00
|
|
|
func (api *API) GetChannelGroupByID(ctx context.Context, channelGroupID string) (map[string]ChannelGroup, error) {
|
|
|
|
return api.getChannelGroups(ctx, channelGroupID)
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 22:55:03 +00:00
|
|
|
func (api *API) GetChat(ctx context.Context, communityID types.HexBytes, chatID string) (*Chat, error) {
|
|
|
|
pubKey := types.EncodeHex(crypto.FromECDSAPub(api.s.messenger.IdentityPublicKey()))
|
|
|
|
messengerChat, community, err := api.getChatAndCommunity(pubKey, communityID, chatID)
|
2022-02-10 15:15:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-23 11:21:11 +00:00
|
|
|
if messengerChat == nil {
|
|
|
|
return nil, ErrChatNotFound
|
|
|
|
}
|
|
|
|
|
2023-03-24 15:19:31 +00:00
|
|
|
result, err := api.toAPIChat(messengerChat, community, pubKey, false)
|
2022-02-10 22:55:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 22:55:03 +00:00
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) GetMembers(ctx context.Context, communityID types.HexBytes, chatID string) (map[string]Member, error) {
|
|
|
|
pubKey := types.EncodeHex(crypto.FromECDSAPub(api.s.messenger.IdentityPublicKey()))
|
|
|
|
messengerChat, community, err := api.getChatAndCommunity(pubKey, communityID, chatID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 22:55:03 +00:00
|
|
|
return getChatMembers(messengerChat, community, pubKey)
|
|
|
|
}
|
2022-02-09 21:58:33 +00:00
|
|
|
|
2022-02-10 22:55:03 +00:00
|
|
|
func (api *API) JoinChat(ctx context.Context, communityID types.HexBytes, chatID string) (*Chat, error) {
|
|
|
|
if len(communityID) != 0 {
|
2022-02-11 16:29:57 +00:00
|
|
|
return nil, ErrCommunitiesNotSupported
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
2022-02-10 22:55:03 +00:00
|
|
|
response, err := api.s.messenger.CreatePublicChat(&requests.CreatePublicChat{ID: chatID})
|
2022-02-09 21:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-02-10 22:55:03 +00:00
|
|
|
pubKey := types.EncodeHex(crypto.FromECDSAPub(api.s.messenger.IdentityPublicKey()))
|
2022-02-11 16:29:57 +00:00
|
|
|
|
2023-03-24 15:19:31 +00:00
|
|
|
return api.toAPIChat(response.Chats()[0], nil, pubKey, false)
|
2022-02-11 16:29:57 +00:00
|
|
|
}
|
|
|
|
|
2023-03-30 10:56:57 +00:00
|
|
|
func (api *API) toAPIChat(protocolChat *protocol.Chat, community *communities.Community, pubKey string, skipPinnedMessages bool) (*Chat, error) {
|
2022-02-09 21:58:33 +00:00
|
|
|
chat := &Chat{
|
|
|
|
ID: strings.TrimPrefix(protocolChat.ID, protocolChat.CommunityID),
|
|
|
|
Name: protocolChat.Name,
|
|
|
|
Description: protocolChat.Description,
|
|
|
|
Color: protocolChat.Color,
|
|
|
|
Emoji: protocolChat.Emoji,
|
|
|
|
Active: protocolChat.Active,
|
|
|
|
ChatType: protocolChat.ChatType,
|
|
|
|
Timestamp: protocolChat.Timestamp,
|
|
|
|
LastClockValue: protocolChat.LastClockValue,
|
|
|
|
DeletedAtClockValue: protocolChat.DeletedAtClockValue,
|
|
|
|
ReadMessagesAtClockValue: protocolChat.ReadMessagesAtClockValue,
|
|
|
|
UnviewedMessagesCount: protocolChat.UnviewedMessagesCount,
|
|
|
|
UnviewedMentionsCount: protocolChat.UnviewedMentionsCount,
|
|
|
|
LastMessage: protocolChat.LastMessage,
|
|
|
|
MembershipUpdates: protocolChat.MembershipUpdates,
|
|
|
|
Alias: protocolChat.Alias,
|
|
|
|
Identicon: protocolChat.Identicon,
|
|
|
|
Muted: protocolChat.Muted,
|
|
|
|
InvitationAdmin: protocolChat.InvitationAdmin,
|
|
|
|
ReceivedInvitationAdmin: protocolChat.ReceivedInvitationAdmin,
|
|
|
|
Profile: protocolChat.Profile,
|
|
|
|
CommunityID: protocolChat.CommunityID,
|
|
|
|
CategoryID: protocolChat.CategoryID,
|
|
|
|
Joined: protocolChat.Joined,
|
|
|
|
SyncedTo: protocolChat.SyncedTo,
|
|
|
|
SyncedFrom: protocolChat.SyncedFrom,
|
2022-09-02 08:36:07 +00:00
|
|
|
FirstMessageTimestamp: protocolChat.FirstMessageTimestamp,
|
2022-02-09 21:58:33 +00:00
|
|
|
Highlight: protocolChat.Highlight,
|
2022-08-08 15:22:22 +00:00
|
|
|
Base64Image: protocolChat.Base64Image,
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if protocolChat.OneToOne() {
|
|
|
|
chat.Name = "" // Emptying since it contains non useful data
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:56:57 +00:00
|
|
|
if !skipPinnedMessages {
|
2023-03-24 15:19:31 +00:00
|
|
|
pinnedMessages, cursor, err := api.s.messenger.PinnedMessageByChatID(protocolChat.ID, "", -1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pinnedMessages) != 0 {
|
|
|
|
chat.PinnedMessages = &PinnedMessages{
|
|
|
|
Cursor: cursor,
|
|
|
|
PinnedMessages: pinnedMessages,
|
|
|
|
}
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 15:19:31 +00:00
|
|
|
err := chat.populateCommunityFields(community)
|
2022-02-09 21:58:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-30 10:56:57 +00:00
|
|
|
chatMembers, err := getChatMembers(protocolChat, community, pubKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-02-10 22:55:03 +00:00
|
|
|
}
|
2023-03-30 10:56:57 +00:00
|
|
|
chat.Members = chatMembers
|
2022-02-10 22:55:03 +00:00
|
|
|
|
2022-02-09 21:58:33 +00:00
|
|
|
return chat, nil
|
|
|
|
}
|
|
|
|
|
2022-02-10 22:55:03 +00:00
|
|
|
func getChatMembers(sourceChat *protocol.Chat, community *communities.Community, userPubKey string) (map[string]Member, error) {
|
|
|
|
result := make(map[string]Member)
|
2023-01-23 11:21:11 +00:00
|
|
|
if sourceChat != nil {
|
|
|
|
if sourceChat.ChatType == protocol.ChatTypePrivateGroupChat && len(sourceChat.Members) > 0 {
|
|
|
|
for _, m := range sourceChat.Members {
|
|
|
|
result[m.ID] = Member{
|
2023-06-14 14:15:46 +00:00
|
|
|
Role: func() protobuf.CommunityMember_Roles {
|
|
|
|
if m.Admin {
|
|
|
|
return protobuf.CommunityMember_ROLE_OWNER
|
|
|
|
}
|
|
|
|
return protobuf.CommunityMember_ROLE_NONE
|
|
|
|
}(),
|
2023-01-23 11:21:11 +00:00
|
|
|
Joined: true,
|
|
|
|
}
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
2023-01-23 11:21:11 +00:00
|
|
|
return result, nil
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 11:21:11 +00:00
|
|
|
if sourceChat.ChatType == protocol.ChatTypeOneToOne {
|
|
|
|
result[sourceChat.ID] = Member{
|
|
|
|
Joined: true,
|
|
|
|
}
|
|
|
|
result[userPubKey] = Member{
|
|
|
|
Joined: true,
|
|
|
|
}
|
|
|
|
return result, nil
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if community != nil {
|
2023-06-14 14:15:46 +00:00
|
|
|
for member := range community.Description().Members {
|
2022-02-10 22:55:03 +00:00
|
|
|
pubKey, err := common.HexToPubkey(member)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
result[member] = Member{
|
2023-06-14 14:15:46 +00:00
|
|
|
Role: community.MemberRole(pubKey),
|
2022-02-10 22:55:03 +00:00
|
|
|
Joined: community.Joined(),
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-10 22:55:03 +00:00
|
|
|
return result, nil
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
2022-02-10 22:55:03 +00:00
|
|
|
|
|
|
|
return nil, nil
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 11:21:11 +00:00
|
|
|
func (api *API) getCommunityByID(id string) (*communities.Community, error) {
|
|
|
|
communityID, err := hexutil.Decode(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
community, err := api.s.messenger.GetCommunityByID(communityID)
|
|
|
|
if community == nil && err == nil {
|
|
|
|
return nil, ErrCommunityNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return community, err
|
|
|
|
}
|
|
|
|
|
2022-02-09 21:58:33 +00:00
|
|
|
func (chat *Chat) populateCommunityFields(community *communities.Community) error {
|
2022-02-10 15:15:27 +00:00
|
|
|
if community == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-09 21:58:33 +00:00
|
|
|
commChat, exists := community.Chats()[chat.ID]
|
|
|
|
if !exists {
|
2022-04-01 22:39:25 +00:00
|
|
|
// Skip unknown community chats. They might be channels that were deleted
|
|
|
|
return nil
|
2022-02-09 21:58:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
canPost, err := community.CanMemberIdentityPost(chat.ID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
chat.CategoryID = commChat.CategoryId
|
|
|
|
chat.Position = commChat.Position
|
|
|
|
chat.Permissions = commChat.Permissions
|
|
|
|
chat.Emoji = commChat.Identity.Emoji
|
|
|
|
chat.Name = commChat.Identity.DisplayName
|
|
|
|
chat.Description = commChat.Identity.Description
|
|
|
|
chat.CanPost = canPost
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-10 22:55:03 +00:00
|
|
|
|
|
|
|
func (api *API) getChatAndCommunity(pubKey string, communityID types.HexBytes, chatID string) (*protocol.Chat, *communities.Community, error) {
|
|
|
|
fullChatID := chatID
|
|
|
|
|
|
|
|
if string(communityID.Bytes()) == pubKey { // Obtaining chats from personal
|
|
|
|
communityID = []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(communityID) != 0 {
|
2023-01-23 11:21:11 +00:00
|
|
|
id := string(communityID.Bytes())
|
|
|
|
|
|
|
|
if chatID == "" {
|
|
|
|
community, err := api.getCommunityByID(id)
|
|
|
|
return nil, community, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fullChatID = id + chatID
|
2022-02-10 22:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
messengerChat := api.s.messenger.Chat(fullChatID)
|
|
|
|
if messengerChat == nil {
|
|
|
|
return nil, nil, ErrChatNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
var community *communities.Community
|
|
|
|
if messengerChat.CommunityID != "" {
|
2023-01-23 11:21:11 +00:00
|
|
|
var err error
|
|
|
|
community, err = api.getCommunityByID(messengerChat.CommunityID)
|
2022-02-10 22:55:03 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return messengerChat, community, nil
|
|
|
|
}
|
2022-07-12 07:48:58 +00:00
|
|
|
|
2022-08-08 15:22:22 +00:00
|
|
|
func (api *API) EditChat(ctx context.Context, communityID types.HexBytes, chatID string, name string, color string, image images.CroppedImage) (*Chat, error) {
|
2022-07-12 07:48:58 +00:00
|
|
|
if len(communityID) != 0 {
|
|
|
|
return nil, ErrCommunitiesNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
chatToEdit := api.s.messenger.Chat(chatID)
|
|
|
|
if chatToEdit == nil {
|
|
|
|
return nil, ErrChatNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if chatToEdit.ChatType != protocol.ChatTypePrivateGroupChat {
|
|
|
|
return nil, ErrChatTypeNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := api.s.messenger.EditGroupChat(ctx, chatID, name, color, image)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKey := types.EncodeHex(crypto.FromECDSAPub(api.s.messenger.IdentityPublicKey()))
|
2023-03-24 15:19:31 +00:00
|
|
|
return api.toAPIChat(response.Chats()[0], nil, pubKey, false)
|
2022-07-12 07:48:58 +00:00
|
|
|
}
|