feat: add and populate IdentityImage::LocalURL

part of: status-im/status-desktop#10886
This commit is contained in:
Patryk Osmaczko 2023-06-06 20:06:11 +02:00 committed by osmaczko
parent d73c886d3b
commit 46768e624a
6 changed files with 39 additions and 6 deletions

View File

@ -17,6 +17,7 @@ type IdentityImage struct {
FileSize int `json:"fileSize"` FileSize int `json:"fileSize"`
ResizeTarget int `json:"resizeTarget"` ResizeTarget int `json:"resizeTarget"`
Clock uint64 `json:"clock"` Clock uint64 `json:"clock"`
LocalURL string `json:"localUrl,omitempty"`
} }
func (i IdentityImage) GetType() (ImageType, error) { func (i IdentityImage) GetType() (ImageType, error) {
@ -51,6 +52,7 @@ func (i IdentityImage) MarshalJSON() ([]byte, error) {
FileSize int `json:"fileSize"` FileSize int `json:"fileSize"`
ResizeTarget int `json:"resizeTarget"` ResizeTarget int `json:"resizeTarget"`
Clock uint64 `json:"clock"` Clock uint64 `json:"clock"`
LocalURL string `json:"localUrl,omitempty"`
}{ }{
KeyUID: i.KeyUID, KeyUID: i.KeyUID,
Name: i.Name, Name: i.Name,
@ -60,6 +62,7 @@ func (i IdentityImage) MarshalJSON() ([]byte, error) {
FileSize: i.FileSize, FileSize: i.FileSize,
ResizeTarget: i.ResizeTarget, ResizeTarget: i.ResizeTarget,
Clock: i.Clock, Clock: i.Clock,
LocalURL: i.LocalURL,
} }
return json.Marshal(temp) return json.Marshal(temp)

View File

@ -1642,6 +1642,9 @@ func (m *Messenger) Init() error {
return err return err
} }
for idx, contact := range contacts { for idx, contact := range contacts {
if err = m.updateContactImagesURL(contact); err != nil {
return err
}
m.allContacts.Store(contact.ID, contacts[idx]) m.allContacts.Store(contact.ID, contacts[idx])
// We only need filters for contacts added by us and not blocked. // We only need filters for contacts added by us and not blocked.
if !contact.added() || contact.Blocked { if !contact.added() || contact.Blocked {

View File

@ -554,6 +554,20 @@ func (m *Messenger) RemoveContact(ctx context.Context, pubKey string) (*Messenge
return response, nil 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 { func (m *Messenger) Contacts() []*Contact {
var contacts []*Contact var contacts []*Contact
m.allContacts.Range(func(contactID string, contact *Contact) (shouldContinue bool) { m.allContacts.Range(func(contactID string, contact *Contact) (shouldContinue bool) {

View File

@ -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} contact.Images[imageType] = images.IdentityImage{Name: imageType, Payload: image.Payload, Clock: ci.Clock}
} }
if err = m.updateContactImagesURL(contact); err != nil {
return err
}
contactModified = true contactModified = true
} }

View File

@ -147,20 +147,21 @@ func handleContactImages(db *sql.DB, logger *zap.Logger) http.HandlerFunc {
logger.Error("no imageName") logger.Error("no imageName")
return return
} }
colorHash, err := colorhash.GenerateFor(pks[0])
if err != nil {
logger.Error("could not generate color hash")
return
}
var payload []byte 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 { if err != nil {
logger.Error("failed to load image.", zap.String("contact id", pks[0]), zap.String("image type", imageNames[0]), zap.Error(err)) logger.Error("failed to load image.", zap.String("contact id", pks[0]), zap.String("image type", imageNames[0]), zap.Error(err))
return return
} }
if ringEnabled(params) { 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) var theme = getTheme(params, logger)
config, _, err := image.DecodeConfig(bytes.NewReader(payload)) config, _, err := image.DecodeConfig(bytes.NewReader(payload))
if err != nil { if err != nil {

View File

@ -130,3 +130,11 @@ func (s *MediaServer) MakeQRURL(qurul string,
return u.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()
}