chore: API for getting all non-approved requests to join for all communities (#4422)

This commit is contained in:
Mykhailo Prakhov 2023-12-05 15:50:45 +01:00 committed by GitHub
parent e28eca1c54
commit 04c533b8d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 18 additions and 132 deletions

View File

@ -265,7 +265,6 @@ func (o *Community) MarshalJSON() ([]byte, error) {
PubsubTopic string `json:"pubsubTopic"` PubsubTopic string `json:"pubsubTopic"`
PubsubTopicKey string `json:"pubsubTopicKey"` PubsubTopicKey string `json:"pubsubTopicKey"`
Shard *shard.Shard `json:"shard"` Shard *shard.Shard `json:"shard"`
RequestsToJoinCommunity []*RequestToJoin `json:"requestsToJoinCommunity"`
}{ }{
ID: o.ID(), ID: o.ID(),
MemberRole: o.MemberRole(o.MemberIdentity()), MemberRole: o.MemberRole(o.MemberIdentity()),
@ -288,7 +287,6 @@ func (o *Community) MarshalJSON() ([]byte, error) {
PubsubTopic: o.PubsubTopic(), PubsubTopic: o.PubsubTopic(),
PubsubTopicKey: o.PubsubTopicKey(), PubsubTopicKey: o.PubsubTopicKey(),
Shard: o.Shard(), Shard: o.Shard(),
RequestsToJoinCommunity: []*RequestToJoin{},
} }
if o.config.CommunityDescription != nil { if o.config.CommunityDescription != nil {
for id, c := range o.config.CommunityDescription.Categories { for id, c := range o.config.CommunityDescription.Categories {
@ -327,7 +325,6 @@ func (o *Community) MarshalJSON() ([]byte, error) {
communityItem.OutroMessage = o.config.CommunityDescription.OutroMessage communityItem.OutroMessage = o.config.CommunityDescription.OutroMessage
communityItem.CommunityTokensMetadata = o.config.CommunityDescription.CommunityTokensMetadata communityItem.CommunityTokensMetadata = o.config.CommunityDescription.CommunityTokensMetadata
communityItem.ActiveMembersCount = o.config.CommunityDescription.ActiveMembersCount communityItem.ActiveMembersCount = o.config.CommunityDescription.ActiveMembersCount
communityItem.RequestsToJoinCommunity = o.config.RequestsToJoin
if o.config.CommunityDescription.Identity != nil { if o.config.CommunityDescription.Identity != nil {
communityItem.Name = o.Name() communityItem.Name = o.Name()

View File

@ -622,21 +622,7 @@ func (m *Manager) publish(subscription *Subscription) {
} }
func (m *Manager) All() ([]*Community, error) { func (m *Manager) All() ([]*Community, error) {
communities, err := m.persistence.AllCommunities(&m.identity.PublicKey) return m.persistence.AllCommunities(&m.identity.PublicKey)
if err != nil {
return nil, err
}
nonApprovedRequestsToJoin, err := m.persistence.AllNonApprovedCommunitiesRequestsToJoin()
if err != nil {
return nil, err
}
for _, community := range communities {
community.config.RequestsToJoin = nonApprovedRequestsToJoin[community.IDString()]
}
return communities, nil
} }
type CommunityShard struct { type CommunityShard struct {
@ -3297,10 +3283,6 @@ func (m *Manager) PendingRequestsToJoinForUser(pk *ecdsa.PublicKey) ([]*RequestT
return m.persistence.RequestsToJoinForUserByState(common.PubkeyToHex(pk), RequestToJoinStatePending) return m.persistence.RequestsToJoinForUserByState(common.PubkeyToHex(pk), RequestToJoinStatePending)
} }
func (m *Manager) AwaitingAddressesRequestsToJoinForUser(pk *ecdsa.PublicKey) ([]*RequestToJoin, error) {
return m.persistence.RequestsToJoinForUserByState(common.PubkeyToHex(pk), RequestToJoinStateAwaitingAddresses)
}
func (m *Manager) PendingRequestsToJoinForCommunity(id types.HexBytes) ([]*RequestToJoin, error) { func (m *Manager) PendingRequestsToJoinForCommunity(id types.HexBytes) ([]*RequestToJoin, error) {
m.logger.Info("fetching pending invitations", zap.String("community-id", id.String())) m.logger.Info("fetching pending invitations", zap.String("community-id", id.String()))
return m.persistence.PendingRequestsToJoinForCommunity(id) return m.persistence.PendingRequestsToJoinForCommunity(id)
@ -3327,7 +3309,11 @@ func (m *Manager) AcceptedPendingRequestsToJoinForCommunity(id types.HexBytes) (
func (m *Manager) DeclinedPendingRequestsToJoinForCommunity(id types.HexBytes) ([]*RequestToJoin, error) { func (m *Manager) DeclinedPendingRequestsToJoinForCommunity(id types.HexBytes) ([]*RequestToJoin, error) {
return m.persistence.DeclinedPendingRequestsToJoinForCommunity(id) return m.persistence.DeclinedPendingRequestsToJoinForCommunity(id)
}
func (m *Manager) AllNonApprovedCommunitiesRequestsToJoin() ([]*RequestToJoin, error) {
m.logger.Info("fetching all non-approved invitations for all communities")
return m.persistence.AllNonApprovedCommunitiesRequestsToJoin()
} }
func (m *Manager) RequestsToJoinForCommunityAwaitingAddresses(id types.HexBytes) ([]*RequestToJoin, error) { func (m *Manager) RequestsToJoinForCommunityAwaitingAddresses(id types.HexBytes) ([]*RequestToJoin, error) {

View File

@ -1894,64 +1894,3 @@ func (s *ManagerSuite) TestCommunityQueueMultipleDifferentSignersIgnoreIfNotRetu
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Equal(clock1, fetchedCommunity.config.CommunityDescription.Clock) s.Require().Equal(clock1, fetchedCommunity.config.CommunityDescription.Clock)
} }
func (s *ManagerSuite) TestGetAllCommunities() {
// default community
communities, err := s.manager.All()
s.Require().NoError(err)
s.Require().Len(communities, 1)
request := &requests.CreateCommunity{
Name: "status",
Description: "token membership description",
Membership: protobuf.CommunityPermissions_AUTO_ACCEPT,
}
community, err := s.manager.CreateCommunity(request, true)
s.Require().NoError(err)
s.Require().NotNil(community)
communities, err = s.manager.All()
s.Require().NoError(err)
s.Require().Len(communities, 2)
// add requests to join to the community
allStates := []RequestToJoinState{
RequestToJoinStatePending,
RequestToJoinStateDeclined,
RequestToJoinStateAccepted,
RequestToJoinStateCanceled,
RequestToJoinStateAcceptedPending,
RequestToJoinStateDeclinedPending,
RequestToJoinStateAwaitingAddresses,
}
clock := uint64(time.Now().Unix())
for i := range allStates {
identity, err := crypto.GenerateKey()
s.Require().NoError(err)
rtj := &RequestToJoin{
ID: types.HexBytes{1, 2, 3, 4, 5, 6, 7, byte(i)},
PublicKey: common.PubkeyToHex(&identity.PublicKey),
Clock: clock,
CommunityID: community.ID(),
State: allStates[i],
}
err = s.manager.SaveRequestToJoin(rtj)
s.Require().NoError(err)
}
communities, err = s.manager.All()
s.Require().NoError(err)
s.Require().Len(communities, 2)
for _, resultCommunity := range communities {
if resultCommunity.IDString() == community.IDString() {
s.Require().Len(resultCommunity.RequestsToJoin(), 6)
} else {
s.Require().Len(resultCommunity.RequestsToJoin(), 0)
}
}
}

View File

@ -1647,8 +1647,8 @@ func (p *Persistence) SetCuratedCommunities(communities *CuratedCommunities) err
return nil return nil
} }
func (p *Persistence) AllNonApprovedCommunitiesRequestsToJoin() (map[string][]*RequestToJoin, error) { func (p *Persistence) AllNonApprovedCommunitiesRequestsToJoin() ([]*RequestToJoin, error) {
nonApprovedRequestsToJoin := make(map[string][]*RequestToJoin) nonApprovedRequestsToJoin := []*RequestToJoin{}
rows, err := p.db.Query(`SELECT id,public_key,clock,ens_name,chat_id,community_id,state FROM communities_requests_to_join WHERE state != ?`, RequestToJoinStateAccepted) rows, err := p.db.Query(`SELECT id,public_key,clock,ens_name,chat_id,community_id,state FROM communities_requests_to_join WHERE state != ?`, RequestToJoinStateAccepted)
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
@ -1665,9 +1665,7 @@ func (p *Persistence) AllNonApprovedCommunitiesRequestsToJoin() (map[string][]*R
if err != nil { if err != nil {
return nil, err return nil, err
} }
nonApprovedRequestsToJoin = append(nonApprovedRequestsToJoin, request)
communityID := types.EncodeHex(request.CommunityID)
nonApprovedRequestsToJoin[communityID] = append(nonApprovedRequestsToJoin[communityID], request)
} }
return nonApprovedRequestsToJoin, nil return nonApprovedRequestsToJoin, nil
} }

View File

@ -725,7 +725,6 @@ func (s *PersistenceSuite) TestAllNonApprovedCommunitiesRequestsToJoin() {
// add a new community // add a new community
community := s.makeNewCommunity(identity) community := s.makeNewCommunity(identity)
err = s.db.SaveCommunity(community)
s.Require().NoError(err) s.Require().NoError(err)
// add requests to join to the community // add requests to join to the community
@ -756,6 +755,5 @@ func (s *PersistenceSuite) TestAllNonApprovedCommunitiesRequestsToJoin() {
result, err = s.db.AllNonApprovedCommunitiesRequestsToJoin() result, err = s.db.AllNonApprovedCommunitiesRequestsToJoin()
s.Require().NoError(err) s.Require().NoError(err)
s.Require().Len(result, 1) s.Require().Len(result, 6) // all except RequestToJoinStateAccepted
s.Require().Len(result[community.IDString()], 6) // all except RequestToJoinStateAccepted
} }

View File

@ -2359,41 +2359,10 @@ func (m *Messenger) MyPendingRequestsToJoin() ([]*communities.RequestToJoin, err
return m.communitiesManager.PendingRequestsToJoinForUser(&m.identity.PublicKey) return m.communitiesManager.PendingRequestsToJoinForUser(&m.identity.PublicKey)
} }
func (m *Messenger) MyAwaitingAddressesRequestsToJoin() ([]*communities.RequestToJoin, error) {
return m.communitiesManager.AwaitingAddressesRequestsToJoinForUser(&m.identity.PublicKey)
}
func (m *Messenger) PendingRequestsToJoinForCommunity(id types.HexBytes) ([]*communities.RequestToJoin, error) { func (m *Messenger) PendingRequestsToJoinForCommunity(id types.HexBytes) ([]*communities.RequestToJoin, error) {
return m.communitiesManager.PendingRequestsToJoinForCommunity(id) return m.communitiesManager.PendingRequestsToJoinForCommunity(id)
} }
func (m *Messenger) AllPendingRequestsToJoinForCommunity(id types.HexBytes) ([]*communities.RequestToJoin, error) {
// TODO: optimize and extract via one query
pendingRequests, err := m.communitiesManager.PendingRequestsToJoinForCommunity(id)
if err != nil {
return nil, err
}
acceptedPendingRequests, err := m.communitiesManager.AcceptedPendingRequestsToJoinForCommunity(id)
if err != nil {
return nil, err
}
declinedPendingRequests, err := m.communitiesManager.DeclinedPendingRequestsToJoinForCommunity(id)
if err != nil {
return nil, err
}
ownershipChangedRequests, err := m.communitiesManager.RequestsToJoinForCommunityAwaitingAddresses(id)
if err != nil {
return nil, err
}
pendingRequests = append(pendingRequests, acceptedPendingRequests...)
pendingRequests = append(pendingRequests, declinedPendingRequests...)
pendingRequests = append(pendingRequests, ownershipChangedRequests...)
return pendingRequests, nil
}
func (m *Messenger) DeclinedRequestsToJoinForCommunity(id types.HexBytes) ([]*communities.RequestToJoin, error) { func (m *Messenger) DeclinedRequestsToJoinForCommunity(id types.HexBytes) ([]*communities.RequestToJoin, error) {
return m.communitiesManager.DeclinedRequestsToJoinForCommunity(id) return m.communitiesManager.DeclinedRequestsToJoinForCommunity(id)
} }
@ -2414,6 +2383,10 @@ func (m *Messenger) DeclinedPendingRequestsToJoinForCommunity(id types.HexBytes)
return m.communitiesManager.DeclinedPendingRequestsToJoinForCommunity(id) return m.communitiesManager.DeclinedPendingRequestsToJoinForCommunity(id)
} }
func (m *Messenger) AllNonApprovedCommunitiesRequestsToJoin() ([]*communities.RequestToJoin, error) {
return m.communitiesManager.AllNonApprovedCommunitiesRequestsToJoin()
}
func (m *Messenger) RemoveUserFromCommunity(id types.HexBytes, pkString string) (*MessengerResponse, error) { func (m *Messenger) RemoveUserFromCommunity(id types.HexBytes, pkString string) (*MessengerResponse, error) {
publicKey, err := common.HexToPubkey(pkString) publicKey, err := common.HexToPubkey(pkString)
if err != nil { if err != nil {

View File

@ -558,11 +558,6 @@ func (api *PublicAPI) MyPendingRequestsToJoin() ([]*communities.RequestToJoin, e
return api.service.messenger.MyPendingRequestsToJoin() return api.service.messenger.MyPendingRequestsToJoin()
} }
// MyAwaitingAddressesRequestsToJoin returns requests to join, which must be auto-accepted when control node will be online
func (api *PublicAPI) MyAwaitingAddressesRequestsToJoin() ([]*communities.RequestToJoin, error) {
return api.service.messenger.MyAwaitingAddressesRequestsToJoin()
}
// MyCanceledRequestsToJoin returns the pending requests for the logged in user // MyCanceledRequestsToJoin returns the pending requests for the logged in user
func (api *PublicAPI) MyCanceledRequestsToJoin() ([]*communities.RequestToJoin, error) { func (api *PublicAPI) MyCanceledRequestsToJoin() ([]*communities.RequestToJoin, error) {
return api.service.messenger.MyCanceledRequestsToJoin() return api.service.messenger.MyCanceledRequestsToJoin()
@ -573,11 +568,6 @@ func (api *PublicAPI) PendingRequestsToJoinForCommunity(id types.HexBytes) ([]*c
return api.service.messenger.PendingRequestsToJoinForCommunity(id) return api.service.messenger.PendingRequestsToJoinForCommunity(id)
} }
// AllPendingRequestsToJoinForCommunity returns the all the pending requests to join, including accepted and rejected ones, for a given community
func (api *PublicAPI) AllPendingRequestsToJoinForCommunity(id types.HexBytes) ([]*communities.RequestToJoin, error) {
return api.service.messenger.AllPendingRequestsToJoinForCommunity(id)
}
// DeclinedRequestsToJoinForCommunity returns the declined requests to join for a given community // DeclinedRequestsToJoinForCommunity returns the declined requests to join for a given community
func (api *PublicAPI) DeclinedRequestsToJoinForCommunity(id types.HexBytes) ([]*communities.RequestToJoin, error) { func (api *PublicAPI) DeclinedRequestsToJoinForCommunity(id types.HexBytes) ([]*communities.RequestToJoin, error) {
return api.service.messenger.DeclinedRequestsToJoinForCommunity(id) return api.service.messenger.DeclinedRequestsToJoinForCommunity(id)
@ -588,6 +578,11 @@ func (api *PublicAPI) CanceledRequestsToJoinForCommunity(id types.HexBytes) ([]*
return api.service.messenger.CanceledRequestsToJoinForCommunity(id) return api.service.messenger.CanceledRequestsToJoinForCommunity(id)
} }
// AllNonApprovedCommunitiesRequestsToJoin returns the all non-approved requests to join for all communities
func (api *PublicAPI) AllNonApprovedCommunitiesRequestsToJoin() ([]*communities.RequestToJoin, error) {
return api.service.messenger.AllNonApprovedCommunitiesRequestsToJoin()
}
// Generates a single hash for each address that needs to be revealed to a community. // Generates a single hash for each address that needs to be revealed to a community.
// Each hash needs to be signed. // Each hash needs to be signed.
// The order of retuned hashes corresponds to the order of addresses in addressesToReveal. // The order of retuned hashes corresponds to the order of addresses in addressesToReveal.