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
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
package main

// Client for the Redacted (Gazelle) API, and the code that mirrors its
// responses into PostgreSQL.
//
// The API is at https://redacted.sh/ajax.php, authenticated by an Authorization
// header, and answers with {"status": "success", "response": ...}. Responses are
// paged; a page count is only present on some endpoints, in which case we assume
// one page.
//
// CAUTION: the JSON stored in redacted.torrents_json.full_json_result is not
// merely a record of the response, it is *input to the database*: the STORED
// generated columns seeding_weight and artist_ids are computed from it (see
// db.go). The normalisation in parseTourGroups below — renaming "snatched" to
// "snatches" and the per-endpoint torrent id field to "torrentId" — is therefore
// load-bearing and must not be "cleaned up".

import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"log/slog"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"

	"github.com/jackc/pgx/v5"
	"go.opentelemetry.io/otel/trace"
	"golang.org/x/sync/errgroup"
)

const redactedHost = "redacted.sh"

// redactedClient talks to the Redacted API.
type redactedClient struct {
	apiKey string
	http   *http.Client
}

func newRedactedClient(apiKey string) *redactedClient {
	return &redactedClient{
		apiKey: apiKey,
		http:   &http.Client{Timeout: 60 * time.Second},
	}
}

// mkRedactedTorrentLink builds the user-facing link to a torrent group.
func mkRedactedTorrentLink(groupID int) string {
	return fmt.Sprintf("https://redacted.sh/torrents.php?id=%d", groupID)
}

// request performs an API call and returns the raw response body.
//
// Rate limiting is handled here: the API answers 429 with a Retry-After header,
// which we honour (clamped to 0..10 seconds, defaulting to 2) and then retry,
// exactly as the Haskell httpJsonWithRateLimit did.
func (c *redactedClient) request(ctx context.Context, action string, args url.Values) ([]byte, error) {
	return inSpan1(ctx, "HTTP Request (JSON)", func(ctx context.Context, span trace.Span) ([]byte, error) {
		q := url.Values{}
		for k, v := range args {
			q[k] = v
		}
		q.Set("action", action)
		u := url.URL{
			Scheme:   "https",
			Host:     redactedHost,
			Path:     "/ajax.php",
			RawQuery: q.Encode(),
		}
		attr(span, "http.url", u.String())
		attr(span, "redacted.action", action)

		for {
			req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
			if err != nil {
				return nil, err
			}
			req.Header.Set("Authorization", c.apiKey)

			resp, err := c.http.Do(req)
			if err != nil {
				return nil, fmt.Errorf("requesting %s: %w", action, err)
			}
			body, readErr := io.ReadAll(resp.Body)
			resp.Body.Close()
			if readErr != nil {
				return nil, fmt.Errorf("reading response for %s: %w", action, readErr)
			}
			attr(span, "http.response.status_code", resp.StatusCode)

			switch {
			case resp.StatusCode == http.StatusOK:
				ct := resp.Header.Get("Content-Type")
				if !strings.HasPrefix(ct, "application/json") {
					return nil, fmt.Errorf("server returned a non-json body, with content-type %q", ct)
				}
				return body, nil

			case resp.StatusCode == http.StatusTooManyRequests:
				wait := retryAfter(resp.Header.Get("Retry-After"))
				slog.Info("rate limited by redacted", "action", action, "retry-after", wait)
				event(span, "rate limited, waiting")
				select {
				case <-ctx.Done():
					return nil, ctx.Err()
				case <-time.After(wait):
				}
				continue

			default:
				return nil, fmt.Errorf("server returned a non-200 error code, code %d: %s",
					resp.StatusCode, truncate(string(body), 500))
			}
		}
	})
}

// retryAfter parses the Retry-After header, clamped to 0..10s as the Haskell
// version did (an unbounded value would hang the request).
func retryAfter(h string) time.Duration {
	n, err := strconv.Atoi(strings.TrimSpace(h))
	if err != nil {
		return 2 * time.Second
	}
	if n < 0 {
		n = 0
	}
	if n > 10 {
		n = 10
	}
	return time.Duration(n) * time.Second
}

func truncate(s string, n int) string {
	if len(s) <= n {
		return s
	}
	return s[:n] + "…"
}

// redactedReply is the standard envelope.
type redactedReply struct {
	Status   string          `json:"status"`
	Error    string          `json:"error"`
	Response json.RawMessage `json:"response"`
}

