Feat: add endpoint for getting latest pending CR for a contact (#4819)

This commit is contained in:
Mikhail Rogachev 2024-02-28 23:00:35 +03:00 committed by GitHub
parent 3f98a34eae
commit 577db512c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 0 deletions

View File

@ -149,6 +149,13 @@ func (s *MessengerContactRequestSuite) receiveContactRequest(messageText string,
s.Require().Greater(len(contactRequests), 0) s.Require().Greater(len(contactRequests), 0)
s.Require().Equal(contactRequests[0].ID, contactRequest.ID) s.Require().Equal(contactRequests[0].ID, contactRequest.ID)
// Confirm latest pending contact request
resp, err = theirMessenger.GetLatestContactRequestForContact(contactRequest.From)
s.Require().NoError(err)
s.Require().Len(resp.Messages(), 1)
s.Require().Equal(contactRequest.ID, resp.Messages()[0].ID)
s.Require().Equal(common.ContactRequestStatePending, resp.Messages()[0].ContactRequestState)
return contactRequest return contactRequest
} }
@ -243,6 +250,13 @@ func (s *MessengerContactRequestSuite) acceptContactRequest(
mutualContacts := receiver.MutualContacts() mutualContacts := receiver.MutualContacts()
s.Require().Len(mutualContacts, 1) s.Require().Len(mutualContacts, 1)
// Confirm latest pending contact request
resp, err = receiver.GetLatestContactRequestForContact(sender.IdentityPublicKeyString())
s.Require().NoError(err)
s.Require().Len(resp.Messages(), 1)
s.Require().Equal(contactRequest.ID, resp.Messages()[0].ID)
s.Require().Equal(common.ContactRequestStateAccepted, resp.Messages()[0].ContactRequestState)
// Wait for the message to reach its destination // Wait for the message to reach its destination
resp, err = WaitOnMessengerResponse(sender, resp, err = WaitOnMessengerResponse(sender,
func(r *MessengerResponse) bool { func(r *MessengerResponse) bool {

View File

@ -27,6 +27,8 @@ const incomingMutualStateEventSentDefaultText = "@%s sent you a contact request"
const incomingMutualStateEventAcceptedDefaultText = "@%s accepted your contact request" const incomingMutualStateEventAcceptedDefaultText = "@%s accepted your contact request"
const incomingMutualStateEventRemovedDefaultText = "@%s removed you as a contact" const incomingMutualStateEventRemovedDefaultText = "@%s removed you as a contact"
var ErrGetLatestContactRequestForContactInvalidID = errors.New("get-latest-contact-request-for-contact: invalid id")
type SelfContactChangeEvent struct { type SelfContactChangeEvent struct {
DisplayNameChanged bool DisplayNameChanged bool
PreferredNameChanged bool PreferredNameChanged bool
@ -1161,6 +1163,28 @@ func (m *Messenger) sendRetractContactRequest(contact *Contact) error {
return err return err
} }
func (m *Messenger) GetLatestContactRequestForContact(contactID string) (*MessengerResponse, error) {
if len(contactID) == 0 {
return nil, ErrGetLatestContactRequestForContactInvalidID
}
contactRequestID, err := m.persistence.LatestPendingContactRequestIDForContact(contactID)
if err != nil {
return nil, err
}
contactRequest, err := m.persistence.MessageByID(contactRequestID)
if err != nil {
m.logger.Error("contact request not found", zap.String("contactRequestID", contactRequestID), zap.Error(err))
return nil, err
}
response := &MessengerResponse{}
response.AddMessage(contactRequest)
return response, nil
}
func (m *Messenger) AcceptLatestContactRequestForContact(ctx context.Context, request *requests.AcceptLatestContactRequestForContact) (*MessengerResponse, error) { func (m *Messenger) AcceptLatestContactRequestForContact(ctx context.Context, request *requests.AcceptLatestContactRequestForContact) (*MessengerResponse, error) {
if err := request.Validate(); err != nil { if err := request.Validate(); err != nil {
return nil, err return nil, err

View File

@ -872,6 +872,10 @@ func (api *PublicAPI) DismissLatestContactRequestForContact(ctx context.Context,
return api.service.messenger.DismissLatestContactRequestForContact(ctx, request) return api.service.messenger.DismissLatestContactRequestForContact(ctx, request)
} }
func (api *PublicAPI) GetLatestContactRequestForContact(ctx context.Context, contactID string) (*protocol.MessengerResponse, error) {
return api.service.messenger.GetLatestContactRequestForContact(contactID)
}
func (api *PublicAPI) RetractContactRequest(ctx context.Context, request *requests.RetractContactRequest) (*protocol.MessengerResponse, error) { func (api *PublicAPI) RetractContactRequest(ctx context.Context, request *requests.RetractContactRequest) (*protocol.MessengerResponse, error) {
return api.service.messenger.RetractContactRequest(request) return api.service.messenger.RetractContactRequest(request)
} }