feat: new-music notices come from the scanner; request notices and the original specs go
release / web (push) Failing after 1m29s
release / go (push) Successful in 1m55s
release / govulncheck (push) Successful in 19s
release / integration (push) Successful in 4m22s
release / android (push) Successful in 4m57s
release / Attach APK to the Release (tag releases only) (push) Canceled after 0s
release / Build + push container image (push) Canceled after 0s
release / Verify release artifacts (tag releases only) (push) Canceled after 0s
release / Build signed APK (push) Canceled after 4m23s

Request notifications (approved, declined, completed, pending) are removed
along with the request flow that produced them. A new_music kind takes the
"new music arrived" slot: when a scan or a watcher batch finishes, the
scanner claims every album not yet announced (albums.announced_at) and tells
every user about the ones that are new: an album with a track on disk and
no track older than itself. The first import into an empty library, and an
album formed only by retagging tracks already held, announce nothing. One
notice per scan, carrying up to 50 albums and the total; it is never
coalesced, so a notice already emailed can't quietly absorb later arrivals.
It feeds the daily summary email, grouped by artist. Defaults: inbox and
email on, phone off.

Migration 0103 deletes request-kind notification rows and prefs, swaps the
kind CHECKs to new_music, and adds albums.announced_at with every album
held marked announced, so the first scan after it announces nothing old.

Web and Android: labels, the Android "Requests" settings section becomes
"New music", the shade channel your_requests becomes new_music (the old one
is deleted at startup), and "/" opens Home in the app. TrackLabel moves to
the notifications package. docs/smart-music-{server,client}-spec.md are
deleted per the operator; an Android comment loses the word "exchanges".