// requestJSON performs a call and unwraps the standard envelope, returning the
// "response" value and the page count (1 when the endpoint does not page).
func (c *redactedClient) requestJSON(ctx context.Context, action string, args url.Values) (json.RawMessage, int, error) {
	body, err := c.request(ctx, action, args)
	if err != nil {
		return nil, 0, err
	}
	var reply redactedReply
	if err := json.Unmarshal(body, &reply); err != nil {
		return nil, 0, fmt.Errorf("response was not valid JSON: %w", err)
	}
	if reply.Status != "success" {
		if reply.Error != "" {
			return nil, 0, fmt.Errorf("status was not \"success\", but %s: %s", reply.Status, reply.Error)
		}
		return nil, 0, fmt.Errorf("status was not \"success\", but %s", reply.Status)
	}
	// `pages` is optional; when missing there is exactly one page.
	var pageInfo struct {
		Pages *json.Number `json:"pages"`
	}
	pages := 1
	if err := json.Unmarshal(reply.Response, &pageInfo); err == nil && pageInfo.Pages != nil {
		if n, err := pageInfo.Pages.Int64(); err == nil && n > 0 {
			pages = int(n)
		}
	}
	return reply.Response, pages, nil
}

// ---------------------------------------------------------------------------
// Tour groups: the shape both `browse` and `artist` return, modulo field names
// ---------------------------------------------------------------------------

// tourGroup is one release group together with its torrents.
type tourGroup struct {
	GroupID        int
	GroupName      string
	FullJSONResult json.RawMessage
	Torrents       []tourGroupTorrent
}

type tourGroupTorrent struct {
	TorrentID      int
	FullJSONResult json.RawMessage
}

// tourGroupFieldNames captures the two ways the API names the same things.
//
// action=browse returns groups with a "torrents" array whose elements have
// "torrentId"; action=artist returns "torrentgroup" entries with a "torrent"
// array whose elements have "id".
type tourGroupFieldNames struct {
	torrentField string // "torrents" or "torrent"
	torrentIDKey string // "torrentId" or "id"
}

var (
	searchFieldNames = tourGroupFieldNames{torrentField: "torrents", torrentIDKey: "torrentId"}
	artistFieldNames = tourGroupFieldNames{torrentField: "torrent", torrentIDKey: "id"}
)

// parseTourGroups extracts release groups from an array of API objects.
//
// Entries without the torrent field are skipped: the API also returns
// non-torrent items (e.g. guitar tabs, see Dream Theater "Systematic Chaos").
//
// The normalisation applied to each torrent object is load-bearing, see the
// warning at the top of this file:
//   - "snatched" is renamed to "snatches", because some endpoints use the former
//     while calc_seeding_weight reads the latter;
//   - the endpoint-specific torrent id key is renamed to "torrentId".
//
// The group object stores everything *except* the torrent array, which is stored
// separately in redacted.torrents_json.
func parseTourGroups(raw json.RawMessage, names tourGroupFieldNames) ([]tourGroup, error) {
	var items []map[string]json.RawMessage
	if err := json.Unmarshal(raw, &items); err != nil {
		return nil, fmt.Errorf("expected an array of torrent groups: %w", err)
	}

	groups := make([]tourGroup, 0, len(items))
	for _, item := range items {
		torrentsRaw, ok := item[names.torrentField]
		if !ok {
			// Not a torrent group.
			continue
		}

		groupID, err := jsonInt(item["groupId"])
		if err != nil {
			return nil, fmt.Errorf("groupId: %w", err)
		}
		var groupName string
		if err := json.Unmarshal(item["groupName"], &groupName); err != nil {
			return nil, fmt.Errorf("groupName: %w", err)
		}

		// The stored group JSON excludes the torrents.
		groupJSON := make(map[string]json.RawMessage, len(item))
		for k, v := range item {
			if k == names.torrentField {
				continue
			}
			groupJSON[k] = v
		}
		groupBytes, err := json.Marshal(groupJSON)
		if err != nil {
			return nil, err
		}

		var torrentObjs []map[string]json.RawMessage
		if err := json.Unmarshal(torrentsRaw, &torrentObjs); err != nil {
			return nil, fmt.Errorf("torrent array of group %d: %w", groupID, err)
		}

		torrents := make([]tourGroupTorrent, 0, len(torrentObjs))
		for _, t := range torrentObjs {
			normalised := make(map[string]json.RawMessage, len(t))
			for k, v := range t {
				switch k {
				case "snatched":
					// Some torrent objects use “snatched” instead of “snatches”.
					normalised["snatches"] = v
				case names.torrentIDKey:
					normalised["torrentId"] = v
				default:
					normalised[k] = v
				}
			}
			torrentID, err := jsonInt(normalised["torrentId"])
			if err != nil {
				return nil, fmt.Errorf("torrent id of group %d: %w", groupID, err)
			}
			b, err := json.Marshal(normalised)
			if err != nil {
				return nil, err
			}
			torrents = append(torrents, tourGroupTorrent{TorrentID: torrentID, FullJSONResult: b})
		}

		groups = append(groups, tourGroup{
			GroupID:        groupID,
			GroupName:      groupName,
			FullJSONResult: groupBytes,
			Torrents:       torrents,
		})
	}
	return groups, nil
}

