Move reuploadAvatar next to updateAvatar

This commit is contained in:
Tulir Asokan 2022-09-28 14:45:36 +03:00
parent 6c517eaaeb
commit 93cbbb7f93
2 changed files with 19 additions and 21 deletions

View file

@ -28,6 +28,7 @@ import (
_ "image/gif" _ "image/gif"
"image/jpeg" "image/jpeg"
"image/png" "image/png"
"io"
"math" "math"
"mime" "mime"
"net/http" "net/http"
@ -941,6 +942,24 @@ func (portal *Portal) SyncParticipants(source *User, metadata *types.GroupInfo)
portal.kickExtraUsers(participantMap) portal.kickExtraUsers(participantMap)
} }
func reuploadAvatar(intent *appservice.IntentAPI, url string) (id.ContentURI, error) {
getResp, err := http.DefaultClient.Get(url)
if err != nil {
return id.ContentURI{}, fmt.Errorf("failed to download avatar: %w", err)
}
data, err := io.ReadAll(getResp.Body)
_ = getResp.Body.Close()
if err != nil {
return id.ContentURI{}, fmt.Errorf("failed to read avatar bytes: %w", err)
}
resp, err := intent.UploadBytes(data, http.DetectContentType(data))
if err != nil {
return id.ContentURI{}, fmt.Errorf("failed to upload avatar to Matrix: %w", err)
}
return resp.ContentURI, nil
}
func (user *User) updateAvatar(jid types.JID, avatarID *string, avatarURL *id.ContentURI, avatarSet *bool, log log.Logger, intent *appservice.IntentAPI) bool { func (user *User) updateAvatar(jid types.JID, avatarID *string, avatarURL *id.ContentURI, avatarSet *bool, log log.Logger, intent *appservice.IntentAPI) bool {
currentID := "" currentID := ""
if *avatarSet && *avatarID != "remove" && *avatarID != "unauthorized" { if *avatarSet && *avatarID != "remove" && *avatarID != "unauthorized" {

View file

@ -18,8 +18,6 @@ package main
import ( import (
"fmt" "fmt"
"io"
"net/http"
"regexp" "regexp"
"sync" "sync"
"time" "time"
@ -226,25 +224,6 @@ func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
return puppet.bridge.AS.Intent(puppet.MXID) return puppet.bridge.AS.Intent(puppet.MXID)
} }
func reuploadAvatar(intent *appservice.IntentAPI, url string) (id.ContentURI, error) {
getResp, err := http.DefaultClient.Get(url)
if err != nil {
return id.ContentURI{}, fmt.Errorf("failed to download avatar: %w", err)
}
data, err := io.ReadAll(getResp.Body)
_ = getResp.Body.Close()
if err != nil {
return id.ContentURI{}, fmt.Errorf("failed to read avatar bytes: %w", err)
}
mime := http.DetectContentType(data)
resp, err := intent.UploadBytes(data, mime)
if err != nil {
return id.ContentURI{}, fmt.Errorf("failed to upload avatar to Matrix: %w", err)
}
return resp.ContentURI, nil
}
func (puppet *Puppet) UpdateAvatar(source *User, forcePortalSync bool) bool { func (puppet *Puppet) UpdateAvatar(source *User, forcePortalSync bool) bool {
changed := source.updateAvatar(puppet.JID, &puppet.Avatar, &puppet.AvatarURL, &puppet.AvatarSet, puppet.log, puppet.DefaultIntent()) changed := source.updateAvatar(puppet.JID, &puppet.Avatar, &puppet.AvatarURL, &puppet.AvatarSet, puppet.log, puppet.DefaultIntent())
if !changed || puppet.Avatar == "unauthorized" { if !changed || puppet.Avatar == "unauthorized" {