Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update profile avatar paths & hashes properly #472

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 19 additions & 14 deletions puppet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was moved to before the return true so that an path change will end up getting saved to the store even when the hash didn't change.

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 {
AndrewFerr marked this conversation as resolved.
Show resolved Hide resolved
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)
Expand All @@ -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
Expand Down