From d451d51b15ee90ac0217bbff3f382f95ce2c3615 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Wed, 21 Feb 2024 16:24:44 -0500 Subject: [PATCH 1/2] Update profile avatar paths & hashes properly - Store updates to profile avatar hashes in signalmeow_contacts - Have puppets use the stored contact avatar hash rather than recomputing it - Have puppets update their avatar path when the profile avatar path changed but the hash did not - Have puppets clear their avatar path when using a contact avatar instead of a profile avatar --- puppet.go | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/puppet.go b/puppet.go index 1ecc2409..d491c940 100644 --- a/puppet.go +++ b/puppet.go @@ -325,8 +325,8 @@ func (puppet *Puppet) updateAvatar(ctx context.Context, source *User, info *type // TODO what to do? 🤔 return false } - puppet.AvatarSet = false puppet.AvatarPath = "" + puppet.AvatarHash = info.ContactAvatar.Hash } else { if puppet.AvatarPath == info.Profile.AvatarPath && puppet.AvatarSet { return false @@ -354,19 +354,24 @@ func (puppet *Puppet) updateAvatar(ctx context.Context, source *User, info *type return true } avatarContentType = http.DetectContentType(avatarData) + puppet.AvatarPath = info.Profile.AvatarPath + hash := sha256.Sum256(avatarData) + newHash := hex.EncodeToString(hash[:]) + if puppet.AvatarHash == newHash && puppet.AvatarSet { + log.Debug(). + Str("avatar_hash", newHash). + Str("new_avatar_path", puppet.AvatarPath). + Msg("Avatar path changed, but hash didn't") + // Path changed, but actual avatar didn't + return true + } + puppet.AvatarHash = newHash + source.Client.Store.ContactStore.StoreContact(ctx, *info) + err = source.Client.Store.ContactStore.StoreContact(ctx, *info) + if err != nil { + log.Warn().Err(err).Msg("error updating contact's profile avatar hash") + } } - hash := sha256.Sum256(avatarData) - newHash := hex.EncodeToString(hash[:]) - if puppet.AvatarHash == newHash && puppet.AvatarSet { - log.Debug(). - Str("avatar_hash", newHash). - Str("new_avatar_path", puppet.AvatarPath). - Msg("Avatar path changed, but hash didn't") - // Path changed, but actual avatar didn't - return true - } - puppet.AvatarPath = info.Profile.AvatarPath - puppet.AvatarHash = newHash puppet.AvatarSet = false puppet.AvatarURL = id.ContentURI{} resp, err := puppet.DefaultIntent().UploadBytes(ctx, avatarData, avatarContentType) @@ -383,7 +388,7 @@ func (puppet *Puppet) updateAvatar(ctx context.Context, source *User, info *type return true } log.Debug(). - Str("avatar_hash", newHash). + Str("avatar_hash", puppet.AvatarHash). Stringer("avatar_mxc", resp.ContentURI). Msg("Avatar updated successfully") puppet.AvatarSet = true From f96ad709388b5022b74b243cdea6a98b86a416c4 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Tue, 12 Mar 2024 10:15:07 -0400 Subject: [PATCH 2/2] Remove unnecessary StoreContact call This call was meant to save changes to the Contact's avatar hash, but the hash is no longer stored there, making this call obsolete. --- puppet.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/puppet.go b/puppet.go index d491c940..627d3af0 100644 --- a/puppet.go +++ b/puppet.go @@ -366,11 +366,6 @@ func (puppet *Puppet) updateAvatar(ctx context.Context, source *User, info *type return true } puppet.AvatarHash = newHash - source.Client.Store.ContactStore.StoreContact(ctx, *info) - err = source.Client.Store.ContactStore.StoreContact(ctx, *info) - if err != nil { - log.Warn().Err(err).Msg("error updating contact's profile avatar hash") - } } puppet.AvatarSet = false puppet.AvatarURL = id.ContentURI{}