mirror of
https://github.com/tulir/mautrix-whatsapp
synced 2024-05-20 12:53:48 +02:00
Move reuploadAvatar next to updateAvatar
This commit is contained in:
parent
6c517eaaeb
commit
93cbbb7f93
19
portal.go
19
portal.go
|
@ -28,6 +28,7 @@ import (
|
|||
_ "image/gif"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"math"
|
||||
"mime"
|
||||
"net/http"
|
||||
|
@ -941,6 +942,24 @@ func (portal *Portal) SyncParticipants(source *User, metadata *types.GroupInfo)
|
|||
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 {
|
||||
currentID := ""
|
||||
if *avatarSet && *avatarID != "remove" && *avatarID != "unauthorized" {
|
||||
|
|
21
puppet.go
21
puppet.go
|
@ -18,8 +18,6 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"sync"
|
||||
"time"
|
||||
|
@ -226,25 +224,6 @@ func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
|
|||
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 {
|
||||
changed := source.updateAvatar(puppet.JID, &puppet.Avatar, &puppet.AvatarURL, &puppet.AvatarSet, puppet.log, puppet.DefaultIntent())
|
||||
if !changed || puppet.Avatar == "unauthorized" {
|
||||
|
|
Loading…
Reference in a new issue