sonda/internal/api/http/handlers_mastodon.go
tgiachi a880b87a9e chore: move the module path to git.kmalloc.org/squid/sonda
Migrating hosting from Codeberg to git.kmalloc.org. Renames the
module path in go.mod, every internal import, and the OCI image
source label in the Dockerfile. Build and full test suite verified
green after the rename.
2026-11-15 17:30:29 +01:00

85 lines
2.9 KiB
Go

package httpapi
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"github.com/gin-gonic/gin"
"git.kmalloc.org/squid/sonda/internal/mastodon"
)
// mastodonHandlers serve the signed-in user's own Mastodon account.
type mastodonHandlers struct{ svc mastodonService }
// mastodonJSON is what a person sees of their account: the token never leaves the server.
type mastodonJSON struct {
Connected bool `json:"connected"`
Instance string `json:"instance,omitempty"`
Handle string `json:"handle,omitempty"`
// Problem is set when posts are paused, for example `token_refused` after the instance refused the token.
Problem string `json:"problem,omitempty"`
}
func (h *mastodonHandlers) get(c *gin.Context) {
acc, connected, err := h.svc.Status(c.Request.Context(), currentUserID(c))
if err != nil {
_ = c.Error(err)
writeError(c, http.StatusInternalServerError, "internal", "internal error")
return
}
if !connected {
c.JSON(http.StatusOK, mastodonJSON{})
return
}
c.JSON(http.StatusOK, mastodonJSON{Connected: true, Instance: acc.Instance, Handle: acc.Handle, Problem: acc.Problem})
}
type mastodonConnectRequest struct {
Instance string `json:"instance"`
Token string `json:"token"`
}
func (h *mastodonHandlers) connect(c *gin.Context) {
body, err := io.ReadAll(c.Request.Body)
if err != nil {
writeError(c, http.StatusBadRequest, "invalid_input", "malformed JSON body")
return
}
// One object, with these two names and nothing else: this is a token.
dec := json.NewDecoder(bytes.NewReader(body))
dec.DisallowUnknownFields()
var req mastodonConnectRequest
if err := dec.Decode(&req); err != nil || dec.More() || req.Instance == "" && req.Token == "" {
writeError(c, http.StatusBadRequest, "invalid_input", "send the instance and the token")
return
}
acc, err := h.svc.Connect(c.Request.Context(), currentUserID(c), req.Instance, req.Token)
switch {
case errors.Is(err, mastodon.ErrBadInstance):
writeError(c, http.StatusBadRequest, "invalid_instance", err.Error())
case errors.Is(err, mastodon.ErrBadToken):
writeError(c, http.StatusBadRequest, "invalid_token", err.Error())
case errors.Is(err, mastodon.ErrRefused):
writeError(c, http.StatusUnprocessableEntity, "mastodon_refused", err.Error())
case errors.Is(err, mastodon.ErrUnreachable):
writeError(c, http.StatusBadGateway, "mastodon_unreachable", "the instance could not be reached")
case err != nil:
_ = c.Error(err)
writeError(c, http.StatusInternalServerError, "internal", "internal error")
default:
c.JSON(http.StatusOK, mastodonJSON{Connected: true, Instance: acc.Instance, Handle: acc.Handle})
}
}
func (h *mastodonHandlers) disconnect(c *gin.Context) {
if err := h.svc.Disconnect(c.Request.Context(), currentUserID(c)); err != nil {
_ = c.Error(err)
writeError(c, http.StatusInternalServerError, "internal", "internal error")
return
}
c.Status(http.StatusNoContent)
}