refactor: separar SetAvatar en UploadMedia + SetAvatarURL

SetAvatar hacia dos cosas: subir la imagen y establecerla como avatar.
Ahora son tres funciones separadas:
- UploadMedia: solo sube, devuelve mxc:// URI
- SetAvatarURL: solo establece avatar con un mxc:// URI existente
- SetAvatar: convenience wrapper que llama a ambas

Permite subir imagenes sin activar el avatar, o reusar imagenes ya subidas.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-09 21:37:06 +00:00
parent 3efde2b9bc
commit 730e415dc1
+30 -7
View File
@@ -8,11 +8,13 @@ import (
"path/filepath"
"maunium.net/go/mautrix"
"maunium.net/go/mautrix/id"
)
// SetAvatar uploads the image at filePath to the Matrix media repository
// and sets it as the bot's avatar. Returns the mxc:// URI of the upload.
func (c *Client) SetAvatar(ctx context.Context, filePath string) (string, error) {
// UploadMedia uploads the file at filePath to the Matrix media repository
// and returns its mxc:// URI. Does NOT set the avatar — use SetAvatarURL
// or SetAvatar for that.
func (c *Client) UploadMedia(ctx context.Context, filePath string) (string, error) {
f, err := os.Open(filePath)
if err != nil {
return "", fmt.Errorf("open %s: %w", filePath, err)
@@ -39,13 +41,34 @@ func (c *Client) SetAvatar(ctx context.Context, filePath string) (string, error)
return "", fmt.Errorf("upload media: %w", err)
}
if err := c.raw.SetAvatarURL(ctx, resp.ContentURI); err != nil {
return "", fmt.Errorf("set avatar URL: %w", err)
}
return resp.ContentURI.String(), nil
}
// SetAvatarURL sets the bot's avatar to an already-uploaded mxc:// URI.
func (c *Client) SetAvatarURL(ctx context.Context, mxcURI string) error {
parsed, err := id.ParseContentURI(mxcURI)
if err != nil {
return fmt.Errorf("parse mxc URI %q: %w", mxcURI, err)
}
if err := c.raw.SetAvatarURL(ctx, parsed); err != nil {
return fmt.Errorf("set avatar URL: %w", err)
}
return nil
}
// SetAvatar uploads the image at filePath and sets it as the bot's avatar.
// Convenience wrapper: calls UploadMedia then SetAvatarURL.
func (c *Client) SetAvatar(ctx context.Context, filePath string) (string, error) {
uri, err := c.UploadMedia(ctx, filePath)
if err != nil {
return "", err
}
if err := c.SetAvatarURL(ctx, uri); err != nil {
return "", err
}
return uri, nil
}
// SetDisplayName sets the bot's display name on the Matrix homeserver.
func (c *Client) SetDisplayName(ctx context.Context, name string) error {
if err := c.raw.SetDisplayName(ctx, name); err != nil {