support users inside selections

This commit is contained in:
Laptop
2025-08-31 00:33:27 +03:00
parent 39787b6cc9
commit e0bfcaadba
2 changed files with 86 additions and 9 deletions

View File

@@ -1,13 +1,65 @@
package sc
import "git.maid.zone/stuff/soundcloak/lib/cfg"
import (
"net/url"
"strings"
// Functions/structions related to featured/suggested content
"git.maid.zone/stuff/soundcloak/lib/cfg"
)
// Functions/structures related to featured/suggested content
type PlaylistOrUser struct {
Kind string `json:"kind"` // "playlist" or "system-playlist" or "user"
Permalink string `json:"permalink"`
// Playlist-specific
TrackCount int64 `json:"track_count"`
Title string `json:"title"`
Author struct {
Permalink string
} `json:"author"`
Artwork string `json:"artwork_url"`
// User-specific
Avatar string `json:"avatar_url"`
Username string `json:"username"`
FullName string `json:"full_name"`
}
func (p PlaylistOrUser) Href() string {
switch p.Kind {
case "system-playlist":
return "/discover/sets/" + p.Permalink
case "playlist":
return "/" + p.Author.Permalink + "/sets/" + p.Permalink
default:
return "/" + p.Permalink
}
}
func (p *PlaylistOrUser) Fix(prefs cfg.Preferences) {
switch p.Kind {
case "user":
if p.Avatar == "https://a1.sndcdn.com/images/default_avatar_large.png" {
p.Avatar = ""
} else {
p.Avatar = strings.Replace(p.Avatar, "-large.", "-t200x200.", 1)
}
default:
if p.Artwork != "" {
p.Artwork = strings.Replace(p.Artwork, "-large.", "-t200x200.", 1)
if cfg.ProxyImages && *prefs.ProxyImages {
p.Artwork = "/_/proxy/images?url=" + url.QueryEscape(p.Artwork)
}
}
}
}
type Selection struct {
Title string `json:"title"`
Kind string `json:"kind"` // should always be "selection"!
Items Paginated[*Playlist] `json:"items"` // ?? why
Items Paginated[*PlaylistOrUser] `json:"items"` // ?? why
}
func GetSelections(cid string, prefs cfg.Preferences) (*Paginated[*Selection], error) {
@@ -27,7 +79,6 @@ func GetSelections(cid string, prefs cfg.Preferences) (*Paginated[*Selection], e
func (s *Selection) Fix(prefs cfg.Preferences) {
for _, p := range s.Items.Collection {
p.Fix("", false, false)
p.Postfix(prefs, false, false)
p.Fix(prefs)
}
}

View File

@@ -5,6 +5,33 @@ import (
"strconv"
)
templ PlaylistOrUserItem(pl *sc.PlaylistOrUser) {
<a class="listing" href={ templ.SafeURL(pl.Href()) }>
{{
img := pl.Artwork
if pl.Kind == "user" {
img = pl.Avatar
}
}}
if img != "" {
<img loading="lazy" fetchpriority="low" src={ img }/>
} else {
<img loading="lazy" fetchpriority="low" src="/_/static/placeholder.jpg"/>
}
<div class="meta">
if pl.Kind == "user" {
<h3>{ pl.Username }</h3>
if pl.FullName != "" {
<span>{ pl.FullName }</span>
}
} else {
<h3>{ pl.Title }</h3>
<p>{ strconv.FormatInt(pl.TrackCount, 10) } tracks</p>
}
</div>
</a>
}
templ Discover(p *sc.Paginated[*sc.Selection]) {
<h1>Discover Playlists</h1> // also tracks apparently? haven't seen any
<span>Got { strconv.FormatInt(int64(len(p.Collection)), 10) } selections</span>
@@ -17,8 +44,7 @@ templ Discover(p *sc.Paginated[*sc.Selection]) {
<h2>{selection.Title}</h2>
for _, pl := range selection.Items.Collection {
// We don't need the username
@PlaylistItem(pl, false)
@PlaylistOrUserItem(pl)
}
}