feat: the radio stations are in the apps only when the person asks
A new setting, radio_in_apps, off until the person turns it on (Settings, Apps and devices, or PATCH /me/settings). With it off, the list of playlists of an OpenSubsonic app is only the playlists of the person, and a station asked for by id is not found. With it on, the stations come as playlists called Radio · Rock. - migration 0036: user_settings.radio_in_apps - usersettings, the settings API and the OpenAPI spec have the new field; the PATCH now keeps each setting apart (naming one no longer sets the other) - Settings has a switch
This commit is contained in:
parent
8e278f04c2
commit
ed32aac228
13 changed files with 310 additions and 25 deletions
|
|
@ -150,7 +150,7 @@ func newTestEnvDB(t *testing.T, mutate func(*Deps, *bun.DB)) (http.Handler, *bun
|
|||
Libraries: deps.Libraries.(opensubsonic.Libraries), ServerVersion: "test",
|
||||
Extras: subsonic, Lists: subsonic, Streams: deps.Streams.(opensubsonic.Streams),
|
||||
Covers: deps.Images.(opensubsonic.Covers), ArtistImages: deps.ArtistImages.(opensubsonic.ArtistImages),
|
||||
Playlists: deps.Playlists.(opensubsonic.Playlists), Listens: deps.Listening.(opensubsonic.Listens), Announcer: deps.Mastodon.(opensubsonic.Announcer), Favorites: deps.Favorites.(opensubsonic.Favorites), Radio: deps.Mixes.(opensubsonic.Radio),
|
||||
Playlists: deps.Playlists.(opensubsonic.Playlists), Listens: deps.Listening.(opensubsonic.Listens), Announcer: deps.Mastodon.(opensubsonic.Announcer), Favorites: deps.Favorites.(opensubsonic.Favorites), Radio: deps.Mixes.(opensubsonic.Radio), Settings: deps.Settings.(opensubsonic.Settings),
|
||||
}
|
||||
}
|
||||
if deps.About == nil {
|
||||
|
|
|
|||
|
|
@ -16,6 +16,11 @@ type userSettingsHandlers struct{ svc userSettingsService }
|
|||
|
||||
type userSettingsJSON struct {
|
||||
DiscoverFromListening bool `json:"discover_from_listening"`
|
||||
RadioInApps bool `json:"radio_in_apps"`
|
||||
}
|
||||
|
||||
func toUserSettingsJSON(s usersettings.Settings) userSettingsJSON {
|
||||
return userSettingsJSON{DiscoverFromListening: s.DiscoverFromListening, RadioInApps: s.RadioInApps}
|
||||
}
|
||||
|
||||
func writeUserSettingsError(c *gin.Context, err error) {
|
||||
|
|
@ -33,11 +38,11 @@ func (h *userSettingsHandlers) get(c *gin.Context) {
|
|||
writeUserSettingsError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, userSettingsJSON{DiscoverFromListening: s.DiscoverFromListening})
|
||||
c.JSON(http.StatusOK, toUserSettingsJSON(s))
|
||||
}
|
||||
|
||||
// settingKeys are the settings a PATCH may name, spelt exactly like this.
|
||||
var settingKeys = map[string]bool{"discover_from_listening": true}
|
||||
var settingKeys = map[string]bool{"discover_from_listening": true, "radio_in_apps": true}
|
||||
|
||||
// parseSettingsPatch reads the body of PATCH /me/settings. It is strict because
|
||||
// the settings are about privacy: one JSON object and nothing after it, only known
|
||||
|
|
@ -56,7 +61,12 @@ func parseSettingsPatch(body []byte) (usersettings.Patch, bool) {
|
|||
if err := json.Unmarshal(value, &b); err != nil {
|
||||
return usersettings.Patch{}, false
|
||||
}
|
||||
p.DiscoverFromListening = b
|
||||
switch key {
|
||||
case "discover_from_listening":
|
||||
p.DiscoverFromListening = b
|
||||
case "radio_in_apps":
|
||||
p.RadioInApps = b
|
||||
}
|
||||
}
|
||||
return p, true
|
||||
}
|
||||
|
|
@ -77,5 +87,5 @@ func (h *userSettingsHandlers) update(c *gin.Context) {
|
|||
writeUserSettingsError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, userSettingsJSON{DiscoverFromListening: s.DiscoverFromListening})
|
||||
c.JSON(http.StatusOK, toUserSettingsJSON(s))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"net/http/httptest"
|
||||
"net/url"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
@ -101,6 +102,16 @@ func TestAnAppSeesTheStationsAsPlaylistsWithOtherSongsEveryTime(t *testing.T) {
|
|||
q.Set("f", "json")
|
||||
return restCall(t, m.h, path, q)
|
||||
}
|
||||
// The stations are not in the apps until the person asks.
|
||||
before, _ := rest("getPlaylists", url.Values{})["playlists"].(map[string]any)["playlist"].([]any) // none at all: the key is absent
|
||||
for _, p := range before {
|
||||
if strings.HasPrefix(p.(map[string]any)["id"].(string), "rd-") {
|
||||
t.Fatalf("a station in the apps before it was asked for: %v", p)
|
||||
}
|
||||
}
|
||||
if r := do(t, m.h, "PATCH", "/api/v1/me/settings", m.alice, map[string]any{"radio_in_apps": true}); r.Code != http.StatusOK {
|
||||
t.Fatalf("setting: %d %v", r.Code, r.Body)
|
||||
}
|
||||
var found bool
|
||||
for _, p := range rest("getPlaylists", url.Values{})["playlists"].(map[string]any)["playlist"].([]any) {
|
||||
if p.(map[string]any)["id"] == "rd-all" {
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ func TestSettingsStartOffAndStayMine(t *testing.T) {
|
|||
if r.Code != http.StatusOK {
|
||||
t.Fatalf("%d %v", r.Code, r.Body)
|
||||
}
|
||||
sameKeys(t, "settings", r.Body, "discover_from_listening")
|
||||
sameKeys(t, "settings", r.Body, "discover_from_listening", "radio_in_apps")
|
||||
if r.Body["discover_from_listening"] != false {
|
||||
t.Errorf("settings = %v, want everything off", r.Body)
|
||||
}
|
||||
|
|
@ -139,3 +139,30 @@ func TestSettingsOfADeletedAccountAreRefused(t *testing.T) {
|
|||
}
|
||||
|
||||
func itoa(n int64) string { return strconv.FormatInt(n, 10) }
|
||||
|
||||
func TestTheRadioInAppsSettingStartsOffAndNamesOnlyItself(t *testing.T) {
|
||||
h := newTestAPI(t)
|
||||
alice := registerAndLogin(t, h)
|
||||
if r := do(t, h, "GET", "/api/v1/me/settings", alice, nil); r.Body["radio_in_apps"] != false {
|
||||
t.Fatalf("at the start: %v", r.Body)
|
||||
}
|
||||
r := do(t, h, "PATCH", "/api/v1/me/settings", alice, map[string]any{"radio_in_apps": true})
|
||||
if r.Code != http.StatusOK || r.Body["radio_in_apps"] != true || r.Body["discover_from_listening"] != false {
|
||||
t.Fatalf("turn on: %d %v", r.Code, r.Body)
|
||||
}
|
||||
// The other setting is not touched by naming this one, nor this one by naming the other.
|
||||
r = do(t, h, "PATCH", "/api/v1/me/settings", alice, map[string]any{"discover_from_listening": true})
|
||||
if r.Body["radio_in_apps"] != true || r.Body["discover_from_listening"] != true {
|
||||
t.Fatalf("the other on: %v", r.Body)
|
||||
}
|
||||
r = do(t, h, "PATCH", "/api/v1/me/settings", alice, map[string]any{"radio_in_apps": nil})
|
||||
if r.Code != http.StatusOK || r.Body["radio_in_apps"] != true {
|
||||
t.Fatalf("null leaves it alone: %d %v", r.Code, r.Body)
|
||||
}
|
||||
if r = do(t, h, "PATCH", "/api/v1/me/settings", alice, map[string]any{"radio_in_apps": "yes"}); r.Code != http.StatusBadRequest {
|
||||
t.Fatalf("a string: %d", r.Code)
|
||||
}
|
||||
if r = do(t, h, "PATCH", "/api/v1/me/settings", alice, map[string]any{"radio_in_app": true}); r.Code != http.StatusBadRequest {
|
||||
t.Fatalf("a misspelt name: %d", r.Code)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6198,8 +6198,14 @@ components:
|
|||
UserSettings:
|
||||
type: object
|
||||
additionalProperties: false
|
||||
required: [discover_from_listening]
|
||||
required: [discover_from_listening, radio_in_apps]
|
||||
properties:
|
||||
radio_in_apps:
|
||||
type: boolean
|
||||
description: |
|
||||
When true, the stations of the radio (see `GET /radio`) are in the list of playlists of the apps that speak
|
||||
OpenSubsonic, as playlists called `Radio · Rock` with other songs every time they are opened. False until you
|
||||
turn it on: there are about two dozen of them. Nothing is sent anywhere either way.
|
||||
discover_from_listening:
|
||||
type: boolean
|
||||
description: |
|
||||
|
|
@ -6345,6 +6351,7 @@ components:
|
|||
additionalProperties: false
|
||||
properties:
|
||||
discover_from_listening: {type: boolean, nullable: true}
|
||||
radio_in_apps: {type: boolean, nullable: true}
|
||||
|
||||
TasteArtist:
|
||||
type: object
|
||||
|
|
|
|||
|
|
@ -56,6 +56,7 @@ type Deps struct {
|
|||
Playlists Playlists
|
||||
Favorites Favorites // optional: with none nothing is a favourite
|
||||
Radio Radio // optional: with none the list of playlists is only the playlists of the person
|
||||
Settings Settings // optional: with none the stations of the radio are not shown to apps
|
||||
Listens Listens
|
||||
Announcer Announcer // optional
|
||||
ServerVersion string
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ import (
|
|||
"git.kmalloc.org/squid/sonda/internal/mixes"
|
||||
"git.kmalloc.org/squid/sonda/internal/playlists"
|
||||
"git.kmalloc.org/squid/sonda/internal/streaming"
|
||||
"git.kmalloc.org/squid/sonda/internal/usersettings"
|
||||
)
|
||||
|
||||
const secret = "0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
|
|
@ -1430,17 +1431,28 @@ func (f *fakeRadio) Radio(_ context.Context, _ int64, id string) (mixes.Mix, []c
|
|||
return mixes.Mix{}, nil, mixes.ErrNotFound
|
||||
}
|
||||
|
||||
func newRadioServer(t *testing.T) http.Handler {
|
||||
type fakeSettings struct{ radio bool }
|
||||
|
||||
func (f fakeSettings) Get(context.Context, int64) (usersettings.Settings, error) {
|
||||
return usersettings.Settings{RadioInApps: f.radio}, nil
|
||||
}
|
||||
|
||||
// newRadioServerWith is a server with the radio, and the setting that shows it in the apps on or off.
|
||||
func newRadioServerWith(t *testing.T, settings Settings) http.Handler {
|
||||
t.Helper()
|
||||
gin.SetMode(gin.TestMode)
|
||||
r := gin.New()
|
||||
Register(r, Deps{
|
||||
Login: fakeLogin{}, Catalog: &fakeCatalog{}, Libraries: fakeLibraries{}, Extras: fakeExtras{}, ServerVersion: "9.9",
|
||||
Playlists: newFakePlaylists(), Radio: &fakeRadio{},
|
||||
Playlists: newFakePlaylists(), Radio: &fakeRadio{}, Settings: settings,
|
||||
})
|
||||
return r
|
||||
}
|
||||
|
||||
func newRadioServer(t *testing.T) http.Handler {
|
||||
return newRadioServerWith(t, fakeSettings{radio: true})
|
||||
}
|
||||
|
||||
func TestTheStationsOfTheRadioAreInTheListOfPlaylists(t *testing.T) {
|
||||
h := newRadioServer(t)
|
||||
q := ok("name", "Mine")
|
||||
|
|
@ -1496,3 +1508,17 @@ func TestWithNoRadioThePlaylistsAreOnlyTheOnesOfThePerson(t *testing.T) {
|
|||
t.Fatalf("playlists %v", list)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheStationsAreNotInTheAppsUntilThePersonAsks(t *testing.T) {
|
||||
for name, settings := range map[string]Settings{"the setting is off": fakeSettings{radio: false}, "there is no setting to read": nil} {
|
||||
h := newRadioServerWith(t, settings)
|
||||
q := ok("name", "Mine")
|
||||
q.Add("songId", "tr-70")
|
||||
get(t, h, "GET", "/rest/createPlaylist", q)
|
||||
list := good(t, get(t, h, "GET", "/rest/getPlaylists", ok()))["playlists"].(map[string]any)["playlist"].([]any)
|
||||
if len(list) != 1 || list[0].(map[string]any)["id"] != "pl-1" {
|
||||
t.Errorf("%s: playlists %v", name, list)
|
||||
}
|
||||
failed(t, get(t, h, "GET", "/rest/getPlaylist", ok("id", "rd-all")), ErrNotFound)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import (
|
|||
"git.kmalloc.org/squid/sonda/internal/listening"
|
||||
"git.kmalloc.org/squid/sonda/internal/mixes"
|
||||
"git.kmalloc.org/squid/sonda/internal/playlists"
|
||||
"git.kmalloc.org/squid/sonda/internal/usersettings"
|
||||
)
|
||||
|
||||
const playlistPrefix = "pl-"
|
||||
|
|
@ -55,6 +56,21 @@ func radioPlaylist(m mixes.Mix, owner string, now time.Time) Playlist {
|
|||
return p
|
||||
}
|
||||
|
||||
// Settings are the choices of a person: *usersettings.Service.
|
||||
type Settings interface {
|
||||
Get(ctx context.Context, userID int64) (usersettings.Settings, error)
|
||||
}
|
||||
|
||||
// radioShown says whether the stations of the radio are in the list of playlists of this person's apps: only when
|
||||
// there is a radio and the person asked for it (there are two dozen of them).
|
||||
func (h *handler) radioShown(k *call) (bool, error) {
|
||||
if h.Radio == nil || h.Settings == nil {
|
||||
return false, nil
|
||||
}
|
||||
s, err := h.Settings.Get(k.ctx, k.principal.UserID)
|
||||
return s.RadioInApps, err
|
||||
}
|
||||
|
||||
// Listens records what a person listened to: *listening.Service.
|
||||
type Listens interface {
|
||||
Record(ctx context.Context, e listening.Event) (listening.Result, error)
|
||||
|
|
@ -102,7 +118,11 @@ func (h *handler) playlists(k *call) (*Response, *Error) {
|
|||
for i, p := range all {
|
||||
out[i] = playlistOf(p, k.principal.Email)
|
||||
}
|
||||
if h.Radio != nil {
|
||||
shown, err := h.radioShown(k)
|
||||
if err != nil {
|
||||
return nil, failure(err)
|
||||
}
|
||||
if shown {
|
||||
stations, err := h.Radio.Radios(k.ctx, k.principal.UserID)
|
||||
if err != nil {
|
||||
return nil, failure(err)
|
||||
|
|
@ -161,7 +181,14 @@ func (h *handler) opened(k *call, id int64) (*Response, *Error) {
|
|||
}
|
||||
|
||||
func (h *handler) playlist(k *call) (*Response, *Error) {
|
||||
if station, ok := strings.CutPrefix(k.param("id"), radioPrefix); ok && h.Radio != nil {
|
||||
if station, ok := strings.CutPrefix(k.param("id"), radioPrefix); ok {
|
||||
shown, err := h.radioShown(k)
|
||||
if err != nil {
|
||||
return nil, failure(err)
|
||||
}
|
||||
if !shown {
|
||||
return nil, notFound()
|
||||
}
|
||||
return h.station(k, station)
|
||||
}
|
||||
id, bad := idParam(k, playlistPrefix)
|
||||
|
|
|
|||
|
|
@ -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 != 35 {
|
||||
t.Errorf("applied migrations = %d, want 35", applied)
|
||||
if applied != 36 {
|
||||
t.Errorf("applied migrations = %d, want 36", applied)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,9 +29,9 @@ type queryer interface {
|
|||
func getUserSettings(ctx context.Context, q queryer, userID int64) (usersettings.Settings, error) {
|
||||
var s usersettings.Settings
|
||||
err := q.QueryRowContext(ctx,
|
||||
`SELECT COALESCE(s.discover_from_listening, FALSE)
|
||||
`SELECT COALESCE(s.discover_from_listening, FALSE), COALESCE(s.radio_in_apps, FALSE)
|
||||
FROM users u LEFT JOIN user_settings s ON s.user_id = u.id WHERE u.id = ?`, userID).
|
||||
Scan(&s.DiscoverFromListening)
|
||||
Scan(&s.DiscoverFromListening, &s.RadioInApps)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return usersettings.Settings{}, usersettings.ErrUserNotFound
|
||||
}
|
||||
|
|
@ -49,12 +49,23 @@ func (r *UserSettingsRepo) Update(ctx context.Context, userID int64, p usersetti
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if p.DiscoverFromListening != nil {
|
||||
cur.DiscoverFromListening = *p.DiscoverFromListening
|
||||
// Each setting that the patch names is written on its own, so that the others keep what they have.
|
||||
for _, w := range []struct {
|
||||
column string
|
||||
value *bool
|
||||
into *bool
|
||||
}{
|
||||
{"discover_from_listening", p.DiscoverFromListening, &cur.DiscoverFromListening},
|
||||
{"radio_in_apps", p.RadioInApps, &cur.RadioInApps},
|
||||
} {
|
||||
if w.value == nil {
|
||||
continue
|
||||
}
|
||||
*w.into = *w.value
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
`INSERT INTO user_settings (user_id, discover_from_listening) VALUES (?, ?)
|
||||
ON CONFLICT (user_id) DO UPDATE SET discover_from_listening = excluded.discover_from_listening`,
|
||||
userID, cur.DiscoverFromListening); err != nil {
|
||||
`INSERT INTO user_settings (user_id, `+w.column+`) VALUES (?, ?)
|
||||
ON CONFLICT (user_id) DO UPDATE SET `+w.column+` = excluded.`+w.column,
|
||||
userID, *w.value); err != nil {
|
||||
if isForeignKeyViolation(err) {
|
||||
return usersettings.ErrUserNotFound // deleted between the read and the write
|
||||
}
|
||||
|
|
|
|||
|
|
@ -78,3 +78,33 @@ func TestAForeignKeyFailureIsRecognised(t *testing.T) {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestTheRadioInAppsSettingIsKeptApartFromTheOthers(t *testing.T) {
|
||||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||||
ctx := context.Background()
|
||||
alice, _ := NewUserRepo(db).CreateUser(ctx, "alice", "h", false)
|
||||
repo := NewUserSettingsRepo(db)
|
||||
on, off := true, false
|
||||
|
||||
if got, _ := repo.Get(ctx, alice.ID); got.RadioInApps || got.DiscoverFromListening {
|
||||
t.Fatalf("new user: %+v, want everything off", got)
|
||||
}
|
||||
got, err := repo.Update(ctx, alice.ID, usersettings.Patch{RadioInApps: &on})
|
||||
if err != nil || !got.RadioInApps || got.DiscoverFromListening {
|
||||
t.Fatalf("radio on: %+v %v", got, err)
|
||||
}
|
||||
// The other setting turned on and then off does not touch it, and both can be named at once.
|
||||
if got, _ = repo.Update(ctx, alice.ID, usersettings.Patch{DiscoverFromListening: &on}); !got.RadioInApps || !got.DiscoverFromListening {
|
||||
t.Fatalf("both on: %+v", got)
|
||||
}
|
||||
if got, _ = repo.Update(ctx, alice.ID, usersettings.Patch{DiscoverFromListening: &off, RadioInApps: &off}); got.RadioInApps || got.DiscoverFromListening {
|
||||
t.Fatalf("both off: %+v", got)
|
||||
}
|
||||
if got, _ = repo.Update(ctx, alice.ID, usersettings.Patch{RadioInApps: &on}); !got.RadioInApps {
|
||||
t.Fatalf("on again: %+v", got)
|
||||
}
|
||||
if got, _ = repo.Get(ctx, alice.ID); !got.RadioInApps || got.DiscoverFromListening {
|
||||
t.Fatalf("kept: %+v", got)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,11 +19,16 @@ type Settings struct {
|
|||
// to most. Doing so sends their names to Last.fm, so it is off until the user
|
||||
// turns it on.
|
||||
DiscoverFromListening bool
|
||||
// RadioInApps puts the stations of the radio in the list of playlists of the apps that speak OpenSubsonic, as
|
||||
// playlists with other songs every time. Nothing leaves the server either way: it is only that there are two dozen
|
||||
// of them, so they come when the user asks.
|
||||
RadioInApps bool
|
||||
}
|
||||
|
||||
// Patch changes some settings. A nil field is left alone.
|
||||
type Patch struct {
|
||||
DiscoverFromListening *bool
|
||||
RadioInApps *bool
|
||||
}
|
||||
|
||||
// Store keeps the settings.
|
||||
|
|
|
|||
|
|
@ -33,6 +33,9 @@ func (f *fakeStore) Update(_ context.Context, id int64, p Patch) (Settings, erro
|
|||
if p.DiscoverFromListening != nil {
|
||||
s.DiscoverFromListening = *p.DiscoverFromListening
|
||||
}
|
||||
if p.RadioInApps != nil {
|
||||
s.RadioInApps = *p.RadioInApps
|
||||
}
|
||||
f.byUser[id] = s
|
||||
return s, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue