diff --git a/shell/matrix/profile.go b/shell/matrix/profile.go index b4dce6e..14d3bde 100644 --- a/shell/matrix/profile.go +++ b/shell/matrix/profile.go @@ -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 {