// jsonInt reads an integer that the API may encode as a number or a string.
func jsonInt(raw json.RawMessage) (int, error) {
	if len(raw) == 0 {
		return 0, fmt.Errorf("missing value")
	}
	var n json.Number
	if err := json.Unmarshal(raw, &n); err == nil {
		i, err := n.Int64()
		if err != nil {
			return 0, err
		}
		return int(i), nil
	}
	var s string
	if err := json.Unmarshal(raw, &s); err == nil {
		return strconv.Atoi(s)
	}
	return 0, fmt.Errorf("not an integer: %s", truncate(string(raw), 60))
}

// ---------------------------------------------------------------------------
// Paged search and insert
// ---------------------------------------------------------------------------

// pagedFetchBatchSize bounds how many API pages are in flight at once.
//
// The Haskell version used a QSem of 5 around a conduit; errgroup.SetLimit is
// the direct equivalent.
const pagedFetchBatchSize = 5

// searchAndInsert runs a `browse` search and stores every page of results.
//
// maxPages == 0 means "all pages", as in the Haskell version.
func (a *app) searchAndInsert(ctx context.Context, args url.Values, maxPages int) ([]int, error) {
	return a.pagedSearchAndInsert(ctx, maxPages, searchFieldNames,
		func(ctx context.Context, page int) (json.RawMessage, int, error) {
			q := url.Values{}
			for k, v := range args {
				q[k] = v
			}
			if page > 0 {
				q.Set("page", strconv.Itoa(page))
			}
			raw, pages, err := a.redacted.requestJSON(ctx, "browse", q)
			if err != nil {
				return nil, 0, err
			}
			// The groups live under "results".
			var wrapper struct {
				Results json.RawMessage `json:"results"`
			}
			if err := json.Unmarshal(raw, &wrapper); err != nil {
				return nil, 0, err
			}
			return wrapper.Results, pages, nil
		})
}

// refreshArtist re-fetches an artist's whole discography and stores it.
func (a *app) refreshArtist(ctx context.Context, artistID int) ([]int, error) {
	return a.pagedSearchAndInsert(ctx, 0, artistFieldNames,
		func(ctx context.Context, page int) (json.RawMessage, int, error) {
			q := url.Values{}
			q.Set("id", strconv.Itoa(artistID))
			if page > 0 {
				q.Set("page", strconv.Itoa(page))
			}
			raw, pages, err := a.redacted.requestJSON(ctx, "artist", q)
			if err != nil {
				return nil, 0, err
			}
			var wrapper struct {
				TorrentGroup json.RawMessage `json:"torrentgroup"`
			}
			if err := json.Unmarshal(raw, &wrapper); err != nil {
				return nil, 0, err
			}
			return wrapper.TorrentGroup, pages, nil
		})
}

