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.
110 lines
3.9 KiB
Go
110 lines
3.9 KiB
Go
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"`
|
|
Problem string `bun:"problem,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, Problem: row.Problem}, 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, Problem: c.Problem, 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("problem = EXCLUDED.problem").
|
|
Set("created_at = EXCLUDED.created_at").
|
|
Exec(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("save mastodon account: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetProblem notes what went wrong with the account of a person; a person with none is not an error.
|
|
func (r *MastodonRepo) SetProblem(ctx context.Context, userID int64, problem string) error {
|
|
if _, err := r.db.NewUpdate().Model((*mastodonRow)(nil)).Set("problem = ?", problem).Where("user_id = ?", userID).Exec(ctx); err != nil {
|
|
return fmt.Errorf("set mastodon problem: %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
|
|
}
|