Refactor code structure for improved readability and maintainability

This commit is contained in:
2026-03-04 19:02:34 +00:00
parent e900464dd6
commit 062dac268f
9 changed files with 347 additions and 53 deletions
+55
View File
@@ -0,0 +1,55 @@
package matrix
import (
"context"
"fmt"
"mime"
"os"
"path/filepath"
"maunium.net/go/mautrix"
)
// 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) {
f, err := os.Open(filePath)
if err != nil {
return "", fmt.Errorf("open %s: %w", filePath, err)
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return "", fmt.Errorf("stat %s: %w", filePath, err)
}
mimeType := mime.TypeByExtension(filepath.Ext(filePath))
if mimeType == "" {
mimeType = "application/octet-stream"
}
resp, err := c.raw.UploadMedia(ctx, mautrix.ReqUploadMedia{
Content: f,
ContentLength: info.Size(),
ContentType: mimeType,
FileName: filepath.Base(filePath),
})
if err != nil {
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
}
// 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 {
return fmt.Errorf("set display name: %w", err)
}
return nil
}