// pagedSearchAndInsert fetches page 1 to learn the page count, then fetches the
// remaining pages with bounded concurrency and inserts everything.
//
// Returns the ids of all torrents seen.
func (a *app) pagedSearchAndInsert(
	ctx context.Context,
	maxPages int,
	names tourGroupFieldNames,
	fetch func(ctx context.Context, page int) (json.RawMessage, int, error),
) ([]int, error) {
	return inSpan1(ctx, "Redacted paged search and insert", func(ctx context.Context, span trace.Span) ([]int, error) {
		firstRaw, pages, err := fetch(ctx, 0)
		if err != nil {
			return nil, err
		}

		total := pages
		if maxPages > 0 && maxPages < total {
			total = maxPages
		}
		attr(span, "search.pages.available", pages)
		attr(span, "search.pages.fetching", total)
		slog.Info("got the first page", "more_pages", total-1)

		// Page results in order: page 1 first, then 2..total.
		raws := make([]json.RawMessage, total)
		raws[0] = firstRaw

		if total > 1 {
			g, gctx := errgroup.WithContext(ctx)
			g.SetLimit(pagedFetchBatchSize)
			for p := 2; p <= total; p++ {
				p := p
				g.Go(func() error {
					raw, _, err := fetch(gctx, p)
					if err != nil {
						return fmt.Errorf("page %d: %w", p, err)
					}
					raws[p-1] = raw
					return nil
				})
			}
			if err := g.Wait(); err != nil {
				return nil, err
			}
		}

		var allGroups []tourGroup
		for _, raw := range raws {
			if len(raw) == 0 {
				continue
			}
			groups, err := parseTourGroups(raw, names)
			if err != nil {
				return nil, err
			}
			allGroups = append(allGroups, groups...)
		}

		if len(allGroups) == 0 {
			return nil, nil
		}

		var torrentIDs []int
		err = withTx(ctx, a.pool, func(ctx context.Context, tx pgx.Tx) error {
			ids, err := insertTourGroupsAndTorrents(ctx, tx, allGroups)
			torrentIDs = ids
			return err
		})
		if err != nil {
			return nil, err
		}
		attr(span, "search.torrents.inserted", len(torrentIDs))
		return torrentIDs, nil
	})
}

// insertTourGroupsAndTorrents writes groups, their torrents and the artists
// mentioned by either.
//
// Groups and torrents are deleted before insertion, matching the Haskell
// version: a re-fetch is authoritative, and the delete also drops torrents that
// disappeared upstream.
func insertTourGroupsAndTorrents(ctx context.Context, tx pgx.Tx, groups []tourGroup) ([]int, error) {
	// Deduplicate groups by id: the same group can appear on several pages, and
	// ON CONFLICT cannot handle a conflict target twice in one statement.
	seenGroup := map[int]bool{}
	deduped := make([]tourGroup, 0, len(groups))
	for _, g := range groups {
		if seenGroup[g.GroupID] {
			continue
		}
		seenGroup[g.GroupID] = true
		deduped = append(deduped, g)
	}

	groupIDs := make([]int, len(deduped))
	groupNames := make([]string, len(deduped))
	groupJSONs := make([][]byte, len(deduped))
	for i, g := range deduped {
		groupIDs[i] = g.GroupID
		groupNames[i] = g.GroupName
		groupJSONs[i] = g.FullJSONResult
	}

	if _, err := tx.Exec(ctx,
		`DELETE FROM redacted.torrent_groups WHERE group_id = ANY ($1::integer[])`,
		groupIDs); err != nil {
		return nil, fmt.Errorf("deleting torrent groups: %w", err)
	}

	// Insert groups, keeping the mapping group_id -> primary key.
	rows, err := tx.Query(ctx, `
		INSERT INTO redacted.torrent_groups (group_id, group_name, full_json_result)
		SELECT * FROM UNNEST($1::integer[], $2::text[], $3::jsonb[])
		ON CONFLICT (group_id) DO UPDATE SET
			group_id = excluded.group_id,
			group_name = excluded.group_name,
			full_json_result = excluded.full_json_result
		RETURNING group_id, id`,
		groupIDs, groupNames, groupJSONs)
	if err != nil {
		return nil, fmt.Errorf("inserting torrent groups: %w", err)
	}
	pgIDByGroupID := map[int]int{}
	for rows.Next() {
		var groupID, pgID int
		if err := rows.Scan(&groupID, &pgID); err != nil {
			rows.Close()
			return nil, err
		}
		pgIDByGroupID[groupID] = pgID
	}
	rows.Close()
	if err := rows.Err(); err != nil {
		return nil, err
	}

	// Torrents, deduplicated by torrent id for the same reason as above.
	var (
		torrentGroupPGIDs []int
		torrentIDs        []int
		torrentJSONs      [][]byte
	)
	seenTorrent := map[int]bool{}
	for _, g := range deduped {
		pgID, ok := pgIDByGroupID[g.GroupID]
		if !ok {
			return nil, fmt.Errorf("no primary key for group %d after insert", g.GroupID)
		}
		for _, t := range g.Torrents {
			if seenTorrent[t.TorrentID] {
				continue
			}
			seenTorrent[t.TorrentID] = true
			torrentGroupPGIDs = append(torrentGroupPGIDs, pgID)
			torrentIDs = append(torrentIDs, t.TorrentID)
			torrentJSONs = append(torrentJSONs, t.FullJSONResult)
		}
	}

	if len(torrentIDs) > 0 {
		if _, err := tx.Exec(ctx,
			`DELETE FROM redacted.torrents_json WHERE torrent_id = ANY ($1::integer[])`,
			torrentIDs); err != nil {
			return nil, fmt.Errorf("deleting torrents: %w", err)
		}
		if _, err := tx.Exec(ctx, `
			INSERT INTO redacted.torrents_json (torrent_group, torrent_id, full_json_result)
			SELECT * FROM UNNEST($1::integer[], $2::integer[], $3::jsonb[])`,
			torrentGroupPGIDs, torrentIDs, torrentJSONs); err != nil {
			return nil, fmt.Errorf("inserting torrents: %w", err)
		}
	}

	// Artists are extracted from both group and torrent JSON.
	var jsons [][]byte
	for _, g := range deduped {
		jsons = append(jsons, g.FullJSONResult)
		for _, t := range g.Torrents {
			jsons = append(jsons, t.FullJSONResult)
		}
	}
	if err := insertArtists(ctx, tx, jsons); err != nil {
		return nil, err
	}

	return torrentIDs, nil
}

