Add community request to response when a response handled
In general, any time a piece of state is updated in the backend, that should be propagated to the client through signals. In this case, when a request was accepted, the client wasn't notified, requiring them to re-fetch the accepted requests and causing inconsistent state between status-go and client.
This commit is contained in:
parent
b6a33089c8
commit
c38ec00a2d
|
@ -1036,19 +1036,20 @@ func (m *Manager) HandleCommunityRequestToJoin(signer *ecdsa.PublicKey, request
|
||||||
return requestToJoin, nil
|
return requestToJoin, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) HandleCommunityRequestToJoinResponse(signer *ecdsa.PublicKey, request *protobuf.CommunityRequestToJoinResponse) error {
|
func (m *Manager) HandleCommunityRequestToJoinResponse(signer *ecdsa.PublicKey, request *protobuf.CommunityRequestToJoinResponse) (*RequestToJoin, error) {
|
||||||
|
pkString := common.PubkeyToHex(&m.identity.PublicKey)
|
||||||
|
|
||||||
community, err := m.persistence.GetByID(&m.identity.PublicKey, request.CommunityId)
|
community, err := m.persistence.GetByID(&m.identity.PublicKey, request.CommunityId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
if community == nil {
|
if community == nil {
|
||||||
return ErrOrgNotFound
|
return nil, ErrOrgNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
communityDescriptionBytes, err := proto.Marshal(request.Community)
|
communityDescriptionBytes, err := proto.Marshal(request.Community)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// We need to wrap `request.Community` in an `ApplicationMetadataMessage`
|
// We need to wrap `request.Community` in an `ApplicationMetadataMessage`
|
||||||
|
@ -1063,23 +1064,33 @@ func (m *Manager) HandleCommunityRequestToJoinResponse(signer *ecdsa.PublicKey,
|
||||||
|
|
||||||
appMetadataMsg, err := proto.Marshal(metadataMessage)
|
appMetadataMsg, err := proto.Marshal(metadataMessage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = community.UpdateCommunityDescription(signer, request.Community, appMetadataMsg)
|
_, err = community.UpdateCommunityDescription(signer, request.Community, appMetadataMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = m.persistence.SaveCommunity(community)
|
err = m.persistence.SaveCommunity(community)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if request.Accepted {
|
if request.Accepted {
|
||||||
return m.markRequestToJoin(&m.identity.PublicKey, community)
|
err = m.markRequestToJoin(&m.identity.PublicKey, community)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
return m.persistence.SetRequestToJoinState(common.PubkeyToHex(&m.identity.PublicKey), community.ID(), RequestToJoinStateDeclined)
|
} else {
|
||||||
|
|
||||||
|
err = m.persistence.SetRequestToJoinState(pkString, community.ID(), RequestToJoinStateDeclined)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return m.persistence.GetRequestToJoinByPkAndCommunityID(pkString, community.ID())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) HandleCommunityRequestToLeave(signer *ecdsa.PublicKey, proto *protobuf.CommunityRequestToLeave) error {
|
func (m *Manager) HandleCommunityRequestToLeave(signer *ecdsa.PublicKey, proto *protobuf.CommunityRequestToLeave) error {
|
||||||
|
|
|
@ -451,6 +451,16 @@ func (p *Persistence) GetRequestToJoin(id []byte) (*RequestToJoin, error) {
|
||||||
return request, nil
|
return request, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Persistence) GetRequestToJoinByPkAndCommunityID(pk string, communityID []byte) (*RequestToJoin, error) {
|
||||||
|
request := &RequestToJoin{}
|
||||||
|
err := p.db.QueryRow(`SELECT id,public_key,clock,ens_name,chat_id,community_id,state FROM communities_requests_to_join WHERE public_key = ? AND community_id = ?`, pk, communityID).Scan(&request.ID, &request.PublicKey, &request.Clock, &request.ENSName, &request.ChatID, &request.CommunityID, &request.State)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return request, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (p *Persistence) GetRequestToJoinIDByPkAndCommunityID(pk string, communityID []byte) ([]byte, error) {
|
func (p *Persistence) GetRequestToJoinIDByPkAndCommunityID(pk string, communityID []byte) ([]byte, error) {
|
||||||
var id []byte
|
var id []byte
|
||||||
err := p.db.QueryRow(`SELECT id FROM communities_requests_to_join WHERE community_id = ? AND public_key = ?`, communityID, pk).Scan(&id)
|
err := p.db.QueryRow(`SELECT id FROM communities_requests_to_join WHERE community_id = ? AND public_key = ?`, communityID, pk).Scan(&id)
|
||||||
|
|
|
@ -1035,8 +1035,10 @@ func (s *MessengerCommunitiesSuite) TestRequestAccess() {
|
||||||
s.Require().NoError(err)
|
s.Require().NoError(err)
|
||||||
s.Require().NotNil(response)
|
s.Require().NotNil(response)
|
||||||
|
|
||||||
s.Require().Len(response.Communities(), 1)
|
s.Require().Len(response.RequestsToJoinCommunity, 1)
|
||||||
|
s.Require().Equal(communities.RequestToJoinStateAccepted, response.RequestsToJoinCommunity[0].State)
|
||||||
|
|
||||||
|
s.Require().Len(response.Communities(), 1)
|
||||||
aliceCommunity := response.Communities()[0]
|
aliceCommunity := response.Communities()[0]
|
||||||
|
|
||||||
s.Require().Equal(community.ID(), aliceCommunity.ID())
|
s.Require().Equal(community.ID(), aliceCommunity.ID())
|
||||||
|
|
|
@ -1221,11 +1221,15 @@ func (m *Messenger) HandleCommunityRequestToJoinResponse(state *ReceivedMessageS
|
||||||
return errors.New("invalid community id")
|
return errors.New("invalid community id")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := m.communitiesManager.HandleCommunityRequestToJoinResponse(signer, &requestToJoinResponseProto)
|
updatedRequest, err := m.communitiesManager.HandleCommunityRequestToJoinResponse(signer, &requestToJoinResponseProto)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if updatedRequest != nil {
|
||||||
|
state.Response.RequestsToJoinCommunity = append(state.Response.RequestsToJoinCommunity, updatedRequest)
|
||||||
|
}
|
||||||
|
|
||||||
if requestToJoinResponseProto.Accepted {
|
if requestToJoinResponseProto.Accepted {
|
||||||
response, err := m.JoinCommunity(context.Background(), requestToJoinResponseProto.CommunityId)
|
response, err := m.JoinCommunity(context.Background(), requestToJoinResponseProto.CommunityId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in New Issue