diff --git a/lib/sc/featured.go b/lib/sc/featured.go index 21d35f6..7525e97 100644 --- a/lib/sc/featured.go +++ b/lib/sc/featured.go @@ -56,6 +56,6 @@ func GetSelections(prefs cfg.Preferences) (*Paginated[*Selection], error) { func (s *Selection) Fix(prefs cfg.Preferences) { for _, p := range s.Items.Collection { p.Fix(false) - p.Postfix(prefs) + p.Postfix(prefs, false) } } diff --git a/lib/sc/playlist.go b/lib/sc/playlist.go index 0691a3d..7a61f22 100644 --- a/lib/sc/playlist.go +++ b/lib/sc/playlist.go @@ -79,7 +79,7 @@ func SearchPlaylists(prefs cfg.Preferences, args string) (*Paginated[*Playlist], for _, p := range p.Collection { p.Fix(false) - p.Postfix(prefs) + p.Postfix(prefs, false) } return &p, nil @@ -107,18 +107,22 @@ func (p *Playlist) Fix(cached bool) error { return nil } -func (p *Playlist) Postfix(prefs cfg.Preferences) []Track { +func (p *Playlist) Postfix(prefs cfg.Preferences, fixTracks bool) []Track { if cfg.ProxyImages && *prefs.ProxyImages && p.Artwork != "" { p.Artwork = "/_/proxy/images?url=" + url.QueryEscape(p.Artwork) } p.Author.Postfix(prefs) - var fixed = make([]Track, len(p.Tracks)) - for i, t := range p.Tracks { - t.Postfix(prefs) - fixed[i] = t + if fixTracks { + var fixed = make([]Track, len(p.Tracks)) + for i, t := range p.Tracks { + t.Postfix(prefs) + fixed[i] = t + } + return fixed } - return fixed + + return nil } func (p Playlist) FormatDescription() string { diff --git a/lib/sc/user.go b/lib/sc/user.go index 117e2fa..ecc08c3 100644 --- a/lib/sc/user.go +++ b/lib/sc/user.go @@ -48,7 +48,7 @@ type Repost struct { Playlist *Playlist // type == playlist-repost } -func (r *Repost) Fix(prefs cfg.Preferences) { +func (r Repost) Fix(prefs cfg.Preferences) { switch r.Type { case TrackRepost: if r.Track != nil { @@ -59,12 +59,27 @@ func (r *Repost) Fix(prefs cfg.Preferences) { case PlaylistRepost: if r.Playlist != nil { r.Playlist.Fix(false) // err always nil if cached == false - r.Playlist.Postfix(prefs) + r.Playlist.Postfix(prefs, false) } return } } +// same thing +type Like struct { + Track *Track + Playlist *Playlist +} + +func (l Like) Fix(prefs cfg.Preferences) { + if l.Track != nil { + l.Track.Fix(false) + l.Track.Postfix(prefs) + } else if l.Playlist != nil { + l.Playlist.Fix(false) + l.Playlist.Postfix(prefs, false) + } +} func GetUser(permalink string) (User, error) { usersCacheLock.RLock() if cell, ok := UsersCache[permalink]; ok && cell.Expires.After(time.Now()) { @@ -189,7 +204,7 @@ func (u *User) GetPlaylists(prefs cfg.Preferences, args string) (*Paginated[*Pla for _, pl := range p.Collection { pl.Fix(false) - pl.Postfix(prefs) + pl.Postfix(prefs, false) } return &p, nil @@ -207,7 +222,7 @@ func (u *User) GetAlbums(prefs cfg.Preferences, args string) (*Paginated[*Playli for _, pl := range p.Collection { pl.Fix(false) - pl.Postfix(prefs) + pl.Postfix(prefs, false) } return &p, nil @@ -229,3 +244,20 @@ func (u *User) GetReposts(prefs cfg.Preferences, args string) (*Paginated[*Repos return &p, nil } + +func (u *User) GetLikes(prefs cfg.Preferences, args string) (*Paginated[*Like], error) { + p := Paginated[*Like]{ + Next: "https://" + api + "/users/" + u.ID + "/likes" + args, + } + + err := p.Proceed(true) + if err != nil { + return nil, err + } + + for _, l := range p.Collection { + l.Fix(prefs) + } + + return &p, nil +} diff --git a/main.go b/main.go index a46ccb3..44bd00a 100644 --- a/main.go +++ b/main.go @@ -310,6 +310,29 @@ func main() { return templates.Base(user.Username, templates.UserReposts(prefs, user, p), templates.UserHeader(user)).Render(context.Background(), c) }) + app.Get("/:user/likes", func(c *fiber.Ctx) error { + prefs, err := preferences.Get(c) + if err != nil { + return err + } + + user, err := sc.GetUser(c.Params("user")) + if err != nil { + log.Printf("error getting %s (likes): %s\n", c.Params("user"), err) + return err + } + user.Postfix(prefs) + + p, err := user.GetLikes(prefs, c.Query("pagination", "?limit=20")) + if err != nil { + log.Printf("error getting %s likes: %s\n", c.Params("user"), err) + return err + } + + c.Set("Content-Type", "text/html") + return templates.Base(user.Username, templates.UserLikes(prefs, user, p), templates.UserHeader(user)).Render(context.Background(), c) + }) + app.Get("/:user/:track", func(c *fiber.Ctx) error { prefs, err := preferences.Get(c) if err != nil { @@ -385,7 +408,7 @@ func main() { return err } // Don't ask why - playlist.Tracks = playlist.Postfix(prefs) + playlist.Tracks = playlist.Postfix(prefs, true) p := c.Query("pagination") if p != "" { diff --git a/templates/user.templ b/templates/user.templ index 397fdb3..afd48d2 100644 --- a/templates/user.templ +++ b/templates/user.templ @@ -1,11 +1,11 @@ package templates import ( + "github.com/maid-zone/soundcloak/lib/cfg" "github.com/maid-zone/soundcloak/lib/sc" "net/url" "strconv" "strings" - "github.com/maid-zone/soundcloak/lib/cfg" ) templ UserHeader(u sc.User) { @@ -59,16 +59,33 @@ templ UserBase(prefs cfg.Preferences, u sc.User) { } +type btn struct { + text string + href string + external bool +} + +templ UserButtons(current string, user string) { +
// this part is the tedious one now, because formatting breaks if i space the list out with newlines + for _, b := range [6]btn{{"tracks", "", false},{"playlists", "/sets",false},{"albums", "/albums", false},{"reposts","/reposts", false},{"likes", "/likes", false},{"view on soundcloud", "https://soundcloud.com/"+user, true}} { + if b.text == current { + {b.text} + } else { + if b.external { + { b.text } + } else { + { b.text } + } + } + } +
+} + templ User(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Track]) { @UserBase(prefs, u) // kinda tedious but whatever, might make it more flexible in the future -
- tracks - playlists - albums - reposts - view on soundcloud -
+ // ^ outdated, no longer tedious but whatever + @UserButtons("tracks", u.Permalink)
if len(p.Collection) != 0 {
@@ -86,13 +103,7 @@ templ User(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Track]) { templ UserPlaylists(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Playlist]) { @UserBase(prefs, u) -
- tracks - playlists - albums - reposts - view on soundcloud -
+ @UserButtons("playlists", u.Permalink)
if len(p.Collection) != 0 {
@@ -110,13 +121,7 @@ templ UserPlaylists(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Playli templ UserAlbums(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Playlist]) { @UserBase(prefs, u) - + @UserButtons("albums", u.Permalink)
if len(p.Collection) != 0 {
@@ -134,13 +139,7 @@ templ UserAlbums(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Playlist] templ UserReposts(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Repost]) { @UserBase(prefs, u) - + @UserButtons("reposts", u.Permalink)
if len(p.Collection) != 0 {
@@ -160,6 +159,28 @@ templ UserReposts(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Repost]) } } +templ UserLikes(prefs cfg.Preferences, u sc.User, p *sc.Paginated[*sc.Like]) { + @UserBase(prefs, u) + @UserButtons("likes", u.Permalink) +
+ if len(p.Collection) != 0 { +
+ for _, like := range p.Collection { + if like.Track != nil { + @TrackItem(like.Track, true) + } else if like.Playlist != nil { + @PlaylistItem(like.Playlist, true) + } + } +
+ if p.Next != "" && len(p.Collection) != int(p.Total) { + more likes + } + } else { + no more likes + } +} + templ SearchUsers(p *sc.Paginated[*sc.User]) { Found { strconv.FormatInt(p.Total, 10) } users