Skip to content

Commit

Permalink
fix message editing. fix tests. address pr comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin committed Oct 2, 2023
1 parent 69545bc commit b463154
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 16 deletions.
2 changes: 1 addition & 1 deletion protocol/common/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -844,8 +844,8 @@ func isValidLinkPreviewForProto(preview LinkPreview) error {
if err := isValidLinkPreviewThumbnail(preview.Thumbnail); err != nil {
return fmt.Errorf("thumbnail is not valid for proto: %w", err)
}
return nil
}
return fmt.Errorf("uncaught link preview type")
}

func (preview *StatusCommunityLinkPreview) isValidForProto() error {
Expand Down
11 changes: 6 additions & 5 deletions protocol/linkpreview_unfurler_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,17 @@ func buildThumbnail(image *images.IdentityImage, thumbnail *common.LinkPreviewTh
}

func (u *StatusUnfurler) buildContactData(contactData *ContactURLData) (*common.StatusContactLinkPreview, error) {
c := new(common.StatusContactLinkPreview)
c.PublicKey = contactData.PublicKey
c.DisplayName = contactData.DisplayName
c.Description = contactData.Description

contactID, err := multiformat.DeserializeCompressedKey(contactData.PublicKey)
if err != nil {
return nil, err
}

c := new(common.StatusContactLinkPreview)
c.PublicKey = contactData.PublicKey
c.DisplayName = contactData.DisplayName
c.Description = contactData.Description

contact := u.m.GetContactByID(contactID)
if contact == nil {
return c, nil
Expand Down Expand Up @@ -109,7 +110,7 @@ func (u *StatusUnfurler) buildCommunityData(data *CommunityURLData) (*common.Sta
// Now check if there's newer information in the database
communityID, err := types.DecodeHex(data.CommunityID)
if err != nil {
return c, fmt.Errorf("failed to decode community id: %w", err)
return nil, fmt.Errorf("failed to decode community id: %w", err)
}

community, err := u.m.GetCommunityByID(communityID)
Expand Down
4 changes: 2 additions & 2 deletions protocol/message_persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -2490,7 +2490,7 @@ func (db sqlitePersistence) SaveEdit(editMessage *EditMessage) error {
return nil
}

_, err := db.db.Exec(`INSERT INTO user_messages_edits (clock, chat_id, message_id, text, source, id, unfurled_links, unfurled_status_links) VALUES(?,?,?,?,?,?,?,?)`, editMessage.Clock, editMessage.ChatId, editMessage.MessageId, editMessage.Text, editMessage.From, editMessage.ID, pq.Array(editMessage.UnfurledLinks), pq.Array(editMessage.UnfurledStatusLinks))
_, err := db.db.Exec(`INSERT INTO user_messages_edits (clock, chat_id, message_id, text, source, id, unfurled_links, unfurled_status_links) VALUES(?,?,?,?,?,?,?,?)`, editMessage.Clock, editMessage.ChatId, editMessage.MessageId, editMessage.Text, editMessage.From, editMessage.ID, pq.Array(editMessage.UnfurledLinks), editMessage.UnfurledStatusLinks)
return err
}

Expand All @@ -2504,7 +2504,7 @@ func (db sqlitePersistence) GetEdits(messageID string, from string) ([]*EditMess
var messages []*EditMessage
for rows.Next() {
e := NewEditMessage()
err := rows.Scan(&e.Clock, &e.ChatId, &e.MessageId, &e.From, &e.Text, &e.ID, pq.Array(&e.UnfurledLinks), pq.Array(&e.UnfurledStatusLinks))
err := rows.Scan(&e.Clock, &e.ChatId, &e.MessageId, &e.From, &e.Text, &e.ID, pq.Array(&e.UnfurledLinks), &e.UnfurledStatusLinks)
if err != nil {
return nil, err
}
Expand Down
29 changes: 25 additions & 4 deletions protocol/messenger_edit_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,13 +498,30 @@ func (s *MessengerEditMessageSuite) TestEditMessageWithLinkPreviews() {
DataURI: "data:image/png;base64,iVBORw0KGgoAAAANSUg=",
}},
},
StatusLinkPreviews: []common.StatusLinkPreview{
{
URL: "https://status.app/u/TestUrl",
Contact: &common.StatusContactLinkPreview{
PublicKey: "TestPublicKey",
DisplayName: "TestDisplayName",
Description: "Test description",
Icon: common.LinkPreviewThumbnail{
Width: 100,
Height: 200,
DataURI: "data:image/png;base64,iVBORw0KGgoAAAANSUg=",
},
},
},
},
}

sendResponse, err = theirMessenger.EditMessage(context.Background(), editedMessage)

s.Require().NoError(err)
s.Require().Len(sendResponse.Messages(), 1)
s.Require().NotEmpty(sendResponse.Messages()[0].LinkPreviews)
s.Require().Len(sendResponse.Messages()[0].LinkPreviews, 1)
s.Require().NotNil(sendResponse.Messages()[0].UnfurledStatusLinks)
s.Require().Len(sendResponse.Messages()[0].UnfurledStatusLinks.UnfurledStatusLinks, 1)
response, err = WaitOnMessengerResponse(
s.m,
func(r *MessengerResponse) bool { return len(r.messages) == 1 },
Expand All @@ -514,7 +531,11 @@ func (s *MessengerEditMessageSuite) TestEditMessageWithLinkPreviews() {

s.Require().Len(response.Chats(), 1)
s.Require().Len(response.Messages(), 1)
s.Require().NotEmpty(response.Messages()[0].EditedAt)
s.Require().NotEmpty(response.Messages()[0].UnfurledLinks)
s.Require().False(response.Messages()[0].New)

responseMessage := response.Messages()[0]
s.Require().NotEmpty(responseMessage.EditedAt)
s.Require().Len(responseMessage.UnfurledLinks, 1)
s.Require().NotNil(responseMessage.UnfurledStatusLinks)
s.Require().Len(responseMessage.UnfurledStatusLinks.UnfurledStatusLinks, 1)
s.Require().False(responseMessage.New)
}
6 changes: 6 additions & 0 deletions protocol/messenger_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ func (m *Messenger) EditMessage(ctx context.Context, request *requests.EditMessa
if len(request.LinkPreviews) > 0 {
message.LinkPreviews = request.LinkPreviews
}
if len(request.StatusLinkPreviews) > 0 {
message.StatusLinkPreviews = request.StatusLinkPreviews
}

clock, _ := chat.NextClockAndTimestamp(m.getTimesource())

Expand All @@ -65,6 +68,7 @@ func (m *Messenger) EditMessage(ctx context.Context, request *requests.EditMessa
editMessage.ChatId = message.ChatId
editMessage.MessageId = message.ID
editMessage.Clock = clock

unfurledLinks, err := message.ConvertLinkPreviewsToProto()
if err != nil {
return nil, err
Expand Down Expand Up @@ -352,6 +356,7 @@ func (m *Messenger) applyEditMessage(editMessage *protobuf.EditMessage, message
message.Text = editMessage.Text
message.EditedAt = editMessage.Clock
message.UnfurledLinks = editMessage.UnfurledLinks
message.UnfurledStatusLinks = editMessage.UnfurledStatusLinks
if editMessage.ContentType != protobuf.ChatMessage_UNKNOWN_CONTENT_TYPE {
message.ContentType = editMessage.ContentType
}
Expand All @@ -366,6 +371,7 @@ func (m *Messenger) applyEditMessage(editMessage *protobuf.EditMessage, message
originalEdit.ContentType = message.ContentType
originalEdit.From = message.From
originalEdit.UnfurledLinks = message.UnfurledLinks
originalEdit.UnfurledStatusLinks = message.UnfurledStatusLinks
err := m.persistence.SaveEdit(originalEdit)
if err != nil {
return err
Expand Down
9 changes: 5 additions & 4 deletions protocol/requests/edit_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ var ErrEditMessageInvalidID = errors.New("edit-message: invalid id")
var ErrEditMessageInvalidText = errors.New("edit-message: invalid text")

type EditMessage struct {
ID types.HexBytes `json:"id"`
Text string `json:"text"`
ContentType protobuf.ChatMessage_ContentType `json:"content-type"`
LinkPreviews []common.LinkPreview `json:"linkPreviews"`
ID types.HexBytes `json:"id"`
Text string `json:"text"`
ContentType protobuf.ChatMessage_ContentType `json:"content-type"`
LinkPreviews []common.LinkPreview `json:"linkPreviews"`
StatusLinkPreviews []common.StatusLinkPreview `json:"statusLinkPreviews"`
}

func (e *EditMessage) Validate() error {
Expand Down

0 comments on commit b463154

Please sign in to comment.