Skip to content

Commit

Permalink
fix_: ensure generated identity-images have a valid clock value (#6239)
Browse files Browse the repository at this point in the history
This changes updates the logic for generating identity-images to populate the `clock` value instead of initialising with zero. This change also updates the identity-image url format functions to include the `clock` value in the returned url so clients can rely on that url for triggering refetches of identity images when they've been updated.
  • Loading branch information
seanstrom authored Jan 18, 2025
1 parent bb7b1f2 commit e6738e5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions images/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package images
import (
"bytes"
"image"
"time"
)

func GenerateImageVariants(cImg image.Image) ([]IdentityImage, error) {
Expand All @@ -25,6 +26,7 @@ func GenerateImageVariants(cImg image.Image) ([]IdentityImage, error) {
Height: rImg.Bounds().Dy(),
FileSize: bb.Len(),
ResizeTarget: int(s),
Clock: uint64(time.Now().UnixMilli()),
}

iis = append(iis, ii)
Expand Down
24 changes: 24 additions & 0 deletions images/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,27 @@ func TestGenerateBannerImage_ShrinkOnly(t *testing.T) {
require.Exactly(t, identityImage.Width, int(BannerDim))
require.Exactly(t, identityImage.Height, 805)
}

func TestGenerateIdentityImages(t *testing.T) {
// Create image data
testImage := image.NewRGBA(image.Rect(0, 0, int(BannerDim)+10, int(BannerDim)+20))

// Create a temporary file for storing the image data
tmpTestFilePath := t.TempDir() + "/test.png"
file, err := os.Create(tmpTestFilePath)
require.NoError(t, err)
defer file.Close()

// Save the image data to the temporary file
err = png.Encode(file, testImage)
require.NoError(t, err)

// Generate the identity images
identityImages, err := GenerateIdentityImages(tmpTestFilePath, 100, 100, 500, 500)
require.NoError(t, err)

// Check the identity images have a valid clock value
for _, image := range identityImages {
require.NotEqual(t, image.Clock, uint64(0))
}
}
2 changes: 1 addition & 1 deletion protocol/messenger_contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,7 @@ func (m *Messenger) updateContactImagesURL(contact *Contact) error {
if err != nil {
return err
}
v.LocalURL = m.httpServer.MakeContactImageURL(common.PubkeyToHex(publicKey), k)
v.LocalURL = m.httpServer.MakeContactImageURL(common.PubkeyToHex(publicKey), k, v.Clock)
contact.Images[k] = v
}
}
Expand Down
4 changes: 2 additions & 2 deletions server/server_media.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,10 @@ func (s *MediaServer) MakeQRURL(qurul string,
return u.String()
}

func (s *MediaServer) MakeContactImageURL(publicKey string, imageType string) string {
func (s *MediaServer) MakeContactImageURL(publicKey string, imageType string, imageClock uint64) string {
u := s.MakeBaseURL()
u.Path = contactImagesPath
u.RawQuery = url.Values{"publicKey": {publicKey}, "imageName": {imageType}}.Encode()
u.RawQuery = url.Values{"publicKey": {publicKey}, "imageName": {imageType}, "clock": {fmt.Sprint(imageClock)}}.Encode()

return u.String()
}
Expand Down
9 changes: 9 additions & 0 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ func (s *ServerURLSuite) TestServer_MakeStickerURL() {
s.serverNoPort.MakeStickerURL("0xdeadbeef4ac0"))
}

func (s *ServerURLSuite) TestServer_MakeContactImageURL() {
s.Require().Equal(
baseURLWithCustomPort+"/contactImages?clock=1&imageName=Test&publicKey=0x1",
s.server.MakeContactImageURL("0x1", "Test", uint64(1)))
s.testNoPort(
baseURLWithDefaultPort+"/contactImages?clock=1&imageName=Test&publicKey=0x1",
s.serverNoPort.MakeContactImageURL("0x1", "Test", uint64(1)))
}

// TestQRCodeGeneration tests if we provide all the correct parameters to the media server
// do we get a valid QR code or not as part of the response payload.
// we have stored a generated QR code in tests folder, and we compare their bytes.
Expand Down

0 comments on commit e6738e5

Please sign in to comment.