// artistRef is an (id, name) pair as it appears in the API's "artists" arrays.
type artistRef struct {
	ID   int    `json:"id"`
	Name string `json:"name"`
}

// insertArtists maintains the artist id -> name lookup table.
//
// The first name seen for an id wins, matching the Haskell version's use of
// Map.fromList.
func insertArtists(ctx context.Context, tx pgx.Tx, jsons [][]byte) error {
	byID := map[int]string{}
	var order []int
	for _, raw := range jsons {
		var obj struct {
			Artists []artistRef `json:"artists"`
		}
		if err := json.Unmarshal(raw, &obj); err != nil {
			// Not every object has an artists array; that is not an error.
			continue
		}
		for _, ar := range obj.Artists {
			if ar.ID == 0 || ar.Name == "" {
				continue
			}
			if _, seen := byID[ar.ID]; !seen {
				byID[ar.ID] = ar.Name
				order = append(order, ar.ID)
			}
		}
	}
	if len(order) == 0 {
		return nil
	}

	ids := make([]int, len(order))
	names := make([]string, len(order))
	for i, id := range order {
		ids[i] = id
		names[i] = byID[id]
	}

	if _, err := tx.Exec(ctx, `
		INSERT INTO redacted.artists (artist_id, artist_name)
		SELECT * FROM UNNEST($1::integer[], $2::text[])
		ON CONFLICT (artist_id) DO UPDATE SET
			artist_name = EXCLUDED.artist_name,
			updated_at = NOW()`,
		ids, names); err != nil {
		return fmt.Errorf("inserting artists: %w", err)
	}
	slog.Info("inserted/updated artists", "count", len(ids))
	return nil
}

// ---------------------------------------------------------------------------
// Torrent files
// ---------------------------------------------------------------------------

// freeleechStatus reports whether a download consumed a freeleech token.
type freeleechStatus int

const (
	freeleechPossible freeleechStatus = iota
	noTokensRemaining
)

// getTorrentFile downloads a .torrent, using a freeleech token when that seems
// worthwhile.
//
// Token handling, ported as-is: tokens are used unless they were found to be
// exhausted less than 24 hours ago. If the API rejects the request because no
// tokens are left, we retry immediately without a token and report that back so
// the caller can record the exhaustion.
func (a *app) getTorrentFile(ctx context.Context, torrentID int, exhaustedAt *time.Time) ([]byte, freeleechStatus, error) {
	type result struct {
		file   []byte
		status freeleechStatus
	}
	r, err := inSpan1(ctx, "Redacted Get Torrent File", func(ctx context.Context, span trace.Span) (result, error) {
		attr(span, "torrent.id", torrentID)

		useTokens := true
		if exhaustedAt != nil {
			if time.Since(*exhaustedAt) < 24*time.Hour {
				useTokens = false
			} else {
				event(span, "Testing freeleech tokens (24+ hours since exhaustion)")
			}
		}
		attr(span, "freeleech.use_tokens", useTokens)

		file, exhausted, err := a.downloadTorrent(ctx, torrentID, useTokens)
		if err != nil {
			return result{}, err
		}
		if !exhausted {
			return result{file: file, status: freeleechPossible}, nil
		}

		event(span, "Freeleech tokens exhausted, retrying without tokens")
		file, exhausted, err = a.downloadTorrent(ctx, torrentID, false)
		if err != nil {
			return result{}, err
		}
		if exhausted {
			return result{}, fmt.Errorf("unexpected token exhaustion error on retry without tokens")
		}
		return result{file: file, status: noTokensRemaining}, nil
	})
	return r.file, r.status, err
}