Grimoire #7666, #7671.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-21 23:29:13 -05:00
co-authored by Claude Opus 5.5
parent 9d267ba8dd
commit 561c1d5eae
42 changed files with 647 additions and 859 deletions
@@ -9,7 +9,7 @@ import kotlinx.datetime.Instant
* Cache row for one artist.
*
* Column names follow Kotlin idiom (camelCase) — the schema is purely
* internal to the app; the sync controller exchanges snake_case JSON
* internal to the app; the sync controller trades snake_case JSON
* with the server and our mappers translate at the boundary.
*/
@Entity(tableName = "cached_artists")
@@ -76,7 +76,7 @@ internal fun announcementFor(fresh: List<CachedNotificationEntity>): Announcemen
channel = if (fresh.all { shadeChannelFor(it.kind) == ShadeChannel.LIBRARY_HEALTH }) {
ShadeChannel.LIBRARY_HEALTH
} else {
ShadeChannel.YOUR_REQUESTS
ShadeChannel.NEW_MUSIC
},
lines = fresh.sortedByDescending { it.createdAt }.take(PILE_LINES).map { it.title },
)
@@ -94,15 +94,18 @@ internal fun phoneOnFor(settings: NotificationSettingsWire?): (String) -> Boolea
* user's system-level choice for it.
*/
internal enum class ShadeChannel(val id: String, val title: String, val description: String) {
YOUR_REQUESTS("your_requests", "Your requests", "Approved, declined, and new music arriving"),
LIBRARY_HEALTH("library_health", "Library health", "For admins: requests to review and library problems"),
NEW_MUSIC("new_music", "New music", "Albums arriving in the library"),
LIBRARY_HEALTH("library_health", "Library health", "For admins: flagged tracks and library problems"),
}
private val requesterKinds = setOf("request_approved", "request_rejected", "request_completed")
/** Channel ids the app does not post to; deleted at startup so system settings lists only live ones. */
internal val retiredShadeChannelIds = listOf("your_requests")
private val listenerKinds = setOf("new_music")
/** Everything a listener can receive is theirs; the rest is admin work. */
internal fun shadeChannelFor(kind: String): ShadeChannel =
if (kind in requesterKinds) ShadeChannel.YOUR_REQUESTS else ShadeChannel.LIBRARY_HEALTH
if (kind in listenerKinds) ShadeChannel.NEW_MUSIC else ShadeChannel.LIBRARY_HEALTH
/** The delivery service runs exactly while signed in with background delivery on. */
internal fun deliveryWanted(signedIn: Boolean, enabled: Boolean): Boolean = signedIn && enabled
@@ -79,6 +79,7 @@ class NotificationPoster @Inject constructor(
private fun ensureChannels() {
val manager = context.getSystemService(NotificationManager::class.java) ?: return
retiredShadeChannelIds.forEach { manager.deleteNotificationChannel(it) }
ShadeChannel.entries.forEach { c ->
manager.createNotificationChannel(
NotificationChannel(c.id, c.title, NotificationManager.IMPORTANCE_DEFAULT)
@@ -4,10 +4,12 @@ import com.fabledsword.minstrel.nav.Admin
import com.fabledsword.minstrel.nav.AdminQuarantine
import com.fabledsword.minstrel.nav.AlbumDetail
import com.fabledsword.minstrel.nav.ArtistDetail
import com.fabledsword.minstrel.nav.Home
/**
* The app screen for a notice's link. The server writes web paths, the same
* for every client; this maps them onto the app's routes. The library's
* for every client; this maps them onto the app's routes. "/" is Home,
* where several new albums at once are found under Recently added. The library's
* health pages sit under Collection on the web: Reports opens the
* app's report screen, and the ones the app has no screen for (duplicates,
* suspect sources, missing files) open the Admin landing, as do other Admin
@@ -18,6 +20,8 @@ private val ADMIN_COLLECTION_PAGES = setOf("duplicates", "suspect-sources", "mis
internal fun routeForLink(link: String): Any? {
val parts = link.substringBefore('?').trim('/').split('/').filter { it.isNotEmpty() }
return when {
link.isBlank() -> null
parts.isEmpty() -> Home
parts.size == 2 && parts[0] == "albums" -> AlbumDetail(parts[1])
parts.size == 2 && parts[0] == "artists" -> ArtistDetail(parts[1])
parts == listOf("collection", "reports") -> AdminQuarantine
@@ -110,7 +110,7 @@ private fun SettingsBody(
verticalArrangement = Arrangement.spacedBy(SectionGap),
) {
Section("This device") { deviceRows() }
Section("Requests", trailing = { ChannelHeaders() }) {
Section("New music", trailing = { ChannelHeaders() }) {
settings.kinds.filter { !it.adminOnly }.forEach { KindRow(it, settings.emailAvailable, onSet) }
if (!settings.emailAvailable) {
Muted(emailUnavailableLine(settings.emailUnavailableReason, isAdmin))
@@ -248,10 +248,7 @@ internal fun channelLabel(c: NotificationChannel): String = when (c) {
/** Short row labels, the same as the web's. Unknown kinds show their key. */
internal fun kindLabel(kind: String): String = when (kind) {
"request_approved" -> "Request approved"
"request_rejected" -> "Request declined"
"request_completed" -> "New music arrived"
"request_pending" -> "Requests to review"
"new_music" -> "New music arrived"
"quarantine_flagged" -> "Tracks flagged"
"scan_failed" -> "Library scan failed"
"tracks_missing" -> "Tracks gone missing"
@@ -38,7 +38,7 @@ class NotificationSettingsRepositoryTest {
private val settings = NotificationSettingsWire(
kinds = listOf(
NotificationKindSettingWire("request_completed", false, inbox = true, phone = true, email = true),
NotificationKindSettingWire("new_music", false, inbox = true, phone = true, email = true),
),
emailAvailable = true,
)
@@ -58,12 +58,12 @@ class NotificationSettingsRepositoryTest {
val body = slot<PutNotificationSettingsBody>()
coEvery { api.putSettings(capture(body)) } returns settings
repo.set("request_completed", NotificationChannel.EMAIL, false)
repo.set("new_music", NotificationChannel.EMAIL, false)
val optimistic = json.decodeFromString(NotificationSettingsWire.serializer(), saved.first().json)
assertFalse(optimistic.kinds.single().email)
assertEquals(
listOf(NotificationSettingChangeWire(kind = "request_completed", email = false)),
listOf(NotificationSettingChangeWire(kind = "new_music", email = false)),
body.captured.kinds,
)
coVerify(exactly = 0) { queue.enqueueNotificationSettingSet(any()) }
@@ -75,10 +75,10 @@ class NotificationSettingsRepositoryTest {
coEvery { mutationDao.hasPending(MutationKind.NOTIFICATION_SETTING_SET) } returns false
coEvery { api.putSettings(any()) } throws IOException("offline")
repo.set("request_completed", NotificationChannel.PHONE, false)
repo.set("new_music", NotificationChannel.PHONE, false)
coVerify {
queue.enqueueNotificationSettingSet(NotificationSettingPayload("request_completed", "phone", false))
queue.enqueueNotificationSettingSet(NotificationSettingPayload("new_music", "phone", false))
}
}
@@ -87,11 +87,11 @@ class NotificationSettingsRepositoryTest {
cached()
coEvery { mutationDao.hasPending(MutationKind.NOTIFICATION_SETTING_SET) } returns true
repo.set("request_completed", NotificationChannel.INBOX, false)
repo.set("new_music", NotificationChannel.INBOX, false)
coVerify(exactly = 0) { api.putSettings(any()) }
coVerify {
queue.enqueueNotificationSettingSet(NotificationSettingPayload("request_completed", "inbox", false))
queue.enqueueNotificationSettingSet(NotificationSettingPayload("new_music", "inbox", false))
}
}
@@ -41,10 +41,10 @@ class NotificationsRepositoryTest {
private fun row(id: String, created: String, readAt: Instant? = null) = CachedNotificationEntity(
id = id,
kind = "request_completed",
kind = "new_music",
title = "t",
body = "b",
link = "/requests",
link = "/",
createdAt = Instant.parse(created),
readAt = readAt,
)
@@ -108,8 +108,8 @@ class NotificationsRepositoryTest {
coEvery { dao.getAll() } returns listOf(row("a", "2026-10-08T10:00:00Z", readAt = readHere))
coEvery { api.list(any()) } returns NotificationsPageWire(
items = listOf(
NotificationWire("a", "request_completed", "t", "b", "/requests", "2026-10-08T10:00:00Z", null),
NotificationWire("b", "request_completed", "t", "b", "/requests", "2026-10-08T11:00:00Z", null),
NotificationWire("a", "new_music", "t", "b", "/", "2026-10-08T10:00:00Z", null),
NotificationWire("b", "new_music", "t", "b", "/", "2026-10-08T11:00:00Z", null),
),
unreadCount = 2,
)
@@ -17,14 +17,14 @@ class DeliveryPlanTest {
private fun notice(
id: String,
minutes: Long,
kind: String = "request_approved",
kind: String = "new_music",
read: Boolean = false,
) = CachedNotificationEntity(
id = id,
kind = kind,
title = "t-$id",
body = "b-$id",
link = "/requests",
link = "/",
createdAt = Instant.fromEpochMilliseconds(t0.toEpochMilliseconds() + minutes * 60_000),
readAt = if (read) t0 else null,
)
@@ -75,17 +75,17 @@ class DeliveryPlanTest {
fun `phone prefs come from the cached settings, with every kind on when none are cached`() {
val settings = NotificationSettingsWire(
kinds = listOf(
NotificationKindSettingWire("request_approved", false, inbox = true, phone = false, email = true),
NotificationKindSettingWire("request_completed", false, inbox = false, phone = true, email = true),
NotificationKindSettingWire("request_rejected", false, inbox = true, phone = true, email = true),
NotificationKindSettingWire("new_music", false, inbox = true, phone = false, email = true),
NotificationKindSettingWire("tracks_missing", true, inbox = false, phone = true, email = true),
NotificationKindSettingWire("quarantine_flagged", true, inbox = true, phone = true, email = true),
),
emailAvailable = true,
)
val phoneOn = phoneOnFor(settings)
assertFalse(phoneOn("request_approved"))
assertFalse(phoneOn("request_completed"), "phone rides on the inbox")
assertTrue(phoneOn("request_rejected"))
assertTrue(phoneOnFor(null)("tracks_missing"))
assertFalse(phoneOn("new_music"))
assertFalse(phoneOn("tracks_missing"), "phone rides on the inbox")
assertTrue(phoneOn("quarantine_flagged"))
assertTrue(phoneOnFor(null)("scan_failed"))
}
@Test
@@ -97,7 +97,7 @@ class DeliveryPlanTest {
val five = (1..5).map { notice("n$it", it.toLong()) }
val pile = assertIs<Announcement.Pile>(announcementFor(five))
assertEquals(5, pile.count)
assertEquals(ShadeChannel.YOUR_REQUESTS, pile.channel)
assertEquals(ShadeChannel.NEW_MUSIC, pile.channel)
assertEquals("5 new notifications", pileText(pile.count))
assertEquals(listOf("t-n5", "t-n4", "t-n3", "t-n2", "t-n1"), pile.lines)
assertNull(pileMore(pile))
@@ -116,15 +116,14 @@ class DeliveryPlanTest {
val admin = (1..4).map { notice("a$it", it.toLong(), kind = "tracks_missing") }
assertEquals(ShadeChannel.LIBRARY_HEALTH, assertIs<Announcement.Pile>(announcementFor(admin)).channel)
val mixed = admin + notice("r", 9)
assertEquals(ShadeChannel.YOUR_REQUESTS, assertIs<Announcement.Pile>(announcementFor(mixed)).channel)
assertEquals(ShadeChannel.NEW_MUSIC, assertIs<Announcement.Pile>(announcementFor(mixed)).channel)
}
@Test
fun `listener kinds are their requests, the rest is library health`() {
listOf("request_approved", "request_rejected", "request_completed").forEach {
assertEquals(ShadeChannel.YOUR_REQUESTS, shadeChannelFor(it))
}
listOf("request_pending", "quarantine_flagged", "scan_failed", "tracks_missing").forEach {
fun `new music has its own channel, the rest is library health`() {
assertEquals(ShadeChannel.NEW_MUSIC, shadeChannelFor("new_music"))
assertTrue(ShadeChannel.entries.none { it.id in retiredShadeChannelIds }, "a live channel is never deleted")
listOf("quarantine_flagged", "scan_failed", "tracks_missing").forEach {
assertEquals(ShadeChannel.LIBRARY_HEALTH, shadeChannelFor(it))
}
}
@@ -5,6 +5,7 @@ import com.fabledsword.minstrel.nav.Admin
import com.fabledsword.minstrel.nav.AdminQuarantine
import com.fabledsword.minstrel.nav.AlbumDetail
import com.fabledsword.minstrel.nav.ArtistDetail
import com.fabledsword.minstrel.nav.Home
import com.fabledsword.minstrel.notifications.data.NotificationChannel
import com.fabledsword.minstrel.shared.widgets.badgeLabel
import kotlinx.datetime.Instant
@@ -17,6 +18,9 @@ import kotlin.test.assertTrue
class NotificationUiTest {
@Test
fun `server links open the matching screens`() {
assertEquals(Home, routeForLink("/"))
// A pile carries no link: it opens the inbox, not Home.
assertNull(routeForLink(""))
assertEquals(AlbumDetail("al-1"), routeForLink("/albums/al-1"))
assertEquals(ArtistDetail("ar-1"), routeForLink("/artists/ar-1"))
assertEquals(AdminQuarantine, routeForLink("/collection/reports"))
@@ -50,7 +54,7 @@ class NotificationUiTest {
@Test
fun `phone and email ride on the inbox, and email needs to be available`() {
val on = NotificationKindSettingWire("request_completed", false, inbox = true, phone = true, email = true)
val on = NotificationKindSettingWire("new_music", false, inbox = true, phone = true, email = true)
val off = on.copy(inbox = false)
assertTrue(channelEnabled(on, NotificationChannel.PHONE, emailAvailable = true))
assertFalse(channelEnabled(off, NotificationChannel.PHONE, emailAvailable = true))
@@ -29,10 +29,10 @@ class NotificationsViewModelTest {
private fun row(id: String, read: Boolean) = CachedNotificationEntity(
id = id,
kind = "request_completed",
kind = "new_music",
title = "t",
body = "b",
link = "/requests",
link = "/",
createdAt = Instant.parse("2026-10-23T12:00:00Z"),
readAt = if (read) Instant.parse("2026-10-23T12:05:00Z") else null,
)
-245
View File
@@ -1,245 +0,0 @@
# Project Spec: Smart Music Client (companion app)
> **History.** This is the original design spec, kept as written. Where it describes an outside library manager doing search, adds and deletes, Minstrel now does that itself.
## 1. Overview
A mobile music player client built as the intentional companion to the Smart Music Server (see `smart-music-server-spec.md`). This is not a general-purpose Subsonic client — it is the client that knows how to consume the server's extended API and surface its novel features (contextual likes, session-aware radio, Lidarr integration, server-canonical state) as first-class UX.
The thesis driving this project, restated: **state and intelligence belong on the server.** The client's job is to be a great playback surface and input device for the server's decisions, not to reimplement them. Compared to existing Subsonic clients (Symfonium, Tempo, Substreamer), this client is thinner in its "brains" and heavier in its integration with a specific server.
**Scope of this spec:** v1 mobile client. Read the server spec first; this document assumes its terminology and data model.
## 2. Goals and non-goals
### Goals
- Native-feeling mobile app on both iOS and Android from a single codebase.
- Full playback: background audio, lock screen controls, notification controls, Bluetooth/headset button handling, CarPlay/Android Auto *awareness* (not necessarily full integration in v1 — see non-goals).
- Offline cache with explicit user control.
- First-class UX for the dual-like model: clear, fast distinction between "like generally" and "like in this context."
- Real-time session awareness: the client knows what session it's in, shows the user what the server thinks the current context is, and surfaces contextually relevant actions.
- Radio flow that feels like a single gesture (seed → queue → playing) with clear affordances for "keep this vibe," "shift vibe," and "stop."
- Lidarr search and add directly from the player when listening to suggested additions.
- Multi-device consistency: state (likes, play counts, current queue if desired) reflects the canonical server state. Two devices signed in to the same user should see the same world.
### Non-goals (v1)
- CarPlay / Android Auto full integration. v1 should not *break* when these launch (lock screen controls must work, basic media session must be present) but dedicated CarPlay UI is deferred.
- Desktop client. The server's web UI covers desktop for v1.
- Cross-server support. This client talks to the Smart Music Server specifically and uses its extended API. Pointing it at a stock Navidrome is not a goal.
- Chromecast / AirPlay. Nice-to-have, not v1.
- Lyrics display.
- Equalizer / DSP.
- Playlist editing UI beyond basic add/remove (full playlist management lives in the server web UI).
- Social features (sharing, following, comments).
## 3. Technology choices
- **Framework:** Flutter. Rationale in the server spec conversation; the audio ecosystem (`just_audio`, `audio_service`) is the most mature cross-platform option for this use case.
- **Language:** Dart.
- **State management:** Riverpod. Rationale: testable, compile-time safe, well-suited to the amount of server state this app reflects. Avoid Bloc's boilerplate; avoid Provider's looseness.
- **HTTP:** `dio` with a typed client layer generated from the server's OpenAPI spec if the server publishes one; otherwise hand-written.
- **Local storage:**
- `sqflite` or `drift` for structured local data (cached library, offline play event queue).
- `path_provider` + direct filesystem for cached audio files.
- `flutter_secure_storage` for credentials.
- **Audio:** `just_audio` for playback, `just_audio_background` / `audio_service` for lock screen and background.
- **Navigation:** `go_router`.
- **Packaging:** standard Flutter build pipeline, published as APK/AAB for Android and TestFlight build for iOS. App store publication is out of scope for v1 (sideload / personal use).
## 4. Architecture
### Layers
1. **Transport layer** — wraps the server's Subsonic and extended APIs. Handles auth, retries, connectivity changes. Exposes typed methods to higher layers.
2. **Repository layer** — merges local cache and remote data. Owns the decision of "when do I serve cached data vs. fetch fresh." Exposes streams that UI subscribes to.
3. **Playback engine** — wraps `just_audio`. Owns the queue, position, playback state. Emits events consumed by the session reporter.
4. **Session reporter** — background worker that translates playback engine events into server event API calls (play start, skip, seek, complete). Batches and retries when offline.
5. **Offline cache manager** — owns the disk cache of audio files and cover art. Implements eviction policy, explicit user-pinned downloads.
6. **UI layer** — Flutter widgets consuming Riverpod providers that wrap repositories.
### Key architectural decisions
- **Server is source of truth, local is cache.** The client does not hold authoritative state for likes, play counts, or queue membership. All user actions are fire-and-persist-locally, then sync to server. Conflict resolution favors server on write failures (with local retry).
- **Offline mode is first-class.** Every user action — liking, skipping, playing — must work offline and sync later. This means local event queue with retry, and UI that doesn't lie about server state it couldn't confirm.
- **No client-side recommendation logic.** Shuffle, radio, "play similar" all round-trip to the server. If offline, fall back to a simple random-from-cache with a UI indicator that smart features are degraded.
- **Optimistic UI.** Liking a song updates the UI immediately; sync happens in the background. Failed syncs surface as a non-blocking indicator, not a modal.
### Threading model
Flutter's main isolate handles UI. Playback and audio service run on their platform-native background threads (handled by `audio_service`). The session reporter and cache manager run as long-lived providers on the main isolate but do their work in async. Heavy operations (e.g. bulk cache cleanup) use `compute()` to offload to a worker isolate.
## 5. Data flow examples
### Starting a play session
1. User taps a track.
2. UI calls playback engine: load queue, start playback.
3. Playback engine emits "track started" event.
4. Session reporter calls `POST /api/v1/events/play` with start metadata.
5. Server creates or updates session, computes session vector, returns session info.
6. Client updates a "current session" provider; UI can display session context (e.g. "tagged: rock, energetic").
7. As playback progresses, session reporter periodically sends progress; on completion or skip, sends terminal event.
### Liking a song
Two buttons in the player UI: **Like** (general) and **Like this vibe** (contextual). Contextual like is only enabled when there is an active session of sufficient length (per server spec, ≥3 tracks in session).
1. User taps Like this vibe.
2. UI optimistically marks the track as contextually liked in this session; shows a brief toast confirming.
3. Client calls `POST /api/v1/likes/contextual`. The server computes the session vector from its own records (the client does not send it — the server is authoritative).
4. On success, the like is confirmed. On failure, the action is queued for retry; UI shows a subtle indicator.
### Starting radio
1. User long-presses a track or taps a "Radio from here" affordance.
2. UI calls `GET /api/v1/radio?seed_track=<id>`.
3. Server returns a queue and optional "suggested additions" (tracks not in library).
4. Playback engine replaces queue; UI renders the queue with suggested additions grouped separately at the bottom with Lidarr-add buttons.
5. As the queue drains, client requests a refresh at 80% consumption.
### Offline playback
1. User goes offline.
2. Client continues playback from cached files.
3. Play/skip events accumulate in the local event queue with their timestamps.
4. On reconnect, event queue is flushed to server in order. Server is expected to backfill events with their original timestamps, reconstruct sessions correctly even retroactively.
5. If a user liked a track offline, the like is queued the same way.
## 6. UI structure
### Screens
- **Sign-in** — server URL, username, password. Remember server. Option to save multiple servers (for dev/prod, single household use).
- **Home** — suggested radios, recently played, recent likes, continue listening. All served by server endpoints.
- **Library** — artists, albums, tracks, playlists browse. Subsonic-standard.
- **Search** — searches server. Also offers "search Lidarr for this" affordance on empty results.
- **Now playing** — full-screen player. Primary affordances: play/pause/skip/seek, like general, like contextual, radio-from-here, add-to-playlist, queue view, session inspector.
- **Queue** — current queue with drag-to-reorder. Shows suggested additions below the real queue.
- **Session inspector** — shows the server's current session vector in human-readable form (recent tracks, detected tags, detected artists). Useful for debugging and for user trust in the "why is this playing" question. Accessible from the now-playing screen.
- **Downloads** — pinned/cached content, cache size, eviction controls.
- **Settings** — account, offline behavior, audio quality, cache size, server info, log out.
### Now-playing affordances (design constraint)
The now-playing screen is where the novel UX lives. It must make the following feel natural:
- A single-tap way to like this song generally.
- A single-tap way to like this song in this context. Visually distinct from general like.
- Clear indication of whether either or both likes are active for the current track.
- An obvious but not-dominant "radio from here" action.
- When playback is inside a radio queue, a "keep this vibe" action that pins the current context, and a "shift" action that mutates it. (Exact semantics tunable; start with "keep" = re-seed radio from current track with current session vector, "shift" = re-seed ignoring session vector.)
The exact visual design is deferred; the spec requires these *capabilities* be reachable without drilling into menus. Buried features won't get used and the contextual like is the whole point.
### Design constraint: don't lie about server state
If an action hasn't succeeded on the server yet, the UI may show the optimistic result but must not present it as fully confirmed. A subtle pending indicator on the like button, for instance. Errors that affect user perception of their library (e.g. a like that never synced) must be surfaceable, not silently dropped.
## 7. Offline cache
### Two kinds of cached content
1. **Transient cache** — populated automatically during playback. LRU-evicted. Size-capped per user config (default 2 GB).
2. **Pinned downloads** — user-explicit "download this album / playlist." Not evicted automatically. Pinned content can be browsed in the Downloads screen.
### Cache decisions
- Cached files are stored at the audio quality the server was asked to deliver. Quality is chosen per user config (e.g. "always original," "transcode to 192k on cellular").
- Cover art cached separately, smaller budget.
- Cache survives app reinstall if possible (Android: scoped external storage; iOS: app container — accepting that iOS reinstall loses it).
- User-visible cache size accounting must be accurate.
### Offline UI
An offline indicator appears in the top chrome when the client can't reach the server. All server-dependent actions show degraded affordances (e.g. smart shuffle shows "shuffling cached tracks only"). Pinned downloads remain fully usable.
## 8. API usage
### Endpoints consumed
From the server's Subsonic surface: `ping`, `getMusicFolders`, `getIndexes`/`getArtists`, `getArtist`, `getAlbum`, `getAlbumList2`, `search3`, `stream`, `getCoverArt`, `getPlaylists`, `getPlaylist`, `createPlaylist`, `updatePlaylist`, `getRandomSongs`, `star`/`unstar` (for general likes — though the extended API's equivalent is preferred for future-proofing).
From the server's extended API: all of `/api/v1/*` described in the server spec. Specifically depended on:
- `POST /api/v1/events/play` (with all event types the server supports)
- `POST /api/v1/likes/contextual`
- `DELETE /api/v1/likes/contextual/:id`
- `GET /api/v1/session/current`
- `GET /api/v1/radio`
- `POST /api/v1/lidarr/search`
- `POST /api/v1/lidarr/add`
- `POST /api/v1/quarantine/:track_id`
- `GET /api/v1/stats/user`
### Auth
Standard Subsonic auth for Subsonic endpoints (token + salt scheme). Bearer token for extended API. Client holds both; transport layer adds them transparently.
### Versioning
Client sends its version in a header on every extended API request. Server may respond with compatibility warnings. Major server API changes bump a version the client checks on startup.
## 9. Reliability
### Event queue durability
Play/skip/like events are written to local SQL before any network attempt. The sync worker drains the queue FIFO with exponential backoff on failures. Events older than 30 days in the queue without successful sync are logged and dropped (with user visibility — a "some events failed to sync" notification).
### Playback robustness
- Network interruption during streaming: if the track is partially buffered, finish it from buffer; on exhaustion, pause and show offline state.
- Track not found (deleted between queue build and playback): skip forward, log, don't crash.
- Corrupt cache file: purge it, redownload or skip.
### Battery and data
- Don't prefetch aggressively on cellular unless user opts in.
- Audio session must release properly when playback ends; no background drain.
## 10. Testing
- Widget tests for the novel UI components (like buttons, session inspector, radio queue).
- Unit tests for the event queue and sync logic — this is where silent bugs lose user trust.
- Integration tests against a local server instance for the full happy-path flows.
- Manual test matrix documented: playback across lock/unlock, app backgrounded, headphone disconnect, network loss mid-stream, receive call mid-stream.
## 11. Implementation order
The client should not start before server step 4 (usable Subsonic baseline). Once that milestone is reached, client work proceeds in parallel:
1. Project skeleton, Riverpod setup, routing, theme.
2. Auth + sign-in screen. Talk to `ping`. Store credentials securely.
3. Library browse: artists, albums, tracks. Read-only against Subsonic API.
4. Playback engine: `just_audio` integration, queue, background audio, lock screen. **Milestone: can play music from the server.**
5. Event ingestion against server (requires server step 5 complete): play start, complete, skip events wired to server.
6. Like buttons (general) wired to Subsonic `star`.
7. Contextual like button + session inspector (requires server step 8).
8. Radio flow (requires server step 11).
9. Lidarr search/add integration (requires server step 12).
10. Offline cache manager.
11. Pinned downloads.
12. Quarantine action from player.
13. Settings polish, stats screen.
14. Packaging, release pipeline.
Steps 5, 7, 8, 9 gate on server features. Plan accordingly; don't block client work on those, sequence around them.
## 12. Open questions to resolve during implementation
- Whether queue state should itself be server-synced (so starting playback on one device resumes on another). Technically appealing but a significant complication — may defer to v2.
- Whether to offer a "this song is playing because of these contextual likes" explanation affordance. Requires server API to expose the reasoning, and it isn't currently in the server spec. Worth considering adding.
- Exact wording and iconography for general vs. contextual like. This needs user testing (even if the user is just you).
- Whether offline-created contextual likes are a good idea at all — the server is the authority on session vectors, and the client's view of a session may drift offline. Possibly contextual likes require connectivity while general likes do not.
- How aggressive to be about prefetching the radio queue in advance for offline continuation.
## 13. Out of scope reminders
If during development any of the following come up, stop and reconsider scope:
- CarPlay / Android Auto native UI
- Chromecast / AirPlay
- Lyrics
- EQ / DSP
- Cross-server support
- Desktop port
- Social / sharing
-342
View File
@@ -1,342 +0,0 @@
# Project Spec: Smart Music Server (working name)
> **History.** This is the original design spec, kept as written. Where it describes an outside library manager doing search, adds and deletes, Minstrel now does that itself.
## 1. Overview
A self-hosted music server and streaming backend built to close a real gap in the self-hosted music ecosystem: **state and intelligence belong on the server, not the client.** Existing Subsonic-compatible servers (Navidrome, Gonic, Airsonic-Advanced) defer all of the "smart" behavior — shuffle logic, radio, play count semantics, like management — to clients. The result is fragmented experience across devices: a song liked on one client is unknown to another; shuffle quality depends on which app you opened; play counts diverge. This project centralizes those decisions on the server so every client sees the same canonical state.
Primary differentiator: a **dual-like model** (general likes vs. contextual likes tied to the recent listening session) combined with **session-aware weighted shuffle** and **"smart radio" playlist generation** that considers user history, session context, and external similarity data from ListenBrainz/MusicBrainz.
The system also integrates with Lidarr for library management and scrobbles to ListenBrainz.
**Scope of this spec:** the backend server. A companion Flutter mobile client is part of the v1 product and is specified in a separate document (`smart-music-client-spec.md`). The two are co-designed: the extended API in this spec is shaped by what the client needs, and the client spec assumes the server features described here. Implementation proceeds in parallel after the server reaches Subsonic-compatible baseline.
## 2. Goals and non-goals
### Goals
- Self-hosted music server running on Linux (primary target: Docker container on Ubuntu/CachyOS).
- OpenSubsonic API compatibility so existing clients (Symfonium, Tempo, Feishin, etc.) work out of the box.
- Server-side smart shuffle that beats random playback in a measurable way for the user.
- Server-side "radio" feature: seed from a track, artist, or album and generate a contextually coherent queue.
- Dual-like model (contextual and general) with retrievable contextual signal.
- Session tracking with play/skip/seek/complete events persisted as first-class data.
- ListenBrainz scrobbling (outbound) and similarity data pull (inbound).
- Lidarr integration: search and add from the server UI; quarantine-based removal workflow (never auto-delete).
- Web UI sufficient to manage the library, likes, Lidarr integration, and system settings.
### Non-goals (v1)
- Multi-tenant / public-facing deployment. Single-household use assumed; multi-user within household is a goal but not hardened against adversarial users.
- Audio fingerprinting / deduplication beyond what MusicBrainz IDs provide.
- Local ML embedding generation. Use ListenBrainz/MusicBrainz similarity data instead.
- Transcoding profiles beyond what OpenSubsonic expects.
- Podcast support.
- Video support.
- Internet radio streaming.
### Explicit non-goals to push back on later
If during implementation there is pressure to add: local audio feature extraction (Essentia/librosa), neural embeddings, or auto-delete-on-dislike — stop and re-scope. These are v2+ discussions.
## 3. Technology choices
- **Language:** Go. Rationale: single-binary deployment, strong concurrency model for streaming + background workers, mature ecosystem for HTTP services, good fit for home lab containerized deployment.
- **HTTP:** Go standard library `net/http` + `chi` or `echo` router. No heavyweight framework.
- **Database:** PostgreSQL. Rationale: the session-vector similarity queries and event aggregations will benefit from real indexing, window functions, and JSONB. SQLite is tempting for single-user deployments but will bottleneck on the analytics queries central to this project's value.
- **Database access:** `sqlc` for typed queries generated from SQL. Migrations via `goose` or `golang-migrate`.
- **Audio/transcoding:** `ffmpeg` as an external binary invoked per stream. Do not bundle or reimplement.
- **Tag reading:** `go-taglib` bindings or `dhowden/tag` for pure-Go tag parsing.
- **MusicBrainz / ListenBrainz:** direct HTTP clients, no wrapper libraries unless a well-maintained one exists at implementation time.
- **Lidarr:** direct HTTP client against Lidarr's v1 API.
- **Web UI:** server-rendered HTML + HTMX for v1. Rationale: avoids a full SPA build pipeline for what is a minority surface. Reconsider for v2 if the UI grows.
- **Auth:** token-based for API clients (OpenSubsonic uses its own auth scheme — support it). Session cookies for the web UI.
- **Deployment:** Dockerfile producing a scratch or distroless image containing the server binary + ffmpeg. Docker Compose example in the repo.
- **Config:** YAML config file with env var override. Sensible defaults.
- **Logging:** structured logging via `log/slog`. Log levels configurable.
## 4. Architecture
### High-level components
1. **Library scanner** — walks configured music folders, parses tags, writes canonical track/artist/album records, resolves MusicBrainz IDs where available.
2. **Stream server** — OpenSubsonic endpoints for browsing, streaming, transcoding on demand.
3. **Event ingestion** — records play/skip/seek/complete events from clients, groups them into sessions.
4. **Session service** — maintains the notion of a "current session" per user, computes session vectors (the previous-N tracks and their aggregate features), handles session timeouts.
5. **Recommendation engine** — the core value layer. Implements weighted shuffle, radio generation, and contextual-like retrieval. Pure functions operating on data from the DB and similarity service.
6. **Similarity service** — wraps ListenBrainz similarity API calls with aggressive caching. Local DB stores similarity edges.
7. **Scrobble worker** — batches completed plays and forwards to ListenBrainz.
8. **Lidarr integration** — adapter for search, add, quarantine operations.
9. **Web UI** — thin surface over the above.
10. **Extended API** — non-Subsonic endpoints for features Subsonic clients cannot express (contextual likes, session inspection, Lidarr actions).
### Key architectural decisions
- **OpenSubsonic compatibility first, extensions second.** All existing Subsonic features must work with third-party clients. The novel features live in a parallel API namespace (e.g. `/api/v1/`) that the future custom client will use.
- **Events are append-only.** The events table is the source of truth for listening history. All aggregates (play counts, skip ratios, session vectors) are computed from it. This enables retroactive model changes without data loss.
- **Session computation is online but session similarity is precomputed.** When a track is played, its "session vector at time of play" is computed and stored on the play event. This makes contextual-like retrieval a straightforward query rather than a replay of history.
- **Recommendation is a pure function of DB state.** No in-memory state that would be lost on restart. Caches are explicit and rebuildable.
### Request lifecycle (streaming)
Client → auth check → track lookup → (optional) transcode ffmpeg pipeline → byte range response. Play event recorded when client reports progress beyond a threshold (see §6).
### Request lifecycle (smart shuffle / radio)
Client → auth → `getRandomSongs` (Subsonic) or `/api/v1/radio` (extended) → recommendation engine → query composer pulls candidate set → weighting applied → ranked list returned.
## 5. Data model
The schema below is the v1 target. Prefer explicit, descriptive column names over abbreviations. All timestamps are `timestamptz`.
### Core library tables
**`artists`** — id (uuid), name, sort_name, mbid (nullable, unique when present), created_at, updated_at.
**`albums`** — id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at.
**`tracks`** — id, title, album_id, artist_id, track_number, disc_number, duration_ms, file_path (unique), file_size, file_format, bitrate, mbid, genre (denormalized text for fast filtering), added_at, updated_at.
**`users`** — id, username, password_hash, api_token, is_admin, created_at.
### Event and session tables
**`play_events`** — id, user_id, track_id, started_at, ended_at (nullable until completion reported), duration_played_ms, completion_ratio (computed: duration_played_ms / track.duration_ms), was_skipped (bool), client_id (nullable), session_id, session_vector_at_play (jsonb — see §6), scrobbled_at (nullable).
**`skip_events`** — id, user_id, track_id, skipped_at, position_ms, session_id. (Subset of play_events where was_skipped=true, but kept as a separate index-friendly table for skip analytics.)
**`sessions`** — id, user_id, started_at, ended_at (nullable), last_event_at, track_count, client_id.
### Like tables
**`general_likes`** — user_id, track_id, liked_at. Primary key (user_id, track_id). A single row per user-track pair. Unliking deletes the row.
**`contextual_likes`** — id, user_id, track_id, liked_at, session_vector (jsonb, snapshot at time of like), session_id. Multiple rows per user-track pair are allowed and expected — each represents a like in a specific context.
**`artist_preferences`** — user_id, artist_id, weight (float, -1.0 to 1.0), updated_at. Aggregate signal, computed from likes/skips but directly adjustable by user.
### Similarity tables
**`track_similarity`** — track_a_id, track_b_id, score (float), source (enum: listenbrainz, musicbrainz_tag, user_cooccurrence), fetched_at. Primary key (track_a_id, track_b_id, source).
**`artist_similarity`** — analogous to track_similarity.
### Integration tables
**`lidarr_quarantine`** — id, track_id, requested_by_user_id, reason (enum: wrong_match, low_quality, disliked, other), requested_at, resolved_at (nullable), resolution (enum: deleted, restored, nullable).
**`scrobble_queue`** — id, play_event_id, queued_at, attempts, last_attempt_at, last_error (nullable), submitted_at (nullable).
### Indexes (non-exhaustive)
- `play_events (user_id, started_at DESC)` for session assembly.
- `play_events (user_id, track_id)` for per-track history.
- `contextual_likes USING gin (session_vector)` for vector similarity queries.
- `track_similarity (track_a_id, score DESC)` for fast candidate generation.
## 6. Core algorithms
### Session definition
A session is a contiguous sequence of plays by a single user with no gap longer than **30 minutes** between events. The timeout is configurable. A new play event more than 30 minutes after the last event in a session starts a new session.
### Session vector
At the moment a track begins playing, compute a vector representing the session context. v1 definition:
- Last N = 5 tracks played in the current session (if fewer than 5, use what exists; if session just started, vector is marked `seed` and excluded from contextual retrieval until session has ≥3 tracks).
- For each of those N tracks, collect: artist_id, genre tags, MusicBrainz tags if available, approximate BPM/energy if available (optional, skip if not).
- Aggregate into a JSONB structure:
- `artists`: set of artist_ids
- `tags`: bag-of-tags with counts
- `recent_track_ids`: ordered list of the N track ids
- Store this JSONB on the play event and on any contextual_like created during this track.
### Session similarity
Two session vectors are compared via weighted Jaccard similarity on the `tags` and `artists` fields. The exact weights should be tunable via config (start with: tags 0.7, artists 0.3). This is simple and good enough for v1; upgrade paths include cosine similarity on tag vectors and eventually audio embeddings.
### Play vs. skip classification
A play event is a **skip** if ended before 50% of track duration AND before 30 seconds of playback. Otherwise it's a **play**. A **complete play** is ≥ 90% of duration. Thresholds configurable.
### Weighted shuffle
Given a candidate set (e.g. all tracks, or all tracks in an album/playlist/genre), compute a score per track:
```
score = base_weight
+ (is_general_liked ? LIKE_BOOST : 0)
+ (contextual_match_score * CONTEXT_WEIGHT)
+ (recency_decay(last_played_at) * RECENCY_WEIGHT)
- (skip_ratio * SKIP_PENALTY)
- (recently_played_penalty)
+ small_random_jitter
```
- `contextual_match_score` = max session similarity between the current session vector and any contextual_like session vector for this track, for this user.
- `recency_decay` returns a value that is high when a track hasn't been played recently and low when it has, but not infinite for never-played tracks (cold-start handling).
- `recently_played_penalty` hard-suppresses anything played in the last hour to prevent immediate repeats.
Weights are in config. Ship with reasonable defaults and document how to tune them.
### Radio generation
Given a seed (track, artist, or album):
1. Build a candidate pool from: (a) ListenBrainz similar tracks to the seed, (b) tracks by similar artists, (c) tracks sharing MusicBrainz tags, (d) user's general likes matching those tags.
2. Filter to the user's library (Lidarr-integrated libraries may choose to also propose additions — see §7).
3. Apply weighted shuffle scoring on the pool.
4. Return an ordered queue of configurable length (default 50).
5. Refresh policy: regenerate when the queue is 80% consumed, biased against tracks already played in this radio session.
### Scrobbling
A play event is eligible for scrobbling when:
- completion_ratio ≥ 0.5, OR
- duration_played_ms ≥ 4 minutes
(These are ListenBrainz's recommended thresholds; confirm at implementation time.) Eligible events enter the scrobble_queue and are batched by the scrobble worker. Retries with exponential backoff on network or 5xx errors. Permanent failures (4xx, malformed) are logged and the row is marked failed.
### ListenBrainz similarity ingest
On library scan completion, and periodically thereafter, for tracks with MBIDs:
- Query ListenBrainz similarity endpoints in batches.
- Populate `track_similarity` and `artist_similarity`.
- Respect ListenBrainz rate limits. Cache aggressively; re-fetch no more than weekly per track.
## 7. Lidarr integration
### Search and add
Expose an endpoint on the extended API that proxies Lidarr's search. Users can initiate an add from the web UI. The server calls Lidarr with the user's configured root folder and quality profile. After Lidarr reports the download complete, the next library scan picks up the file.
### Quarantine (never auto-delete)
When a user flags a track as wrong/low-quality/disliked:
1. A `lidarr_quarantine` row is created.
2. The track is hidden from playback for that user (soft-hide; other users in the household still see it).
3. The file is NOT deleted.
4. An admin UI lists quarantined tracks. An admin can choose to delete (via Lidarr's delete endpoint) or restore.
**Rationale:** a one-tap deletion of a 40MB FLAC rip during a user-hostile moment is unacceptable. Quarantine is cheap; deletion is destructive. This is the correct tradeoff even though it adds a UI step.
### Radio with add-suggestions
When generating a radio queue, if the server has similarity data pointing to tracks not in the library AND Lidarr integration is enabled, surface these as "suggested additions" in the queue response (separate field, not interleaved with playable tracks). The user can one-tap add-to-Lidarr from the client.
## 8. API surface
### Subsonic / OpenSubsonic endpoints
Implement the full OpenSubsonic spec as of implementation time. At minimum: `ping`, `getLicense`, `getMusicFolders`, `getIndexes`, `getArtists`, `getArtist`, `getAlbum`, `getAlbumList2`, `getSong`, `search3`, `stream`, `download`, `scrobble`, `star`, `unstar`, `getStarred2`, `getRandomSongs`, `getSimilarSongs2`, `createPlaylist`, `getPlaylists`, `getPlaylist`, `updatePlaylist`, `deletePlaylist`, `getCoverArt`.
Map Subsonic concepts onto the extended model:
- Subsonic `star` → creates a `general_likes` row.
- Subsonic `scrobble` with `submission=true` → creates a play_event.
- Subsonic `scrobble` with `submission=false` → updates now-playing state.
- Subsonic `getRandomSongs` → invokes weighted shuffle.
- Subsonic `getSimilarSongs2` → invokes a simplified radio generation.
### Extended API (`/api/v1/`)
- `POST /api/v1/events/play` — richer play event reporting (includes seek events, client metadata).
- `POST /api/v1/likes/contextual` — create a contextual like. Server computes session vector.
- `DELETE /api/v1/likes/contextual/:id` — remove a specific contextual like.
- `GET /api/v1/session/current` — inspect the caller's current session.
- `GET /api/v1/radio` — generate radio queue with explicit parameters (seed type, pool size, weights override).
- `POST /api/v1/lidarr/search` — proxy search.
- `POST /api/v1/lidarr/add` — request add.
- `POST /api/v1/quarantine/:track_id` — flag a track.
- `POST /api/v1/quarantine/:id/resolve` — admin resolution.
- `GET /api/v1/stats/user` — listening stats for the caller.
All extended endpoints use JSON request/response, not Subsonic's XML/JSON hybrid.
## 9. Web UI
Server-rendered HTML + HTMX. Pages needed:
- Login.
- Library overview (artist/album/track browse, mostly a fallback — real browsing happens in clients).
- Now playing / current session view.
- Likes management (both lists, with ability to see contextual likes grouped by detected "contexts").
- Lidarr search and add.
- Quarantine admin.
- User settings (scrobble target, shuffle weight overrides).
- Server settings (library paths, scan triggers, integration config) — admin only.
- Listening stats.
Design constraint: functional and legible over fancy. The custom client is where UX effort goes.
## 10. Configuration
YAML config at `/etc/smartmusic/config.yaml` by default, overridable with `--config` flag and env vars (prefixed `SMARTMUSIC_`).
Required config sections:
- `server` (listen address, external URL)
- `database` (postgres connection)
- `library` (scan paths, scan schedule)
- `transcoding` (ffmpeg path, default profiles)
- `listenbrainz` (token, enabled)
- `lidarr` (url, api key, enabled, default root folder, default quality profile)
- `shuffle` (weight overrides)
- `session` (timeout minutes, N for session vector)
- `auth` (token lifetime, admin bootstrap)
## 11. Testing
- Unit tests for the recommendation engine with fixed inputs / expected rankings.
- Unit tests for session boundary logic.
- Integration tests for the Subsonic endpoint compatibility using a known-good client's test suite if one exists, otherwise golden-response tests.
- Integration test harness with a Postgres testcontainer.
- Load test for the stream endpoint under concurrent clients.
Target: ≥70% coverage on the recommendation engine and session code specifically. Do not chase coverage on the Subsonic handler code — those tests should be scenario-driven.
## 12. Operational concerns
- **Library scan** should be incremental (by mtime) with a full-rescan option. Large libraries (>100k tracks) should scan in under 10 minutes incremental, under 2 hours full.
- **Streaming** should support byte-range requests correctly for seek support.
- **Transcoding** results should be cacheable on disk with LRU eviction, configurable cache size.
- **Backup** guidance: the Postgres DB is the only stateful component other than the music files themselves. Document pg_dump-based backup in the README.
- **Metrics:** expose `/metrics` in Prometheus format. Key metrics: active streams, scan duration, scrobble queue depth, recommendation latency.
## 13. Implementation order
This is the suggested sequencing for the server. The mobile client is specified separately and its work runs in parallel starting from step 5, once the server has a verifiable Subsonic baseline.
1. Project skeleton, config loading, logging, DB migrations framework.
2. Core schema (library tables, users).
3. Library scanner — walk, tag parse, write records.
4. Basic Subsonic API subset: auth, browse, stream. Verify against a real client (Symfonium or Feishin) end-to-end. **Milestone: usable Subsonic server.**
5. Event ingestion + session service. *(Client work can begin here against a stubbed API.)*
6. Like tables + Subsonic star/unstar wired to general_likes.
7. Recommendation engine v1: weighted shuffle without contextual signal yet.
8. Contextual likes + session vectors + contextual signal in recommendation.
9. ListenBrainz scrobble worker.
10. ListenBrainz similarity ingest.
11. Radio generation.
12. Lidarr integration (search, add, quarantine).
13. Web UI.
14. Metrics, packaging, Docker image, documentation.
Each step should produce a runnable, testable increment. Don't implement step N+1 before step N is verified working. Server API changes discovered during client development should feed back into this spec and be implemented as they come up, rather than batched.
## 14. Out of scope reminders
If during development any of the following come up, stop and reconsider scope:
- Local audio feature extraction
- Neural network anything
- Adversarial multi-user hardening
- Podcast / video / internet radio
- Custom audio format support beyond what ffmpeg handles natively
## 15. Open questions to resolve during implementation
- Exact session vector JSONB schema — finalize after looking at real MusicBrainz tag data.
- Whether to store the `session_vector_at_play` on every play event or only on events that generate contextual likes. Storage cost vs. query simplicity tradeoff.
- Whether `getRandomSongs` from Subsonic should use the full weighted shuffle or a lighter version, since some clients call it on a loop.
- Handling of tracks without MBIDs (a significant fraction of many libraries). They can't participate in similarity-based radio. Decide whether to fall back to artist/genre/tag similarity or exclude them from radio entirely.
- Whether contextual likes should decay over time (old contextual likes become weaker signal).
+9 -11
View File
@@ -44,8 +44,9 @@ func notifyN(t *testing.T, pool *pgxpool.Pool, user dbq.User, n int) {
t.Helper()
nt := notifications.New(pool, nil, nil)
for i := 0; i < n; i++ {
if err := nt.Notify(context.Background(), notifications.KindRequestCompleted,
notifications.ToUser(user.ID), notifications.Payload{Name: "Album", AlbumID: "al-1"}.Map()); err != nil {
p := notifications.Payload{Count: 1, Albums: []notifications.Arrival{{Artist: "Artist", Title: "Album", AlbumID: "al-1"}}}
if err := nt.Notify(context.Background(), notifications.KindNewMusic,
notifications.ToUser(user.ID), p.Map()); err != nil {
t.Fatalf("notify: %v", err)
}
}
@@ -67,7 +68,7 @@ func TestMyNotifications_ListRendersPagesAndCounts(t *testing.T) {
t.Fatalf("first page = %d items, unread %d, next %q; want 2, 3, a cursor", len(page.Items), page.UnreadCount, page.NextBefore)
}
first := page.Items[0]
if first.Title != "Now in your library" || first.Body != "Album has arrived." || first.Link != "/albums/al-1" || first.ReadAt != nil {
if first.Title != "New album" || first.Body != "Artist – Album is in the library." || first.Link != "/albums/al-1" || first.ReadAt != nil {
t.Errorf("rendered item = %+v", first)
}
@@ -199,27 +200,24 @@ func TestMyNotificationSettings_DefaultsRoundTripAndAdminKinds(t *testing.T) {
}
// A partial change touches only what it names.
body := `{"kinds":[{"kind":"request_completed","email":false}]}`
body := `{"kinds":[{"kind":"new_music","phone":true}]}`
if code := callAs(t, r, user, http.MethodPut, "/api/me/notification-settings", body, &s); code != http.StatusOK {
t.Fatalf("put = %d", code)
}
for _, k := range s.Kinds {
if k.Kind == notifications.KindRequestCompleted && (k.Email || !k.Inbox || !k.Phone) {
t.Errorf("after PUT request_completed = %+v, want inbox+phone on, email off", k)
}
if k.Kind == notifications.KindRequestApproved && !k.Email {
t.Errorf("an untouched kind changed: %+v", k)
if k.Kind == notifications.KindNewMusic && (!k.Phone || !k.Inbox || !k.Email) {
t.Errorf("after PUT new_music = %+v, want phone on and inbox, email left on", k)
}
}
// A non-admin can't set an admin kind; nothing in the batch is applied.
bad := `{"kinds":[{"kind":"request_approved","inbox":false},{"kind":"tracks_missing","inbox":false}]}`
bad := `{"kinds":[{"kind":"new_music","inbox":false},{"kind":"tracks_missing","inbox":false}]}`
if code := callAs(t, r, user, http.MethodPut, "/api/me/notification-settings", bad, nil); code != http.StatusBadRequest {
t.Errorf("non-admin setting an admin kind = %d, want 400", code)
}
callAs(t, r, user, http.MethodGet, "/api/me/notification-settings", "", &s)
for _, k := range s.Kinds {
if k.Kind == notifications.KindRequestApproved && !k.Inbox {
if k.Kind == notifications.KindNewMusic && !k.Inbox {
t.Error("a refused batch was partly applied")
}
}
+1 -2
View File
@@ -6,7 +6,6 @@ import (
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/library"
"git.fabledsword.com/bvandeusen/minstrel/internal/notifications"
)
@@ -48,7 +47,7 @@ func (h *handlers) trackLabel(ctx context.Context, trackID pgtype.UUID) string {
return ""
}
if a, aerr := q.GetArtistByID(ctx, t.ArtistID); aerr == nil {
return library.TrackLabel(a.Name, t.Title)
return notifications.TrackLabel(a.Name, t.Title)
}
return t.Title
}
+100 -17
View File
@@ -11,6 +11,72 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
const anyAlbumAnnounced = `-- name: AnyAlbumAnnounced :one
SELECT EXISTS (SELECT 1 FROM albums WHERE announced_at IS NOT NULL)
`
// False on a library no scan has announced yet: its first import is the
// library being set up, not new music arriving.
func (q *Queries) AnyAlbumAnnounced(ctx context.Context) (bool, error) {
row := q.db.QueryRow(ctx, anyAlbumAnnounced)
var exists bool
err := row.Scan(&exists)
return exists, err
}
const claimUnannouncedAlbums = `-- name: ClaimUnannouncedAlbums :many
WITH claimed AS (
UPDATE albums SET announced_at = now()
WHERE announced_at IS NULL
RETURNING id, title, artist_id, created_at
)
SELECT c.id, c.title, ar.id AS artist_id, ar.name AS artist_name,
(EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = c.id AND t.missing_since IS NULL)
AND NOT EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = c.id AND t.added_at < c.created_at))::boolean AS arrived
FROM claimed c
JOIN artists ar ON ar.id = c.artist_id
ORDER BY c.created_at, c.id
`
type ClaimUnannouncedAlbumsRow struct {
ID pgtype.UUID
Title string
ArtistID pgtype.UUID
ArtistName string
Arrived bool
}
// Stamps every album not yet announced and returns them, so each album is
// claimed by exactly one scan even when a full scan and the watcher overlap.
// arrived is whether it is news: it has a track on disk, and none of its
// tracks is older than the album. An album that formed because files already
// in the library were retagged holds older tracks, and is not new music.
func (q *Queries) ClaimUnannouncedAlbums(ctx context.Context) ([]ClaimUnannouncedAlbumsRow, error) {
rows, err := q.db.Query(ctx, claimUnannouncedAlbums)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ClaimUnannouncedAlbumsRow
for rows.Next() {
var i ClaimUnannouncedAlbumsRow
if err := rows.Scan(
&i.ID,
&i.Title,
&i.ArtistID,
&i.ArtistName,
&i.Arrived,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const countAlbums = `-- name: CountAlbums :one
SELECT COUNT(*) FROM albums
`
@@ -72,7 +138,7 @@ func (q *Queries) DeleteAlbumIfEmpty(ctx context.Context, id pgtype.UUID) (Delet
}
const getAlbumByArtistAndTitle = `-- name: GetAlbumByArtistAndTitle :one
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid FROM albums
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at FROM albums
WHERE albums.artist_id = $1 AND albums.title = $2
AND ($3::text IS NULL OR albums.release_group_mbid IS NULL
OR albums.release_group_mbid = $3)
@@ -119,12 +185,13 @@ func (q *Queries) GetAlbumByArtistAndTitle(ctx context.Context, arg GetAlbumByAr
&i.CoverArtSource,
&i.CoverArtSourcesVersion,
&i.ReleaseGroupMbid,
&i.AnnouncedAt,
)
return i, err
}
const getAlbumByID = `-- name: GetAlbumByID :one
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid FROM albums WHERE id = $1
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid, announced_at FROM albums WHERE id = $1
`
func (q *Queries) GetAlbumByID(ctx context.Context, id pgtype.UUID) (Album, error) {
@@ -143,6 +210,7 @@ func (q *Queries) GetAlbumByID(ctx context.Context, id pgtype.UUID) (Album, erro
&i.CoverArtSource,
&i.CoverArtSourcesVersion,
&i.ReleaseGroupMbid,
&i.AnnouncedAt,
)
return i, err
}
@@ -193,7 +261,7 @@ func (q *Queries) GetAlbumCoverageRollup(ctx context.Context) (GetAlbumCoverageR
}
const getAlbumWithArtist = `-- name: GetAlbumWithArtist :one
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, artists.name AS artist_name
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at, artists.name AS artist_name
FROM albums
JOIN artists ON artists.id = albums.artist_id
WHERE albums.id = $1
@@ -223,13 +291,14 @@ func (q *Queries) GetAlbumWithArtist(ctx context.Context, id pgtype.UUID) (GetAl
&i.Album.CoverArtSource,
&i.Album.CoverArtSourcesVersion,
&i.Album.ReleaseGroupMbid,
&i.Album.AnnouncedAt,
&i.ArtistName,
)
return i, err
}
const getAlbumsByIDs = `-- name: GetAlbumsByIDs :many
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid FROM albums WHERE id = ANY($1::uuid[])
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid, announced_at FROM albums WHERE id = ANY($1::uuid[])
`
// Batched lookup used by /api/library/sync to hydrate upsert payloads
@@ -256,6 +325,7 @@ func (q *Queries) GetAlbumsByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([
&i.CoverArtSource,
&i.CoverArtSourcesVersion,
&i.ReleaseGroupMbid,
&i.AnnouncedAt,
); err != nil {
return nil, err
}
@@ -268,7 +338,7 @@ func (q *Queries) GetAlbumsByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([
}
const listAlbumsAlphaByArtist = `-- name: ListAlbumsAlphaByArtist :many
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, artists.sort_name AS artist_sort_name
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at, artists.sort_name AS artist_sort_name
FROM albums
JOIN artists ON artists.id = albums.artist_id
ORDER BY artists.sort_name, albums.sort_title
@@ -309,6 +379,7 @@ func (q *Queries) ListAlbumsAlphaByArtist(ctx context.Context, arg ListAlbumsAlp
&i.Album.CoverArtSource,
&i.Album.CoverArtSourcesVersion,
&i.Album.ReleaseGroupMbid,
&i.Album.AnnouncedAt,
&i.ArtistSortName,
); err != nil {
return nil, err
@@ -322,7 +393,7 @@ func (q *Queries) ListAlbumsAlphaByArtist(ctx context.Context, arg ListAlbumsAlp
}
const listAlbumsAlphaByName = `-- name: ListAlbumsAlphaByName :many
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid FROM albums ORDER BY sort_title LIMIT $1 OFFSET $2
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid, announced_at FROM albums ORDER BY sort_title LIMIT $1 OFFSET $2
`
type ListAlbumsAlphaByNameParams struct {
@@ -352,6 +423,7 @@ func (q *Queries) ListAlbumsAlphaByName(ctx context.Context, arg ListAlbumsAlpha
&i.CoverArtSource,
&i.CoverArtSourcesVersion,
&i.ReleaseGroupMbid,
&i.AnnouncedAt,
); err != nil {
return nil, err
}
@@ -364,7 +436,7 @@ func (q *Queries) ListAlbumsAlphaByName(ctx context.Context, arg ListAlbumsAlpha
}
const listAlbumsAlphaWithArtist = `-- name: ListAlbumsAlphaWithArtist :many
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, artists.name AS artist_name
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at, artists.name AS artist_name
FROM albums
JOIN artists ON artists.id = albums.artist_id
ORDER BY albums.sort_title, albums.id
@@ -405,6 +477,7 @@ func (q *Queries) ListAlbumsAlphaWithArtist(ctx context.Context, arg ListAlbumsA
&i.Album.CoverArtSource,
&i.Album.CoverArtSourcesVersion,
&i.Album.ReleaseGroupMbid,
&i.Album.AnnouncedAt,
&i.ArtistName,
); err != nil {
return nil, err
@@ -418,7 +491,7 @@ func (q *Queries) ListAlbumsAlphaWithArtist(ctx context.Context, arg ListAlbumsA
}
const listAlbumsByArtist = `-- name: ListAlbumsByArtist :many
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid FROM albums WHERE artist_id = $1 ORDER BY release_date NULLS LAST, sort_title
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid, announced_at FROM albums WHERE artist_id = $1 ORDER BY release_date NULLS LAST, sort_title
`
func (q *Queries) ListAlbumsByArtist(ctx context.Context, artistID pgtype.UUID) ([]Album, error) {
@@ -443,6 +516,7 @@ func (q *Queries) ListAlbumsByArtist(ctx context.Context, artistID pgtype.UUID)
&i.CoverArtSource,
&i.CoverArtSourcesVersion,
&i.ReleaseGroupMbid,
&i.AnnouncedAt,
); err != nil {
return nil, err
}
@@ -455,7 +529,7 @@ func (q *Queries) ListAlbumsByArtist(ctx context.Context, artistID pgtype.UUID)
}
const listAlbumsByArtistWithTrackCount = `-- name: ListAlbumsByArtistWithTrackCount :many
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid,
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at,
(SELECT count(*) FROM tracks t WHERE t.album_id = albums.id)::bigint
AS track_count
FROM albums
@@ -493,6 +567,7 @@ func (q *Queries) ListAlbumsByArtistWithTrackCount(ctx context.Context, artistID
&i.Album.CoverArtSource,
&i.Album.CoverArtSourcesVersion,
&i.Album.ReleaseGroupMbid,
&i.Album.AnnouncedAt,
&i.TrackCount,
); err != nil {
return nil, err
@@ -506,7 +581,7 @@ func (q *Queries) ListAlbumsByArtistWithTrackCount(ctx context.Context, artistID
}
const listAlbumsByGenre = `-- name: ListAlbumsByGenre :many
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at
FROM albums
WHERE EXISTS (
SELECT 1
@@ -558,6 +633,7 @@ func (q *Queries) ListAlbumsByGenre(ctx context.Context, arg ListAlbumsByGenrePa
&i.CoverArtSource,
&i.CoverArtSourcesVersion,
&i.ReleaseGroupMbid,
&i.AnnouncedAt,
); err != nil {
return nil, err
}
@@ -582,7 +658,7 @@ WITH plays AS (
WHERE t.missing_since IS NULL
GROUP BY t.album_id
)
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at
FROM per_album pa
JOIN albums ON albums.id = pa.album_id
ORDER BY pa.cnt DESC, albums.id
@@ -621,6 +697,7 @@ func (q *Queries) ListAlbumsFrequentlyPlayedByUser(ctx context.Context, arg List
&i.CoverArtSource,
&i.CoverArtSourcesVersion,
&i.ReleaseGroupMbid,
&i.AnnouncedAt,
); err != nil {
return nil, err
}
@@ -689,7 +766,7 @@ func (q *Queries) ListAlbumsMissingMbidWithTrack(ctx context.Context, limit int3
}
const listAlbumsNewest = `-- name: ListAlbumsNewest :many
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid FROM albums ORDER BY created_at DESC LIMIT $1 OFFSET $2
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid, announced_at FROM albums ORDER BY created_at DESC LIMIT $1 OFFSET $2
`
type ListAlbumsNewestParams struct {
@@ -719,6 +796,7 @@ func (q *Queries) ListAlbumsNewest(ctx context.Context, arg ListAlbumsNewestPara
&i.CoverArtSource,
&i.CoverArtSourcesVersion,
&i.ReleaseGroupMbid,
&i.AnnouncedAt,
); err != nil {
return nil, err
}
@@ -731,7 +809,7 @@ func (q *Queries) ListAlbumsNewest(ctx context.Context, arg ListAlbumsNewestPara
}
const listAlbumsRandom = `-- name: ListAlbumsRandom :many
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid FROM albums ORDER BY random() LIMIT $1
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid, announced_at FROM albums ORDER BY random() LIMIT $1
`
func (q *Queries) ListAlbumsRandom(ctx context.Context, limit int32) ([]Album, error) {
@@ -756,6 +834,7 @@ func (q *Queries) ListAlbumsRandom(ctx context.Context, limit int32) ([]Album, e
&i.CoverArtSource,
&i.CoverArtSourcesVersion,
&i.ReleaseGroupMbid,
&i.AnnouncedAt,
); err != nil {
return nil, err
}
@@ -780,7 +859,7 @@ WITH plays AS (
WHERE t.missing_since IS NULL
GROUP BY t.album_id
)
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at
FROM per_album pa
JOIN albums ON albums.id = pa.album_id
ORDER BY pa.last_played DESC, albums.id
@@ -823,6 +902,7 @@ func (q *Queries) ListAlbumsRecentlyPlayedByUser(ctx context.Context, arg ListAl
&i.CoverArtSource,
&i.CoverArtSourcesVersion,
&i.ReleaseGroupMbid,
&i.AnnouncedAt,
); err != nil {
return nil, err
}
@@ -835,7 +915,7 @@ func (q *Queries) ListAlbumsRecentlyPlayedByUser(ctx context.Context, arg ListAl
}
const listRecentlyAddedAlbumsWithArtist = `-- name: ListRecentlyAddedAlbumsWithArtist :many
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, artists.name AS artist_name
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at, artists.name AS artist_name
FROM albums
JOIN artists ON artists.id = albums.artist_id
ORDER BY albums.created_at DESC, albums.id
@@ -873,6 +953,7 @@ func (q *Queries) ListRecentlyAddedAlbumsWithArtist(ctx context.Context, limit i
&i.Album.CoverArtSource,
&i.Album.CoverArtSourcesVersion,
&i.Album.ReleaseGroupMbid,
&i.Album.AnnouncedAt,
&i.ArtistName,
); err != nil {
return nil, err
@@ -886,7 +967,7 @@ func (q *Queries) ListRecentlyAddedAlbumsWithArtist(ctx context.Context, limit i
}
const searchAlbums = `-- name: SearchAlbums :many
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid FROM albums
SELECT id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid, announced_at FROM albums
WHERE title ILIKE '%' || $1 || '%'
ORDER BY sort_title
LIMIT $2 OFFSET $3
@@ -920,6 +1001,7 @@ func (q *Queries) SearchAlbums(ctx context.Context, arg SearchAlbumsParams) ([]A
&i.CoverArtSource,
&i.CoverArtSourcesVersion,
&i.ReleaseGroupMbid,
&i.AnnouncedAt,
); err != nil {
return nil, err
}
@@ -1011,7 +1093,7 @@ DO UPDATE SET
cover_art_path = EXCLUDED.cover_art_path,
release_group_mbid = COALESCE(EXCLUDED.release_group_mbid, albums.release_group_mbid),
updated_at = now()
RETURNING id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid
RETURNING id, title, sort_title, artist_id, release_date, mbid, cover_art_path, created_at, updated_at, cover_art_source, cover_art_sources_version, release_group_mbid, announced_at
`
type UpsertAlbumParams struct {
@@ -1048,6 +1130,7 @@ func (q *Queries) UpsertAlbum(ctx context.Context, arg UpsertAlbumParams) (Album
&i.CoverArtSource,
&i.CoverArtSourcesVersion,
&i.ReleaseGroupMbid,
&i.AnnouncedAt,
)
return i, err
}
+4 -2
View File
@@ -100,7 +100,7 @@ func (q *Queries) ListAlbumYearsWithCount(ctx context.Context) ([]ListAlbumYears
}
const listAlbumsByGenreWithArtist = `-- name: ListAlbumsByGenreWithArtist :many
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, artists.name AS artist_name
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at, artists.name AS artist_name
FROM albums
JOIN artists ON artists.id = albums.artist_id
WHERE EXISTS (
@@ -153,6 +153,7 @@ func (q *Queries) ListAlbumsByGenreWithArtist(ctx context.Context, arg ListAlbum
&i.Album.CoverArtSource,
&i.Album.CoverArtSourcesVersion,
&i.Album.ReleaseGroupMbid,
&i.Album.AnnouncedAt,
&i.ArtistName,
); err != nil {
return nil, err
@@ -166,7 +167,7 @@ func (q *Queries) ListAlbumsByGenreWithArtist(ctx context.Context, arg ListAlbum
}
const listAlbumsByYearRangeWithArtist = `-- name: ListAlbumsByYearRangeWithArtist :many
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, artists.name AS artist_name
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at, artists.name AS artist_name
FROM albums
JOIN artists ON artists.id = albums.artist_id
WHERE albums.release_date IS NOT NULL
@@ -221,6 +222,7 @@ func (q *Queries) ListAlbumsByYearRangeWithArtist(ctx context.Context, arg ListA
&i.Album.CoverArtSource,
&i.Album.CoverArtSourcesVersion,
&i.Album.ReleaseGroupMbid,
&i.Album.AnnouncedAt,
&i.ArtistName,
); err != nil {
return nil, err
+2 -1
View File
@@ -141,7 +141,7 @@ func (q *Queries) ListLikedAlbumIDs(ctx context.Context, userID pgtype.UUID) ([]
}
const listLikedAlbumRows = `-- name: ListLikedAlbumRows :many
SELECT a.id, a.title, a.sort_title, a.artist_id, a.release_date, a.mbid, a.cover_art_path, a.created_at, a.updated_at, a.cover_art_source, a.cover_art_sources_version, a.release_group_mbid FROM albums a
SELECT a.id, a.title, a.sort_title, a.artist_id, a.release_date, a.mbid, a.cover_art_path, a.created_at, a.updated_at, a.cover_art_source, a.cover_art_sources_version, a.release_group_mbid, a.announced_at FROM albums a
JOIN general_likes_albums l ON l.album_id = a.id
WHERE l.user_id = $1
ORDER BY l.liked_at DESC
@@ -176,6 +176,7 @@ func (q *Queries) ListLikedAlbumRows(ctx context.Context, arg ListLikedAlbumRows
&i.CoverArtSource,
&i.CoverArtSourcesVersion,
&i.ReleaseGroupMbid,
&i.AnnouncedAt,
); err != nil {
return nil, err
}
+1
View File
@@ -248,6 +248,7 @@ type Album struct {
CoverArtSource *string
CoverArtSourcesVersion int32
ReleaseGroupMbid *string
AnnouncedAt pgtype.Timestamptz
}
type AlbumLoudness struct {
+24
View File
@@ -331,6 +331,30 @@ func (q *Queries) ListNotifications(ctx context.Context, arg ListNotificationsPa
return items, nil
}
const listUserIDs = `-- name: ListUserIDs :many
SELECT id FROM users ORDER BY created_at, id
`
func (q *Queries) ListUserIDs(ctx context.Context) ([]pgtype.UUID, error) {
rows, err := q.db.Query(ctx, listUserIDs)
if err != nil {
return nil, err
}
defer rows.Close()
var items []pgtype.UUID
for rows.Next() {
var id pgtype.UUID
if err := rows.Scan(&id); err != nil {
return nil, err
}
items = append(items, id)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const markAllNotificationsRead = `-- name: MarkAllNotificationsRead :execrows
UPDATE user_notifications
SET read_at = now()
+4 -2
View File
@@ -380,7 +380,7 @@ func (q *Queries) ListMostPlayedTracksForUser(ctx context.Context, arg ListMostP
}
const listRediscoverAlbumsFallbackForUser = `-- name: ListRediscoverAlbumsFallbackForUser :many
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, artists.name AS artist_name
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at, artists.name AS artist_name
FROM general_likes_albums gla
JOIN albums ON albums.id = gla.album_id
JOIN artists ON artists.id = albums.artist_id
@@ -429,6 +429,7 @@ func (q *Queries) ListRediscoverAlbumsFallbackForUser(ctx context.Context, arg L
&i.Album.CoverArtSource,
&i.Album.CoverArtSourcesVersion,
&i.Album.ReleaseGroupMbid,
&i.Album.AnnouncedAt,
&i.ArtistName,
); err != nil {
return nil, err
@@ -470,7 +471,7 @@ eligible AS (
HAVING COALESCE(max(pe.started_at), '1970-01-01'::timestamptz)
< now() - interval '14 days'
)
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, artists.name AS artist_name
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at, artists.name AS artist_name
FROM eligible e
JOIN albums ON albums.id = e.album_id
JOIN artists ON artists.id = albums.artist_id
@@ -522,6 +523,7 @@ func (q *Queries) ListRediscoverAlbumsForUser(ctx context.Context, arg ListRedis
&i.Album.CoverArtSource,
&i.Album.CoverArtSourcesVersion,
&i.Album.ReleaseGroupMbid,
&i.Album.AnnouncedAt,
&i.ArtistName,
); err != nil {
return nil, err
+4 -2
View File
@@ -102,7 +102,7 @@ WITH liked_album_ids AS (
JOIN tracks trk ON trk.id = likt.track_id
WHERE likt.user_id = $1
)
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, artists.name AS artist_name
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at, artists.name AS artist_name
FROM liked_album_ids lai
JOIN albums ON albums.id = lai.album_id
JOIN artists ON artists.id = albums.artist_id
@@ -144,6 +144,7 @@ func (q *Queries) ListYouMightLikeAlbumFallbackForUser(ctx context.Context, arg
&i.Album.CoverArtSource,
&i.Album.CoverArtSourcesVersion,
&i.Album.ReleaseGroupMbid,
&i.Album.AnnouncedAt,
&i.ArtistName,
); err != nil {
return nil, err
@@ -157,7 +158,7 @@ func (q *Queries) ListYouMightLikeAlbumFallbackForUser(ctx context.Context, arg
}
const listYouMightLikeAlbumsForUser = `-- name: ListYouMightLikeAlbumsForUser :many
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, artists.name AS artist_name
SELECT albums.id, albums.title, albums.sort_title, albums.artist_id, albums.release_date, albums.mbid, albums.cover_art_path, albums.created_at, albums.updated_at, albums.cover_art_source, albums.cover_art_sources_version, albums.release_group_mbid, albums.announced_at, artists.name AS artist_name
FROM you_might_like_albums yml
JOIN albums ON albums.id = yml.album_id
JOIN artists ON artists.id = albums.artist_id
@@ -203,6 +204,7 @@ func (q *Queries) ListYouMightLikeAlbumsForUser(ctx context.Context, arg ListYou
&i.Album.CoverArtSource,
&i.Album.CoverArtSourcesVersion,
&i.Album.ReleaseGroupMbid,
&i.Album.AnnouncedAt,
&i.ArtistName,
); err != nil {
return nil, err
@@ -0,0 +1,17 @@
ALTER TABLE albums DROP COLUMN IF EXISTS announced_at;
DELETE FROM user_notifications WHERE kind = 'new_music';
DELETE FROM user_notification_prefs WHERE kind = 'new_music';
ALTER TABLE user_notifications DROP CONSTRAINT user_notifications_kind_check;
ALTER TABLE user_notifications ADD CONSTRAINT user_notifications_kind_check
CHECK (kind IN ('request_approved', 'request_rejected', 'request_completed',
'request_pending', 'quarantine_flagged', 'scan_failed',
'tracks_missing', 'duplicates_found', 'duplicates_resolved',
'playback_errors'));
ALTER TABLE user_notification_prefs DROP CONSTRAINT user_notification_prefs_kind_check;
ALTER TABLE user_notification_prefs ADD CONSTRAINT user_notification_prefs_kind_check
CHECK (kind IN ('request_approved', 'request_rejected', 'request_completed',
'request_pending', 'quarantine_flagged', 'scan_failed',
'tracks_missing', 'duplicates_found', 'duplicates_resolved',
'playback_errors'));
@@ -0,0 +1,29 @@
-- New-music notices come from the library itself instead of from requests.
--
-- The request_* kinds announced the stages of a request: asked, approved,
-- declined, arrived. Minstrel no longer takes requests, so their rows and
-- per-user preferences are deleted, and the kind CHECKs swap them for
-- new_music, which announces albums a scan brought into the library.
--
-- albums.announced_at marks an album as already told about. A scan claims
-- every album without one and tells users about those that are new. Every
-- album held now counts as announced, so the first scan after this migration
-- announces nothing old.
DELETE FROM user_notifications
WHERE kind IN ('request_approved', 'request_rejected', 'request_completed', 'request_pending');
DELETE FROM user_notification_prefs
WHERE kind IN ('request_approved', 'request_rejected', 'request_completed', 'request_pending');
ALTER TABLE user_notifications DROP CONSTRAINT user_notifications_kind_check;
ALTER TABLE user_notifications ADD CONSTRAINT user_notifications_kind_check
CHECK (kind IN ('new_music', 'quarantine_flagged', 'scan_failed',
'tracks_missing', 'duplicates_found', 'duplicates_resolved',
'playback_errors'));
ALTER TABLE user_notification_prefs DROP CONSTRAINT user_notification_prefs_kind_check;
ALTER TABLE user_notification_prefs ADD CONSTRAINT user_notification_prefs_kind_check
CHECK (kind IN ('new_music', 'quarantine_flagged', 'scan_failed',
'tracks_missing', 'duplicates_found', 'duplicates_resolved',
'playback_errors'));
ALTER TABLE albums ADD COLUMN announced_at timestamptz;
UPDATE albums SET announced_at = created_at;
+23
View File
@@ -12,6 +12,29 @@ DO UPDATE SET
updated_at = now()
RETURNING *;
-- name: AnyAlbumAnnounced :one
-- False on a library no scan has announced yet: its first import is the
-- library being set up, not new music arriving.
SELECT EXISTS (SELECT 1 FROM albums WHERE announced_at IS NOT NULL);
-- name: ClaimUnannouncedAlbums :many
-- Stamps every album not yet announced and returns them, so each album is
-- claimed by exactly one scan even when a full scan and the watcher overlap.
-- arrived is whether it is news: it has a track on disk, and none of its
-- tracks is older than the album. An album that formed because files already
-- in the library were retagged holds older tracks, and is not new music.
WITH claimed AS (
UPDATE albums SET announced_at = now()
WHERE announced_at IS NULL
RETURNING id, title, artist_id, created_at
)
SELECT c.id, c.title, ar.id AS artist_id, ar.name AS artist_name,
(EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = c.id AND t.missing_since IS NULL)
AND NOT EXISTS (SELECT 1 FROM tracks t WHERE t.album_id = c.id AND t.added_at < c.created_at))::boolean AS arrived
FROM claimed c
JOIN artists ar ON ar.id = c.artist_id
ORDER BY c.created_at, c.id;
-- name: GetAlbumByID :one
SELECT * FROM albums WHERE id = $1;
+3
View File
@@ -83,6 +83,9 @@ DELETE FROM user_notifications
-- name: ListAdminUserIDs :many
SELECT id FROM users WHERE is_admin = true ORDER BY created_at, id;
-- name: ListUserIDs :many
SELECT id FROM users ORDER BY created_at, id;
-- name: ListNotificationPrefsForUser :many
SELECT kind, inbox, phone, email
FROM user_notification_prefs
+2 -2
View File
@@ -208,7 +208,7 @@ func ResolveDuplicatesWith(
merged[g.id] = true
res.Merged++
if len(mergedNames) < notifyItems {
mergedNames = append(mergedNames, TrackLabel(g.members[0].ArtistName, g.members[0].Title))
mergedNames = append(mergedNames, notifications.TrackLabel(g.members[0].ArtistName, g.members[0].Title))
}
}
@@ -278,7 +278,7 @@ func foldAlbumCopies(
gone[i] = true
n++
if len(names) < notifyItems {
names = append(names, TrackLabel(g.members[i].ArtistName, g.members[i].Title))
names = append(names, notifications.TrackLabel(g.members[i].ArtistName, g.members[i].Title))
}
}
}
+2 -2
View File
@@ -239,7 +239,7 @@ func settleWrongSlots(
}
settled[g.id] = true
for _, i := range losers {
names = append(names, TrackLabel(g.members[i].ArtistName, g.members[i].Title))
names = append(names, notifications.TrackLabel(g.members[i].ArtistName, g.members[i].Title))
}
}
return removed, names
@@ -563,7 +563,7 @@ func notifyNewForReview(ctx context.Context, groups []*resolveGroup, settled map
}
count++
if len(items) < notifyItems {
items = append(items, TrackLabel(g.members[0].ArtistName, g.members[0].Title))
items = append(items, notifications.TrackLabel(g.members[0].ArtistName, g.members[0].Title))
}
}
if count > 0 {
+96
View File
@@ -0,0 +1,96 @@
package library
import (
"context"
"encoding/json"
"io"
"log/slog"
"path/filepath"
"testing"
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
"git.fabledsword.com/bvandeusen/minstrel/internal/dbtest"
"git.fabledsword.com/bvandeusen/minstrel/internal/notifications"
)
// newMusicNotices returns user's unread new_music notices' payloads.
func newMusicNotices(t *testing.T, q *dbq.Queries, user dbq.User) []notifications.Payload {
t.Helper()
rows, err := q.ListNotifications(context.Background(), dbq.ListNotificationsParams{UserID: user.ID, PageLimit: 50})
if err != nil {
t.Fatalf("list notifications: %v", err)
}
var out []notifications.Payload
for _, r := range rows {
if r.ReadAt.Valid || r.Kind != string(notifications.KindNewMusic) {
continue
}
var p notifications.Payload
if err := json.Unmarshal(r.Payload, &p); err != nil {
t.Fatalf("payload: %v", err)
}
out = append(out, p)
}
return out
}
// Every user hears about an album new to the library, once. The first import
// into an empty library is the library being set up, and an album that only
// formed because tracks already held were retagged into it is not new music.
func TestAnnounceNewAlbums_TellsEveryoneOfEachNewAlbumOnce(t *testing.T) {
pool := newPool(t)
admin := notifyingAdmin(t, pool)
ctx := context.Background()
q := dbq.New(pool)
listener, err := q.CreateUser(ctx, dbq.CreateUserParams{
Username: dbtest.TestUserPrefix + "listener", PasswordHash: "x", ApiTokenHash: "listener",
})
if err != nil {
t.Fatal(err)
}
s := New(pool, slog.New(slog.NewTextHandler(io.Discard, nil)), nil, nil)
dir := t.TempDir()
// First import: claimed, not announced.
held, _, artist := seedTrack(t, pool, filepath.Join(dir, "a.mp3"))
s.announceNewAlbums(ctx)
if got := newMusicNotices(t, q, admin); len(got) != 0 {
t.Fatalf("the first import was announced: %+v", got)
}
// A new album with its own new track.
arrived, err := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: "Geogaddi", SortTitle: "Geogaddi", ArtistID: artist.ID})
if err != nil {
t.Fatal(err)
}
if _, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{
Title: "Music Is Math", AlbumID: arrived.ID, ArtistID: artist.ID,
DurationMs: 1000, FilePath: filepath.Join(dir, "b.mp3"), FileSize: 100, FileFormat: "mp3",
}); err != nil {
t.Fatal(err)
}
// And an album formed by retagging a track the library already held.
retagged, err := q.UpsertAlbum(ctx, dbq.UpsertAlbumParams{Title: "Retitled", SortTitle: "Retitled", ArtistID: artist.ID})
if err != nil {
t.Fatal(err)
}
if _, err := pool.Exec(ctx, `UPDATE tracks SET album_id = $2 WHERE id = $1`, held.ID, retagged.ID); err != nil {
t.Fatal(err)
}
s.announceNewAlbums(ctx)
for _, u := range []dbq.User{admin, listener} {
got := newMusicNotices(t, q, u)
if len(got) != 1 || got[0].Count != 1 || len(got[0].Albums) != 1 ||
got[0].Albums[0].Title != "Geogaddi" || got[0].Albums[0].AlbumID != arrived.ID.String() ||
got[0].Albums[0].Artist != "Delete Test Artist" {
t.Fatalf("%s's notices = %+v, want one naming Geogaddi alone", u.Username, got)
}
}
// Nothing new since: nothing more to say.
s.announceNewAlbums(ctx)
if got := newMusicNotices(t, q, listener); len(got) != 1 {
t.Fatalf("an announced album was announced again: %+v", got)
}
}
+44 -7
View File
@@ -39,13 +39,50 @@ func notifyAdmins(ctx context.Context, kind notifications.Kind, p notifications.
// keeps the newest ten across a coalesced burst.
const notifyItems = 10
// TrackLabel names a track as a notice does: "Artist – Title", or the title
// alone when the artist is unknown.
func TrackLabel(artist, title string) string {
if artist == "" {
return title
// announceNewAlbums claims the albums no scan has announced yet and tells
// every user about the ones that are new music. Called when a scan or a
// watcher batch has finished adding files, so an album is announced whole.
// The first import into an empty library is claimed without a notice.
func (s *Scanner) announceNewAlbums(ctx context.Context) {
if ctx.Err() != nil {
return
}
return artist + " – " + title
q := dbq.New(s.pool)
announcedBefore, err := q.AnyAlbumAnnounced(ctx)
if err != nil {
s.logger.Warn("library scan: reading announced albums failed", "err", err)
return
}
rows, err := q.ClaimUnannouncedAlbums(ctx)
if err != nil {
s.logger.Warn("library scan: claiming new albums failed", "err", err)
return
}
if !announcedBefore {
return
}
var p notifications.Payload
for _, r := range rows {
if !r.Arrived {
continue
}
p.Count++
if len(p.Albums) < notifications.AlbumsKept {
p.Albums = append(p.Albums, notifications.Arrival{
Artist: r.ArtistName,
Title: r.Title,
ArtistID: r.ArtistID.String(),
AlbumID: r.ID.String(),
})
}
}
if p.Count == 0 {
return
}
notifierMu.RLock()
n := notifier
notifierMu.RUnlock()
n.NotifyLogged(ctx, notifications.KindNewMusic, notifications.ToEveryone(), p.Map())
}
// notifyStillMissing tells admins which of the tracks a scan marked missing
@@ -66,7 +103,7 @@ func (s *Scanner) notifyStillMissing(ctx context.Context, marked []pgtype.UUID)
}
items := make([]string, 0, min(len(rows), notifyItems))
for _, r := range rows[:min(len(rows), notifyItems)] {
items = append(items, TrackLabel(r.ArtistName, r.Title))
items = append(items, notifications.TrackLabel(r.ArtistName, r.Title))
}
notifyAdmins(ctx, notifications.KindTracksMissing, notifications.Payload{Count: int64(len(rows)), Items: items})
}
+4
View File
@@ -213,6 +213,7 @@ func (s *Scanner) Scan(ctx context.Context, progressCb func(Stats)) (Stats, erro
// Told only now, so a file that moved or was upgraded, which this scan
// marked and then healed, is not reported as lost.
s.notifyStillMissing(ctx, stats.marked)
s.announceNewAlbums(ctx)
return stats, nil
}
@@ -509,6 +510,9 @@ func (s *Scanner) ScanFiles(ctx context.Context, paths []string) ([]pgtype.UUID,
s.logger.Warn("library watch: missing-pair pass failed", "err", err)
}
}
if stats.Added > 0 {
s.announceNewAlbums(ctx)
}
return changed, nil
}
+25 -24
View File
@@ -32,14 +32,14 @@ func digestWith(pool *pgxpool.Pool) (*notifications.Digest, *mailer.FakeSender)
func TestDigest_BatchGoesOutOnceAWindowAfterTheFirstItem(t *testing.T) {
pool := testPool(t)
ctx := context.Background()
alice := emailUser(t, pool, "alice", false)
alice := emailUser(t, pool, "alice", true)
n := notifications.New(pool, nil, nil)
d, fake := digestWith(pool)
start := time.Now()
require.NoError(t, n.Notify(ctx, notifications.KindRequestApproved, notifications.ToUser(alice),
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(alice),
notifications.Payload{Name: "Moe Shop – WWW"}.Map()))
require.NoError(t, n.Notify(ctx, notifications.KindRequestRejected, notifications.ToUser(alice),
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(alice),
notifications.Payload{Name: "Tycho – Awake", Reason: "already have it"}.Map()))
d.Tick(ctx, start.Add(30*time.Minute))
@@ -49,8 +49,8 @@ func TestDigest_BatchGoesOutOnceAWindowAfterTheFirstItem(t *testing.T) {
require.Len(t, fake.Sent, 1, "both items, one email")
require.Equal(t, "alice@example.com", fake.Sent[0].To)
require.Equal(t, "Minstrel: 2 updates", fake.Sent[0].Subject)
require.Contains(t, fake.Sent[0].TextBody, "Moe Shop – WWW is on its way.")
require.Contains(t, fake.Sent[0].TextBody, "Tycho – Awake was declined: already have it")
require.Contains(t, fake.Sent[0].TextBody, "Someone flagged Moe Shop – WWW.")
require.Contains(t, fake.Sent[0].TextBody, "Someone flagged Tycho – Awake: already have it")
d.Tick(ctx, start.Add(3*time.Hour))
require.Len(t, fake.Sent, 1, "never sent twice")
@@ -60,12 +60,12 @@ func TestDigest_ReadBeforeTheEmailIsNotEmailed(t *testing.T) {
pool := testPool(t)
ctx := context.Background()
q := dbq.New(pool)
alice := emailUser(t, pool, "alice", false)
alice := emailUser(t, pool, "alice", true)
n := notifications.New(pool, nil, nil)
d, fake := digestWith(pool)
start := time.Now()
require.NoError(t, n.Notify(ctx, notifications.KindRequestApproved, notifications.ToUser(alice), nil))
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(alice), nil))
_, err := q.MarkAllNotificationsRead(ctx, dbq.MarkAllNotificationsReadParams{UserID: alice})
require.NoError(t, err)
@@ -77,20 +77,20 @@ func TestDigest_EmailOffAtNotifyTimeIsNeverSent(t *testing.T) {
pool := testPool(t)
ctx := context.Background()
q := dbq.New(pool)
alice := emailUser(t, pool, "alice", false)
alice := emailUser(t, pool, "alice", true)
off := false
_, err := notifications.SaveSettings(ctx, q, alice, false, []notifications.SettingChange{
{Kind: notifications.KindRequestApproved, Email: &off},
_, err := notifications.SaveSettings(ctx, q, alice, true, []notifications.SettingChange{
{Kind: notifications.KindQuarantineFlagged, Email: &off},
})
require.NoError(t, err)
n := notifications.New(pool, nil, nil)
d, fake := digestWith(pool)
start := time.Now()
require.NoError(t, n.Notify(ctx, notifications.KindRequestApproved, notifications.ToUser(alice), nil))
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(alice), nil))
on := true
_, err = notifications.SaveSettings(ctx, q, alice, false, []notifications.SettingChange{
{Kind: notifications.KindRequestApproved, Email: &on},
_, err = notifications.SaveSettings(ctx, q, alice, true, []notifications.SettingChange{
{Kind: notifications.KindQuarantineFlagged, Email: &on},
})
require.NoError(t, err)
@@ -101,11 +101,11 @@ func TestDigest_EmailOffAtNotifyTimeIsNeverSent(t *testing.T) {
func TestDigest_NoAddressNoEmail(t *testing.T) {
pool := testPool(t)
ctx := context.Background()
bob := mkUser(t, dbq.New(pool), "bob", false)
bob := mkUser(t, dbq.New(pool), "bob", true)
n := notifications.New(pool, nil, nil)
d, fake := digestWith(pool)
require.NoError(t, n.Notify(ctx, notifications.KindRequestApproved, notifications.ToUser(bob), nil))
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(bob), nil))
d.Tick(ctx, time.Now().Add(2*time.Hour))
require.Empty(t, fake.Sent)
}
@@ -136,12 +136,12 @@ func TestDigest_CoalescedItemAppearsOnceWithItsLatestCount(t *testing.T) {
func TestDigest_MailerFailureRetriesWithBackoffAndSendsOnce(t *testing.T) {
pool := testPool(t)
ctx := context.Background()
alice := emailUser(t, pool, "alice", false)
alice := emailUser(t, pool, "alice", true)
n := notifications.New(pool, nil, nil)
d, fake := digestWith(pool)
start := time.Now()
require.NoError(t, n.Notify(ctx, notifications.KindRequestApproved, notifications.ToUser(alice), nil))
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(alice), nil))
fake.FailNext = errors.New("smtp: 421 try later")
d.Tick(ctx, start.Add(61*time.Minute))
@@ -160,7 +160,7 @@ func TestDigest_MailerFailureRetriesWithBackoffAndSendsOnce(t *testing.T) {
func TestDigest_NewMusicWaitsForTheSummaryHour(t *testing.T) {
pool := testPool(t)
ctx := context.Background()
alice := emailUser(t, pool, "alice", false) // timezone defaults to UTC
alice := emailUser(t, pool, "alice", true) // timezone defaults to UTC
n := notifications.New(pool, nil, nil)
d, fake := digestWith(pool)
@@ -170,11 +170,12 @@ func TestDigest_NewMusicWaitsForTheSummaryHour(t *testing.T) {
slot = slot.AddDate(0, 0, 1)
}
for _, p := range []notifications.Payload{
{Name: "Moe Shop – WWW", Artist: "Moe Shop", Title: "WWW"},
{Name: "Moe Shop – Pure", Artist: "Moe Shop", Title: "Pure"},
for _, a := range []notifications.Arrival{
{Artist: "Moe Shop", Title: "WWW"},
{Artist: "Moe Shop", Title: "Pure"},
} {
require.NoError(t, n.Notify(ctx, notifications.KindRequestCompleted, notifications.ToUser(alice), p.Map()))
p := notifications.Payload{Count: 1, Albums: []notifications.Arrival{a}}
require.NoError(t, n.Notify(ctx, notifications.KindNewMusic, notifications.ToUser(alice), p.Map()))
}
d.Tick(ctx, slot.Add(-time.Minute))
@@ -210,14 +211,14 @@ func TestEmailSettings_DefaultsMatchTheMigrationAndRoundTrip(t *testing.T) {
func TestDigest_UsesTheConfiguredBatchWindow(t *testing.T) {
pool := testPool(t)
ctx := context.Background()
alice := emailUser(t, pool, "alice", false)
alice := emailUser(t, pool, "alice", true)
_, err := notifications.SaveEmailSettings(ctx, dbq.New(pool), notifications.EmailSettings{SummaryHour: 9, BatchWindowMinutes: 15})
require.NoError(t, err)
n := notifications.New(pool, nil, nil)
d, fake := digestWith(pool)
start := time.Now()
require.NoError(t, n.Notify(ctx, notifications.KindRequestApproved, notifications.ToUser(alice), nil))
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, notifications.ToUser(alice), nil))
d.Tick(ctx, start.Add(16*time.Minute))
require.Len(t, fake.Sent, 1)
}
+11 -14
View File
@@ -77,36 +77,33 @@ func renderDigest(group EmailGroup, name string, items []pendingItem, base strin
}
// summaryArtists groups new-music arrivals by artist, in the order each
// artist first arrived, with each artist's albums and tracks under them. An
// artist request has no title of its own; the artist links to its page.
// artist first arrived, with each artist's albums under them.
func summaryArtists(items []pendingItem, base string) []digestArtist {
var out []digestArtist
index := map[string]int{}
for _, it := range items {
var p Payload
_ = json.Unmarshal(it.Payload, &p)
artist, title := p.Artist, p.Title
for _, a := range p.Albums {
artist := a.Artist
if artist == "" {
// Rows from before Artist was recorded carry only "Artist – Title".
artist, title, _ = strings.Cut(p.Name, " – ")
}
if artist == "" {
artist = "Something you asked for"
artist = "Unknown artist"
}
i, ok := index[artist]
if !ok {
i = len(out)
index[artist] = i
out = append(out, digestArtist{Name: artist})
if a.ArtistID != "" {
out[i].URL = joinURL(base, "/artists/"+a.ArtistID)
}
link := Render(it.Kind, it.Payload).Link
if title == "" {
if p.ArtistID != "" {
out[i].URL = joinURL(base, link)
}
continue
link := "/"
if a.AlbumID != "" {
link = "/albums/" + a.AlbumID
}
out[i].Titles = append(out[i].Titles, digestLine{Title: a.Title, URL: joinURL(base, link)})
}
out[i].Titles = append(out[i].Titles, digestLine{Title: title, URL: joinURL(base, link)})
}
return out
}
+21 -22
View File
@@ -28,8 +28,8 @@ func allEmail() map[Kind]bool {
}
func TestPlanUser_BatchWindow(t *testing.T) {
first := item(KindRequestApproved, t0)
later := item(KindRequestRejected, t0.Add(40*time.Minute))
first := item(KindQuarantineFlagged, t0)
later := item(KindTracksMissing, t0.Add(40*time.Minute))
cases := []struct {
name string
now time.Time
@@ -63,7 +63,7 @@ func TestPlanUser_QuietBatchSendsNothing(t *testing.T) {
func TestPlanUser_SummaryHourInUserTimezone(t *testing.T) {
ny, err := time.LoadLocation("America/New_York")
require.NoError(t, err)
arrived := []pendingItem{item(KindRequestCompleted, time.Date(2026, 7, 1, 3, 0, 0, 0, time.UTC))}
arrived := []pendingItem{item(KindNewMusic, time.Date(2026, 7, 1, 3, 0, 0, 0, time.UTC))}
// 09:00 in New York in July is 13:00 UTC (EDT, UTC-4).
cases := []struct {
name string
@@ -90,7 +90,7 @@ func TestPlanUser_SummaryHourInUserTimezone(t *testing.T) {
func TestPlanUser_SummaryAcrossDST(t *testing.T) {
ny, err := time.LoadLocation("America/New_York")
require.NoError(t, err)
arrived := []pendingItem{item(KindRequestCompleted, time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC))}
arrived := []pendingItem{item(KindNewMusic, time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC))}
// Clocks go back on 1 November 2026: 09:00 the day before is 13:00 UTC
// (EDT), and 09:00 that day is 14:00 UTC (EST).
before := groupState{LastSentAt: time.Date(2026, 10, 31, 13, 0, 0, 0, time.UTC)}
@@ -102,7 +102,7 @@ func TestPlanUser_SummaryAcrossDST(t *testing.T) {
// Spring forward, 8 March 2026: 02:00 does not exist. A summary hour of
// 2 still goes out that day, once.
gap := EmailSettings{SummaryHour: 2, BatchWindowMinutes: 60}
spring := []pendingItem{item(KindRequestCompleted, time.Date(2026, 3, 8, 0, 0, 0, 0, time.UTC))}
spring := []pendingItem{item(KindNewMusic, time.Date(2026, 3, 8, 0, 0, 0, 0, time.UTC))}
at := time.Date(2026, 3, 8, 9, 0, 0, 0, time.UTC) // 05:00 EDT
p = planUser(at, gap, ny, spring, allEmail(), groupState{}, groupState{LastSentAt: time.Date(2026, 3, 7, 7, 0, 0, 0, time.UTC)})
require.True(t, p.SummaryDue)
@@ -112,10 +112,10 @@ func TestPlanUser_SummaryAcrossDST(t *testing.T) {
func TestPlanUser_EmailOffAndStaleItemsAreSkippedNotSent(t *testing.T) {
off := allEmail()
off[KindRequestRejected] = false
off[KindTracksMissing] = false
items := []pendingItem{
item(KindRequestRejected, t0),
item(KindRequestApproved, t0.Add(-8*24*time.Hour)),
item(KindTracksMissing, t0),
item(KindQuarantineFlagged, t0.Add(-8*24*time.Hour)),
}
p := planUser(t0.Add(2*time.Hour), digestCfg, time.UTC, items, off, groupState{}, groupState{})
require.Len(t, p.Skip, 2)
@@ -132,15 +132,14 @@ func TestRetryDelay_DoublesToACap(t *testing.T) {
}
func TestRenderDigest_SummaryGroupsByArtist(t *testing.T) {
arrival := func(p Payload) pendingItem {
b, _ := json.Marshal(p.Map())
return pendingItem{Kind: KindRequestCompleted, Payload: b}
scan := func(albums ...Arrival) pendingItem {
b, _ := json.Marshal(Payload{Count: int64(len(albums)), Albums: albums}.Map())
return pendingItem{Kind: KindNewMusic, Payload: b}
}
items := []pendingItem{
arrival(Payload{Name: "Moe Shop – WWW", Artist: "Moe Shop", Title: "WWW", AlbumID: "al-1"}),
arrival(Payload{Name: "Boards of Canada", Artist: "Boards of Canada", ArtistID: "ar-2"}),
arrival(Payload{Name: "Moe Shop – Pure", Artist: "Moe Shop", Title: "Pure", AlbumID: "al-3"}),
arrival(Payload{Name: "Tycho – Awake"}), // stored before Artist was recorded
scan(Arrival{Artist: "Moe Shop", Title: "WWW", ArtistID: "ar-1", AlbumID: "al-1"},
Arrival{Artist: "Boards of Canada", Title: "Geogaddi", AlbumID: "al-2"}),
scan(Arrival{Artist: "Moe Shop", Title: "Pure", ArtistID: "ar-1", AlbumID: "al-3"}),
}
e, err := renderDigest(EmailSummary, "alice", items, "https://music.example/")
require.NoError(t, err)
@@ -148,12 +147,12 @@ func TestRenderDigest_SummaryGroupsByArtist(t *testing.T) {
moe := strings.Index(e.Text, "Moe Shop")
require.GreaterOrEqual(t, moe, 0)
require.Equal(t, moe, strings.LastIndex(e.Text, "Moe Shop\n"), "one heading per artist")
require.Equal(t, moe, strings.LastIndex(e.Text, "Moe Shop\n"), "one heading per artist, across scans")
require.Less(t, moe, strings.Index(e.Text, "Boards of Canada"), "artists in the order they arrived")
require.Contains(t, e.Text, "Moe Shop\n https://music.example/artists/ar-1")
require.Contains(t, e.Text, " - WWW\n https://music.example/albums/al-1")
require.Contains(t, e.Text, " - Pure\n https://music.example/albums/al-3")
require.Contains(t, e.Text, "Boards of Canada\n https://music.example/artists/ar-2")
require.Contains(t, e.Text, "Tycho\n - Awake")
require.Contains(t, e.Text, " - Geogaddi\n https://music.example/albums/al-2")
require.Contains(t, e.Text, "https://music.example/settings/notifications")
require.Contains(t, e.HTML, `<a href="https://music.example/albums/al-1"`)
}
@@ -161,18 +160,18 @@ func TestRenderDigest_SummaryGroupsByArtist(t *testing.T) {
func TestRenderDigest_BatchListsEachItemAndEscapesHTML(t *testing.T) {
b, _ := json.Marshal(Payload{Name: "<script>x</script>", Reason: "dup"}.Map())
items := []pendingItem{
{Kind: KindRequestApproved, Payload: []byte(`{"name":"WWW"}`)},
{Kind: KindRequestRejected, Payload: b},
{Kind: KindQuarantineFlagged, Payload: []byte(`{"name":"WWW","actor":"Ann"}`)},
{Kind: KindQuarantineFlagged, Payload: b},
}
e, err := renderDigest(EmailBatch, "alice", items, "")
require.NoError(t, err)
require.Equal(t, "Minstrel: 2 updates", e.Subject)
require.Contains(t, e.Text, "- Request approved\n WWW is on its way.")
require.Contains(t, e.Text, "- Track flagged\n Ann flagged WWW.")
require.NotContains(t, e.Text, "http", "no public address: no links")
require.Contains(t, e.Text, "Settings → Notifications")
require.NotContains(t, e.HTML, "<script>")
one, err := renderDigest(EmailBatch, "alice", items[:1], "")
require.NoError(t, err)
require.Equal(t, "Minstrel: Request approved", one.Subject)
require.Equal(t, "Minstrel: Track flagged", one.Subject)
}
+11 -16
View File
@@ -2,8 +2,8 @@
// inbox. Producers across the server call Notifier.Notify; nothing else
// inserts into user_notifications.
//
// The event bus is fire-and-forget: a client that is not connected when a
// request completes, or when tracks go missing, never hears of it. A row here
// The event bus is fire-and-forget: a client that is not connected when new
// music arrives, or when tracks go missing, never hears of it. A row here
// is the durable record. The bus only nudges open clients to come and read
// it.
package notifications
@@ -14,10 +14,9 @@ package notifications
type Kind string
const (
KindRequestApproved Kind = "request_approved"
KindRequestRejected Kind = "request_rejected"
KindRequestCompleted Kind = "request_completed"
KindRequestPending Kind = "request_pending"
// KindNewMusic: albums new to the library, one notice per scan that
// brought them in, for every user.
KindNewMusic Kind = "new_music"
KindQuarantineFlagged Kind = "quarantine_flagged"
KindScanFailed Kind = "scan_failed"
KindTracksMissing Kind = "tracks_missing"
@@ -33,7 +32,7 @@ const (
type Audience int
const (
AudienceRequester Audience = iota
AudienceEveryone Audience = iota
AudienceAdmin
)
@@ -80,13 +79,12 @@ var (
// inboxOnly: news an admin may want to read but never needs to be
// interrupted for.
inboxOnly = Channels{Inbox: true}
// readLater: news worth a daily email but not a buzz in the pocket.
readLater = Channels{Inbox: true, Email: true}
)
var specs = map[Kind]spec{
KindRequestApproved: {audience: AudienceRequester, group: EmailBatch, defaults: allOn},
KindRequestRejected: {audience: AudienceRequester, group: EmailBatch, defaults: allOn},
KindRequestCompleted: {audience: AudienceRequester, group: EmailSummary, defaults: allOn},
KindRequestPending: {audience: AudienceAdmin, group: EmailBatch, defaults: allOn},
KindNewMusic: {audience: AudienceEveryone, group: EmailSummary, defaults: readLater},
KindQuarantineFlagged: {audience: AudienceAdmin, group: EmailBatch, defaults: allOn},
KindScanFailed: {audience: AudienceAdmin, coalesce: true, group: EmailBatch, defaults: healthAlert},
KindTracksMissing: {audience: AudienceAdmin, coalesce: true, group: EmailBatch, defaults: healthAlert},
@@ -95,13 +93,10 @@ var specs = map[Kind]spec{
KindPlaybackErrors: {audience: AudienceAdmin, coalesce: true, group: EmailBatch, defaults: healthAlert},
}
// order is the display order for settings screens: the requester's own kinds
// order is the display order for settings screens: the kind every user gets
// first, then the admin ones.
var order = []Kind{
KindRequestApproved,
KindRequestRejected,
KindRequestCompleted,
KindRequestPending,
KindNewMusic,
KindQuarantineFlagged,
KindScanFailed,
KindTracksMissing,
+13 -10
View File
@@ -33,26 +33,29 @@ func TestKinds_OrderAndSpecsAgree(t *testing.T) {
}
}
func TestKinds_AdminKindsDefaultEmailOffExceptRequestQueue(t *testing.T) {
// The burst-prone health kinds stay out of email unless an admin opts
// in; the request queue and quarantine flags are things an admin acts on.
func TestKinds_HealthKindsStayOutOfEmailAndCoalesce(t *testing.T) {
// The burst-prone health kinds stay out of email unless an admin opts in.
for _, k := range []Kind{KindScanFailed, KindTracksMissing, KindDuplicatesFound, KindDuplicatesResolved, KindPlaybackErrors} {
require.True(t, k.AdminOnly(), k)
require.False(t, k.Defaults().Email, k)
require.NotEmpty(t, k.coalesceKey(), "%s should coalesce", k)
}
for _, k := range []Kind{KindRequestApproved, KindRequestRejected, KindRequestCompleted} {
require.False(t, k.AdminOnly(), k)
require.Equal(t, Channels{true, true, true}, k.Defaults(), k)
require.Empty(t, k.coalesceKey(), "%s must never coalesce: each request is its own news", k)
}
require.Equal(t, EmailSummary, KindRequestCompleted.EmailGroup())
require.Equal(t, EmailBatch, KindRequestApproved.EmailGroup())
// New music reaches everyone, goes out in the daily summary, and never buzzes
// a phone unless the user asks. Each scan's notice is its own row, so a
// notice that was already emailed never silently absorbs later arrivals.
func TestKinds_NewMusicIsForEveryoneAndDailyByEmail(t *testing.T) {
require.False(t, KindNewMusic.AdminOnly())
require.Equal(t, EmailSummary, KindNewMusic.EmailGroup())
require.Equal(t, Channels{Inbox: true, Email: true}, KindNewMusic.Defaults())
require.Empty(t, KindNewMusic.coalesceKey())
require.Equal(t, EmailBatch, KindQuarantineFlagged.EmailGroup())
}
func TestNotify_NilNotifierIsANoOp(t *testing.T) {
var n *Notifier
require.NoError(t, n.Notify(context.Background(), KindRequestApproved, Recipients{}, nil))
require.NoError(t, n.Notify(context.Background(), KindNewMusic, Recipients{}, nil))
}
func TestNotify_UnknownKindIsRefusedBeforeTouchingTheDB(t *testing.T) {
+14 -2
View File
@@ -25,14 +25,19 @@ type Recipients struct {
Users []pgtype.UUID
// Admins adds every admin.
Admins bool
// Except is never notified, even when it is in Users or is an admin. The
// admin who filed a request needs no notice that it is pending.
// Everyone adds every user.
Everyone bool
// Except is never notified, even when it is in Users or is an admin: the
// person who caused an event needs no notice of it.
Except pgtype.UUID
}
// ToUser addresses one user.
func ToUser(id pgtype.UUID) Recipients { return Recipients{Users: []pgtype.UUID{id}} }
// ToEveryone addresses every user.
func ToEveryone() Recipients { return Recipients{Everyone: true} }
// ToAdmins addresses every admin except one (pass the zero UUID for none).
func ToAdmins(except pgtype.UUID) Recipients { return Recipients{Admins: true, Except: except} }
@@ -131,6 +136,13 @@ func (n *Notifier) resolve(ctx context.Context, q *dbq.Queries, kind Kind, to Re
if to.Admins {
candidates = append(candidates, admins...)
}
if to.Everyone {
all, err := q.ListUserIDs(ctx)
if err != nil {
return nil, fmt.Errorf("notifications: list users: %w", err)
}
candidates = append(candidates, all...)
}
seen := make(map[pgtype.UUID]bool, len(candidates))
out := make([]pgtype.UUID, 0, len(candidates))
+34 -15
View File
@@ -82,14 +82,14 @@ func TestNotify_WritesARowAndNudgesOnlyThatUser(t *testing.T) {
defer unsub()
n := notifications.New(pool, bus, nil)
require.NoError(t, n.Notify(ctx, notifications.KindRequestApproved,
notifications.ToUser(alice), map[string]any{"request_id": "r1", "title": "WWW"}))
require.NoError(t, n.Notify(ctx, notifications.KindNewMusic,
notifications.ToUser(alice), map[string]any{"count": 1, "name": "WWW"}))
rows := rowsFor(t, q, alice)
require.Len(t, rows, 1)
require.Equal(t, "request_approved", rows[0].Kind)
require.Equal(t, "new_music", rows[0].Kind)
require.False(t, rows[0].ReadAt.Valid)
require.JSONEq(t, `{"request_id":"r1","title":"WWW"}`, string(rows[0].Payload))
require.JSONEq(t, `{"count":1,"name":"WWW"}`, string(rows[0].Payload))
require.Empty(t, rowsFor(t, q, bob))
select {
@@ -106,17 +106,17 @@ func TestNotify_AdminKindReachesAdminsOnly_AndNeverTheExcepted(t *testing.T) {
pool := testPool(t)
q := dbq.New(pool)
ctx := context.Background()
requester := mkUser(t, q, "admin-requester", true)
flagger := mkUser(t, q, "admin-flagger", true)
other := mkUser(t, q, "admin-other", true)
plain := mkUser(t, q, "plain", false)
n := notifications.New(pool, nil, nil)
// A plain user named directly must still get nothing: the kind is admin-only.
to := notifications.ToAdmins(requester)
to := notifications.ToAdmins(flagger)
to.Users = []pgtype.UUID{plain}
require.NoError(t, n.Notify(ctx, notifications.KindRequestPending, to, nil))
require.NoError(t, n.Notify(ctx, notifications.KindQuarantineFlagged, to, nil))
require.Empty(t, rowsFor(t, q, requester), "the admin who filed it needs no notice")
require.Empty(t, rowsFor(t, q, flagger), "the admin who flagged it needs no notice")
require.Len(t, rowsFor(t, q, other), 1)
require.Empty(t, rowsFor(t, q, plain))
}
@@ -183,18 +183,37 @@ func TestNotify_ACoalescedUpdateMergesNamesAndKeepsItsArrival(t *testing.T) {
require.True(t, emailed.Valid, "an emailed notice is not emailed again because it grew")
}
func TestNotify_RequestKindsNeverCoalesce(t *testing.T) {
// Each scan's new music is its own notice: one already emailed in the daily
// summary must not quietly take in albums that arrive after it.
func TestNotify_NewMusicNeverCoalesces(t *testing.T) {
pool := testPool(t)
q := dbq.New(pool)
ctx := context.Background()
alice := mkUser(t, q, "alice", false)
n := notifications.New(pool, nil, nil)
for _, id := range []string{"r1", "r2"} {
require.NoError(t, n.Notify(ctx, notifications.KindRequestCompleted, notifications.ToUser(alice), map[string]any{"request_id": id}))
for range 2 {
require.NoError(t, n.Notify(ctx, notifications.KindNewMusic, notifications.ToUser(alice), map[string]any{"count": 1}))
}
require.Len(t, rowsFor(t, q, alice), 2)
}
func TestNotify_EveryoneReachesEveryUser(t *testing.T) {
pool := testPool(t)
q := dbq.New(pool)
ctx := context.Background()
admin := mkUser(t, q, "admin", true)
plain := mkUser(t, q, "plain", false)
n := notifications.New(pool, nil, nil)
require.NoError(t, n.Notify(ctx, notifications.KindNewMusic, notifications.ToEveryone(), map[string]any{"count": 1}))
require.Len(t, rowsFor(t, q, admin), 1)
require.Len(t, rowsFor(t, q, plain), 1)
// An admin kind addressed to everyone still reaches admins only.
require.NoError(t, n.Notify(ctx, notifications.KindScanFailed, notifications.ToEveryone(), map[string]any{"count": 1}))
require.Len(t, rowsFor(t, q, admin), 2)
require.Len(t, rowsFor(t, q, plain), 1)
}
func TestNotify_InboxOffStoresNothing_AndDefaultsApplyWithoutAPref(t *testing.T) {
pool := testPool(t)
q := dbq.New(pool)
@@ -202,12 +221,12 @@ func TestNotify_InboxOffStoresNothing_AndDefaultsApplyWithoutAPref(t *testing.T)
muted := mkUser(t, q, "muted", false)
fresh := mkUser(t, q, "fresh", false)
require.NoError(t, q.UpsertNotificationPref(ctx, dbq.UpsertNotificationPrefParams{
UserID: muted, Kind: "request_rejected", Inbox: false, Phone: true, Email: true,
UserID: muted, Kind: "new_music", Inbox: false, Phone: true, Email: true,
}))
n := notifications.New(pool, nil, nil)
to := notifications.Recipients{Users: []pgtype.UUID{muted, fresh}}
require.NoError(t, n.Notify(ctx, notifications.KindRequestRejected, to, nil))
require.NoError(t, n.Notify(ctx, notifications.KindNewMusic, to, nil))
require.Empty(t, rowsFor(t, q, muted), "inbox off is off, whatever phone and email say")
require.Len(t, rowsFor(t, q, fresh), 1)
@@ -220,7 +239,7 @@ func TestMarkRead_ScopedToTheOwner_AndIdempotent(t *testing.T) {
alice := mkUser(t, q, "alice", false)
mallory := mkUser(t, q, "mallory", false)
n := notifications.New(pool, nil, nil)
require.NoError(t, n.Notify(ctx, notifications.KindRequestApproved, notifications.ToUser(alice), nil))
require.NoError(t, n.Notify(ctx, notifications.KindNewMusic, notifications.ToUser(alice), nil))
id := rowsFor(t, q, alice)[0].ID
got, err := q.MarkNotificationRead(ctx, dbq.MarkNotificationReadParams{ID: id, UserID: mallory})
@@ -271,7 +290,7 @@ func TestRetention_TrimsOldReadRowsAndAncientUnreadOnes(t *testing.T) {
readAt = *read
}
_, err := pool.Exec(ctx,
`INSERT INTO user_notifications (user_id, kind, created_at, read_at) VALUES ($1, 'request_approved', $2, $3)`,
`INSERT INTO user_notifications (user_id, kind, created_at, read_at) VALUES ($1, 'new_music', $2, $3)`,
user, created, readAt)
require.NoError(t, err)
}
+51 -36
View File
@@ -23,19 +23,11 @@ type Rendered struct {
// Payload is what producers put in a notification. Every field is optional;
// each kind documents the ones it uses in Render.
type Payload struct {
RequestID string `json:"request_id,omitempty"`
RequestKind string `json:"request_kind,omitempty"` // artist | album | track
// Name is what was requested or flagged, as the user would say it:
// "WWW", "Moe Shop", "Moe Shop – WWW".
// Name is what was flagged, as the user would say it: "Moe Shop – WWW".
Name string `json:"name,omitempty"`
// Artist and Title are Name's parts, for request_completed: the new-music
// summary email groups arrivals by artist. Title is empty for an artist
// request.
Artist string `json:"artist,omitempty"`
Title string `json:"title,omitempty"`
ArtistID string `json:"artist_id,omitempty"`
AlbumID string `json:"album_id,omitempty"`
// Actor is the other person involved: who asked, who flagged.
// Albums are new_music's arrivals, the first AlbumsKept of Count.
Albums []Arrival `json:"albums,omitempty"`
// Actor is the other person involved: who flagged.
Actor string `json:"actor,omitempty"`
Reason string `json:"reason,omitempty"`
// Count is the coalesced kinds' running total since the notice was last
@@ -50,6 +42,18 @@ type Payload struct {
Detail string `json:"detail,omitempty"`
}
// Arrival is one album new to the library.
type Arrival struct {
Artist string `json:"artist"`
Title string `json:"title"`
ArtistID string `json:"artist_id,omitempty"`
AlbumID string `json:"album_id,omitempty"`
}
// AlbumsKept is how many arrivals a new_music notice carries. The count says
// how many there were in all.
const AlbumsKept = 50
// Map turns a Payload into the map Notify takes, dropping empty fields.
func (p Payload) Map() map[string]any {
b, _ := json.Marshal(p)
@@ -65,33 +69,12 @@ func Render(kind Kind, payload []byte) Rendered {
_ = json.Unmarshal(payload, &p)
name := p.Name
if name == "" {
name = "Your request"
name = "A track"
}
switch kind {
case KindRequestApproved:
return Rendered{Title: "Request approved", Body: name + " is on its way.", Link: "/requests"}
case KindRequestRejected:
body := name + " was declined."
if p.Reason != "" {
body = name + " was declined: " + p.Reason
}
return Rendered{Title: "Request declined", Body: body, Link: "/requests"}
case KindRequestCompleted:
link := "/requests"
switch {
case p.AlbumID != "":
link = "/albums/" + p.AlbumID
case p.ArtistID != "":
link = "/artists/" + p.ArtistID
}
return Rendered{Title: "Now in your library", Body: name + " has arrived.", Link: link}
case KindRequestPending:
who := p.Actor
if who == "" {
who = "Someone"
}
return Rendered{Title: "Request to review", Body: who + " asked for " + name + ".", Link: "/admin/requests"}
case KindNewMusic:
return renderNewMusic(p)
case KindQuarantineFlagged:
who := p.Actor
if who == "" {
@@ -160,6 +143,38 @@ func Render(kind Kind, payload []byte) Rendered {
}
}
// renderNewMusic: a single album opens itself; several open Home, whose
// Recently added row starts with them.
func renderNewMusic(p Payload) Rendered {
total := max(p.Count, int64(len(p.Albums)))
if total == 1 && len(p.Albums) == 1 {
a := p.Albums[0]
link := "/"
if a.AlbumID != "" {
link = "/albums/" + a.AlbumID
}
return Rendered{Title: "New album", Body: TrackLabel(a.Artist, a.Title) + " is in the library.", Link: link}
}
labels := make([]string, 0, min(len(p.Albums), namesShown))
for _, a := range p.Albums[:min(len(p.Albums), namesShown)] {
labels = append(labels, TrackLabel(a.Artist, a.Title))
}
body := "New albums are in the library."
if names := nameList(labels, total); names != "" {
body = "Now in the library: " + names + "."
}
return Rendered{Title: plural(total, "new album", "new albums"), Body: body, Link: "/"}
}
// TrackLabel names a track or album as a notice does: "Artist – Title", or
// the title alone when the artist is unknown.
func TrackLabel(artist, title string) string {
if artist == "" {
return title
}
return artist + " – " + title
}
// namesShown is how many of a notice's items its body names.
const namesShown = 3
+20 -11
View File
@@ -25,16 +25,25 @@ func TestRender_EveryKindHasATitleAndALink(t *testing.T) {
}
}
func TestRender_RequestCompletedLinksToWhatArrived(t *testing.T) {
require.Equal(t, "/albums/al-1", render(KindRequestCompleted, Payload{Name: "WWW", AlbumID: "al-1", ArtistID: "ar-1"}).Link)
require.Equal(t, "/artists/ar-1", render(KindRequestCompleted, Payload{Name: "Moe Shop", ArtistID: "ar-1"}).Link)
require.Equal(t, "/requests", render(KindRequestCompleted, Payload{Name: "WWW"}).Link)
require.Equal(t, "WWW has arrived.", render(KindRequestCompleted, Payload{Name: "WWW"}).Body)
func TestRender_NewMusicOpensWhatArrived(t *testing.T) {
one := render(KindNewMusic, Payload{Count: 1, Albums: []Arrival{{Artist: "Moe Shop", Title: "WWW", AlbumID: "al-1"}}})
require.Equal(t, "New album", one.Title)
require.Equal(t, "Moe Shop – WWW is in the library.", one.Body)
require.Equal(t, "/albums/al-1", one.Link)
// Several open Home, whose Recently added row starts with them; the
// count, not only the albums carried, is what "more" counts.
many := render(KindNewMusic, Payload{Count: 60, Albums: []Arrival{
{Artist: "A", Title: "1"}, {Artist: "B", Title: "2"}, {Artist: "C", Title: "3"}, {Artist: "D", Title: "4"},
}})
require.Equal(t, "60 new albums", many.Title)
require.Equal(t, "Now in the library: A – 1, B – 2, C – 3 and 57 more.", many.Body)
require.Equal(t, "/", many.Link)
}
func TestRender_RejectionCarriesTheReasonWhenThereIsOne(t *testing.T) {
require.Equal(t, "WWW was declined.", render(KindRequestRejected, Payload{Name: "WWW"}).Body)
require.Equal(t, "WWW was declined: already owned", render(KindRequestRejected, Payload{Name: "WWW", Reason: "already owned"}).Body)
func TestRender_FlagCarriesTheReasonWhenThereIsOne(t *testing.T) {
require.Equal(t, "Ann flagged WWW.", render(KindQuarantineFlagged, Payload{Name: "WWW", Actor: "Ann"}).Body)
require.Equal(t, "Ann flagged WWW: bad rip", render(KindQuarantineFlagged, Payload{Name: "WWW", Actor: "Ann", Reason: "bad rip"}).Body)
}
func TestRender_CoalescedCountsReadNaturally(t *testing.T) {
@@ -76,9 +85,9 @@ func TestRender_AScanFailureSaysWhatStopped(t *testing.T) {
}
func TestRender_AnUnreadablePayloadStillRenders(t *testing.T) {
r := Render(KindRequestApproved, []byte(`not json`))
require.Equal(t, "Request approved", r.Title)
require.Equal(t, "Your request is on its way.", r.Body)
r := Render(KindQuarantineFlagged, []byte(`not json`))
require.Equal(t, "Track flagged", r.Title)
require.Equal(t, "Someone flagged A track.", r.Body)
}
func TestPayloadMap_DropsEmptyFields(t *testing.T) {
+1 -4
View File
@@ -125,10 +125,7 @@ export function createNotificationSettingsQuery() {
/** Short row labels, menu-style. Unknown kinds show their key. */
export const NOTIFICATION_KIND_LABELS: Record<string, string> = {
request_approved: 'Request approved',
request_rejected: 'Request declined',
request_completed: 'New music arrived',
request_pending: 'Requests to review',
new_music: 'New music arrived',
quarantine_flagged: 'Tracks flagged',
scan_failed: 'Library scan failed',
tracks_missing: 'Tracks gone missing',
@@ -37,9 +37,9 @@ import {
function item(over: Partial<NotificationItem> = {}): NotificationItem {
return {
id: 'n1',
kind: 'request_completed',
title: 'Arrived: Boards of Canada – Geogaddi',
body: 'Your request is in the library.',
kind: 'new_music',
title: 'New album',
body: 'Boards of Canada – Geogaddi is in the library.',
link: '/albums/al-1',
created_at: new Date().toISOString(),
read_at: null,
@@ -95,7 +95,7 @@ describe('NotificationBell', () => {
test('lists each kind with the server-rendered title and body, unread marked', async () => {
setInbox([
item({ id: 'a', kind: 'request_approved', title: 'Request approved', body: 'Geogaddi is on its way.' }),
item({ id: 'a', kind: 'new_music', title: 'New album', body: 'Geogaddi is in the library.' }),
item({
id: 'b',
kind: 'tracks_missing',
@@ -110,7 +110,7 @@ describe('NotificationBell', () => {
const rows = screen.getAllByTestId('notification-item');
expect(rows).toHaveLength(2);
expect(rows[0]).toHaveTextContent('Request approved');
expect(rows[0]).toHaveTextContent('New album');
expect(rows[0]).toHaveTextContent('Geogaddi is on its way.');
expect(rows[0]).toHaveAttribute('data-unread', 'true');
expect(rows[1]).toHaveTextContent('4 tracks went missing');
@@ -33,16 +33,15 @@ import {
type NotificationSettings as Settings
} from '#lib/api/notifications.js';
const requesterKinds: NotificationKindSetting[] = [
{ kind: 'request_approved', admin_only: false, inbox: true, phone: true, email: true },
{ kind: 'request_completed', admin_only: false, inbox: true, phone: true, email: true }
const listenerKinds: NotificationKindSetting[] = [
{ kind: 'new_music', admin_only: false, inbox: true, phone: false, email: true }
];
const adminKinds: NotificationKindSetting[] = [
{ kind: 'tracks_missing', admin_only: true, inbox: true, phone: true, email: false }
];
function setSettings(s: Partial<Settings>) {
const data: Settings = { kinds: requesterKinds, email_available: true, ...s };
const data: Settings = { kinds: listenerKinds, email_available: true, ...s };
(createNotificationSettingsQuery as ReturnType<typeof vi.fn>).mockReturnValue(mockQuery({ data }));
return data;
}
@@ -56,14 +55,14 @@ describe('NotificationSettings', () => {
test('a listener sees their kinds with a toggle per channel and no admin heading', () => {
setSettings({});
render(NotificationSettings);
expect(screen.getByRole('checkbox', { name: 'Request approved: Inbox' })).toBeChecked();
expect(screen.getByRole('checkbox', { name: 'New music arrived: Inbox' })).toBeChecked();
expect(screen.getByRole('checkbox', { name: 'New music arrived: Email' })).toBeChecked();
expect(screen.queryByText('Library health')).toBeNull();
});
test('an admin also sees library health under its own heading', () => {
userState.current = { id: '1', username: 'root', is_admin: true };
setSettings({ kinds: [...requesterKinds, ...adminKinds] });
setSettings({ kinds: [...listenerKinds, ...adminKinds] });
render(NotificationSettings);
expect(screen.getByText('Library health')).toBeInTheDocument();
expect(screen.getByRole('checkbox', { name: 'Tracks gone missing: Email' })).not.toBeChecked();
@@ -76,9 +75,9 @@ describe('NotificationSettings', () => {
await fireEvent.click(screen.getByRole('checkbox', { name: 'New music arrived: Email' }));
expect(putNotificationSettings).toHaveBeenCalledWith([{ kind: 'request_completed', email: false }]);
expect(putNotificationSettings).toHaveBeenCalledWith([{ kind: 'new_music', email: false }]);
const optimistic = setQueryData.mock.calls[0][1] as Settings;
expect(optimistic.kinds.find((k) => k.kind === 'request_completed')?.email).toBe(false);
expect(optimistic.kinds.find((k) => k.kind === 'new_music')?.email).toBe(false);
});
test('a failed save puts the switch back and says so', async () => {
@@ -86,7 +85,7 @@ describe('NotificationSettings', () => {
(putNotificationSettings as ReturnType<typeof vi.fn>).mockRejectedValue(new Error('offline'));
render(NotificationSettings);
await fireEvent.click(screen.getByRole('checkbox', { name: 'Request approved: Phone' }));
await fireEvent.click(screen.getByRole('checkbox', { name: 'New music arrived: Phone' }));
await waitFor(() => expect(pushToast).toHaveBeenCalled());
expect(setQueryData).toHaveBeenLastCalledWith(['notificationSettings'], data);
@@ -94,18 +93,18 @@ describe('NotificationSettings', () => {
test('inbox off disables phone and email for that kind', () => {
setSettings({
kinds: [{ kind: 'request_approved', admin_only: false, inbox: false, phone: true, email: true }]
kinds: [{ kind: 'new_music', admin_only: false, inbox: false, phone: true, email: true }]
});
render(NotificationSettings);
expect(screen.getByRole('checkbox', { name: 'Request approved: Phone' })).toBeDisabled();
expect(screen.getByRole('checkbox', { name: 'Request approved: Email' })).toBeDisabled();
expect(screen.getByRole('checkbox', { name: 'Request approved: Inbox' })).toBeEnabled();
expect(screen.getByRole('checkbox', { name: 'New music arrived: Phone' })).toBeDisabled();
expect(screen.getByRole('checkbox', { name: 'New music arrived: Email' })).toBeDisabled();
expect(screen.getByRole('checkbox', { name: 'New music arrived: Inbox' })).toBeEnabled();
});
test('no email address: the email column is off and points at the profile', () => {
setSettings({ email_available: false, email_unavailable_reason: 'no_address' });
render(NotificationSettings);
expect(screen.getByRole('checkbox', { name: 'Request approved: Email' })).toBeDisabled();
expect(screen.getByRole('checkbox', { name: 'New music arrived: Email' })).toBeDisabled();
expect(screen.getByRole('link', { name: 'add an email address in your profile' })).toHaveAttribute(
'href',
'/settings/account#profile'