sonda/internal/api/http/handlers_mastodon.go
tgiachi a7a258464b feat(mastodon): say in Settings when the instance no longer accepts the token
When the instance refuses the token on a post (it was revoked or expired), the account now remembers it: GET /me/mastodon says problem: token_refused and nothing is posted until I connect again with a new token.

- migration 0027 adds mastodon_accounts.problem (SQLite and PostgreSQL).
- Only a refused token marks the account; an instance that is down does not.
- Saving the account again clears the problem.
- Settings shows the warning and the form with the instance already filled in, next to Disconnect.
- English and Italian.
2026-11-14 10:04:07 +01:00

85 lines
2.9 KiB
Go

package httpapi
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"github.com/gin-gonic/gin"
"codeberg.org/squidd/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)
}