feat: post what I listen to on Mastodon, if I connect an account
In Settings a person connects their Mastodon account: the address of their instance and an access token that can only write posts. Nothing is connected by default, and disconnecting forgets the token. - When a track of this server counts as a listen, it is posted as an unlisted status: title, artists, album and #NowPlaying, with no link. Music of a friend is never reported as a listen, so it is never posted. At most one post a minute for each person, in the background, with a bounded queue. - PUT, GET and DELETE /me/mastodon. The token is never sent back. - The address of the instance is typed by a person, so it must be a domain name on https, and the client never connects to a private, loopback or link-local address (checked on the address it really dials, so a name that points inside is no way in), never follows a redirect, ignores proxy settings and reads at most a megabyte. - New table mastodon_accounts (SQLite and PostgreSQL) that goes with the account. - README: the feature, and the credit for butterchurn and its presets.
This commit is contained in:
parent
7aa822f6bb
commit
0dc7782028
14 changed files with 1953 additions and 7 deletions
|
|
@ -27,6 +27,7 @@ import (
|
|||
"codeberg.org/squidd/sonda/internal/jobs"
|
||||
"codeberg.org/squidd/sonda/internal/library"
|
||||
"codeberg.org/squidd/sonda/internal/listening"
|
||||
"codeberg.org/squidd/sonda/internal/mastodon"
|
||||
"codeberg.org/squidd/sonda/internal/mixes"
|
||||
"codeberg.org/squidd/sonda/internal/naming"
|
||||
"codeberg.org/squidd/sonda/internal/playlists"
|
||||
|
|
@ -109,6 +110,7 @@ func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun
|
|||
Settings: usersettings.NewService(storage.NewUserSettingsRepo(db)),
|
||||
Streams: streaming.NewService(noFiles{}),
|
||||
Federation: federation.NewService(storage.NewFederationRepo(db), federation.NewClient()),
|
||||
Mastodon: mastodon.NewService(storage.NewMastodonRepo(db), &fakeMastodon{handle: "@test@mastodon.social"}, zap.NewNop()),
|
||||
}
|
||||
libRepo := storage.NewLibraryRepo(db)
|
||||
deps.PeerCatalog = catalog.NewService(storage.NewSharedCatalogRepo(db))
|
||||
|
|
|
|||
|
|
@ -825,6 +825,20 @@ func TestResponsesMatchTheSpec(t *testing.T) {
|
|||
c.expect(c.call("PATCH", "/me/settings", token, map[string]any{"nope": true}), 400, "a setting that does not exist", "invalid_input")
|
||||
c.expect(c.call("GET", "/me/settings", "", nil), 401, "settings without a token", "unauthorized")
|
||||
|
||||
// Sharing what I listen to on Mastodon: nothing is connected until I do it, and the token never comes back.
|
||||
if got := c.expect(c.call("GET", "/me/mastodon", token, nil), 200, "no Mastodon account yet"); got["connected"] != false {
|
||||
c.t.Errorf("connected at the start: %v", got)
|
||||
}
|
||||
c.expect(c.call("PUT", "/me/mastodon", token, map[string]any{"instance": "mastodon.social", "token": "secret"}), 200, "connect Mastodon")
|
||||
if got := c.expect(c.call("GET", "/me/mastodon", token, nil), 200, "the Mastodon account"); got["connected"] != true || got["instance"] != "https://mastodon.social" {
|
||||
c.t.Errorf("account = %v", got)
|
||||
}
|
||||
c.expect(c.call("PUT", "/me/mastodon", token, map[string]any{"instance": "http://mastodon.social", "token": "secret"}), 400, "an instance on plain http", "invalid_instance")
|
||||
c.expect(c.call("PUT", "/me/mastodon", token, map[string]any{"instance": "mastodon.social", "token": " "}), 400, "an empty token", "invalid_token")
|
||||
c.expect(c.call("PUT", "/me/mastodon", token, map[string]any{"instance": "mastodon.social", "nope": 1}), 400, "a name that does not exist", "invalid_input")
|
||||
c.expect(c.call("DELETE", "/me/mastodon", token, nil), 204, "disconnect Mastodon")
|
||||
c.expect(c.call("GET", "/me/mastodon", "", nil), 401, "Mastodon without a token", "unauthorized")
|
||||
|
||||
// ---- duplicate songs ----
|
||||
c.duplicates(t, token, libID)
|
||||
|
||||
|
|
|
|||
|
|
@ -10,7 +10,15 @@ import (
|
|||
"codeberg.org/squidd/sonda/internal/listening"
|
||||
)
|
||||
|
||||
type listenHandlers struct{ svc listenService }
|
||||
// announcer is told about every listen that counts, so that it can be shared where the person asked for it.
|
||||
type announcer interface {
|
||||
Announce(userID, trackID int64)
|
||||
}
|
||||
|
||||
type listenHandlers struct {
|
||||
svc listenService
|
||||
announce announcer
|
||||
}
|
||||
|
||||
type listenRequest struct {
|
||||
TrackID int64 `json:"track_id"`
|
||||
|
|
@ -60,6 +68,9 @@ func (h *listenHandlers) record(c *gin.Context) {
|
|||
_ = c.Error(err)
|
||||
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
||||
case res.Created:
|
||||
if res.Counted {
|
||||
h.announce.Announce(uid, req.TrackID)
|
||||
}
|
||||
c.JSON(http.StatusCreated, gin.H{"status": "recorded", "counted": res.Counted})
|
||||
default:
|
||||
c.JSON(http.StatusOK, gin.H{"status": "duplicate", "counted": res.Counted})
|
||||
|
|
|
|||
83
internal/api/http/handlers_mastodon.go
Normal file
83
internal/api/http/handlers_mastodon.go
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
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"`
|
||||
}
|
||||
|
||||
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})
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
200
internal/api/http/mastodon_test.go
Normal file
200
internal/api/http/mastodon_test.go
Normal file
|
|
@ -0,0 +1,200 @@
|
|||
package httpapi
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"codeberg.org/squidd/sonda/internal/mastodon"
|
||||
"codeberg.org/squidd/sonda/internal/storage"
|
||||
)
|
||||
|
||||
// fakeMastodon stands in for the instance: it answers like the one a test sets up and tells what was posted.
|
||||
type fakeMastodon struct {
|
||||
mu sync.Mutex
|
||||
handle string
|
||||
verify error
|
||||
posted chan [2]string // token, status
|
||||
}
|
||||
|
||||
func (f *fakeMastodon) Verify(_ context.Context, _, _ string) (string, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return f.handle, f.verify
|
||||
}
|
||||
|
||||
func (f *fakeMastodon) Post(_ context.Context, _, token, status, _ string) error {
|
||||
if f.posted != nil {
|
||||
f.posted <- [2]string{token, status}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// mastodonEnv is the API with the Mastodon service on the given instance. The worker that posts runs for the length of the test.
|
||||
func mastodonEnv(t *testing.T, remote mastodon.Remote) http.Handler {
|
||||
t.Helper()
|
||||
h, _ := newTestEnvDB(t, func(d *Deps, db *bun.DB) {
|
||||
svc := mastodon.NewService(storage.NewMastodonRepo(db), remote, zap.NewNop())
|
||||
ctx, stop := context.WithCancel(context.Background())
|
||||
t.Cleanup(stop)
|
||||
go svc.Run(ctx)
|
||||
d.Mastodon = svc
|
||||
})
|
||||
return h
|
||||
}
|
||||
|
||||
func TestMastodonNeedsASignedInUser(t *testing.T) {
|
||||
h := newTestAPI(t)
|
||||
for _, m := range []string{"GET", "PUT", "DELETE"} {
|
||||
if r := do(t, h, m, "/api/v1/me/mastodon", "", map[string]any{}); r.Code != http.StatusUnauthorized {
|
||||
t.Errorf("%s without a token: %d", m, r.Code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMastodonStartsDisconnected(t *testing.T) {
|
||||
h := newTestAPI(t)
|
||||
alice := registerAndLogin(t, h)
|
||||
r := do(t, h, "GET", "/api/v1/me/mastodon", alice, nil)
|
||||
if r.Code != http.StatusOK {
|
||||
t.Fatalf("%d %v", r.Code, r.Body)
|
||||
}
|
||||
sameKeys(t, "status", r.Body, "connected")
|
||||
if r.Body["connected"] != false {
|
||||
t.Errorf("status = %v", r.Body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectingAnAccountKeepsItAndNeverShowsTheToken(t *testing.T) {
|
||||
h := mastodonEnv(t, &fakeMastodon{handle: "@squid@mastodon.social"})
|
||||
alice := registerAndLogin(t, h)
|
||||
r := do(t, h, "PUT", "/api/v1/me/mastodon", alice, map[string]any{"instance": "Mastodon.Social/", "token": "secret-token-value"})
|
||||
if r.Code != http.StatusOK {
|
||||
t.Fatalf("%d %v", r.Code, r.Body)
|
||||
}
|
||||
sameKeys(t, "connected", r.Body, "connected", "instance", "handle")
|
||||
if r.Body["connected"] != true || r.Body["instance"] != "https://mastodon.social" || r.Body["handle"] != "@squid@mastodon.social" {
|
||||
t.Errorf("body = %v", r.Body)
|
||||
}
|
||||
g := do(t, h, "GET", "/api/v1/me/mastodon", alice, nil)
|
||||
sameKeys(t, "status", g.Body, "connected", "instance", "handle")
|
||||
for _, resp := range []map[string]any{r.Body, g.Body} {
|
||||
for k, v := range resp {
|
||||
if s, ok := v.(string); ok && strings.Contains(s, "secret-token") {
|
||||
t.Errorf("the token came back in %q: %v", k, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnAccountThatCannotBeConnectedIsNotKept(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
verify error
|
||||
instance string
|
||||
token string
|
||||
status int
|
||||
code string
|
||||
}{
|
||||
{"a refused token", mastodon.ErrRefused, "mastodon.social", "tok", http.StatusUnprocessableEntity, "mastodon_refused"},
|
||||
{"an instance that is not there", mastodon.ErrUnreachable, "mastodon.social", "tok", http.StatusBadGateway, "mastodon_unreachable"},
|
||||
{"a plain http address", nil, "http://mastodon.social", "tok", http.StatusBadRequest, "invalid_instance"},
|
||||
{"a name with no dot", nil, "localhost", "tok", http.StatusBadRequest, "invalid_instance"},
|
||||
{"an empty token", nil, "mastodon.social", " ", http.StatusBadRequest, "invalid_token"},
|
||||
} {
|
||||
h := mastodonEnv(t, &fakeMastodon{handle: "@a@b", verify: c.verify})
|
||||
alice := registerAndLogin(t, h)
|
||||
r := do(t, h, "PUT", "/api/v1/me/mastodon", alice, map[string]any{"instance": c.instance, "token": c.token})
|
||||
if r.Code != c.status || errCode(r) != c.code {
|
||||
t.Errorf("%s: %d %v, want %d %s", c.name, r.Code, r.Body, c.status, c.code)
|
||||
}
|
||||
if g := do(t, h, "GET", "/api/v1/me/mastodon", alice, nil); g.Body["connected"] != false {
|
||||
t.Errorf("%s: something was kept: %v", c.name, g.Body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAMastodonBodyThatIsNotWhatWasAskedForIsRefused(t *testing.T) {
|
||||
h := newTestAPI(t)
|
||||
alice := registerAndLogin(t, h)
|
||||
for name, body := range map[string]any{
|
||||
"a list": []any{"mastodon.social"},
|
||||
"a number": map[string]any{"instance": 5, "token": "t"},
|
||||
"no instance": map[string]any{"token": "t"},
|
||||
"no token": map[string]any{"instance": "mastodon.social"},
|
||||
"an unknown name": map[string]any{"instance": "mastodon.social", "token": "t", "visibility": "public"},
|
||||
} {
|
||||
if r := do(t, h, "PUT", "/api/v1/me/mastodon", alice, body); r.Code != http.StatusBadRequest {
|
||||
t.Errorf("%s: %d %v, want 400", name, r.Code, r.Body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisconnectingForgetsTheAccountAndCanBeRepeated(t *testing.T) {
|
||||
h := mastodonEnv(t, &fakeMastodon{handle: "@a@b"})
|
||||
alice := registerAndLogin(t, h)
|
||||
do(t, h, "PUT", "/api/v1/me/mastodon", alice, map[string]any{"instance": "mastodon.social", "token": "tok"})
|
||||
for i := 0; i < 2; i++ {
|
||||
if r := do(t, h, "DELETE", "/api/v1/me/mastodon", alice, nil); r.Code != http.StatusNoContent {
|
||||
t.Fatalf("delete %d: %d %v", i, r.Code, r.Body)
|
||||
}
|
||||
}
|
||||
if g := do(t, h, "GET", "/api/v1/me/mastodon", alice, nil); g.Body["connected"] != false {
|
||||
t.Errorf("still connected: %v", g.Body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEachPersonHasTheirOwnMastodonAccount(t *testing.T) {
|
||||
h := mastodonEnv(t, &fakeMastodon{handle: "@a@b"})
|
||||
alice := registerAndLogin(t, h)
|
||||
do(t, h, "PUT", "/api/v1/me/mastodon", alice, map[string]any{"instance": "mastodon.social", "token": "tok"})
|
||||
cred := map[string]any{"email": "bob@example.org", "password": "longenough"}
|
||||
if c := do(t, h, "POST", "/api/v1/users", alice, cred); c.Code != http.StatusCreated {
|
||||
t.Fatalf("create bob: %d %v", c.Code, c.Body)
|
||||
}
|
||||
bob, _ := do(t, h, "POST", "/api/v1/auth/login", "", cred).Body["access_token"].(string)
|
||||
if g := do(t, h, "GET", "/api/v1/me/mastodon", bob, nil); g.Body["connected"] != false {
|
||||
t.Errorf("bob sees alice's account: %v", g.Body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOnlyAListenThatCountsIsPostedAndOnlyForWhoConnected(t *testing.T) {
|
||||
remote := &fakeMastodon{handle: "@squid@mastodon.social", posted: make(chan [2]string, 4)}
|
||||
h := mastodonEnv(t, remote)
|
||||
alice := registerAndLogin(t, h)
|
||||
cred := map[string]any{"email": "bob@example.org", "password": "longenough"}
|
||||
do(t, h, "POST", "/api/v1/users", alice, cred)
|
||||
bob, _ := do(t, h, "POST", "/api/v1/auth/login", "", cred).Body["access_token"].(string)
|
||||
do(t, h, "PUT", "/api/v1/me/mastodon", alice, map[string]any{"instance": "mastodon.social", "token": "tok"})
|
||||
|
||||
listen := func(token, key, kind string, ms int) {
|
||||
t.Helper()
|
||||
r := do(t, h, "POST", "/api/v1/listens", token, map[string]any{"track_id": 1, "event_type": kind, "duration_played_ms": ms, "idempotency_key": key})
|
||||
if r.Code != http.StatusCreated {
|
||||
t.Fatalf("listen %s: %d %v", key, r.Code, r.Body)
|
||||
}
|
||||
}
|
||||
listen(bob, "b1", "complete", 30000) // counts, but bob connected nothing
|
||||
listen(alice, "a1", "skip", 5000) // alice connected, but it does not count
|
||||
listen(alice, "a2", "complete", 30000) // this one is posted
|
||||
|
||||
select {
|
||||
case p := <-remote.posted:
|
||||
if p[0] != "tok" || !strings.Contains(p[1], "song") || !strings.HasSuffix(p[1], "#NowPlaying") {
|
||||
t.Errorf("post = %v", p)
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("nothing was posted for a listen that counts")
|
||||
}
|
||||
select {
|
||||
case p := <-remote.posted:
|
||||
t.Errorf("a second post: %v", p)
|
||||
case <-time.After(200 * time.Millisecond):
|
||||
}
|
||||
}
|
||||
|
|
@ -24,6 +24,7 @@ import (
|
|||
"codeberg.org/squidd/sonda/internal/jobs"
|
||||
"codeberg.org/squidd/sonda/internal/library"
|
||||
"codeberg.org/squidd/sonda/internal/listening"
|
||||
"codeberg.org/squidd/sonda/internal/mastodon"
|
||||
"codeberg.org/squidd/sonda/internal/mixes"
|
||||
"codeberg.org/squidd/sonda/internal/naming"
|
||||
"codeberg.org/squidd/sonda/internal/playlists"
|
||||
|
|
@ -170,6 +171,14 @@ type userSettingsService interface {
|
|||
Update(ctx context.Context, userID int64, p usersettings.Patch) (usersettings.Settings, error)
|
||||
}
|
||||
|
||||
type mastodonService interface {
|
||||
Status(ctx context.Context, userID int64) (mastodon.Account, bool, error)
|
||||
Connect(ctx context.Context, userID int64, instance, token string) (mastodon.Account, error)
|
||||
Disconnect(ctx context.Context, userID int64) error
|
||||
// Announce asks for a listen to be posted, if the person connected an account. It never waits.
|
||||
Announce(userID, trackID int64)
|
||||
}
|
||||
|
||||
type catalogService interface {
|
||||
Artists(ctx context.Context, f catalog.Filter) (catalog.Page[catalog.Artist], error)
|
||||
Albums(ctx context.Context, f catalog.Filter) (catalog.Page[catalog.Album], error)
|
||||
|
|
@ -197,6 +206,7 @@ type Deps struct {
|
|||
Auth authService
|
||||
Tokens tokenParser
|
||||
Listening listenService
|
||||
Mastodon mastodonService
|
||||
Scan scanService
|
||||
Libraries libraryService
|
||||
Catalog catalogService
|
||||
|
|
@ -282,7 +292,7 @@ func NewRouter(d Deps) *gin.Engine {
|
|||
protected := v1.Group("", requireAuth(d.Tokens, d.Auth, false))
|
||||
protected.GET("/auth/me", a.me)
|
||||
protected.POST("/auth/password", a.changePassword)
|
||||
l := &listenHandlers{svc: d.Listening}
|
||||
l := &listenHandlers{svc: d.Listening, announce: d.Mastodon}
|
||||
protected.POST("/listens", l.record)
|
||||
protected.GET("/listens", l.list)
|
||||
|
||||
|
|
@ -343,6 +353,10 @@ func NewRouter(d Deps) *gin.Engine {
|
|||
us := &userSettingsHandlers{svc: d.Settings}
|
||||
protected.GET("/me/settings", us.get)
|
||||
protected.PATCH("/me/settings", us.update)
|
||||
ma := &mastodonHandlers{svc: d.Mastodon}
|
||||
protected.GET("/me/mastodon", ma.get)
|
||||
protected.PUT("/me/mastodon", ma.connect)
|
||||
protected.DELETE("/me/mastodon", ma.disconnect)
|
||||
|
||||
lib := &libraryHandlers{scan: d.Scan}
|
||||
protected.GET("/library/scan", lib.scanStatus)
|
||||
|
|
|
|||
|
|
@ -2093,6 +2093,75 @@ paths:
|
|||
'400': {$ref: '#/components/responses/InvalidInput'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
|
||||
/me/mastodon:
|
||||
get:
|
||||
tags: [Taste]
|
||||
operationId: getMyMastodon
|
||||
summary: The Mastodon account you connected
|
||||
description: |
|
||||
Whether you connected a Mastodon account to share what you listen to, and which one. Nothing is connected
|
||||
until you do it. The access token is never sent back.
|
||||
security: [{bearerAuth: []}]
|
||||
responses:
|
||||
'200':
|
||||
description: Your account, or `connected` false when there is none.
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/MastodonAccount'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
put:
|
||||
tags: [Taste]
|
||||
operationId: connectMyMastodon
|
||||
summary: Connect a Mastodon account
|
||||
description: |
|
||||
Connects your Mastodon account, or replaces the one you had. Send the address of your instance (a domain name
|
||||
on https, like `mastodon.social`; an address that points at a private network is never reached) and an access
|
||||
token. Make the token in your instance, under Preferences, Development, New application, and give it only
|
||||
the `write:statuses` permission. The server asks the instance who the token belongs to; if the instance
|
||||
refuses it nothing is kept.
|
||||
|
||||
From then on, each time a track of this server counts as a listen, the server posts it to your account as an
|
||||
unlisted status: title, artists, album and `#NowPlaying`, with no link. Music of a friend is never posted. At
|
||||
most one post a minute. The token stays on the server and is forgotten by `DELETE /me/mastodon`. The body
|
||||
must be one JSON object with these two names and nothing else.
|
||||
security: [{bearerAuth: []}]
|
||||
requestBody:
|
||||
required: true
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/ConnectMastodon'}
|
||||
responses:
|
||||
'200':
|
||||
description: The account that is now connected.
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/MastodonAccount'}
|
||||
'400':
|
||||
description: The body is malformed (`invalid_input`), the instance is not a domain name on https (`invalid_instance`) or the token is empty or too long (`invalid_token`).
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/Error'}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
'422':
|
||||
description: The instance refused the token (`mastodon_refused`).
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/Error'}
|
||||
'502':
|
||||
description: The instance could not be reached, or did not answer like Mastodon (`mastodon_unreachable`).
|
||||
content:
|
||||
application/json:
|
||||
schema: {$ref: '#/components/schemas/Error'}
|
||||
delete:
|
||||
tags: [Taste]
|
||||
operationId: disconnectMyMastodon
|
||||
summary: Disconnect your Mastodon account
|
||||
description: Forgets the account and its token. Nothing more is posted. Disconnecting when nothing is connected is not an error.
|
||||
security: [{bearerAuth: []}]
|
||||
responses:
|
||||
'204': {description: Disconnected.}
|
||||
'401': {$ref: '#/components/responses/Unauthorized'}
|
||||
|
||||
/artists/{id}/lidarr:
|
||||
parameters:
|
||||
- {$ref: '#/components/parameters/Id'}
|
||||
|
|
@ -2967,6 +3036,9 @@ paths:
|
|||
**Plausibility.** When the length of the track is known, `duration_played_ms` may not exceed
|
||||
twice that length plus 10 seconds (a listener can seek back and loop). Streaming a file never
|
||||
records anything on its own.
|
||||
|
||||
**Sharing.** A new event that counts is also posted to the Mastodon account of the listener, but only
|
||||
when they connected one with `PUT /me/mastodon`. That happens in the background and never changes the answer.
|
||||
security: [{bearerAuth: []}]
|
||||
requestBody:
|
||||
required: true
|
||||
|
|
@ -5308,6 +5380,23 @@ components:
|
|||
shared: another user can see from `retrieved_at` in `GET /artists/{id}/similar` that
|
||||
somebody looked an artist up, but not who.
|
||||
|
||||
MastodonAccount:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required: [connected]
|
||||
properties:
|
||||
connected: {type: boolean}
|
||||
instance: {type: string, description: "The instance, like `https://mastodon.social`. Only when connected."}
|
||||
handle: {type: string, description: "Your handle, like `@name@mastodon.social`. Only when connected."}
|
||||
|
||||
ConnectMastodon:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required: [instance, token]
|
||||
properties:
|
||||
instance: {type: string, maxLength: 300, description: "The address of your instance: `mastodon.social` or `https://mastodon.social`."}
|
||||
token: {type: string, maxLength: 512, description: "An access token with the `write:statuses` permission. Never sent back."}
|
||||
|
||||
UpdateUserSettings:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
|
|
|
|||
170
internal/mastodon/client.go
Normal file
170
internal/mastodon/client.go
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
package mastodon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/netip"
|
||||
"net/url"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
requestTimeout = 15 * time.Second
|
||||
// maxAnswer is far more than any answer of Mastodon that is read.
|
||||
maxAnswer = 1 << 20
|
||||
// maxHandle is the longest handle that is believed.
|
||||
maxHandle = 320
|
||||
)
|
||||
|
||||
// HTTPRemote is the real Remote. The address of the instance is typed by a person, so the client is built to be sent
|
||||
// nowhere it should not go: it never reaches a private, loopback or link-local address (checked on the address it
|
||||
// really connects to, so a name that points inside is no way in either), never follows a redirect (the token would go
|
||||
// along), keeps no cookies, ignores proxy settings and reads at most a megabyte.
|
||||
type HTTPRemote struct {
|
||||
client *http.Client
|
||||
userAgent string
|
||||
}
|
||||
|
||||
// NewRemote builds the client. userAgent says who is asking.
|
||||
func NewRemote(userAgent string) *HTTPRemote {
|
||||
dialer := &net.Dialer{
|
||||
Timeout: 10 * time.Second,
|
||||
Control: func(_, address string, _ syscall.RawConn) error {
|
||||
host, _, err := net.SplitHostPort(address)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !publicIP(net.ParseIP(host)) {
|
||||
return errors.New("not a public address")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
transport := &http.Transport{
|
||||
Proxy: nil,
|
||||
DialContext: dialer.DialContext,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ResponseHeaderTimeout: requestTimeout,
|
||||
MaxIdleConns: 4,
|
||||
IdleConnTimeout: 30 * time.Second,
|
||||
DisableCompression: false,
|
||||
}
|
||||
return &HTTPRemote{
|
||||
userAgent: userAgent,
|
||||
client: &http.Client{
|
||||
Transport: transport,
|
||||
Timeout: requestTimeout,
|
||||
CheckRedirect: refuseRedirect,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// refuseRedirect keeps the answer that says "go elsewhere" as the answer: the token must not follow it.
|
||||
func refuseRedirect(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
|
||||
|
||||
// publicIP says whether an address is one on the open internet.
|
||||
func publicIP(ip net.IP) bool {
|
||||
addr, ok := netip.AddrFromSlice(ip)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
addr = addr.Unmap()
|
||||
switch {
|
||||
case !addr.IsValid(), addr.IsLoopback(), addr.IsPrivate(), addr.IsLinkLocalUnicast(), addr.IsLinkLocalMulticast(),
|
||||
addr.IsMulticast(), addr.IsUnspecified(), addr.IsInterfaceLocalMulticast():
|
||||
return false
|
||||
}
|
||||
// The shared address space of carriers (100.64.0.0/10), which is not public either.
|
||||
if addr.Is4() {
|
||||
if b := addr.As4(); b[0] == 100 && b[1]&0xC0 == 64 {
|
||||
return false
|
||||
}
|
||||
if addr == netip.AddrFrom4([4]byte{255, 255, 255, 255}) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (r *HTTPRemote) do(ctx context.Context, method, base, path, token string, body io.Reader, headers map[string]string) ([]byte, int, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, method, base+path, body)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("%w: %v", ErrUnreachable, err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
req.Header.Set("User-Agent", r.userAgent)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
for k, v := range headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
resp, err := r.client.Do(req)
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("%w: %v", ErrUnreachable, unwrapURLError(err))
|
||||
}
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
data, err := io.ReadAll(io.LimitReader(resp.Body, maxAnswer))
|
||||
if err != nil {
|
||||
return nil, resp.StatusCode, fmt.Errorf("%w: reading the answer", ErrUnreachable)
|
||||
}
|
||||
switch {
|
||||
case resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden:
|
||||
return nil, resp.StatusCode, ErrRefused
|
||||
case resp.StatusCode < 200 || resp.StatusCode > 299:
|
||||
return nil, resp.StatusCode, fmt.Errorf("%w: the instance answered %d", ErrUnreachable, resp.StatusCode)
|
||||
}
|
||||
return data, resp.StatusCode, nil
|
||||
}
|
||||
|
||||
// unwrapURLError drops the address from a network error: it is typed by a person and goes into a log.
|
||||
func unwrapURLError(err error) error {
|
||||
var ue *url.Error
|
||||
if errors.As(err, &ue) {
|
||||
return ue.Err
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// plausibleAcct says whether an answer looks like name or name@domain: what the instance says about itself is not trusted.
|
||||
func plausibleAcct(acct string) bool {
|
||||
return acct != "" && len(acct) <= maxHandle && !strings.ContainsAny(acct, " \t\r\n/") && strings.Count(acct, "@") <= 1 && !strings.HasPrefix(acct, "@")
|
||||
}
|
||||
|
||||
// Verify implements Remote.
|
||||
func (r *HTTPRemote) Verify(ctx context.Context, instance, token string) (string, error) {
|
||||
data, _, err := r.do(ctx, http.MethodGet, instance, "/api/v1/accounts/verify_credentials", token, nil, nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var account struct {
|
||||
Acct string `json:"acct"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &account); err != nil || !plausibleAcct(account.Acct) {
|
||||
return "", fmt.Errorf("%w: not an account", ErrUnreachable)
|
||||
}
|
||||
acct := account.Acct
|
||||
if !strings.Contains(acct, "@") {
|
||||
u, err := url.Parse(instance)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%w: %v", ErrUnreachable, err)
|
||||
}
|
||||
acct += "@" + u.Hostname()
|
||||
}
|
||||
return "@" + acct, nil
|
||||
}
|
||||
|
||||
// Post implements Remote.
|
||||
func (r *HTTPRemote) Post(ctx context.Context, instance, token, status, key string) error {
|
||||
form := url.Values{"status": {status}, "visibility": {"unlisted"}}
|
||||
_, _, err := r.do(ctx, http.MethodPost, instance, "/api/v1/statuses", token, strings.NewReader(form.Encode()), map[string]string{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Idempotency-Key": key,
|
||||
})
|
||||
return err
|
||||
}
|
||||
149
internal/mastodon/client_test.go
Normal file
149
internal/mastodon/client_test.go
Normal file
|
|
@ -0,0 +1,149 @@
|
|||
package mastodon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// testRemote reaches the test server on the loopback, which the real one never does.
|
||||
func testRemote(srv *httptest.Server) *HTTPRemote {
|
||||
client := *srv.Client()
|
||||
client.CheckRedirect = refuseRedirect // the one the real client has
|
||||
return &HTTPRemote{client: &client, userAgent: "Sonda-test"}
|
||||
}
|
||||
|
||||
func TestVerifyAsksWhoTheTokenBelongsToAndAnswersWithTheHandle(t *testing.T) {
|
||||
var gotAuth, gotAgent, gotPath string
|
||||
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotAuth, gotAgent, gotPath = r.Header.Get("Authorization"), r.Header.Get("User-Agent"), r.URL.Path
|
||||
_, _ = io.WriteString(w, `{"acct":"squid","username":"squid"}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
handle, err := testRemote(srv).Verify(context.Background(), srv.URL, "tok")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
host := strings.TrimPrefix(srv.URL, "https://")
|
||||
if handle != "@squid@"+hostOnly(host) {
|
||||
t.Errorf("handle = %q", handle)
|
||||
}
|
||||
if gotAuth != "Bearer tok" || gotAgent != "Sonda-test" || gotPath != "/api/v1/accounts/verify_credentials" {
|
||||
t.Errorf("request: %q %q %q", gotAuth, gotAgent, gotPath)
|
||||
}
|
||||
}
|
||||
|
||||
func hostOnly(h string) string {
|
||||
if host, _, err := net.SplitHostPort(h); err == nil {
|
||||
return host
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func TestVerifyKeepsTheDomainOfAFederatedHandle(t *testing.T) {
|
||||
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = io.WriteString(w, `{"acct":"squid@other.example"}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
handle, err := testRemote(srv).Verify(context.Background(), srv.URL, "tok")
|
||||
if err != nil || handle != "@squid@other.example" {
|
||||
t.Errorf("handle = %q, %v", handle, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnInstanceThatRefusesTheTokenIsRefusedAndOneThatIsBrokenIsUnreachable(t *testing.T) {
|
||||
for status, want := range map[int]error{401: ErrRefused, 403: ErrRefused, 500: ErrUnreachable, 503: ErrUnreachable, 404: ErrUnreachable, 429: ErrUnreachable} {
|
||||
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(status) }))
|
||||
if _, err := testRemote(srv).Verify(context.Background(), srv.URL, "tok"); !errors.Is(err, want) {
|
||||
t.Errorf("verify %d: %v, want %v", status, err, want)
|
||||
}
|
||||
if err := testRemote(srv).Post(context.Background(), srv.URL, "tok", "hi", "k"); !errors.Is(err, want) {
|
||||
t.Errorf("post %d: %v, want %v", status, err, want)
|
||||
}
|
||||
srv.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnAnswerThatIsNotAnAccountIsNotTrusted(t *testing.T) {
|
||||
for _, body := range []string{"", "not json", `{}`, `{"acct":""}`, `{"acct":"a b"}`, `{"acct":"` + strings.Repeat("a", 400) + `"}`} {
|
||||
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, _ = io.WriteString(w, body) }))
|
||||
if handle, err := testRemote(srv).Verify(context.Background(), srv.URL, "tok"); !errors.Is(err, ErrUnreachable) {
|
||||
t.Errorf("body %.20q: handle %q, err %v", body, handle, err)
|
||||
}
|
||||
srv.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func TestPostSendsAnUnlistedStatusWithAnIdempotencyKey(t *testing.T) {
|
||||
var form url.Values
|
||||
var method, auth, key, contentType string
|
||||
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
method, auth, key, contentType = r.Method, r.Header.Get("Authorization"), r.Header.Get("Idempotency-Key"), r.Header.Get("Content-Type")
|
||||
_ = r.ParseForm()
|
||||
form = r.PostForm
|
||||
_, _ = io.WriteString(w, `{"id":"1"}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
if err := testRemote(srv).Post(context.Background(), srv.URL, "tok", "🎵 Sonar #NowPlaying", "abc123"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if method != "POST" || auth != "Bearer tok" || key != "abc123" || !strings.HasPrefix(contentType, "application/x-www-form-urlencoded") {
|
||||
t.Errorf("request: %s %q %q %q", method, auth, key, contentType)
|
||||
}
|
||||
if form.Get("status") != "🎵 Sonar #NowPlaying" || form.Get("visibility") != "unlisted" {
|
||||
t.Errorf("form = %v", form)
|
||||
}
|
||||
}
|
||||
|
||||
func TestARedirectIsNeverFollowed(t *testing.T) {
|
||||
hit := false
|
||||
other := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { hit = true }))
|
||||
defer other.Close()
|
||||
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, other.URL, http.StatusFound)
|
||||
}))
|
||||
defer srv.Close()
|
||||
if _, err := testRemote(srv).Verify(context.Background(), srv.URL, "tok"); !errors.Is(err, ErrUnreachable) {
|
||||
t.Errorf("err = %v", err)
|
||||
}
|
||||
if hit {
|
||||
t.Error("the redirect was followed, and the token went with it")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheRealClientNeverReachesAPrivateNetwork(t *testing.T) {
|
||||
hit := false
|
||||
srv := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { hit = true }))
|
||||
defer srv.Close()
|
||||
real := NewRemote("Sonda-test")
|
||||
for _, base := range []string{srv.URL, "https://127.0.0.1:1", "https://[::1]:1", "https://10.1.2.3", "https://192.168.1.10", "https://169.254.169.254", "https://localhost:1"} {
|
||||
if _, err := real.Verify(context.Background(), base, "tok"); !errors.Is(err, ErrUnreachable) {
|
||||
t.Errorf("%s: err = %v", base, err)
|
||||
}
|
||||
if err := real.Post(context.Background(), base, "tok", "x", "k"); !errors.Is(err, ErrUnreachable) {
|
||||
t.Errorf("%s: post err = %v", base, err)
|
||||
}
|
||||
}
|
||||
if hit {
|
||||
t.Error("the server on the loopback was reached")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPublicAddressesAreTheOnlyOnesAllowed(t *testing.T) {
|
||||
for ip, want := range map[string]bool{
|
||||
"8.8.8.8": true, "93.184.216.34": true, "2606:4700:4700::1111": true,
|
||||
"127.0.0.1": false, "::1": false, "10.0.0.1": false, "172.16.5.5": false, "192.168.0.1": false, "169.254.1.1": false,
|
||||
"100.64.0.1": false, "0.0.0.0": false, "::": false, "fe80::1": false, "fc00::1": false, "224.0.0.1": false, "255.255.255.255": false,
|
||||
"::ffff:127.0.0.1": false, "::ffff:10.0.0.1": false,
|
||||
} {
|
||||
if got := publicIP(net.ParseIP(ip)); got != want {
|
||||
t.Errorf("publicIP(%s) = %v, want %v", ip, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
270
internal/mastodon/mastodon.go
Normal file
270
internal/mastodon/mastodon.go
Normal file
|
|
@ -0,0 +1,270 @@
|
|||
// Package mastodon lets a person share what they listen to on their own Mastodon account.
|
||||
//
|
||||
// Nothing is posted until a person connects an account (the address of their instance and an access token that can
|
||||
// write posts), and disconnecting forgets the token. When a track of this server counts as a listen, it is posted as
|
||||
// an unlisted status: title, artists, album and a tag, with no link, because the server is private. A friend's music
|
||||
// never reaches this package: it has no track of this server.
|
||||
//
|
||||
// The token stays on the server. The address of the instance is whatever the person typed, so it is checked hard (https,
|
||||
// a name and not a number) and the client refuses to reach a private network (see Remote).
|
||||
package mastodon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Errors returned by the service.
|
||||
var (
|
||||
ErrBadInstance = errors.New("the address of the instance must be a domain name, like mastodon.social")
|
||||
ErrBadToken = errors.New("the access token is empty or too long")
|
||||
ErrRefused = errors.New("the instance refused the access token")
|
||||
ErrUnreachable = errors.New("the instance could not be reached")
|
||||
)
|
||||
|
||||
const (
|
||||
// maxPost is the length of a post on a stock Mastodon instance.
|
||||
maxPost = 500
|
||||
// postTag closes every post.
|
||||
postTag = " #NowPlaying"
|
||||
// minGap is the shortest time between two posts of one person.
|
||||
minGap = time.Minute
|
||||
// maxToken is far more than any token is long.
|
||||
maxToken = 512
|
||||
// queueSize is how many listens wait for the worker. When it is full, the newest are dropped: a post is never worth
|
||||
// holding the server back.
|
||||
queueSize = 64
|
||||
postTimeout = 20 * time.Second
|
||||
)
|
||||
|
||||
// Account is what a person may see of their connection: never the token.
|
||||
type Account struct {
|
||||
Instance string // https://mastodon.social
|
||||
Handle string // @squid@mastodon.social
|
||||
}
|
||||
|
||||
// Credentials are what is kept for a person.
|
||||
type Credentials struct {
|
||||
Instance string
|
||||
Token string
|
||||
Handle string
|
||||
}
|
||||
|
||||
// Track is what a post says about a track.
|
||||
type Track struct {
|
||||
Title string
|
||||
Artists []string
|
||||
Album string
|
||||
}
|
||||
|
||||
// Store keeps the accounts and knows the tracks.
|
||||
type Store interface {
|
||||
// Get returns what is kept for a person, with found=false when nothing is.
|
||||
Get(ctx context.Context, userID int64) (Credentials, bool, error)
|
||||
// Save keeps the credentials of a person, replacing older ones.
|
||||
Save(ctx context.Context, userID int64, c Credentials) error
|
||||
// Delete forgets them. A person with nothing kept is not an error.
|
||||
Delete(ctx context.Context, userID int64) error
|
||||
// Track returns what a post needs to say, with found=false for a track that is gone.
|
||||
Track(ctx context.Context, trackID int64) (Track, bool, error)
|
||||
}
|
||||
|
||||
// Remote talks to a Mastodon instance. The address is always one Normalize accepted.
|
||||
type Remote interface {
|
||||
// Verify asks the instance who the token belongs to and returns the handle (@name@instance).
|
||||
// ErrRefused means the token is not valid; ErrUnreachable that the instance could not be asked.
|
||||
Verify(ctx context.Context, instance, token string) (string, error)
|
||||
// Post publishes an unlisted status. `key` makes a retry of the same post harmless.
|
||||
Post(ctx context.Context, instance, token, status, key string) error
|
||||
}
|
||||
|
||||
// Normalize turns what a person typed (mastodon.social, https://mastodon.social/) into https://mastodon.social. It
|
||||
// refuses what is not a domain name on https: a number, a name with no dot, a path, a password.
|
||||
func Normalize(in string) (string, error) {
|
||||
in = strings.TrimSpace(in)
|
||||
if in == "" || strings.ContainsAny(in, " \t\r\n@") {
|
||||
return "", ErrBadInstance
|
||||
}
|
||||
if !strings.Contains(in, "://") {
|
||||
in = "https://" + in
|
||||
}
|
||||
u, err := url.Parse(in)
|
||||
if err != nil || u.Scheme != "https" || u.User != nil || u.RawQuery != "" || u.Fragment != "" || u.Opaque != "" {
|
||||
return "", ErrBadInstance
|
||||
}
|
||||
if strings.Trim(u.Path, "/") != "" {
|
||||
return "", ErrBadInstance
|
||||
}
|
||||
host := strings.ToLower(u.Hostname())
|
||||
if host == "" || len(host) > 253 || !strings.Contains(host, ".") || strings.HasSuffix(host, ".") {
|
||||
return "", ErrBadInstance
|
||||
}
|
||||
for _, r := range host {
|
||||
ok := r == '-' || r == '.' || (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || r > 127
|
||||
if !ok {
|
||||
return "", ErrBadInstance
|
||||
}
|
||||
}
|
||||
// A number is not the name of an instance (and an address like 10.0.0.5 would be a way into a private network).
|
||||
if last := host[strings.LastIndex(host, ".")+1:]; last == "" || strings.Trim(last, "0123456789") == "" {
|
||||
return "", ErrBadInstance
|
||||
}
|
||||
if port := u.Port(); port != "" {
|
||||
return "https://" + host + ":" + port, nil
|
||||
}
|
||||
return "https://" + host, nil
|
||||
}
|
||||
|
||||
// Compose is the text of a post: "🎵 Title – Artists (Album) #NowPlaying", cut with a mark where it would be too long.
|
||||
func Compose(t Track) string {
|
||||
text := "🎵 " + t.Title
|
||||
if len(t.Artists) > 0 {
|
||||
text += " – " + strings.Join(t.Artists, ", ")
|
||||
}
|
||||
if t.Album != "" {
|
||||
text += " (" + t.Album + ")"
|
||||
}
|
||||
room := maxPost - utf8.RuneCountInString(postTag)
|
||||
if utf8.RuneCountInString(text) > room {
|
||||
text = string([]rune(text)[:room-1]) + "…"
|
||||
}
|
||||
return text + postTag
|
||||
}
|
||||
|
||||
type listen struct{ userID, trackID int64 }
|
||||
|
||||
// Service connects accounts and posts listens.
|
||||
type Service struct {
|
||||
store Store
|
||||
remote Remote
|
||||
log *zap.Logger
|
||||
now func() time.Time
|
||||
queue chan listen
|
||||
|
||||
mu sync.Mutex
|
||||
last map[int64]time.Time
|
||||
}
|
||||
|
||||
// NewService builds a Service. Call Run to have it post.
|
||||
func NewService(store Store, remote Remote, log *zap.Logger) *Service {
|
||||
return &Service{store: store, remote: remote, log: log, now: time.Now, queue: make(chan listen, queueSize), last: map[int64]time.Time{}}
|
||||
}
|
||||
|
||||
// Status says which account a person connected.
|
||||
func (s *Service) Status(ctx context.Context, userID int64) (Account, bool, error) {
|
||||
c, found, err := s.store.Get(ctx, userID)
|
||||
if err != nil || !found {
|
||||
return Account{}, false, err
|
||||
}
|
||||
return Account{Instance: c.Instance, Handle: c.Handle}, true, nil
|
||||
}
|
||||
|
||||
// Connect checks the token with the instance and keeps the account. Nothing is kept when the check fails.
|
||||
func (s *Service) Connect(ctx context.Context, userID int64, instance, token string) (Account, error) {
|
||||
base, err := Normalize(instance)
|
||||
if err != nil {
|
||||
return Account{}, err
|
||||
}
|
||||
token = strings.TrimSpace(token)
|
||||
if token == "" || len(token) > maxToken {
|
||||
return Account{}, ErrBadToken
|
||||
}
|
||||
handle, err := s.remote.Verify(ctx, base, token)
|
||||
if err != nil {
|
||||
return Account{}, err
|
||||
}
|
||||
if err := s.store.Save(ctx, userID, Credentials{Instance: base, Token: token, Handle: handle}); err != nil {
|
||||
return Account{}, fmt.Errorf("keep the account: %w", err)
|
||||
}
|
||||
return Account{Instance: base, Handle: handle}, nil
|
||||
}
|
||||
|
||||
// Disconnect forgets the account and its token.
|
||||
func (s *Service) Disconnect(ctx context.Context, userID int64) error {
|
||||
s.mu.Lock()
|
||||
delete(s.last, userID)
|
||||
s.mu.Unlock()
|
||||
return s.store.Delete(ctx, userID)
|
||||
}
|
||||
|
||||
// Announce asks for a track to be posted for a person, if they connected an account. It never waits and never fails:
|
||||
// when too many are waiting the listen is simply not posted.
|
||||
func (s *Service) Announce(userID, trackID int64) {
|
||||
select {
|
||||
case s.queue <- listen{userID, trackID}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// Run posts what Announce was asked until the context ends.
|
||||
func (s *Service) Run(ctx context.Context) {
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case l := <-s.queue:
|
||||
pctx, cancel := context.WithTimeout(ctx, postTimeout)
|
||||
if err := s.announce(pctx, l.userID, l.trackID); err != nil {
|
||||
// No name, no track: only what went wrong.
|
||||
s.log.Warn("mastodon post failed", zap.Error(err))
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// announce posts one listen, unless nothing is connected, the track is gone or a post went out less than a minute ago.
|
||||
func (s *Service) announce(ctx context.Context, userID, trackID int64) error {
|
||||
c, found, err := s.store.Get(ctx, userID)
|
||||
if err != nil || !found {
|
||||
return err
|
||||
}
|
||||
track, found, err := s.store.Track(ctx, trackID)
|
||||
if err != nil || !found {
|
||||
return err
|
||||
}
|
||||
now := s.now()
|
||||
s.mu.Lock()
|
||||
if at, ok := s.last[userID]; ok && now.Sub(at) < minGap {
|
||||
s.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
// The minute starts now, so that a burst does not post twice while the first is still on its way; a failure gives it back.
|
||||
prev, had := s.last[userID]
|
||||
s.last[userID] = now
|
||||
s.mu.Unlock()
|
||||
|
||||
key, err := newKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.remote.Post(ctx, c.Instance, c.Token, Compose(track), key); err != nil {
|
||||
s.mu.Lock()
|
||||
if had {
|
||||
s.last[userID] = prev
|
||||
} else {
|
||||
delete(s.last, userID)
|
||||
}
|
||||
s.mu.Unlock()
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newKey() (string, error) {
|
||||
b := make([]byte, 16)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(b), nil
|
||||
}
|
||||
303
internal/mastodon/mastodon_test.go
Normal file
303
internal/mastodon/mastodon_test.go
Normal file
|
|
@ -0,0 +1,303 @@
|
|||
package mastodon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func TestNormalizeTakesTheAddressOfAnInstanceInTheWaysPeopleWriteIt(t *testing.T) {
|
||||
for in, want := range map[string]string{
|
||||
"mastodon.social": "https://mastodon.social",
|
||||
" Mastodon.Social ": "https://mastodon.social",
|
||||
"https://mastodon.social": "https://mastodon.social",
|
||||
"https://mastodon.social/": "https://mastodon.social",
|
||||
"https://mastodon.uno:8443": "https://mastodon.uno:8443",
|
||||
"social.example.org/": "https://social.example.org",
|
||||
"https://social.example.org//": "https://social.example.org",
|
||||
} {
|
||||
got, err := Normalize(in)
|
||||
if err != nil || got != want {
|
||||
t.Errorf("Normalize(%q) = %q, %v; want %q", in, got, err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeRefusesWhatIsNotTheAddressOfAPublicInstance(t *testing.T) {
|
||||
long := strings.Repeat("a", 250) + ".org"
|
||||
for _, in := range []string{
|
||||
"", " ", "http://mastodon.social", "ftp://mastodon.social", "localhost", "intranet", "127.0.0.1", "https://10.0.0.5",
|
||||
"https://[::1]", "https://user:pass@mastodon.social", "https://mastodon.social/path", "https://mastodon.social/?x=1",
|
||||
"https://mastodon.social#top", "mastodon social", "https://", long, "@squid@mastodon.social",
|
||||
} {
|
||||
if got, err := Normalize(in); !errors.Is(err, ErrBadInstance) {
|
||||
t.Errorf("Normalize(%q) = %q, %v; want ErrBadInstance", in, got, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposeSaysWhatWasHeardAndNothingMore(t *testing.T) {
|
||||
got := Compose(Track{Title: "Sonar", Artists: []string{"Nora Valdés", "Teo"}, Album: "Luce Sommersa"})
|
||||
want := "🎵 Sonar – Nora Valdés, Teo (Luce Sommersa) #NowPlaying"
|
||||
if got != want {
|
||||
t.Errorf("Compose = %q, want %q", got, want)
|
||||
}
|
||||
if got := Compose(Track{Title: "Sonar"}); got != "🎵 Sonar #NowPlaying" {
|
||||
t.Errorf("a track with no artist and no album: %q", got)
|
||||
}
|
||||
if got := Compose(Track{Title: "Sonar", Artists: []string{"Nora"}}); got != "🎵 Sonar – Nora #NowPlaying" {
|
||||
t.Errorf("a track with no album: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposeNeverPassesTheLimitOfAPost(t *testing.T) {
|
||||
got := Compose(Track{Title: strings.Repeat("é", 800), Artists: []string{"Nora"}, Album: "x"})
|
||||
if n := len([]rune(got)); n > maxPost {
|
||||
t.Errorf("%d characters, the limit is %d", n, maxPost)
|
||||
}
|
||||
if !strings.HasSuffix(got, " #NowPlaying") || !strings.Contains(got, "…") {
|
||||
t.Errorf("it is cut with a mark and keeps the tag: %q", got[len(got)-30:])
|
||||
}
|
||||
}
|
||||
|
||||
type fakeStore struct {
|
||||
mu sync.Mutex
|
||||
creds map[int64]Credentials
|
||||
tracks map[int64]Track
|
||||
}
|
||||
|
||||
func newFakeStore() *fakeStore {
|
||||
return &fakeStore{creds: map[int64]Credentials{}, tracks: map[int64]Track{7: {Title: "Sonar", Artists: []string{"Nora"}, Album: "Luce"}}}
|
||||
}
|
||||
|
||||
func (f *fakeStore) Get(_ context.Context, id int64) (Credentials, bool, error) {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
c, ok := f.creds[id]
|
||||
return c, ok, nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Save(_ context.Context, id int64, c Credentials) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
f.creds[id] = c
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Delete(_ context.Context, id int64) error {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
delete(f.creds, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeStore) Track(_ context.Context, id int64) (Track, bool, error) {
|
||||
t, ok := f.tracks[id]
|
||||
return t, ok, nil
|
||||
}
|
||||
|
||||
type post struct{ instance, token, status, key string }
|
||||
|
||||
type fakeRemote struct {
|
||||
mu sync.Mutex
|
||||
handle string
|
||||
verify error
|
||||
postErr error
|
||||
posts []post
|
||||
posted chan post
|
||||
}
|
||||
|
||||
func (f *fakeRemote) Verify(_ context.Context, instance, token string) (string, error) {
|
||||
if f.verify != nil {
|
||||
return "", f.verify
|
||||
}
|
||||
return f.handle, nil
|
||||
}
|
||||
|
||||
func (f *fakeRemote) Post(_ context.Context, instance, token, status, key string) error {
|
||||
f.mu.Lock()
|
||||
p := post{instance, token, status, key}
|
||||
f.posts = append(f.posts, p)
|
||||
err := f.postErr
|
||||
f.mu.Unlock()
|
||||
if f.posted != nil {
|
||||
f.posted <- p
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (f *fakeRemote) count() int {
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
return len(f.posts)
|
||||
}
|
||||
|
||||
func newService(t *testing.T) (*Service, *fakeStore, *fakeRemote) {
|
||||
t.Helper()
|
||||
st, rm := newFakeStore(), &fakeRemote{handle: "@squid@mastodon.social"}
|
||||
return NewService(st, rm, zap.NewNop()), st, rm
|
||||
}
|
||||
|
||||
func TestNothingIsConnectedUntilTheUserConnectsAnAccount(t *testing.T) {
|
||||
s, _, _ := newService(t)
|
||||
if _, connected, err := s.Status(context.Background(), 1); err != nil || connected {
|
||||
t.Fatalf("Status = connected %v, %v", connected, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectChecksTheTokenAndKeepsTheAccountWithoutShowingTheToken(t *testing.T) {
|
||||
s, st, _ := newService(t)
|
||||
acc, err := s.Connect(context.Background(), 1, "Mastodon.Social/", "secret-token")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if acc.Instance != "https://mastodon.social" || acc.Handle != "@squid@mastodon.social" {
|
||||
t.Errorf("account = %+v", acc)
|
||||
}
|
||||
if got := st.creds[1]; got.Token != "secret-token" || got.Instance != "https://mastodon.social" {
|
||||
t.Errorf("kept = %+v", got)
|
||||
}
|
||||
got, connected, _ := s.Status(context.Background(), 1)
|
||||
if !connected || got != acc {
|
||||
t.Errorf("Status = %+v, %v", got, connected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConnectKeepsNothingWhenTheTokenIsRefusedOrTheInstanceIsNotThere(t *testing.T) {
|
||||
for _, bad := range []error{ErrRefused, ErrUnreachable} {
|
||||
s, st, rm := newService(t)
|
||||
rm.verify = bad
|
||||
if _, err := s.Connect(context.Background(), 1, "mastodon.social", "tok"); !errors.Is(err, bad) {
|
||||
t.Errorf("err = %v, want %v", err, bad)
|
||||
}
|
||||
if len(st.creds) != 0 {
|
||||
t.Errorf("%v: something was kept: %+v", bad, st.creds)
|
||||
}
|
||||
}
|
||||
s, _, _ := newService(t)
|
||||
if _, err := s.Connect(context.Background(), 1, "http://mastodon.social", "tok"); !errors.Is(err, ErrBadInstance) {
|
||||
t.Errorf("a plain http address: %v", err)
|
||||
}
|
||||
if _, err := s.Connect(context.Background(), 1, "mastodon.social", " "); !errors.Is(err, ErrBadToken) {
|
||||
t.Errorf("an empty token: %v", err)
|
||||
}
|
||||
if _, err := s.Connect(context.Background(), 1, "mastodon.social", strings.Repeat("x", 600)); !errors.Is(err, ErrBadToken) {
|
||||
t.Errorf("a token that is far too long: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisconnectForgetsTheToken(t *testing.T) {
|
||||
s, st, _ := newService(t)
|
||||
_, _ = s.Connect(context.Background(), 1, "mastodon.social", "tok")
|
||||
if err := s.Disconnect(context.Background(), 1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(st.creds) != 0 {
|
||||
t.Errorf("still kept: %+v", st.creds)
|
||||
}
|
||||
if err := s.Disconnect(context.Background(), 1); err != nil {
|
||||
t.Errorf("disconnecting twice is not an error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnAnnouncementIsAnUnlistedPostOfTheTrackToTheAccountOfWhoListened(t *testing.T) {
|
||||
s, st, rm := newService(t)
|
||||
_ = st.Save(context.Background(), 1, Credentials{Instance: "https://mastodon.social", Token: "tok", Handle: "@squid@mastodon.social"})
|
||||
if err := s.announce(context.Background(), 1, 7); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if rm.count() != 1 {
|
||||
t.Fatalf("%d posts", rm.count())
|
||||
}
|
||||
p := rm.posts[0]
|
||||
if p.instance != "https://mastodon.social" || p.token != "tok" || p.status != "🎵 Sonar – Nora (Luce) #NowPlaying" || p.key == "" {
|
||||
t.Errorf("post = %+v", p)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNothingIsPostedForWhoDidNotConnectAnAccountOrForATrackThatIsGone(t *testing.T) {
|
||||
s, st, rm := newService(t)
|
||||
if err := s.announce(context.Background(), 1, 7); err != nil || rm.count() != 0 {
|
||||
t.Errorf("not connected: %v, %d posts", err, rm.count())
|
||||
}
|
||||
_ = st.Save(context.Background(), 1, Credentials{Instance: "https://mastodon.social", Token: "tok"})
|
||||
if err := s.announce(context.Background(), 1, 999); err != nil || rm.count() != 0 {
|
||||
t.Errorf("unknown track: %v, %d posts", err, rm.count())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAtMostOnePostAMinuteForEachPerson(t *testing.T) {
|
||||
s, st, rm := newService(t)
|
||||
now := time.Date(2026, 11, 14, 10, 0, 0, 0, time.UTC)
|
||||
s.now = func() time.Time { return now }
|
||||
for _, id := range []int64{1, 2} {
|
||||
_ = st.Save(context.Background(), id, Credentials{Instance: "https://mastodon.social", Token: "tok"})
|
||||
}
|
||||
_ = s.announce(context.Background(), 1, 7)
|
||||
now = now.Add(30 * time.Second)
|
||||
_ = s.announce(context.Background(), 1, 7)
|
||||
_ = s.announce(context.Background(), 2, 7) // somebody else is not held back by the first
|
||||
if rm.count() != 2 {
|
||||
t.Fatalf("%d posts, want 2", rm.count())
|
||||
}
|
||||
now = now.Add(31 * time.Second)
|
||||
_ = s.announce(context.Background(), 1, 7)
|
||||
if rm.count() != 3 {
|
||||
t.Errorf("a minute later it posts again: %d posts", rm.count())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAFailedPostIsNotRetriedAndDoesNotCountAgainstTheMinute(t *testing.T) {
|
||||
s, st, rm := newService(t)
|
||||
_ = st.Save(context.Background(), 1, Credentials{Instance: "https://mastodon.social", Token: "tok"})
|
||||
rm.postErr = ErrUnreachable
|
||||
if err := s.announce(context.Background(), 1, 7); !errors.Is(err, ErrUnreachable) {
|
||||
t.Errorf("err = %v", err)
|
||||
}
|
||||
rm.postErr = nil
|
||||
_ = s.announce(context.Background(), 1, 7)
|
||||
if rm.count() != 2 {
|
||||
t.Errorf("%d posts: the failed one must not hold back the next", rm.count())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnnounceReturnsAtOnceAndTheWorkerPostsInTheBackground(t *testing.T) {
|
||||
s, st, rm := newService(t)
|
||||
rm.posted = make(chan post, 1)
|
||||
_ = st.Save(context.Background(), 1, Credentials{Instance: "https://mastodon.social", Token: "tok"})
|
||||
ctx, stop := context.WithCancel(context.Background())
|
||||
done := make(chan struct{})
|
||||
go func() { s.Run(ctx); close(done) }()
|
||||
s.Announce(1, 7)
|
||||
select {
|
||||
case p := <-rm.posted:
|
||||
if p.status == "" {
|
||||
t.Error("empty post")
|
||||
}
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("nothing was posted")
|
||||
}
|
||||
stop()
|
||||
<-done
|
||||
}
|
||||
|
||||
func TestAnnounceNeverBlocksEvenWhenNobodyIsWorking(t *testing.T) {
|
||||
s, _, _ := newService(t)
|
||||
finished := make(chan struct{})
|
||||
go func() {
|
||||
for i := 0; i < 500; i++ {
|
||||
s.Announce(1, 7)
|
||||
}
|
||||
close(finished)
|
||||
}()
|
||||
select {
|
||||
case <-finished:
|
||||
case <-time.After(3 * time.Second):
|
||||
t.Fatal("Announce blocked")
|
||||
}
|
||||
}
|
||||
100
internal/storage/mastodon.go
Normal file
100
internal/storage/mastodon.go
Normal file
|
|
@ -0,0 +1,100 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
|
||||
"codeberg.org/squidd/sonda/internal/mastodon"
|
||||
)
|
||||
|
||||
type mastodonRow struct {
|
||||
bun.BaseModel `bun:"table:mastodon_accounts"`
|
||||
|
||||
UserID int64 `bun:"user_id,pk"`
|
||||
Instance string `bun:"instance,notnull"`
|
||||
Token string `bun:"token,notnull"`
|
||||
Handle string `bun:"handle,notnull"`
|
||||
CreatedAt int64 `bun:"created_at,notnull"`
|
||||
}
|
||||
|
||||
// MastodonRepo keeps the Mastodon accounts people connected, and knows the tracks a post speaks about. It implements
|
||||
// mastodon.Store.
|
||||
type MastodonRepo struct{ db *bun.DB }
|
||||
|
||||
// NewMastodonRepo builds a MastodonRepo.
|
||||
func NewMastodonRepo(db *bun.DB) *MastodonRepo { return &MastodonRepo{db: db} }
|
||||
|
||||
// Get returns what is kept for a person, with found=false when nothing is.
|
||||
func (r *MastodonRepo) Get(ctx context.Context, userID int64) (mastodon.Credentials, bool, error) {
|
||||
var row mastodonRow
|
||||
err := r.db.NewSelect().Model(&row).Where("user_id = ?", userID).Limit(1).Scan(ctx)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return mastodon.Credentials{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return mastodon.Credentials{}, false, fmt.Errorf("select mastodon account: %w", err)
|
||||
}
|
||||
return mastodon.Credentials{Instance: row.Instance, Token: row.Token, Handle: row.Handle}, true, nil
|
||||
}
|
||||
|
||||
// Save keeps the account of a person, replacing an older one.
|
||||
func (r *MastodonRepo) Save(ctx context.Context, userID int64, c mastodon.Credentials) error {
|
||||
row := &mastodonRow{UserID: userID, Instance: c.Instance, Token: c.Token, Handle: c.Handle, CreatedAt: time.Now().Unix()}
|
||||
_, err := r.db.NewInsert().Model(row).
|
||||
On("CONFLICT (user_id) DO UPDATE").
|
||||
Set("instance = EXCLUDED.instance").
|
||||
Set("token = EXCLUDED.token").
|
||||
Set("handle = EXCLUDED.handle").
|
||||
Set("created_at = EXCLUDED.created_at").
|
||||
Exec(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("save mastodon account: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete forgets the account of a person. A person with none is not an error.
|
||||
func (r *MastodonRepo) Delete(ctx context.Context, userID int64) error {
|
||||
if _, err := r.db.NewDelete().Model((*mastodonRow)(nil)).Where("user_id = ?", userID).Exec(ctx); err != nil {
|
||||
return fmt.Errorf("delete mastodon account: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Track returns the title, the main artists and the album of a track, with found=false for a track that is gone.
|
||||
func (r *MastodonRepo) Track(ctx context.Context, trackID int64) (mastodon.Track, bool, error) {
|
||||
var head struct {
|
||||
Title string `bun:"title"`
|
||||
Album sql.NullString `bun:"album"`
|
||||
}
|
||||
err := r.db.NewSelect().
|
||||
TableExpr("tracks AS t").
|
||||
ColumnExpr("t.title AS title, al.title AS album").
|
||||
Join("LEFT JOIN albums AS al ON al.id = t.album_id").
|
||||
Where("t.id = ?", trackID).
|
||||
Limit(1).
|
||||
Scan(ctx, &head)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return mastodon.Track{}, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return mastodon.Track{}, false, fmt.Errorf("select track for a post: %w", err)
|
||||
}
|
||||
var names []string
|
||||
err = r.db.NewSelect().
|
||||
TableExpr("track_artists AS ta").
|
||||
ColumnExpr("a.name").
|
||||
Join("JOIN artists AS a ON a.id = ta.artist_id").
|
||||
Where("ta.track_id = ? AND ta.role = 'main'", trackID).
|
||||
OrderExpr("a.id").
|
||||
Scan(ctx, &names)
|
||||
if err != nil {
|
||||
return mastodon.Track{}, false, fmt.Errorf("select artists for a post: %w", err)
|
||||
}
|
||||
return mastodon.Track{Title: head.Title, Artists: names, Album: head.Album.String}, true, nil
|
||||
}
|
||||
119
internal/storage/mastodon_test.go
Normal file
119
internal/storage/mastodon_test.go
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
|
||||
"codeberg.org/squidd/sonda/internal/mastodon"
|
||||
)
|
||||
|
||||
func seedUser(t *testing.T, db *bun.DB, email string) int64 {
|
||||
t.Helper()
|
||||
var id int64
|
||||
if err := db.QueryRow("INSERT INTO users (email, password_hash, is_admin, created_at) VALUES (?, 'x', ?, 0) RETURNING id", email, false).Scan(&id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func TestAMastodonAccountIsKeptReplacedAndForgotten(t *testing.T) {
|
||||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||||
ctx := context.Background()
|
||||
repo := NewMastodonRepo(db)
|
||||
uid := seedUser(t, db, "a@example.org")
|
||||
if _, found, err := repo.Get(ctx, uid); err != nil || found {
|
||||
t.Fatalf("before any save: found %v, %v", found, err)
|
||||
}
|
||||
first := mastodon.Credentials{Instance: "https://mastodon.social", Token: "one", Handle: "@a@mastodon.social"}
|
||||
if err := repo.Save(ctx, uid, first); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, found, err := repo.Get(ctx, uid); err != nil || !found || got != first {
|
||||
t.Fatalf("Get = %+v, %v, %v", got, found, err)
|
||||
}
|
||||
second := mastodon.Credentials{Instance: "https://mastodon.uno", Token: "two", Handle: "@a@mastodon.uno"}
|
||||
if err := repo.Save(ctx, uid, second); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got, _, _ := repo.Get(ctx, uid); got != second {
|
||||
t.Errorf("after the second save: %+v", got)
|
||||
}
|
||||
if err := repo.Delete(ctx, uid); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, found, _ := repo.Get(ctx, uid); found {
|
||||
t.Error("still kept after Delete")
|
||||
}
|
||||
if err := repo.Delete(ctx, uid); err != nil {
|
||||
t.Errorf("deleting nothing is not an error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestAMastodonAccountBelongsToOnePersonAndGoesWithThem(t *testing.T) {
|
||||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||||
ctx := context.Background()
|
||||
repo := NewMastodonRepo(db)
|
||||
a, b := seedUser(t, db, "a@example.org"), seedUser(t, db, "b@example.org")
|
||||
_ = repo.Save(ctx, a, mastodon.Credentials{Instance: "https://mastodon.social", Token: "a", Handle: "@a@x"})
|
||||
if _, found, _ := repo.Get(ctx, b); found {
|
||||
t.Error("somebody else's account was found")
|
||||
}
|
||||
if _, err := db.Exec("DELETE FROM users WHERE id = ?", a); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, found, _ := repo.Get(ctx, a); found {
|
||||
t.Error("the account of a deleted person is still kept")
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestTheTrackOfAPostHasItsTitleItsMainArtistsAndItsAlbum(t *testing.T) {
|
||||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||||
ctx := context.Background()
|
||||
repo := NewMastodonRepo(db)
|
||||
var artist1, artist2, feat, album, track, bare int64
|
||||
for name, dst := range map[string]*int64{"Nora Valdés": &artist1, "Teo": &artist2, "Guest": &feat} {
|
||||
if err := db.QueryRow("INSERT INTO artists (name) VALUES (?) RETURNING id", name).Scan(dst); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := db.QueryRow("INSERT INTO albums (title) VALUES ('Luce Sommersa') RETURNING id").Scan(&album); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.QueryRow("INSERT INTO tracks (title, album_id) VALUES ('Sonar', ?) RETURNING id", album).Scan(&track); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.QueryRow("INSERT INTO tracks (title) VALUES ('Bare') RETURNING id").Scan(&bare); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, a := range []struct {
|
||||
id int64
|
||||
role string
|
||||
}{{artist1, "main"}, {artist2, "main"}, {feat, "featured"}} {
|
||||
if _, err := db.Exec("INSERT INTO track_artists (track_id, artist_id, role) VALUES (?, ?, ?)", track, a.id, a.role); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
got, found, err := repo.Track(ctx, track)
|
||||
if err != nil || !found {
|
||||
t.Fatalf("Track = %+v, %v, %v", got, found, err)
|
||||
}
|
||||
if got.Title != "Sonar" || got.Album != "Luce Sommersa" || len(got.Artists) != 2 || got.Artists[0] != "Nora Valdés" && got.Artists[1] != "Nora Valdés" {
|
||||
t.Errorf("track = %+v", got)
|
||||
}
|
||||
for _, name := range got.Artists {
|
||||
if name == "Guest" {
|
||||
t.Error("a featured artist is not one of the main ones")
|
||||
}
|
||||
}
|
||||
if got, found, _ := repo.Track(ctx, bare); !found || got.Title != "Bare" || got.Album != "" || len(got.Artists) != 0 {
|
||||
t.Errorf("a track with no album and no artist: %+v, %v", got, found)
|
||||
}
|
||||
if _, found, err := repo.Track(ctx, 99999); err != nil || found {
|
||||
t.Errorf("unknown track: found %v, %v", found, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
@ -147,7 +147,7 @@ func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) {
|
|||
if err := Migrate(context.Background(), db, fsys); err != nil {
|
||||
t.Fatalf("second Migrate: %v", err)
|
||||
}
|
||||
for _, table := range []string{"artists", "albums", "tracks", "track_artists", "audio_files", "users", "listening_events", "refresh_tokens", "libraries", "provider_settings", "artist_similar", "provider_fetches", "artist_tags", "track_genres", "user_settings", "artist_feedback", "images", "artist_descriptions", "federation_settings", "federation_invites", "federation_friends", "federation_requests", "track_peaks"} {
|
||||
for _, table := range []string{"artists", "albums", "tracks", "track_artists", "audio_files", "users", "listening_events", "refresh_tokens", "libraries", "provider_settings", "artist_similar", "provider_fetches", "artist_tags", "track_genres", "user_settings", "artist_feedback", "images", "artist_descriptions", "federation_settings", "federation_invites", "federation_friends", "federation_requests", "track_peaks", "mastodon_accounts"} {
|
||||
if !tableExists(t, db, driver, table) {
|
||||
t.Errorf("table %s missing", table)
|
||||
}
|
||||
|
|
@ -156,8 +156,8 @@ func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) {
|
|||
if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&applied); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if applied != 25 {
|
||||
t.Errorf("applied migrations = %d, want 25", applied)
|
||||
if applied != 26 {
|
||||
t.Errorf("applied migrations = %d, want 26", applied)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue