Feat: Add 'IsDisplayNameDupeOfCommunityMember' endpoint (#4839)

* feat: Add 'TestScanCommunitiesMembersForName' endpoint

* feat: validate display name amoung community members
This commit is contained in:
Mikhail Rogachev 2024-03-01 15:46:48 +03:00 committed by GitHub
parent 03732ad12b
commit 5d29c20f94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 74 additions and 0 deletions

View File

@ -72,6 +72,10 @@ func (s *MessengerCommunitiesSuite) SetupTest() {
s.Require().NoError(err)
_, err = s.alice.Start()
s.Require().NoError(err)
s.setMessengerDisplayName(s.owner, "Charlie")
s.setMessengerDisplayName(s.bob, "Bobby")
s.setMessengerDisplayName(s.alice, "Alice")
}
func (s *MessengerCommunitiesSuite) TearDownTest() {
@ -97,6 +101,19 @@ func (s *MessengerCommunitiesSuite) newMessenger() *Messenger {
return s.newMessengerWithKey(privateKey)
}
func (s *MessengerCommunitiesSuite) setMessengerDisplayName(m *Messenger, name string) {
profileKp := accounts.GetProfileKeypairForTest(true, false, false)
profileKp.KeyUID = m.account.KeyUID
profileKp.Name = DefaultProfileDisplayName
profileKp.Accounts[0].KeyUID = m.account.KeyUID
err := m.settings.SaveOrUpdateKeypair(profileKp)
s.Require().NoError(err)
err = m.SetDisplayName(name)
s.Require().NoError(err)
}
func (s *MessengerCommunitiesSuite) TestCreateCommunity() {
description := &requests.CreateCommunity{
Membership: protobuf.CommunityPermissions_AUTO_ACCEPT,
@ -4163,3 +4180,22 @@ func (s *MessengerCommunitiesSuite) TestBanUserAndDeleteAllUserMessages() {
s.Require().False(community.Joined())
s.Require().False(community.Spectated())
}
func (s *MessengerCommunitiesSuite) TestIsDisplayNameDupeOfCommunityMember() {
community, _ := s.createCommunity()
advertiseCommunityToUserOldWay(&s.Suite, community, s.owner, s.alice)
s.joinCommunity(community, s.owner, s.alice)
result, err := s.alice.IsDisplayNameDupeOfCommunityMember("Charlie")
s.Require().NoError(err)
s.Require().True(result)
result, err = s.alice.IsDisplayNameDupeOfCommunityMember("Alice")
s.Require().NoError(err)
s.Require().True(result)
result, err = s.alice.IsDisplayNameDupeOfCommunityMember("Bobby")
s.Require().NoError(err)
s.Require().False(result)
}

View File

@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
"time"
@ -599,6 +600,28 @@ func (m *Messenger) JoinedCommunities() ([]*communities.Community, error) {
return m.communitiesManager.Joined()
}
func (m *Messenger) IsDisplayNameDupeOfCommunityMember(name string) (bool, error) {
controlled, err := m.communitiesManager.Controlled()
if err != nil {
return false, err
}
joined, err := m.communitiesManager.Joined()
if err != nil {
return false, err
}
for _, community := range append(controlled, joined...) {
for memberKey := range community.Members() {
contact := m.GetContactByID(memberKey)
if strings.Compare(contact.DisplayName, name) == 0 {
return true, nil
}
}
}
return false, nil
}
func (m *Messenger) CommunityUpdateLastOpenedAt(communityID string) (int64, error) {
id, err := hexutil.Decode(communityID)
if err != nil {

View File

@ -26,6 +26,7 @@ var ErrInvalidDisplayNameEthSuffix = errors.New(`usernames ending with "eth" are
var ErrInvalidDisplayNameNotAllowed = errors.New("name is not allowed")
var ErrInvalidBioLength = errors.New("invalid bio length")
var ErrInvalidSocialLinkTextLength = errors.New("invalid social link text length")
var ErrDisplayNameDupeOfCommunityMember = errors.New("display name duplicates on of community members")
func ValidateDisplayName(displayName *string) error {
name := strings.TrimSpace(*displayName)
@ -66,6 +67,15 @@ func (m *Messenger) SetDisplayName(displayName string) error {
return err
}
isDupe, err := m.IsDisplayNameDupeOfCommunityMember(displayName)
if err != nil {
return err
}
if isDupe {
return ErrDisplayNameDupeOfCommunityMember
}
m.account.Name = displayName
err = m.multiAccounts.UpdateDisplayName(m.account.KeyUID, displayName)
if err != nil {

View File

@ -401,6 +401,11 @@ func (api *PublicAPI) JoinedCommunities(parent context.Context) ([]*communities.
return api.service.messenger.JoinedCommunities()
}
// IsDisplayNameDupeOfCommunityMember returns if any controlled or joined community has a member with provided display name
func (api *PublicAPI) IsDisplayNameDupeOfCommunityMember(name string) (bool, error) {
return api.service.messenger.IsDisplayNameDupeOfCommunityMember(name)
}
// CommunityTags return the list of possible community tags
func (api *PublicAPI) CommunityTags(parent context.Context) map[string]string {
return requests.TagsEmojies