// downloadTorrent performs one download attempt.
//
// The second return value reports the specific "no freeleech tokens left"
// failure, which the API signals with a 400 and a JSON error message.
func (a *app) downloadTorrent(ctx context.Context, torrentID int, useToken bool) ([]byte, bool, error) {
	q := url.Values{}
	q.Set("action", "download")
	q.Set("id", strconv.Itoa(torrentID))
	if useToken {
		q.Set("usetoken", "1")
	}
	u := url.URL{Scheme: "https", Host: redactedHost, Path: "/ajax.php", RawQuery: q.Encode()}

	req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
	if err != nil {
		return nil, false, err
	}
	req.Header.Set("Authorization", a.redacted.apiKey)

	resp, err := a.redacted.http.Do(req)
	if err != nil {
		return nil, false, err
	}
	defer resp.Body.Close()
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, false, err
	}

	ct := resp.Header.Get("Content-Type")
	switch {
	case resp.StatusCode == http.StatusOK && strings.HasPrefix(ct, "application/x-bittorrent"):
		return body, false, nil

	case resp.StatusCode == http.StatusOK && ct != "":
		return nil, false, fmt.Errorf("redacted returned a non-torrent body, with content-type %q", ct)

	case resp.StatusCode == http.StatusOK:
		return nil, false, fmt.Errorf("redacted returned a body with unspecified content type")

	case resp.StatusCode == http.StatusBadRequest:
		var errReply struct {
			Error string `json:"error"`
		}
		if err := json.Unmarshal(body, &errReply); err == nil &&
			strings.Contains(errReply.Error, "You do not have any freeleech tokens left") {
			return nil, true, nil
		}
		return nil, false, fmt.Errorf("redacted returned a 400 error: %s", truncate(string(body), 300))

	default:
		return nil, false, fmt.Errorf("redacted returned a non-200 error code, code %d", resp.StatusCode)
	}
}

// getTorrentFileAndInsert downloads a torrent file and stores it.
func (a *app) getTorrentFileAndInsert(ctx context.Context, torrentID int, exhaustedAt *time.Time) ([]byte, freeleechStatus, error) {
	file, status, err := a.getTorrentFile(ctx, torrentID, exhaustedAt)
	if err != nil {
		return nil, 0, err
	}
	tag, err := a.pool.Exec(ctx, `
		UPDATE redacted.torrents_json
		SET torrent_file = $1::bytea
		WHERE torrent_id = $2::integer`,
		file, torrentID)
	if err != nil {
		return nil, 0, fmt.Errorf("storing torrent file: %w", err)
	}
	if err := assertOneUpdated("getTorrentFileAndInsert", tag.RowsAffected()); err != nil {
		return nil, 0, err
	}
	return file, status, nil
}

// getTorrentFileByID returns a previously downloaded torrent file, if any.
func (a *app) getTorrentFileByID(ctx context.Context, torrentID int) ([]byte, error) {
	var file []byte
	err := a.pool.QueryRow(ctx, `
		SELECT torrent_file FROM redacted.torrents
		WHERE torrent_id = $1::integer`, torrentID).Scan(&file)
	if err == pgx.ErrNoRows {
		return nil, nil
	}
	if err != nil {
		return nil, err
	}
	return file, nil
}

// updateTransmissionTorrentHashByID records which Transmission torrent
// corresponds to a Redacted torrent.
func (a *app) updateTransmissionTorrentHashByID(ctx context.Context, torrentID int, hash string) error {
	_, err := a.pool.Exec(ctx, `
		UPDATE redacted.torrents_json
		SET transmission_torrent_hash = $1::text
		WHERE torrent_id = $2::integer`, hash, torrentID)
	return err
}

// getTorrentByID returns the stored API JSON for a torrent, for the UI's
// "show raw data" toggle.
func (a *app) getTorrentByID(ctx context.Context, torrentID int) (json.RawMessage, error) {
	var raw json.RawMessage
	err := a.pool.QueryRow(ctx, `
		SELECT full_json_result FROM redacted.torrents
		WHERE torrent_id = $1::integer`, torrentID).Scan(&raw)
	if err != nil {
		return nil, err
	}
	return raw, nil
}

