Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
151 lines
5.3 KiB
Go
151 lines
5.3 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/discovery"
|
|
"git.kmalloc.org/squid/sonda/internal/providers/lidarr"
|
|
)
|
|
|
|
type lidarrArtistService interface {
|
|
Send(ctx context.Context, artistID int64) (lidarr.Outcome, error)
|
|
Presence(ctx context.Context, artistID int64) (lidarr.Presence, error)
|
|
}
|
|
|
|
// lidarrRequestService keeps and lists the artists a person sent to Lidarr.
|
|
type lidarrRequestService interface {
|
|
Record(ctx context.Context, userID, artistID int64) error
|
|
List(ctx context.Context, userID int64, status string, limit, offset int) (lidarr.RequestPage, error)
|
|
}
|
|
|
|
type lidarrArtistHandlers struct {
|
|
svc lidarrArtistService
|
|
requests lidarrRequestService
|
|
}
|
|
|
|
// send adds an artist to the admin's own Lidarr, which then looks for the music. It is an explicit
|
|
// action of an admin on one artist, never done by itself.
|
|
func (h *lidarrArtistHandlers) send(c *gin.Context) {
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
out, err := h.svc.Send(c.Request.Context(), id)
|
|
switch {
|
|
case err == nil:
|
|
// Lidarr has the artist now. That it could not be written down does not undo it: say so in the log, not to the admin.
|
|
if rerr := h.requests.Record(c.Request.Context(), currentUserID(c), id); rerr != nil {
|
|
_ = c.Error(rerr)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"status": string(out)})
|
|
case errors.Is(err, discovery.ErrNotFound):
|
|
writeError(c, http.StatusNotFound, "artist_not_found", "not found")
|
|
case errors.Is(err, lidarr.ErrNoID):
|
|
writeError(c, http.StatusUnprocessableEntity, "no_musicbrainz_id", "the artist has no MusicBrainz id, and Sonda does not look one up by name")
|
|
case errors.Is(err, lidarr.ErrDisabled):
|
|
writeError(c, http.StatusConflict, "lidarr_disabled", "Lidarr is switched off")
|
|
case errors.Is(err, lidarr.ErrNotSetUp):
|
|
writeError(c, http.StatusConflict, "lidarr_not_configured", "the quality profile, the metadata profile and the root folder of Lidarr have not been chosen yet")
|
|
case errors.Is(err, lidarr.ErrNotFound):
|
|
writeError(c, http.StatusUnprocessableEntity, "lidarr_unknown_artist", "Lidarr does not know this artist")
|
|
case errors.Is(err, lidarr.ErrRejected):
|
|
writeError(c, http.StatusUnprocessableEntity, "lidarr_rejected", err.Error())
|
|
case errors.Is(err, context.Canceled):
|
|
c.AbortWithStatus(statusClientClosed) // the one who clicked left
|
|
case errors.Is(err, lidarr.ErrUnreachable), errors.Is(err, lidarr.ErrBadResponse), errors.Is(err, context.DeadlineExceeded):
|
|
// The request may have got there before the answer was lost: say so.
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusBadGateway, "lidarr_unavailable", "Lidarr could not be asked, or did not answer in time: the artist may have been added, check with GET /artists/{id}/lidarr")
|
|
default:
|
|
writeLidarrError(c, err)
|
|
}
|
|
}
|
|
|
|
type lidarrPresenceJSON struct {
|
|
ProviderStatus string `json:"provider_status"`
|
|
InLidarr bool `json:"in_lidarr"`
|
|
Monitored bool `json:"monitored"`
|
|
}
|
|
|
|
// presence says whether Lidarr has the artist.
|
|
func (h *lidarrArtistHandlers) presence(c *gin.Context) {
|
|
id, ok := pathID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
p, err := h.svc.Presence(c.Request.Context(), id)
|
|
switch {
|
|
case errors.Is(err, discovery.ErrNotFound):
|
|
writeError(c, http.StatusNotFound, "artist_not_found", "not found")
|
|
case errors.Is(err, context.Canceled):
|
|
c.AbortWithStatus(statusClientClosed)
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
default:
|
|
c.JSON(http.StatusOK, lidarrPresenceJSON{ProviderStatus: p.Status, InLidarr: p.InLidarr, Monitored: p.Monitored})
|
|
}
|
|
}
|
|
|
|
type lidarrRequestJSON struct {
|
|
ID int64 `json:"id"`
|
|
Artist lidarrArtistRefJSON `json:"artist"`
|
|
MBID *string `json:"mbid"`
|
|
RequestedAt time.Time `json:"requested_at"`
|
|
Status string `json:"status"`
|
|
ArrivedAt *time.Time `json:"arrived_at"`
|
|
}
|
|
|
|
type lidarrArtistRefJSON struct {
|
|
ID *int64 `json:"id"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// requestList lists the artists the signed-in admin sent to Lidarr, newest first, saying which have music in the
|
|
// library now.
|
|
func (h *lidarrArtistHandlers) requestList(c *gin.Context) {
|
|
limit, ok := queryInt(c, "limit")
|
|
if !ok {
|
|
return
|
|
}
|
|
offset, ok := queryInt(c, "offset")
|
|
if !ok {
|
|
return
|
|
}
|
|
page, err := h.requests.List(c.Request.Context(), currentUserID(c), c.Query("status"), clampInt(limit), clampInt(offset))
|
|
switch {
|
|
case errors.Is(err, lidarr.ErrInvalidQuery):
|
|
writeError(c, http.StatusBadRequest, "invalid_input", err.Error())
|
|
return
|
|
case err != nil:
|
|
_ = c.Error(err)
|
|
writeError(c, http.StatusInternalServerError, "internal", "internal error")
|
|
return
|
|
}
|
|
items := make([]lidarrRequestJSON, 0, len(page.Items))
|
|
for _, r := range page.Items {
|
|
j := lidarrRequestJSON{ID: r.ID, Artist: lidarrArtistRefJSON{Name: r.Name}, RequestedAt: r.RequestedAt.UTC(), Status: r.Status()}
|
|
if r.ArtistID != 0 {
|
|
id := r.ArtistID
|
|
j.Artist.ID = &id
|
|
}
|
|
if r.MBID != "" {
|
|
m := r.MBID
|
|
j.MBID = &m
|
|
}
|
|
if !r.ArrivedAt.IsZero() {
|
|
at := r.ArrivedAt.UTC()
|
|
j.ArrivedAt = &at
|
|
}
|
|
items = append(items, j)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"items": items, "total": page.Total, "limit": page.Limit, "offset": page.Offset,
|
|
"counts": gin.H{"waiting": page.Waiting, "arrived": page.Arrived},
|
|
})
|
|
}
|