Add reply message

This commit is contained in:
Andrea Maria Piana 2022-10-28 10:51:54 +01:00
parent 2341dedfba
commit 98f7324ae1
3 changed files with 63 additions and 23 deletions

View File

@ -727,6 +727,23 @@ func (db sqlitePersistence) UpdateActivityCenterNotificationContactVerificationS
}
func (db sqlitePersistence) UpdateActivityCenterNotificationFields(id types.HexBytes, message *common.Message, replyMessage *common.Message, status verification.RequestStatus) error {
encodedMessage, err := json.Marshal(message)
if err != nil {
return err
}
encodedReplyMessage, err := json.Marshal(replyMessage)
if err != nil {
return err
}
_, err = db.db.Exec(`UPDATE activity_center_notifications SET message = ?, reply_message = ?, contact_verification_status = ? WHERE id = ?`, encodedMessage, encodedReplyMessage, status, id)
return err
}
func (db sqlitePersistence) AcceptActivityCenterNotificationsForInvitesFromUser(userPublicKey string) ([]*ActivityCenterNotification, error) {
var tx *sql.Tx
var err error

View File

@ -266,32 +266,31 @@ func (m *Messenger) AcceptContactVerificationRequest(ctx context.Context, id str
VerificationRequests: []*verification.Request{verifRequest},
}
chatMessage, err := m.createLocalContactVerificationMessage(response, chat, rawMessage.ID, common.ContactVerificationStateAccepted)
replyMessage, err := m.createLocalContactVerificationMessage(response, chat, rawMessage.ID, common.ContactVerificationStateAccepted)
if err != nil {
return nil, err
}
err = m.persistence.SaveMessages([]*common.Message{chatMessage})
err = m.persistence.SaveMessages([]*common.Message{replyMessage})
if err != nil {
return nil, err
}
resp.AddMessage(chatMessage)
resp.AddMessage(replyMessage)
if notification != nil {
// TODO: Should we update only the message or only the notification or both?
err := m.persistence.UpdateActivityCenterNotificationContactVerificationStatus(notification.ID, verification.RequestStatusACCEPTED)
if err != nil {
return nil, err
}
notification.ContactVerificationStatus = verification.RequestStatusACCEPTED
message := notification.Message
message.ContactVerificationState = common.ContactVerificationStateAccepted
err = m.persistence.UpdateActivityCenterNotificationMessage(notification.ID, message)
notification.ReplyMessage = replyMessage
err := m.persistence.UpdateActivityCenterNotificationFields(notification.ID, message, replyMessage, verification.RequestStatusACCEPTED)
if err != nil {
return nil, err
}
resp.AddActivityCenterNotification(notification)
}
@ -639,8 +638,7 @@ func (m *Messenger) HandleRequestContactVerification(state *ReceivedMessageState
state.AllVerificationRequests = append(state.AllVerificationRequests, persistedVR)
// TODO: update activity center notification, this only creates a new one
return m.createContactVerificationNotification(contact, state, persistedVR, chatMessage)
return m.createContactVerificationNotification(contact, state, persistedVR, chatMessage, nil)
}
func ValidateAcceptContactVerification(request protobuf.AcceptContactVerification) error {
@ -676,7 +674,6 @@ func (m *Messenger) HandleAcceptContactVerification(state *ReceivedMessageState,
m.logger.Debug("Error obtaining verification request", zap.Error(err))
return err
}
m.logger.Info("PAST 1")
if persistedVR != nil && persistedVR.RepliedAt > request.Clock {
return nil // older message, ignore it
@ -685,10 +682,8 @@ func (m *Messenger) HandleAcceptContactVerification(state *ReceivedMessageState,
if persistedVR.RequestStatus == verification.RequestStatusCANCELED {
return nil // Do nothing, We have already cancelled the verification request
}
m.logger.Info("PAST 2")
if persistedVR == nil {
m.logger.Info("PAST 3")
// This is a response for which we have not received its request before
persistedVR = &verification.Request{}
persistedVR.ID = request.Id
@ -705,15 +700,12 @@ func (m *Messenger) HandleAcceptContactVerification(state *ReceivedMessageState,
m.logger.Debug("Error storing verification request", zap.Error(err))
return err
}
m.logger.Info("PAST 4")
err = m.SyncVerificationRequest(context.Background(), persistedVR)
if err != nil {
return err
}
m.logger.Info("PAST 5")
chat, ok := m.allChats.Load(contactID)
if !ok {
publicKey, err := contact.PublicKey()
@ -734,15 +726,21 @@ func (m *Messenger) HandleAcceptContactVerification(state *ReceivedMessageState,
state.Response.AddMessage(chatMessage)
err = m.createContactVerificationNotification(contact, state, persistedVR, chatMessage)
msg, err := m.persistence.MessageByID(request.Id)
if err != nil {
return err
}
msg.ContactVerificationState = common.ContactVerificationStateAccepted
state.Response.AddMessage(msg)
err = m.createContactVerificationNotification(contact, state, persistedVR, msg, chatMessage)
if err != nil {
return err
}
state.AllVerificationRequests = append(state.AllVerificationRequests, persistedVR)
// TODO: create or update activity center notification
return nil
}
@ -807,7 +805,7 @@ func (m *Messenger) HandleDeclineContactVerification(state *ReceivedMessageState
state.Response.AddMessage(msg)
}
return m.createContactVerificationNotification(contact, state, persistedVR, msg)
return m.createContactVerificationNotification(contact, state, persistedVR, msg, nil)
}
func (m *Messenger) HandleContactVerificationTrusted(state *ReceivedMessageState, request protobuf.ContactVerificationTrusted) error {
@ -898,13 +896,14 @@ func (m *Messenger) GetLatestVerificationRequestFrom(contactID string) (*verific
return m.verificationDatabase.GetLatestVerificationRequestFrom(contactID)
}
func (m *Messenger) createContactVerificationNotification(contact *Contact, messageState *ReceivedMessageState, vr *verification.Request, chatMessage *common.Message) error {
func (m *Messenger) createContactVerificationNotification(contact *Contact, messageState *ReceivedMessageState, vr *verification.Request, chatMessage *common.Message, replyMessage *common.Message) error {
notification := &ActivityCenterNotification{
ID: types.FromHex(vr.ID),
Name: contact.CanonicalName(),
Type: ActivityCenterNotificationTypeContactVerification,
Author: messageState.CurrentMessageState.Contact.ID,
Message: chatMessage,
ReplyMessage: replyMessage,
Timestamp: messageState.CurrentMessageState.WhisperTimestamp,
ChatID: contact.ID,
ContactVerificationStatus: vr.RequestStatus,

View File

@ -178,6 +178,7 @@ func (s *MessengerVerificationRequests) TestAcceptVerificationRequests() {
verificationRequestID := resp.VerificationRequests[0].ID
s.Require().Len(resp.Messages(), 1)
s.Require().NotEmpty(resp.Messages()[0].OutgoingStatus)
s.Require().Equal(challenge, resp.Messages()[0].Text)
s.Require().Equal(common.ContactVerificationStatePending, resp.Messages()[0].ContactVerificationState)
@ -199,6 +200,7 @@ func (s *MessengerVerificationRequests) TestAcceptVerificationRequests() {
s.Require().Equal(challenge, resp.ActivityCenterNotifications()[0].Message.Text)
s.Require().Equal(common.ContactVerificationStatePending, resp.ActivityCenterNotifications()[0].Message.ContactVerificationState)
s.Require().Len(resp.Messages(), 1)
s.Require().Empty(resp.Messages()[0].OutgoingStatus)
s.Require().Equal(challenge, resp.Messages()[0].Text)
s.Require().Equal(common.ContactVerificationStatePending, resp.Messages()[0].ContactVerificationState)
@ -220,6 +222,10 @@ func (s *MessengerVerificationRequests) TestAcceptVerificationRequests() {
s.Require().Len(resp.Messages(), 1)
s.Require().Equal(common.ContactVerificationStateAccepted, resp.Messages()[0].ContactVerificationState)
s.Require().NotNil(resp.ActivityCenterNotifications()[0].ReplyMessage)
s.Require().NotEmpty(resp.ActivityCenterNotifications()[0].ReplyMessage.OutgoingStatus)
s.Require().Equal("hello back", resp.ActivityCenterNotifications()[0].ReplyMessage.Text)
// Wait for the message to reach its destination
resp, err = WaitOnMessengerResponse(
s.m,
@ -232,13 +238,31 @@ func (s *MessengerVerificationRequests) TestAcceptVerificationRequests() {
s.Require().Len(resp.VerificationRequests, 1)
s.Require().Equal(resp.VerificationRequests[0].ID, verificationRequestID)
s.Require().Len(resp.Messages(), 1)
s.Require().Equal(common.ContactVerificationStateAccepted, resp.Messages()[0].ContactVerificationState)
messages := resp.Messages()
s.Require().Len(messages, 2)
var originalMessage *common.Message
var replyMessage *common.Message
if messages[0].ID == resp.VerificationRequests[0].ID {
originalMessage = messages[0]
replyMessage = messages[1]
} else {
originalMessage = messages[1]
replyMessage = messages[0]
}
s.Require().Equal(common.ContactVerificationStateAccepted, originalMessage.ContactVerificationState)
// Not sure this is the right state for this, it should probably be pendingtrustedverification or something
s.Require().Equal(common.ContactVerificationStateAccepted, replyMessage.ContactVerificationState)
s.Require().Len(resp.ActivityCenterNotifications(), 1)
s.Require().Equal(resp.ActivityCenterNotifications()[0].ID.String(), verificationRequestID)
s.Require().Equal(resp.ActivityCenterNotifications()[0].ContactVerificationStatus, verification.RequestStatusACCEPTED)
s.Require().Equal(common.ContactVerificationStateAccepted, resp.ActivityCenterNotifications()[0].Message.ContactVerificationState)
s.Require().NotNil(resp.ActivityCenterNotifications()[0].ReplyMessage)
s.Require().Empty(resp.ActivityCenterNotifications()[0].ReplyMessage.OutgoingStatus)
s.Require().Equal("hello back", resp.ActivityCenterNotifications()[0].ReplyMessage.Text)
}
func (s *MessengerVerificationRequests) TestDeclineVerificationRequests() {