// getArtistNameByID resolves an artist id to a name for page titles.
func (a *app) getArtistNameByID(ctx context.Context, artistID int) (string, error) {
	var name string
	err := a.pool.QueryRow(ctx, `
		SELECT artist_name FROM redacted.artists
		WHERE artist_id = $1::int
		LIMIT 1`, artistID).Scan(&name)
	if err == pgx.ErrNoRows {
		return "", nil
	}
	if err != nil {
		return "", err
	}
	return name, nil
}

// ---------------------------------------------------------------------------
// Similar artists (recommendations)
// ---------------------------------------------------------------------------

type similarArtist struct {
	ArtistID   int    `json:"id"`
	ArtistName string `json:"name"`
	Score      int    `json:"score"`
}

// getSimilarArtists asks the API for artists related to the given one.
func (a *app) getSimilarArtists(ctx context.Context, artistID int, limit int) ([]similarArtist, error) {
	q := url.Values{}
	q.Set("id", strconv.Itoa(artistID))
	if limit > 0 {
		q.Set("limit", strconv.Itoa(limit))
	}
	raw, _, err := a.redacted.requestJSON(ctx, "similar_artists", q)
	if err != nil {
		return nil, err
	}
	var out []similarArtist
	if err := json.Unmarshal(raw, &out); err != nil {
		return nil, fmt.Errorf("parsing similar artists: %w", err)
	}
	return out, nil
}

// insertSimilarArtists replaces the stored recommendations for one artist.
func insertSimilarArtists(ctx context.Context, tx pgx.Tx, artistID int, similar []similarArtist) error {
	if _, err := tx.Exec(ctx,
		`DELETE FROM redacted.similar_artists WHERE artist_id = $1::integer`, artistID); err != nil {
		return err
	}
	if len(similar) == 0 {
		return nil
	}
	artistIDs := make([]int, len(similar))
	similarIDs := make([]int, len(similar))
	names := make([]string, len(similar))
	scores := make([]int, len(similar))
	for i, s := range similar {
		artistIDs[i] = artistID
		similarIDs[i] = s.ArtistID
		names[i] = s.ArtistName
		scores[i] = s.Score
	}
	_, err := tx.Exec(ctx, `
		INSERT INTO redacted.similar_artists
			(artist_id, similar_artist_id, similar_artist_name, score)
		SELECT * FROM UNNEST($1::integer[], $2::integer[], $3::text[], $4::integer[])`,
		artistIDs, similarIDs, names, scores)
	return err
}

// populateSimilarArtistsForFavourites fetches related artists for every
// favourite artist.
//
// "Favourite" means: an artist we have snatched a torrent of, or one explicitly
// marked by visiting their page.
func (a *app) populateSimilarArtistsForFavourites(ctx context.Context) (int, error) {
	return inSpan1(ctx, "Populate Similar Artists for Favorites", func(ctx context.Context, span trace.Span) (int, error) {
		rows, err := a.pool.Query(ctx, `
			SELECT DISTINCT unnest(artist_ids) as artist_id
			FROM redacted.torrents_json
			WHERE transmission_torrent_hash IS NOT NULL
			UNION
			SELECT artist_id
			FROM redacted.artist_favourites
			ORDER BY artist_id`)
		if err != nil {
			return 0, err
		}
		var ids []int
		for rows.Next() {
			var id int
			if err := rows.Scan(&id); err != nil {
				rows.Close()
				return 0, err
			}
			ids = append(ids, id)
		}
		rows.Close()
		if err := rows.Err(); err != nil {
			return 0, err
		}
		attr(span, "favorite-artists.count", len(ids))

		for _, id := range ids {
			similar, err := a.getSimilarArtists(ctx, id, 20)
			if err != nil {
				return 0, err
			}
			err = withTx(ctx, a.pool, func(ctx context.Context, tx pgx.Tx) error {
				return insertSimilarArtists(ctx, tx, id, similar)
			})
			if err != nil {
				return 0, err
			}
			slog.Info("populated similar artists", "artist", id, "count", len(similar))
		}
		return len(ids), nil
	})
}

