From 46768e624aa391934d613d09a890f27a311b6822 Mon Sep 17 00:00:00 2001 From: Patryk Osmaczko Date: Tue, 6 Jun 2023 20:06:11 +0200 Subject: [PATCH] feat: add and populate IdentityImage::LocalURL part of: status-im/status-desktop#10886 --- images/identity.go | 3 +++ protocol/messenger.go | 3 +++ protocol/messenger_contacts.go | 14 ++++++++++++++ protocol/messenger_handler.go | 4 ++++ server/handlers.go | 13 +++++++------ server/server_media.go | 8 ++++++++ 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/images/identity.go b/images/identity.go index 548cadc8c..50ec8a6b3 100644 --- a/images/identity.go +++ b/images/identity.go @@ -17,6 +17,7 @@ type IdentityImage struct { FileSize int `json:"fileSize"` ResizeTarget int `json:"resizeTarget"` Clock uint64 `json:"clock"` + LocalURL string `json:"localUrl,omitempty"` } func (i IdentityImage) GetType() (ImageType, error) { @@ -51,6 +52,7 @@ func (i IdentityImage) MarshalJSON() ([]byte, error) { FileSize int `json:"fileSize"` ResizeTarget int `json:"resizeTarget"` Clock uint64 `json:"clock"` + LocalURL string `json:"localUrl,omitempty"` }{ KeyUID: i.KeyUID, Name: i.Name, @@ -60,6 +62,7 @@ func (i IdentityImage) MarshalJSON() ([]byte, error) { FileSize: i.FileSize, ResizeTarget: i.ResizeTarget, Clock: i.Clock, + LocalURL: i.LocalURL, } return json.Marshal(temp) diff --git a/protocol/messenger.go b/protocol/messenger.go index 7d91b6ed2..f286e80bf 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -1642,6 +1642,9 @@ func (m *Messenger) Init() error { return err } for idx, contact := range contacts { + if err = m.updateContactImagesURL(contact); err != nil { + return err + } m.allContacts.Store(contact.ID, contacts[idx]) // We only need filters for contacts added by us and not blocked. if !contact.added() || contact.Blocked { diff --git a/protocol/messenger_contacts.go b/protocol/messenger_contacts.go index 618624047..bee4280f8 100644 --- a/protocol/messenger_contacts.go +++ b/protocol/messenger_contacts.go @@ -554,6 +554,20 @@ func (m *Messenger) RemoveContact(ctx context.Context, pubKey string) (*Messenge return response, nil } +func (m *Messenger) updateContactImagesURL(contact *Contact) error { + if m.httpServer != nil { + for k, v := range contact.Images { + publicKey, err := contact.PublicKey() + if err != nil { + return err + } + v.LocalURL = m.httpServer.MakeContactImageURL(common.PubkeyToHex(publicKey), k) + contact.Images[k] = v + } + } + return nil +} + func (m *Messenger) Contacts() []*Contact { var contacts []*Contact m.allContacts.Range(func(contactID string, contact *Contact) (shouldContinue bool) { diff --git a/protocol/messenger_handler.go b/protocol/messenger_handler.go index 203ddf123..3b3a9d6ff 100644 --- a/protocol/messenger_handler.go +++ b/protocol/messenger_handler.go @@ -2671,6 +2671,10 @@ func (m *Messenger) HandleChatIdentity(state *ReceivedMessageState, ci protobuf. contact.Images[imageType] = images.IdentityImage{Name: imageType, Payload: image.Payload, Clock: ci.Clock} } + if err = m.updateContactImagesURL(contact); err != nil { + return err + } + contactModified = true } diff --git a/server/handlers.go b/server/handlers.go index 925f5495f..3fb134134 100644 --- a/server/handlers.go +++ b/server/handlers.go @@ -147,20 +147,21 @@ func handleContactImages(db *sql.DB, logger *zap.Logger) http.HandlerFunc { logger.Error("no imageName") return } - colorHash, err := colorhash.GenerateFor(pks[0]) - if err != nil { - logger.Error("could not generate color hash") - return - } var payload []byte - err = db.QueryRow(`SELECT payload FROM chat_identity_contacts WHERE contact_id = ? and image_type = ?`, pks[0], imageNames[0]).Scan(&payload) + err := db.QueryRow(`SELECT payload FROM chat_identity_contacts WHERE contact_id = ? and image_type = ?`, pks[0], imageNames[0]).Scan(&payload) if err != nil { logger.Error("failed to load image.", zap.String("contact id", pks[0]), zap.String("image type", imageNames[0]), zap.Error(err)) return } if ringEnabled(params) { + colorHash, err := colorhash.GenerateFor(pks[0]) + if err != nil { + logger.Error("could not generate color hash") + return + } + var theme = getTheme(params, logger) config, _, err := image.DecodeConfig(bytes.NewReader(payload)) if err != nil { diff --git a/server/server_media.go b/server/server_media.go index 1f0cdefcd..49418c8e0 100644 --- a/server/server_media.go +++ b/server/server_media.go @@ -130,3 +130,11 @@ func (s *MediaServer) MakeQRURL(qurul string, return u.String() } + +func (s *MediaServer) MakeContactImageURL(publicKey string, imageType string) string { + u := s.MakeBaseURL() + u.Path = contactImagesPath + u.RawQuery = url.Values{"publicKey": {publicKey}, "imageName": {imageType}}.Encode() + + return u.String() +}