diff --git a/protocol/communities/community.go b/protocol/communities/community.go index 798f2f67d..64a759e9c 100644 --- a/protocol/communities/community.go +++ b/protocol/communities/community.go @@ -2384,7 +2384,7 @@ func (o *Community) RequestsToJoin() []*RequestToJoin { return o.config.RequestsToJoin } -func (o *Community) AddMember(publicKey *ecdsa.PublicKey, roles []protobuf.CommunityMember_Roles) (*CommunityChanges, error) { +func (o *Community) AddMember(publicKey *ecdsa.PublicKey, roles []protobuf.CommunityMember_Roles, lastUpdateClock uint64) (*CommunityChanges, error) { if !o.IsControlNode() { return nil, ErrNotControlNode } @@ -2397,11 +2397,12 @@ func (o *Community) AddMember(publicKey *ecdsa.PublicKey, roles []protobuf.Commu } if _, ok := o.config.CommunityDescription.Members[memberKey]; !ok { - o.config.CommunityDescription.Members[memberKey] = &protobuf.CommunityMember{Roles: roles} + o.config.CommunityDescription.Members[memberKey] = &protobuf.CommunityMember{Roles: roles, LastUpdateClock: lastUpdateClock} changes.MembersAdded[memberKey] = o.config.CommunityDescription.Members[memberKey] } o.increaseClock() + return changes, nil } @@ -2506,21 +2507,6 @@ func (o *Community) AllowsAllMembersToPinMessage() bool { return o.config.CommunityDescription.AdminSettings != nil && o.config.CommunityDescription.AdminSettings.PinMessageAllMembersEnabled } -func (o *Community) AddMemberWithRevealedAccounts(dbRequest *RequestToJoin, roles []protobuf.CommunityMember_Roles, accounts []*protobuf.RevealedAccount) (*CommunityChanges, error) { - o.mutex.Lock() - defer o.mutex.Unlock() - - if !o.IsControlNode() { - return nil, ErrNotControlNode - } - - changes := o.addMemberWithRevealedAccounts(dbRequest.PublicKey, roles, accounts, dbRequest.Clock) - - o.increaseClock() - - return changes, nil -} - func (o *Community) CreateDeepCopy() *Community { return &Community{ encryptor: o.encryptor, @@ -2753,25 +2739,6 @@ func (o *Community) deleteTokenPermission(permissionID string) (*CommunityChange return changes, nil } -func (o *Community) addMemberWithRevealedAccounts(memberKey string, roles []protobuf.CommunityMember_Roles, accounts []*protobuf.RevealedAccount, clock uint64) *CommunityChanges { - changes := o.emptyCommunityChanges() - - if o.config.CommunityDescription.Members == nil { - o.config.CommunityDescription.Members = make(map[string]*protobuf.CommunityMember) - } - - if _, ok := o.config.CommunityDescription.Members[memberKey]; !ok { - o.config.CommunityDescription.Members[memberKey] = &protobuf.CommunityMember{Roles: roles} - changes.MembersAdded[memberKey] = o.config.CommunityDescription.Members[memberKey] - } - - o.config.CommunityDescription.Members[memberKey].RevealedAccounts = accounts - o.config.CommunityDescription.Members[memberKey].LastUpdateClock = clock - changes.MemberWalletsAdded[memberKey] = o.config.CommunityDescription.Members[memberKey].RevealedAccounts - - return changes -} - func (o *Community) DeclineRequestToJoin(dbRequest *RequestToJoin) (adminEventCreated bool, err error) { o.mutex.Lock() defer o.mutex.Unlock() diff --git a/protocol/communities/community_encryption_key_action_test.go b/protocol/communities/community_encryption_key_action_test.go index de01f1e2d..1b8d19843 100644 --- a/protocol/communities/community_encryption_key_action_test.go +++ b/protocol/communities/community_encryption_key_action_test.go @@ -4,6 +4,7 @@ import ( "crypto/ecdsa" "reflect" "testing" + "time" "github.com/stretchr/testify/suite" @@ -506,12 +507,12 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Members modified := origin.CreateDeepCopy() for _, member := range tc.originMembers { - _, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}) + _, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}, origin.Clock()) s.Require().NoError(err) } for _, member := range tc.modifiedMembers { - _, err := modified.AddMember(member, []protobuf.CommunityMember_Roles{}) + _, err := modified.AddMember(member, []protobuf.CommunityMember_Roles{}, origin.Clock()) s.Require().NoError(err) } @@ -608,7 +609,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Permiss s.Require().NoError(err) } for _, member := range tc.originMembers { - _, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}) + _, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}, origin.Clock()) s.Require().NoError(err) } @@ -617,7 +618,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestCommunityLevelKeyActions_Permiss s.Require().NoError(err) } for _, member := range tc.modifiedMembers { - _, err := modified.AddMember(member, []protobuf.CommunityMember_Roles{}) + _, err := modified.AddMember(member, []protobuf.CommunityMember_Roles{}, origin.Clock()) s.Require().NoError(err) } @@ -759,7 +760,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestChannelLevelKeyActions() { s.Require().NoError(err) } for _, member := range tc.originMembers { - _, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}) + _, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}, origin.Clock()) s.Require().NoError(err) _, err = origin.AddMemberToChat(channelID, member, []protobuf.CommunityMember_Roles{}, protobuf.CommunityMember_CHANNEL_ROLE_POSTER) s.Require().NoError(err) @@ -770,7 +771,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestChannelLevelKeyActions() { s.Require().NoError(err) } for _, member := range tc.modifiedMembers { - _, err := modified.AddMember(member, []protobuf.CommunityMember_Roles{}) + _, err := modified.AddMember(member, []protobuf.CommunityMember_Roles{}, origin.Clock()) s.Require().NoError(err) _, err = modified.AddMemberToChat(channelID, member, []protobuf.CommunityMember_Roles{}, protobuf.CommunityMember_CHANNEL_ROLE_POSTER) s.Require().NoError(err) @@ -832,7 +833,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestNilOrigin() { func (s *CommunityEncryptionKeyActionSuite) TestControlNodeChange() { channelID := "1234" chatID := types.EncodeHex(crypto.CompressPubkey(&s.identity.PublicKey)) + channelID - + clock := uint64(time.Now().Unix()) testCases := []struct { name string permissions []*protobuf.CommunityTokenPermission @@ -874,8 +875,8 @@ func (s *CommunityEncryptionKeyActionSuite) TestControlNodeChange() { CommunityKeyAction: EncryptionKeyAction{ ActionType: EncryptionKeyRekey, Members: map[string]*protobuf.CommunityMember{ - s.member1Key: &protobuf.CommunityMember{}, - s.member2Key: &protobuf.CommunityMember{}, + s.member1Key: &protobuf.CommunityMember{LastUpdateClock: clock}, + s.member2Key: &protobuf.CommunityMember{LastUpdateClock: clock}, }, }, ChannelKeysActions: map[string]EncryptionKeyAction{ @@ -935,8 +936,12 @@ func (s *CommunityEncryptionKeyActionSuite) TestControlNodeChange() { CommunityKeyAction: EncryptionKeyAction{ ActionType: EncryptionKeyRekey, Members: map[string]*protobuf.CommunityMember{ - s.member1Key: &protobuf.CommunityMember{}, - s.member2Key: &protobuf.CommunityMember{}, + s.member1Key: &protobuf.CommunityMember{ + LastUpdateClock: clock, + }, + s.member2Key: &protobuf.CommunityMember{ + LastUpdateClock: clock, + }, }, }, ChannelKeysActions: map[string]EncryptionKeyAction{ @@ -968,7 +973,7 @@ func (s *CommunityEncryptionKeyActionSuite) TestControlNodeChange() { s.Require().NoError(err) } for _, member := range tc.members { - _, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}) + _, err := origin.AddMember(member, []protobuf.CommunityMember_Roles{}, clock) s.Require().NoError(err) } for _, member := range tc.channelMembers { diff --git a/protocol/communities/manager.go b/protocol/communities/manager.go index 1936c4e26..0f1b5a5be 100644 --- a/protocol/communities/manager.go +++ b/protocol/communities/manager.go @@ -2807,7 +2807,7 @@ func (m *Manager) AcceptRequestToJoin(dbRequest *RequestToJoin) (*Community, err memberRoles = []protobuf.CommunityMember_Roles{role} } - _, err = community.AddMember(pk, memberRoles) + _, err = community.AddMember(pk, memberRoles, dbRequest.Clock) if err != nil { return nil, err } @@ -3693,7 +3693,7 @@ func (m *Manager) AddMemberOwnerToCommunity(communityID types.HexBytes, pk *ecds return nil, err } - _, err = community.AddMember(pk, []protobuf.CommunityMember_Roles{protobuf.CommunityMember_ROLE_OWNER}) + _, err = community.AddMember(pk, []protobuf.CommunityMember_Roles{protobuf.CommunityMember_ROLE_OWNER}, community.Clock()) if err != nil { return nil, err } @@ -4677,7 +4677,7 @@ func (m *Manager) promoteSelfToControlNode(community *Community, clock uint64) ( if exists := community.HasMember(&m.identity.PublicKey); !exists { ownerRole := []protobuf.CommunityMember_Roles{protobuf.CommunityMember_ROLE_OWNER} - _, err = community.AddMember(&m.identity.PublicKey, ownerRole) + _, err = community.AddMember(&m.identity.PublicKey, ownerRole, community.Clock()) if err != nil { return false, err } diff --git a/protocol/communities_messenger_test.go b/protocol/communities_messenger_test.go index 82142c22f..97f726e9b 100644 --- a/protocol/communities_messenger_test.go +++ b/protocol/communities_messenger_test.go @@ -2283,7 +2283,7 @@ func (s *MessengerCommunitiesSuite) TestShareCommunityWithPreviousMember() { communityChat := response.Chats()[0] // Add Alice to the community before sharing it - _, err = community.AddMember(&s.alice.identity.PublicKey, []protobuf.CommunityMember_Roles{}) + _, err = community.AddMember(&s.alice.identity.PublicKey, []protobuf.CommunityMember_Roles{}, community.Clock()) s.Require().NoError(err) err = s.bob.communitiesManager.SaveCommunity(community) @@ -4778,14 +4778,14 @@ func (s *MessengerCommunitiesSuite) TestIgnoreOutdatedCommunityDescription() { signer, description1, err := communities.UnwrapCommunityDescriptionMessage(wrappedDescription1) s.Require().NoError(err) - _, err = community.AddMember(&s.alice.identity.PublicKey, []protobuf.CommunityMember_Roles{}) + _, err = community.AddMember(&s.alice.identity.PublicKey, []protobuf.CommunityMember_Roles{}, community.Clock()) s.Require().NoError(err) wrappedDescription2, err := community.ToProtocolMessageBytes() s.Require().NoError(err) _, description2, err := communities.UnwrapCommunityDescriptionMessage(wrappedDescription2) s.Require().NoError(err) - _, err = community.AddMember(&s.bob.identity.PublicKey, []protobuf.CommunityMember_Roles{}) + _, err = community.AddMember(&s.bob.identity.PublicKey, []protobuf.CommunityMember_Roles{}, community.Clock()) s.Require().NoError(err) wrappedDescription3, err := community.ToProtocolMessageBytes() s.Require().NoError(err) diff --git a/protocol/communities_messenger_token_permissions_test.go b/protocol/communities_messenger_token_permissions_test.go index 98d756123..4b491f5aa 100644 --- a/protocol/communities_messenger_token_permissions_test.go +++ b/protocol/communities_messenger_token_permissions_test.go @@ -2186,7 +2186,7 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) TestReevaluateMemberPermissi s.Require().NoError(err) err = s.owner.communitiesManager.SaveRequestToJoinRevealedAddresses(requestId, requestToJoin.RevealedAccounts) s.Require().NoError(err) - _, err = community.AddMember(&privateKey.PublicKey, communityRole) + _, err = community.AddMember(&privateKey.PublicKey, communityRole, requestToJoin.Clock) s.Require().NoError(err) _, err = community.AddMemberToChat(chat.CommunityChatID(), &privateKey.PublicKey, communityRole, protobuf.CommunityMember_CHANNEL_ROLE_POSTER) s.Require().NoError(err) diff --git a/protocol/messenger_unread_test.go b/protocol/messenger_unread_test.go index ce4d08fce..e16bec631 100644 --- a/protocol/messenger_unread_test.go +++ b/protocol/messenger_unread_test.go @@ -303,7 +303,7 @@ func (s *MessengerSuite) TestMarkMessageWithNotificationAsUnreadInCommunityChatS community := response.Communities()[0] communityChat := response.Chats()[0] - _, err = community.AddMember(&s.m.identity.PublicKey, []protobuf.CommunityMember_Roles{}) + _, err = community.AddMember(&s.m.identity.PublicKey, []protobuf.CommunityMember_Roles{}, community.Clock()) s.Require().NoError(err) err = other.communitiesManager.SaveCommunity(community)