// populateReleasesForSimilarArtists searches for releases by recommended artists
// we have no data for yet, so that recommendations have something to point at.
func (a *app) populateReleasesForSimilarArtists(ctx context.Context) (int, error) {
	return inSpan1(ctx, "Populate Releases for Similar Artists", func(ctx context.Context, span trace.Span) (int, error) {
		rows, err := a.pool.Query(ctx, `
			SELECT DISTINCT sa.similar_artist_id, sa.similar_artist_name
			FROM redacted.similar_artists sa
			WHERE sa.similar_artist_id NOT IN (
				SELECT DISTINCT unnest(artist_ids)
				FROM redacted.torrents_json
			)
			ORDER BY sa.similar_artist_id
			LIMIT 20`)
		if err != nil {
			return 0, err
		}
		type artist struct {
			id   int
			name string
		}
		var artists []artist
		for rows.Next() {
			var ar artist
			if err := rows.Scan(&ar.id, &ar.name); err != nil {
				rows.Close()
				return 0, err
			}
			artists = append(artists, ar)
		}
		rows.Close()
		if err := rows.Err(); err != nil {
			return 0, err
		}
		attr(span, "similar-artists-to-fetch.count", len(artists))

		for _, ar := range artists {
			slog.Info("searching for releases by similar artist", "name", ar.name, "id", ar.id)
			q := url.Values{}
			q.Set("artistname", ar.name)
			q.Set("releasetype", releaseTypesOverapproximatedWithout(releaseTypeCompilation))
			// Only the first page: enough to suggest a release.
			ids, err := a.searchAndInsert(ctx, q, 1)
			if err != nil {
				return 0, err
			}
			slog.Info("found new torrents for similar artist", "name", ar.name, "count", len(ids))
		}
		return len(artists), nil
	})
}

// ---------------------------------------------------------------------------
// Settings
// ---------------------------------------------------------------------------

// settings is the small key/value store in redacted.settings.
type settings struct {
	// freeleechTokensExhaustedAt is when the API last told us we were out of
	// freeleech tokens; nil means "not exhausted".
	freeleechTokensExhaustedAt *time.Time
}

func (a *app) getSettings(ctx context.Context) (settings, error) {
	return inSpan1(ctx, "Get Settings", func(ctx context.Context, span trace.Span) (settings, error) {
		var s settings
		rows, err := a.pool.Query(ctx, `SELECT key, value FROM redacted.settings`)
		if err != nil {
			return s, err
		}
		defer rows.Close()
		for rows.Next() {
			var key string
			var value []byte
			if err := rows.Scan(&key, &value); err != nil {
				return s, err
			}
			if key != "freelechTokensExhaustedAt" {
				continue
			}
			var ts string
			// A JSON null means the exhaustion was explicitly cleared. Note
			// that unmarshalling null into a string succeeds and yields "", so
			// it has to be checked for separately.
			if err := json.Unmarshal(value, &ts); err != nil || ts == "" {
				continue
			}
			t, err := parseSettingsTime(ts)
			if err != nil {
				slog.Warn("ignoring unparseable freeleech timestamp", "value", ts, "err", err)
				continue
			}
			s.freeleechTokensExhaustedAt = &t
		}
		return s, rows.Err()
	})
}

// parseSettingsTime accepts both the Haskell `show`n UTCTime already in the
// database and RFC 3339, so the two implementations can share a database.
func parseSettingsTime(s string) (time.Time, error) {
	if t, err := time.Parse(time.RFC3339Nano, s); err == nil {
		return t, nil
	}
	// Haskell's UTCTime Show instance, e.g. "2025-01-09 12:34:56.789 UTC".
	for _, layout := range []string{
		"2006-01-02 15:04:05.999999999 UTC",
		"2006-01-02 15:04:05 UTC",
	} {
		if t, err := time.Parse(layout, s); err == nil {
			return t, nil
		}
	}
	return time.Time{}, fmt.Errorf("unrecognised time format %q", s)
}

// setFreeleechExhausted records or clears the token exhaustion timestamp.
func (a *app) setFreeleechExhausted(ctx context.Context, at *time.Time) error {
	var value []byte
	if at == nil {
		value = []byte("null")
	} else {
		b, err := json.Marshal(at.UTC().Format(time.RFC3339Nano))
		if err != nil {
			return err
		}
		value = b
	}
	_, err := a.pool.Exec(ctx, `
		INSERT INTO redacted.settings (key, value)
		VALUES ('freelechTokensExhaustedAt', $1::jsonb)
		ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value`, value)
	return err
}