2021-01-11 10:32:51 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2021-04-19 12:09:46 +00:00
|
|
|
"fmt"
|
2021-01-11 10:32:51 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/communities"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
2021-04-19 12:09:46 +00:00
|
|
|
"github.com/status-im/status-go/protocol/transport"
|
2021-01-11 10:32:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (m *Messenger) publishOrg(org *communities.Community) error {
|
|
|
|
m.logger.Debug("publishing org", zap.String("org-id", org.IDString()), zap.Any("org", org))
|
|
|
|
payload, err := org.MarshaledDescription()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMessage := common.RawMessage{
|
|
|
|
Payload: payload,
|
|
|
|
Sender: org.PrivateKey(),
|
|
|
|
// we don't want to wrap in an encryption layer message
|
|
|
|
SkipEncryption: true,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_COMMUNITY_DESCRIPTION,
|
|
|
|
}
|
2021-06-23 14:13:48 +00:00
|
|
|
_, err = m.sender.SendPublic(context.Background(), org.IDString(), rawMessage)
|
2021-01-11 10:32:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) publishOrgInvitation(org *communities.Community, invitation *protobuf.CommunityInvitation) error {
|
|
|
|
m.logger.Debug("publishing org invitation", zap.String("org-id", org.IDString()), zap.Any("org", org))
|
|
|
|
pk, err := crypto.DecompressPubkey(invitation.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := proto.Marshal(invitation)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMessage := common.RawMessage{
|
|
|
|
Payload: payload,
|
|
|
|
Sender: org.PrivateKey(),
|
|
|
|
// we don't want to wrap in an encryption layer message
|
|
|
|
SkipEncryption: true,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_COMMUNITY_INVITATION,
|
|
|
|
}
|
2021-06-23 14:13:48 +00:00
|
|
|
_, err = m.sender.SendPrivate(context.Background(), pk, &rawMessage)
|
2021-01-11 10:32:51 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleCommunitiesSubscription handles events from communities
|
|
|
|
func (m *Messenger) handleCommunitiesSubscription(c chan *communities.Subscription) {
|
|
|
|
|
|
|
|
var lastPublished int64
|
|
|
|
// We check every 5 minutes if we need to publish
|
|
|
|
ticker := time.NewTicker(5 * time.Minute)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case sub, more := <-c:
|
|
|
|
if !more {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if sub.Community != nil {
|
|
|
|
err := m.publishOrg(sub.Community)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Warn("failed to publish org", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, invitation := range sub.Invitations {
|
|
|
|
err := m.publishOrgInvitation(sub.Community, invitation)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Warn("failed to publish org invitation", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m.logger.Debug("published org")
|
|
|
|
case <-ticker.C:
|
|
|
|
// If we are not online, we don't even try
|
|
|
|
if !m.online() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// If not enough time has passed since last advertisement, we skip this
|
|
|
|
if time.Now().Unix()-lastPublished < communityAdvertiseIntervalSecond {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
orgs, err := m.communitiesManager.Created()
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Warn("failed to retrieve orgs", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx := range orgs {
|
|
|
|
org := orgs[idx]
|
|
|
|
err := m.publishOrg(org)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Warn("failed to publish org", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// set lastPublished
|
|
|
|
lastPublished = time.Now().Unix()
|
|
|
|
|
|
|
|
case <-m.quit:
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) Communities() ([]*communities.Community, error) {
|
|
|
|
return m.communitiesManager.All()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) JoinedCommunities() ([]*communities.Community, error) {
|
|
|
|
return m.communitiesManager.Joined()
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:41:49 +00:00
|
|
|
func (m *Messenger) JoinCommunity(ctx context.Context, communityID types.HexBytes) (*MessengerResponse, error) {
|
2021-08-06 15:40:23 +00:00
|
|
|
mr, err := m.joinCommunity(ctx, communityID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if com, ok := mr.communities[communityID.String()]; ok {
|
|
|
|
err = m.syncCommunity(context.Background(), com)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
|
2021-08-06 15:40:23 +00:00
|
|
|
return mr, nil
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
|
2021-07-22 17:41:49 +00:00
|
|
|
func (m *Messenger) joinCommunity(ctx context.Context, communityID types.HexBytes) (*MessengerResponse, error) {
|
2021-08-06 15:40:23 +00:00
|
|
|
logger := m.logger.Named("joinCommunity")
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
response := &MessengerResponse{}
|
|
|
|
|
|
|
|
community, err := m.communitiesManager.JoinCommunity(communityID)
|
|
|
|
if err != nil {
|
2021-08-06 15:40:23 +00:00
|
|
|
logger.Debug("m.communitiesManager.JoinCommunity error", zap.Error(err))
|
2021-01-11 10:32:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:41:49 +00:00
|
|
|
chatIDs := community.DefaultFilters()
|
2021-01-11 10:32:51 +00:00
|
|
|
|
|
|
|
chats := CreateCommunityChats(community, m.getTimesource())
|
|
|
|
response.AddChats(chats)
|
|
|
|
|
|
|
|
for _, chat := range response.Chats() {
|
|
|
|
chatIDs = append(chatIDs, chat.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load transport filters
|
|
|
|
filters, err := m.transport.InitPublicFilters(chatIDs)
|
|
|
|
if err != nil {
|
2021-08-06 15:40:23 +00:00
|
|
|
logger.Debug("m.transport.InitPublicFilters error", zap.Error(err))
|
2021-01-11 10:32:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-10 12:04:52 +00:00
|
|
|
if community.IsAdmin() {
|
|
|
|
// Init the community filter so we can receive messages on the community
|
|
|
|
communityFilters, err := m.transport.InitCommunityFilters([]*ecdsa.PrivateKey{community.PrivateKey()})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
filters = append(filters, communityFilters...)
|
|
|
|
}
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
willSync, err := m.scheduleSyncFilters(filters)
|
|
|
|
if err != nil {
|
2021-08-06 15:40:23 +00:00
|
|
|
logger.Debug("m.scheduleSyncFilters error", zap.Error(err))
|
2021-05-14 10:55:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !willSync {
|
2021-05-28 11:05:01 +00:00
|
|
|
defaultSyncPeriod, err := m.settings.GetDefaultSyncPeriod()
|
|
|
|
if err != nil {
|
2021-08-06 15:40:23 +00:00
|
|
|
logger.Debug("m.settings.GetDefaultSyncPeriod error", zap.Error(err))
|
2021-05-28 11:05:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-31 14:35:14 +00:00
|
|
|
timestamp := uint32(m.getTimesource().GetCurrentTime()/1000) - defaultSyncPeriod
|
2021-05-14 10:55:42 +00:00
|
|
|
for idx := range chats {
|
|
|
|
chats[idx].SyncedTo = timestamp
|
|
|
|
chats[idx].SyncedFrom = timestamp
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 15:15:22 +00:00
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
response.AddCommunity(community)
|
|
|
|
|
2021-07-22 17:41:49 +00:00
|
|
|
if err = m.saveChats(chats); err != nil {
|
2021-08-06 15:40:23 +00:00
|
|
|
logger.Debug("m.saveChats error", zap.Error(err))
|
2021-07-22 17:41:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-01 11:57:31 +00:00
|
|
|
err = m.reregisterForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:41:49 +00:00
|
|
|
err = m.sendCurrentUserStatusToCommunity(ctx, community)
|
|
|
|
if err != nil {
|
2021-08-06 15:40:23 +00:00
|
|
|
logger.Debug("m.sendCurrentUserStatusToCommunity error", zap.Error(err))
|
2021-07-22 17:41:49 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 13:29:43 +00:00
|
|
|
func (m *Messenger) SetMuted(communityID types.HexBytes, muted bool) error {
|
|
|
|
return m.communitiesManager.SetMuted(communityID, muted)
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (m *Messenger) RequestToJoinCommunity(request *requests.RequestToJoinCommunity) (*MessengerResponse, error) {
|
2021-08-06 15:40:23 +00:00
|
|
|
logger := m.logger.Named("RequestToJoinCommunity")
|
2021-01-11 10:32:51 +00:00
|
|
|
if err := request.Validate(); err != nil {
|
2021-08-06 15:40:23 +00:00
|
|
|
logger.Debug("request failed to validate", zap.Error(err), zap.Any("request", request))
|
2021-01-11 10:32:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
community, requestToJoin, err := m.communitiesManager.RequestToJoin(&m.identity.PublicKey, request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-06 15:40:23 +00:00
|
|
|
err = m.syncCommunity(context.Background(), community)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
|
|
|
|
requestToJoinProto := &protobuf.CommunityRequestToJoin{
|
|
|
|
Clock: requestToJoin.Clock,
|
|
|
|
EnsName: requestToJoin.ENSName,
|
|
|
|
CommunityId: community.ID(),
|
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := proto.Marshal(requestToJoinProto)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rawMessage := common.RawMessage{
|
|
|
|
Payload: payload,
|
|
|
|
SkipEncryption: true,
|
|
|
|
MessageType: protobuf.ApplicationMetadataMessage_COMMUNITY_REQUEST_TO_JOIN,
|
|
|
|
}
|
2021-06-23 14:13:48 +00:00
|
|
|
_, err = m.sender.SendCommunityMessage(context.Background(), community.PublicKey(), rawMessage)
|
2021-01-11 10:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &MessengerResponse{RequestsToJoinCommunity: []*communities.RequestToJoin{requestToJoin}}
|
|
|
|
response.AddCommunity(community)
|
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
// We send a push notification in the background
|
|
|
|
go func() {
|
|
|
|
if m.pushNotificationClient != nil {
|
|
|
|
pks, err := community.CanManageUsersPublicKeys()
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("failed to get pks", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, publicKey := range pks {
|
|
|
|
pkString := common.PubkeyToHex(publicKey)
|
|
|
|
_, err = m.pushNotificationClient.SendNotification(publicKey, nil, requestToJoin.ID, pkString, protobuf.PushNotification_REQUEST_TO_JOIN_COMMUNITY)
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Error("error sending notification", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2021-05-23 13:34:17 +00:00
|
|
|
func (m *Messenger) CreateCommunityCategory(request *requests.CreateCommunityCategory) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
community, changes, err := m.communitiesManager.CreateCategory(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.AddCommunity(community)
|
|
|
|
response.CommunityChanges = []*communities.CommunityChanges{changes}
|
|
|
|
|
|
|
|
return &response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) EditCommunityCategory(request *requests.EditCommunityCategory) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
community, changes, err := m.communitiesManager.EditCategory(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.AddCommunity(community)
|
|
|
|
response.CommunityChanges = []*communities.CommunityChanges{changes}
|
|
|
|
|
|
|
|
return &response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ReorderCommunityCategories(request *requests.ReorderCommunityCategories) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
community, changes, err := m.communitiesManager.ReorderCategories(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.AddCommunity(community)
|
|
|
|
response.CommunityChanges = []*communities.CommunityChanges{changes}
|
|
|
|
|
|
|
|
return &response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ReorderCommunityChat(request *requests.ReorderCommunityChat) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
community, changes, err := m.communitiesManager.ReorderChat(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.AddCommunity(community)
|
|
|
|
response.CommunityChanges = []*communities.CommunityChanges{changes}
|
|
|
|
|
|
|
|
return &response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) DeleteCommunityCategory(request *requests.DeleteCommunityCategory) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response MessengerResponse
|
|
|
|
community, changes, err := m.communitiesManager.DeleteCategory(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.AddCommunity(community)
|
|
|
|
response.CommunityChanges = []*communities.CommunityChanges{changes}
|
|
|
|
|
|
|
|
return &response, nil
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (m *Messenger) AcceptRequestToJoinCommunity(request *requests.AcceptRequestToJoinCommunity) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
community, err := m.communitiesManager.AcceptRequestToJoin(request)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
response.AddCommunity(community)
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) DeclineRequestToJoinCommunity(request *requests.DeclineRequestToJoinCommunity) error {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return m.communitiesManager.DeclineRequestToJoin(request)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) LeaveCommunity(communityID types.HexBytes) (*MessengerResponse, error) {
|
2021-08-06 15:40:23 +00:00
|
|
|
mr, err := m.leaveCommunity(communityID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if com, ok := mr.communities[communityID.String()]; ok {
|
|
|
|
err = m.syncCommunity(context.Background(), com)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
|
2021-08-06 15:40:23 +00:00
|
|
|
return mr, nil
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) leaveCommunity(communityID types.HexBytes) (*MessengerResponse, error) {
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
|
|
|
|
community, err := m.communitiesManager.LeaveCommunity(communityID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make chat inactive
|
|
|
|
for chatID := range community.Chats() {
|
|
|
|
communityChatID := communityID.String() + chatID
|
|
|
|
err := m.deleteChat(communityChatID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.AddRemovedChat(communityChatID)
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
_, err = m.transport.RemoveFilterByChatID(communityChatID)
|
2021-01-11 10:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
_, err = m.transport.RemoveFilterByChatID(communityID.String())
|
2021-01-11 10:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.AddCommunity(community)
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) CreateCommunityChat(communityID types.HexBytes, c *protobuf.CommunityChat) (*MessengerResponse, error) {
|
|
|
|
var response MessengerResponse
|
|
|
|
community, changes, err := m.communitiesManager.CreateChat(communityID, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.AddCommunity(community)
|
|
|
|
response.CommunityChanges = []*communities.CommunityChanges{changes}
|
|
|
|
|
|
|
|
var chats []*Chat
|
|
|
|
var chatIDs []string
|
|
|
|
for chatID, chat := range changes.ChatsAdded {
|
|
|
|
c := CreateCommunityChat(community.IDString(), chatID, chat, m.getTimesource())
|
|
|
|
chats = append(chats, c)
|
|
|
|
chatIDs = append(chatIDs, c.ID)
|
|
|
|
response.AddChat(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load filters
|
|
|
|
filters, err := m.transport.InitPublicFilters(chatIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-14 10:55:42 +00:00
|
|
|
_, err = m.scheduleSyncFilters(filters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
|
2021-09-01 09:03:45 +00:00
|
|
|
err = m.saveChats(chats)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.reregisterForPushNotifications()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &response, nil
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-01 12:13:17 +00:00
|
|
|
func (m *Messenger) EditCommunityChat(communityID types.HexBytes, chatID string, c *protobuf.CommunityChat) (*MessengerResponse, error) {
|
|
|
|
var response MessengerResponse
|
|
|
|
community, changes, err := m.communitiesManager.EditChat(communityID, chatID, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.AddCommunity(community)
|
|
|
|
response.CommunityChanges = []*communities.CommunityChanges{changes}
|
|
|
|
|
|
|
|
var chats []*Chat
|
|
|
|
var chatIDs []string
|
2021-06-03 10:49:04 +00:00
|
|
|
for chatID, change := range changes.ChatsModified {
|
|
|
|
c := CreateCommunityChat(community.IDString(), chatID, change.ChatModified, m.getTimesource())
|
2021-06-01 12:13:17 +00:00
|
|
|
chats = append(chats, c)
|
|
|
|
chatIDs = append(chatIDs, c.ID)
|
|
|
|
response.AddChat(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load filters
|
|
|
|
filters, err := m.transport.InitPublicFilters(chatIDs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
_, err = m.scheduleSyncFilters(filters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &response, m.saveChats(chats)
|
|
|
|
}
|
|
|
|
|
2021-07-30 17:05:44 +00:00
|
|
|
func (m *Messenger) DeleteCommunityChat(communityID types.HexBytes, chatID string) (*MessengerResponse, error) {
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
|
|
|
|
community, _, err := m.communitiesManager.DeleteChat(communityID, chatID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = m.deleteChat(chatID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response.AddRemovedChat(chatID)
|
|
|
|
|
|
|
|
_, err = m.transport.RemoveFilterByChatID(chatID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.AddCommunity(community)
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (m *Messenger) CreateCommunity(request *requests.CreateCommunity) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-18 19:32:15 +00:00
|
|
|
community, err := m.communitiesManager.CreateCommunity(request)
|
2021-01-11 10:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init the community filter so we can receive messages on the community
|
2021-03-25 15:15:22 +00:00
|
|
|
_, err = m.transport.InitCommunityFilters([]*ecdsa.PrivateKey{community.PrivateKey()})
|
2021-01-11 10:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:41:49 +00:00
|
|
|
// Init the default community filters
|
|
|
|
_, err = m.transport.InitPublicFilters(community.DefaultFilters())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
response := &MessengerResponse{}
|
2021-01-11 10:32:51 +00:00
|
|
|
response.AddCommunity(community)
|
2021-08-06 15:40:23 +00:00
|
|
|
err = m.syncCommunity(context.Background(), community)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2021-05-18 19:32:15 +00:00
|
|
|
func (m *Messenger) EditCommunity(request *requests.EditCommunity) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
community, err := m.communitiesManager.EditCommunity(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
response.AddCommunity(community)
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func (m *Messenger) ExportCommunity(id types.HexBytes) (*ecdsa.PrivateKey, error) {
|
|
|
|
return m.communitiesManager.ExportCommunity(id)
|
|
|
|
}
|
|
|
|
|
2021-07-22 17:41:49 +00:00
|
|
|
func (m *Messenger) ImportCommunity(ctx context.Context, key *ecdsa.PrivateKey) (*MessengerResponse, error) {
|
2021-03-25 15:15:22 +00:00
|
|
|
community, err := m.communitiesManager.ImportCommunity(key)
|
2021-01-11 10:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load filters
|
2021-07-22 17:41:49 +00:00
|
|
|
_, err = m.transport.InitPublicFilters(community.DefaultFilters())
|
2021-01-11 10:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-19 12:09:46 +00:00
|
|
|
//request info already stored on mailserver, but its success is not crucial
|
|
|
|
// for import
|
2021-09-17 09:02:04 +00:00
|
|
|
_, _ = m.RequestCommunityInfoFromMailserver(community.IDString())
|
2021-03-25 15:15:22 +00:00
|
|
|
|
|
|
|
// We add ourselves
|
|
|
|
_, err = m.communitiesManager.InviteUsersToCommunity(community.ID(), []*ecdsa.PublicKey{&m.identity.PublicKey})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-19 12:09:46 +00:00
|
|
|
|
2021-07-22 17:41:49 +00:00
|
|
|
return m.JoinCommunity(ctx, community.ID())
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) InviteUsersToCommunity(request *requests.InviteUsersToCommunity) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
var messages []*common.Message
|
|
|
|
|
|
|
|
var publicKeys []*ecdsa.PublicKey
|
2021-06-29 13:56:06 +00:00
|
|
|
community, err := m.communitiesManager.GetByID(request.CommunityID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
for _, pkBytes := range request.Users {
|
|
|
|
publicKey, err := common.HexToPubkey(pkBytes.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
publicKeys = append(publicKeys, publicKey)
|
|
|
|
|
|
|
|
message := &common.Message{}
|
|
|
|
message.ChatId = pkBytes.String()
|
|
|
|
message.CommunityID = request.CommunityID.String()
|
2021-06-29 13:56:06 +00:00
|
|
|
message.Text = fmt.Sprintf("You have been invited to community %s", community.Name())
|
2021-01-11 10:32:51 +00:00
|
|
|
messages = append(messages, message)
|
|
|
|
r, err := m.CreateOneToOneChat(&requests.CreateOneToOneChat{ID: pkBytes})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := response.Merge(r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 13:56:06 +00:00
|
|
|
community, err = m.communitiesManager.InviteUsersToCommunity(request.CommunityID, publicKeys)
|
2021-01-11 10:32:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sendMessagesResponse, err := m.SendChatMessages(context.Background(), messages)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := response.Merge(sendMessagesResponse); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response.AddCommunity(community)
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) ShareCommunity(request *requests.ShareCommunity) (*MessengerResponse, error) {
|
|
|
|
if err := request.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
|
2021-06-29 13:56:06 +00:00
|
|
|
community, err := m.communitiesManager.GetByID(request.CommunityID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
var messages []*common.Message
|
|
|
|
for _, pk := range request.Users {
|
|
|
|
message := &common.Message{}
|
|
|
|
message.ChatId = pk.String()
|
|
|
|
message.CommunityID = request.CommunityID.String()
|
2021-06-29 13:56:06 +00:00
|
|
|
message.Text = fmt.Sprintf("Community %s has been shared with you", community.Name())
|
2021-01-11 10:32:51 +00:00
|
|
|
messages = append(messages, message)
|
|
|
|
r, err := m.CreateOneToOneChat(&requests.CreateOneToOneChat{ID: pk})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := response.Merge(r); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessagesResponse, err := m.SendChatMessages(context.Background(), messages)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := response.Merge(sendMessagesResponse); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) MyPendingRequestsToJoin() ([]*communities.RequestToJoin, error) {
|
|
|
|
return m.communitiesManager.PendingRequestsToJoinForUser(&m.identity.PublicKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) PendingRequestsToJoinForCommunity(id types.HexBytes) ([]*communities.RequestToJoin, error) {
|
|
|
|
return m.communitiesManager.PendingRequestsToJoinForCommunity(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Messenger) RemoveUserFromCommunity(id types.HexBytes, pkString string) (*MessengerResponse, error) {
|
|
|
|
publicKey, err := common.HexToPubkey(pkString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
community, err := m.communitiesManager.RemoveUserFromCommunity(id, publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &MessengerResponse{}
|
|
|
|
response.AddCommunity(community)
|
|
|
|
return response, nil
|
|
|
|
}
|
2021-03-19 09:15:45 +00:00
|
|
|
|
|
|
|
func (m *Messenger) BanUserFromCommunity(request *requests.BanUserFromCommunity) (*MessengerResponse, error) {
|
|
|
|
community, err := m.communitiesManager.BanUserFromCommunity(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
response := &MessengerResponse{}
|
2021-11-25 15:21:42 +00:00
|
|
|
response, err = m.DeclineAllPendingGroupInvitesFromUser(response, request.User.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-19 09:15:45 +00:00
|
|
|
response.AddCommunity(community)
|
|
|
|
return response, nil
|
|
|
|
}
|
2021-04-19 12:09:46 +00:00
|
|
|
|
|
|
|
// RequestCommunityInfoFromMailserver installs filter for community and requests its details
|
|
|
|
// from mailserver. When response received it will be passed through signals handler
|
2021-09-17 09:02:04 +00:00
|
|
|
func (m *Messenger) RequestCommunityInfoFromMailserver(communityID string) (*communities.Community, error) {
|
|
|
|
return m.requestCommunityInfoFromMailserver(communityID)
|
2021-08-05 13:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RequestCommunityInfoFromMailserverSync installs filter for community and requests its details
|
|
|
|
// from mailserver. It will wait for a response and return it if any community is found
|
|
|
|
func (m *Messenger) RequestCommunityInfoFromMailserverSync(communityID string) (*communities.Community, error) {
|
2021-09-17 09:02:04 +00:00
|
|
|
return m.requestCommunityInfoFromMailserver(communityID)
|
2021-08-05 13:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RequestCommunityInfoFromMailserver installs filter for community and requests its details
|
|
|
|
// from mailserver. When response received it will be passed through signals handler
|
2021-09-17 09:02:04 +00:00
|
|
|
func (m *Messenger) requestCommunityInfoFromMailserver(communityID string) (*communities.Community, error) {
|
2021-04-19 12:09:46 +00:00
|
|
|
|
|
|
|
if _, ok := m.requestedCommunities[communityID]; ok {
|
2021-08-05 13:27:47 +00:00
|
|
|
return nil, nil
|
2021-04-19 12:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//If filter wasn't installed we create it and remember for deinstalling after
|
|
|
|
//response received
|
|
|
|
filter := m.transport.FilterByChatID(communityID)
|
|
|
|
if filter == nil {
|
|
|
|
filters, err := m.transport.InitPublicFilters([]string{communityID})
|
|
|
|
if err != nil {
|
2021-08-05 13:27:47 +00:00
|
|
|
return nil, fmt.Errorf("Can't install filter for community: %v", err)
|
2021-04-19 12:09:46 +00:00
|
|
|
}
|
|
|
|
if len(filters) != 1 {
|
2021-08-05 13:27:47 +00:00
|
|
|
return nil, fmt.Errorf("Unexpected amount of filters created")
|
2021-04-19 12:09:46 +00:00
|
|
|
}
|
|
|
|
filter = filters[0]
|
|
|
|
m.requestedCommunities[communityID] = filter
|
|
|
|
} else {
|
|
|
|
//we don't remember filter id associated with community because it was already installed
|
|
|
|
m.requestedCommunities[communityID] = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
now := uint32(m.transport.GetCurrentTime() / 1000)
|
|
|
|
monthAgo := now - (86400 * 30)
|
|
|
|
|
2021-07-21 19:02:50 +00:00
|
|
|
_, _, err := m.RequestHistoricMessagesForFilter(context.Background(),
|
2021-04-19 12:09:46 +00:00
|
|
|
monthAgo,
|
|
|
|
now,
|
|
|
|
nil,
|
2021-07-21 19:02:50 +00:00
|
|
|
nil,
|
2021-04-19 12:09:46 +00:00
|
|
|
filter,
|
2021-09-17 09:02:04 +00:00
|
|
|
true)
|
2021-04-19 12:09:46 +00:00
|
|
|
|
2021-08-05 13:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-19 12:09:46 +00:00
|
|
|
|
2021-09-17 09:02:04 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
var community *communities.Community
|
|
|
|
|
|
|
|
fetching := true
|
2021-04-19 12:09:46 +00:00
|
|
|
|
2021-09-17 09:02:04 +00:00
|
|
|
for fetching {
|
|
|
|
select {
|
|
|
|
case <-time.After(200 * time.Millisecond):
|
|
|
|
//send signal to client that message status updated
|
|
|
|
community, err = m.communitiesManager.GetByIDString(communityID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-08-05 13:27:47 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 09:02:04 +00:00
|
|
|
if community != nil && community.Name() != "" && community.DescriptionText() != "" {
|
|
|
|
fetching = false
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
fetching = false
|
|
|
|
}
|
2021-08-05 13:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if community == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//if there is no info helpful for client, we don't post it
|
2021-09-17 09:02:04 +00:00
|
|
|
if community.Name() == "" && community.DescriptionText() == "" {
|
2021-08-05 13:27:47 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m.forgetCommunityRequest(communityID)
|
|
|
|
|
|
|
|
return community, nil
|
2021-04-19 12:09:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// forgetCommunityRequest removes community from requested ones and removes filter
|
|
|
|
func (m *Messenger) forgetCommunityRequest(communityID string) {
|
|
|
|
filter, ok := m.requestedCommunities[communityID]
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if filter != nil {
|
|
|
|
err := m.transport.RemoveFilters([]*transport.Filter{filter})
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Warn("cant remove filter", zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(m.requestedCommunities, communityID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// passStoredCommunityInfoToSignalHandler calls signal handler with community info
|
|
|
|
func (m *Messenger) passStoredCommunityInfoToSignalHandler(communityID string) {
|
|
|
|
if m.config.messengerSignalsHandler == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//send signal to client that message status updated
|
|
|
|
community, err := m.communitiesManager.GetByIDString(communityID)
|
|
|
|
if community == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
//if there is no info helpful for client, we don't post it
|
2021-08-06 15:40:23 +00:00
|
|
|
if community.Name() == "" && community.DescriptionText() == "" && community.MembersCount() == 0 {
|
2021-04-19 12:09:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
m.logger.Warn("cant get community and pass it to signal handler", zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m.config.messengerSignalsHandler.CommunityInfoFound(community)
|
|
|
|
m.forgetCommunityRequest(communityID)
|
|
|
|
}
|
2021-06-01 09:29:37 +00:00
|
|
|
|
|
|
|
// handleCommunityDescription handles an community description
|
|
|
|
func (m *Messenger) handleCommunityDescription(state *ReceivedMessageState, signer *ecdsa.PublicKey, description protobuf.CommunityDescription, rawPayload []byte) error {
|
|
|
|
communityResponse, err := m.communitiesManager.HandleCommunityDescriptionMessage(signer, &description, rawPayload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
community := communityResponse.Community
|
|
|
|
|
|
|
|
state.Response.AddCommunity(community)
|
|
|
|
state.Response.CommunityChanges = append(state.Response.CommunityChanges, communityResponse.Changes)
|
|
|
|
|
|
|
|
// If we haven't joined the org, nothing to do
|
|
|
|
if !community.Joined() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update relevant chats names and add new ones
|
|
|
|
// Currently removal is not supported
|
|
|
|
chats := CreateCommunityChats(community, state.Timesource)
|
|
|
|
var chatIDs []string
|
|
|
|
for i, chat := range chats {
|
|
|
|
|
|
|
|
oldChat, ok := state.AllChats.Load(chat.ID)
|
|
|
|
if !ok {
|
|
|
|
// Beware, don't use the reference in the range (i.e chat) as it's a shallow copy
|
|
|
|
state.AllChats.Store(chat.ID, chats[i])
|
|
|
|
|
|
|
|
state.Response.AddChat(chat)
|
|
|
|
chatIDs = append(chatIDs, chat.ID)
|
|
|
|
// Update name, currently is the only field is mutable
|
2021-06-03 10:49:04 +00:00
|
|
|
} else if oldChat.Name != chat.Name ||
|
2021-10-04 13:02:25 +00:00
|
|
|
oldChat.Description != chat.Description ||
|
|
|
|
oldChat.Emoji != chat.Emoji ||
|
|
|
|
oldChat.Color != chat.Color {
|
2021-06-01 09:29:37 +00:00
|
|
|
oldChat.Name = chat.Name
|
2021-06-03 10:49:04 +00:00
|
|
|
oldChat.Description = chat.Description
|
2021-10-04 13:02:25 +00:00
|
|
|
oldChat.Emoji = chat.Emoji
|
|
|
|
oldChat.Color = chat.Color
|
2021-06-01 09:29:37 +00:00
|
|
|
// TODO(samyoul) remove storing of an updated reference pointer?
|
|
|
|
state.AllChats.Store(chat.ID, oldChat)
|
|
|
|
state.Response.AddChat(chat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load transport filters
|
|
|
|
filters, err := m.transport.InitPublicFilters(chatIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = m.scheduleSyncFilters(filters)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-06 15:40:23 +00:00
|
|
|
|
|
|
|
func (m *Messenger) handleSyncCommunity(messageState *ReceivedMessageState, syncCommunity protobuf.SyncCommunity) error {
|
|
|
|
logger := m.logger.Named("handleSyncCommunity")
|
|
|
|
|
|
|
|
// Should handle community
|
|
|
|
shouldHandle, err := m.communitiesManager.ShouldHandleSyncCommunity(&syncCommunity)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug("m.communitiesManager.ShouldHandleSyncCommunity error", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logger.Debug("ShouldHandleSyncCommunity result", zap.Bool("shouldHandle", shouldHandle))
|
|
|
|
if !shouldHandle {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle any community requests to join.
|
|
|
|
// MUST BE HANDLED BEFORE DESCRIPTION!
|
|
|
|
pending := false
|
|
|
|
for _, rtj := range syncCommunity.RequestsToJoin {
|
|
|
|
req := new(communities.RequestToJoin)
|
|
|
|
req.InitFromSyncProtobuf(rtj)
|
|
|
|
|
|
|
|
if req.State == communities.RequestToJoinStatePending {
|
|
|
|
pending = true
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.communitiesManager.SaveRequestToJoin(req)
|
2021-11-11 16:37:04 +00:00
|
|
|
if err != nil && err != communities.ErrOldRequestToJoin {
|
2021-08-06 15:40:23 +00:00
|
|
|
logger.Debug("m.communitiesManager.SaveRequestToJoin error", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logger.Debug("community requests to join pending state", zap.Bool("pending", pending))
|
|
|
|
|
|
|
|
// Don't use the public key of the private key, uncompress the community id
|
|
|
|
orgPubKey, err := crypto.DecompressPubkey(syncCommunity.Id)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug("crypto.DecompressPubkey error", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
logger.Debug("crypto.DecompressPubkey result", zap.Any("orgPubKey", orgPubKey))
|
|
|
|
|
|
|
|
var amm protobuf.ApplicationMetadataMessage
|
|
|
|
err = proto.Unmarshal(syncCommunity.Description, &amm)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug("proto.Unmarshal protobuf.ApplicationMetadataMessage error", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var cd protobuf.CommunityDescription
|
|
|
|
err = proto.Unmarshal(amm.Payload, &cd)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug("proto.Unmarshal protobuf.CommunityDescription error", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = m.handleCommunityDescription(messageState, orgPubKey, cd, syncCommunity.Description)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug("m.handleCommunityDescription error", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-10 12:04:52 +00:00
|
|
|
// associate private key with community if set
|
|
|
|
if syncCommunity.PrivateKey != nil {
|
|
|
|
orgPrivKey, err := crypto.ToECDSA(syncCommunity.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug("crypto.ToECDSA", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = m.communitiesManager.SetPrivateKey(syncCommunity.Id, orgPrivKey)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug("m.communitiesManager.SetPrivateKey", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 15:40:23 +00:00
|
|
|
// if we are not waiting for approval, join or leave the community
|
|
|
|
if !pending {
|
|
|
|
var mr *MessengerResponse
|
|
|
|
if syncCommunity.Joined {
|
|
|
|
mr, err = m.joinCommunity(context.Background(), syncCommunity.Id)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug("m.joinCommunity error", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mr, err = m.leaveCommunity(syncCommunity.Id)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug("m.leaveCommunity error", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = messageState.Response.Merge(mr)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug("messageState.Response.Merge error", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update the clock value
|
|
|
|
err = m.communitiesManager.SetSyncClock(syncCommunity.Id, syncCommunity.Clock)
|
|
|
|
if err != nil {
|
|
|
|
logger.Debug("m.communitiesManager.SetSyncClock", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|