Repository navigation
Expand file tree
/
Copy pathmarks.go
More file actions
372 lines (353 loc) · 8.87 KB
/
Copy pathmarks.go
File metadata and controls
372 lines (353 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package main
import (
"slices"
"strconv"
"strings"
"time"
"github.com/datahearth/streamline/extensions/sdk"
"github.com/streamline-hq/streamline-subsonic-ext/envelope"
"github.com/streamline-hq/streamline-subsonic-ext/ids"
)
func init() {
register("star", star)
register("unstar", unstar)
register("setRating", setRating)
register("scrobble", scrobble)
registerDecorator(decorateMarks)
}
// mark is one listener's annotation on one target.
type mark struct {
starredAt time.Time
rating uint8
playCount uint32
playedAt time.Time
}
// markTargets maps a Subsonic id kind to the core view holding its rows.
var markTargets = map[string]string{
ids.KindTrack: "streamline_tracks",
ids.KindAlbum: "streamline_albums",
ids.KindArtist: "streamline_artists",
}
// decorateMarks fills the caller's own star, rating and play count into every
// song, album and artist of b after one read per kind.
func decorateMarks(c *reqCtx, b *body) error {
col := collectMarkTargets(b)
byKind := map[string][]uint32{}
add := func(raw string) {
if kind, id, err := ids.Parse(raw); err == nil {
byKind[kind] = append(byKind[kind], id)
}
}
for _, ch := range col.children {
add(ch.ID)
}
for _, al := range col.albums {
add(al.ID)
}
for _, ar := range col.artists {
add(ar.ID)
}
if col.dir != nil {
add(col.dir.ID)
}
if len(byKind) == 0 {
return nil
}
m, err := loadMarks(c.user.ID, byKind)
if err != nil {
return err
}
if len(m) == 0 {
return nil
}
for _, ch := range col.children {
if a, ok := m[ch.ID]; ok {
ch.Starred = optTimestamp(a.starredAt)
ch.UserRating = a.rating
ch.PlayCount = a.playCount
ch.Played = optTimestamp(a.playedAt)
}
}
for _, al := range col.albums {
if a, ok := m[al.ID]; ok {
al.Starred = optTimestamp(a.starredAt)
al.UserRating = a.rating
al.PlayCount = a.playCount
al.Played = optTimestamp(a.playedAt)
}
}
for _, ar := range col.artists {
if a, ok := m[ar.ID]; ok {
ar.Starred = optTimestamp(a.starredAt)
ar.UserRating = a.rating
}
}
if col.dir != nil {
if a, ok := m[col.dir.ID]; ok {
col.dir.Starred = optTimestamp(a.starredAt)
col.dir.UserRating = a.rating
col.dir.PlayCount = a.playCount
col.dir.Played = optTimestamp(a.playedAt)
}
}
return nil
}
// loadMarks reads the user's annotations on the given ids, keyed by Subsonic
// id.
func loadMarks(userID uint32, byKind map[string][]uint32) (map[string]mark, error) {
out := map[string]mark{}
for kind, list := range byKind {
if _, known := markTargets[kind]; !known {
continue
}
slices.Sort(list)
list = slices.Compact(list)
rows, err := queryIn(
`SELECT target_id, starred_at, rating, play_count, played_at
FROM annotations
WHERE user_id = ? AND target_kind = ? AND target_id IN ({in})`,
list, userID, kind,
)
if err != nil {
return nil, err
}
for _, r := range rows {
out[ids.Format(kind, r.uint(0))] = readMark(r, 1)
}
}
return out, nil
}
// readMark reads starred_at, rating, play_count, played_at from column i.
func readMark(r row, i int) mark {
return mark{
starredAt: r.time(i),
rating: uint8(min(max(r.int(i+1), 0), 5)),
playCount: r.uint(i + 2),
playedAt: r.time(i + 3),
}
}
// setMark reads the caller's annotation on one target, lets fn change it and
// writes it back, creating the row on first use. It runs inside the caller's
// transaction. A target that is no row of the core library is error 70.
func setMark(
userID uint32,
kind string,
id uint32,
fn func(*mark),
) error {
table, known := markTargets[kind]
if !known {
return envelope.NotFound()
}
if _, found, err := queryRow(
"SELECT 1 FROM "+table+" WHERE id = ?",
id,
); err != nil {
return err
} else if !found {
return envelope.NotFound()
}
var cur mark
if r, found, err := queryRow(
`SELECT starred_at, rating, play_count, played_at FROM annotations
WHERE user_id = ? AND target_kind = ? AND target_id = ?`,
userID, kind, id,
); err != nil {
return err
} else if found {
cur = readMark(r, 0)
}
fn(&cur)
_, err := exec(
`INSERT INTO annotations
(user_id, target_kind, target_id, starred_at, rating, play_count, played_at,
updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT (user_id, target_kind, target_id) DO UPDATE SET
starred_at = excluded.starred_at, rating = excluded.rating,
play_count = excluded.play_count, played_at = excluded.played_at,
updated_at = excluded.updated_at`,
userID, kind, id, storedOrNil(cur.starredAt), cur.rating, cur.playCount,
storedOrNil(cur.playedAt), nowStored(),
)
return err
}
// storedOrNil is a time as the extension's tables keep it, NULL for the zero
// time.
func storedOrNil(t time.Time) any {
if t.IsZero() {
return nil
}
return formatStored(t)
}
type markTarget struct {
kind string
id uint32
}
// starTargets reads star and unstar's three id parameters. id may name any
// kind; albumId and artistId are the ID3 forms and must be of their own.
func starTargets(c *reqCtx) ([]markTarget, error) {
var out []markTarget
for _, p := range []struct{ name, kind string }{
{"id", ""},
{"albumId", ids.KindAlbum},
{"artistId", ids.KindArtist},
} {
for _, raw := range c.multi(p.name) {
kind, id, err := ids.Parse(raw)
if err != nil || (p.kind != "" && kind != p.kind) {
return nil, envelope.NotFound()
}
if _, known := markTargets[kind]; !known {
return nil, envelope.NotFound()
}
out = append(out, markTarget{kind, id})
}
}
if len(out) == 0 {
return nil, envelope.MissingParam("id")
}
return out, nil
}
func star(c *reqCtx) sdk.Response { return applyStar(c, true) }
func unstar(c *reqCtx) sdk.Response { return applyStar(c, false) }
func applyStar(c *reqCtx, on bool) sdk.Response {
targets, err := starTargets(c)
if err != nil {
return c.failErr(err)
}
now := time.Now().UTC()
err = tx(func() error {
for _, t := range targets {
if err := setMark(c.user.ID, t.kind, t.id, func(m *mark) {
switch {
case !on:
m.starredAt = time.Time{}
case m.starredAt.IsZero():
m.starredAt = now
}
}); err != nil {
return err
}
}
return nil
})
if err != nil {
return c.failErr(err)
}
return c.ok(nil)
}
func setRating(c *reqCtx) sdk.Response {
if err := c.require(permRate); err != nil {
return c.failErr(err)
}
kind, id, err := c.anyIDParam("id")
if err != nil {
return c.failErr(err)
}
if c.param("rating") == "" {
return c.failErr(envelope.MissingParam("rating"))
}
rating := c.intParam("rating", -1, -1, 6)
if rating < 0 || rating > 5 {
return c.fail(envelope.CodeGeneric, "rating must be between 0 and 5")
}
err = tx(func() error {
return setMark(c.user.ID, kind, id, func(m *mark) {
m.rating = uint8(rating) //nolint:gosec // bounded above
})
})
if err != nil {
return c.failErr(err)
}
return c.ok(nil)
}
// play is one submitted scrobble: a track and when it was heard.
type play struct {
trackID uint32
albumID uint32
artistID uint32
duration uint32
at time.Time
}
// scrobble records plays (submission=true, the default) or reports what is
// playing now (submission=false). A play counts on the track, its album and
// its artist, so the album lists' frequent and recent read their own rows.
func scrobble(c *reqCtx) sdk.Response {
raw := c.multi("id")
if len(raw) == 0 {
return c.failErr(envelope.MissingParam("id"))
}
times := c.multi("time")
plays := make([]play, 0, len(raw))
for i, s := range raw {
kind, id, err := ids.Parse(s)
if err != nil || kind != ids.KindTrack {
return c.failErr(envelope.NotFound())
}
r, found, err := queryRow(
`SELECT t.album_id, a.artist_id, t.duration
FROM streamline_tracks t JOIN streamline_albums a ON a.id = t.album_id
WHERE t.id = ?`, id)
if err != nil {
return c.failErr(err)
}
if !found {
return c.failErr(envelope.NotFound())
}
at := time.Now().UTC()
if i < len(times) {
if ms, err := strconv.ParseInt(
strings.TrimSpace(times[i]),
10,
64,
); err == nil &&
ms > 0 {
at = time.UnixMilli(ms).UTC()
}
}
plays = append(plays, play{id, r.uint(0), r.uint(1), r.uint(2), at})
}
player := c.param("c")
if !c.boolParam("submission", true) {
last := plays[len(plays)-1]
if err := setNowPlaying(
c.user.ID,
player,
last.trackID,
last.duration,
); err != nil {
return c.failErr(err)
}
return c.ok(nil)
}
err := tx(func() error {
for _, p := range plays {
bump := func(m *mark) {
m.playCount++
if m.playedAt.IsZero() || p.at.After(m.playedAt) {
m.playedAt = p.at
}
}
for _, t := range []markTarget{
{ids.KindTrack, p.trackID},
{ids.KindAlbum, p.albumID},
{ids.KindArtist, p.artistID},
} {
if err := setMark(c.user.ID, t.kind, t.id, bump); err != nil {
return err
}
}
}
return nil
})
if err != nil {
return c.failErr(err)
}
for _, p := range plays {
if err := finishNowPlaying(c.user.ID, player, p.trackID); err != nil {
return c.failErr(err)
}
}
return c.ok(nil)
}