From a317a1f29be7f90870917b7ecfe2461792d6def1 Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 19 Mar 2018 12:25:31 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20tracks=20to=20user=20profile.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/index.go | 5 +++ pages/profile/profile.pixy | 6 +++ pages/profile/profilequotes/added.go | 18 ++++++++ pages/profile/profilequotes/liked.go | 18 ++++++++ pages/profile/profilequotes/quotes.pixy | 21 +++++++++ pages/profile/profilequotes/render.go | 57 +++++++++++++++++++++++++ tests.go | 16 +++++++ 7 files changed, 141 insertions(+) create mode 100644 pages/profile/profilequotes/added.go create mode 100644 pages/profile/profilequotes/liked.go create mode 100644 pages/profile/profilequotes/quotes.pixy create mode 100644 pages/profile/profilequotes/render.go diff --git a/pages/index.go b/pages/index.go index 6143c4be..99f1bd03 100644 --- a/pages/index.go +++ b/pages/index.go @@ -44,6 +44,7 @@ import ( "github.com/animenotifier/notify.moe/pages/popular" "github.com/animenotifier/notify.moe/pages/posts" "github.com/animenotifier/notify.moe/pages/profile" + "github.com/animenotifier/notify.moe/pages/profile/profilequotes" "github.com/animenotifier/notify.moe/pages/profile/profiletracks" "github.com/animenotifier/notify.moe/pages/quote" "github.com/animenotifier/notify.moe/pages/quotes" @@ -176,6 +177,10 @@ func Configure(app *aero.Application) { l.Page("/user/:nick/soundtracks/added/from/:index", profiletracks.Added) l.Page("/user/:nick/soundtracks/liked", profiletracks.Liked) l.Page("/user/:nick/soundtracks/liked/from/:index", profiletracks.Liked) + l.Page("/user/:nick/quotes/added", profilequotes.Added) + l.Page("/user/:nick/quotes/added/from/:index", profilequotes.Added) + l.Page("/user/:nick/quotes/liked", profilequotes.Liked) + l.Page("/user/:nick/quotes/liked/from/:index", profilequotes.Liked) l.Page("/user/:nick/stats", profile.GetStatsByUser) l.Page("/user/:nick/followers", profile.GetFollowers) l.Page("/user/:nick/animelist/anime/:id", animelistitem.Get) diff --git a/pages/profile/profile.pixy b/pages/profile/profile.pixy index 829d9fa6..ea8c660d 100644 --- a/pages/profile/profile.pixy +++ b/pages/profile/profile.pixy @@ -23,6 +23,7 @@ component ProfileTabs(viewUser *arn.User, uri string) //- Tab("Collection", "list", "/+" + viewUser.Nick + "/animelist/watching") Tab("Forum", "comment", "/+" + viewUser.Nick + "/forum/threads") Tab("Tracks", "music", "/+" + viewUser.Nick + "/soundtracks/liked") + Tab("Quotes", "quote-left", "/+" + viewUser.Nick + "/quotes/liked") Tab("Stats", "area-chart", "/+" + viewUser.Nick + "/stats") Tab("Followers", "users", "/+" + viewUser.Nick + "/followers") @@ -34,6 +35,11 @@ component ProfileTabs(viewUser *arn.User, uri string) Tab("Liked", "heart", "/+" + viewUser.Nick + "/soundtracks/liked") Tab("Added", "music", "/+" + viewUser.Nick + "/soundtracks/added") + if strings.Contains(uri, "/quotes") + .tabs + Tab("Liked", "heart", "/+" + viewUser.Nick + "/quotes/liked") + Tab("Added", "music", "/+" + viewUser.Nick + "/quotes/added") + component ProfileHeader(viewUser *arn.User, user *arn.User, uri string) ProfileHead(viewUser, user, uri) ProfileTabs(viewUser, uri) diff --git a/pages/profile/profilequotes/added.go b/pages/profile/profilequotes/added.go new file mode 100644 index 00000000..4de371ab --- /dev/null +++ b/pages/profile/profilequotes/added.go @@ -0,0 +1,18 @@ +package profilequotes + +import ( + "github.com/aerogo/aero" + "github.com/animenotifier/arn" +) + +// Added shows all quotes added by a particular user. +func Added(ctx *aero.Context) string { + return render(ctx, addedQuotes) +} + +// addedQuotes returns all quotes that the user with the given user ID published. +func addedQuotes(userID string) []*arn.Quote { + return arn.FilterQuotes(func(quote *arn.Quote) bool { + return !quote.IsDraft && len(quote.Text.English) > 0 && quote.CreatedBy == userID + }) +} diff --git a/pages/profile/profilequotes/liked.go b/pages/profile/profilequotes/liked.go new file mode 100644 index 00000000..00a05228 --- /dev/null +++ b/pages/profile/profilequotes/liked.go @@ -0,0 +1,18 @@ +package profilequotes + +import ( + "github.com/aerogo/aero" + "github.com/animenotifier/arn" +) + +// Liked shows all quotes liked by a particular user. +func Liked(ctx *aero.Context) string { + return render(ctx, likedQuotes) +} + +// likedQuotes returns all quotes that the user with the given user ID liked. +func likedQuotes(userID string) []*arn.Quote { + return arn.FilterQuotes(func(track *arn.Quote) bool { + return !track.IsDraft && len(track.Text.English) > 0 && track.LikedBy(userID) + }) +} diff --git a/pages/profile/profilequotes/quotes.pixy b/pages/profile/profilequotes/quotes.pixy new file mode 100644 index 00000000..002ec310 --- /dev/null +++ b/pages/profile/profilequotes/quotes.pixy @@ -0,0 +1,21 @@ +component ProfileQuotes(quotes []*arn.Quote, viewUser *arn.User, nextIndex int, user *arn.User, uri string) + ProfileHeader(viewUser, user, uri) + + if strings.Contains(uri, "/added") + h1.page-title= "Quotes added by " + viewUser.Nick + else + h1.page-title= "Quotes liked by " + viewUser.Nick + + if len(quotes) == 0 + if strings.Contains(uri, "/added") + p.no-data.mountable= viewUser.Nick + " hasn't added any quotes yet." + else + p.no-data.mountable= viewUser.Nick + " hasn't liked any quotes yet." + else + #load-more-target.quotes + QuotesScrollable(quotes, user) + + if nextIndex != -1 + .buttons + LoadMore(nextIndex) + \ No newline at end of file diff --git a/pages/profile/profilequotes/render.go b/pages/profile/profilequotes/render.go new file mode 100644 index 00000000..6a4c7069 --- /dev/null +++ b/pages/profile/profilequotes/render.go @@ -0,0 +1,57 @@ +package profilequotes + +import ( + "net/http" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" + "github.com/animenotifier/notify.moe/components" + "github.com/animenotifier/notify.moe/utils" + "github.com/animenotifier/notify.moe/utils/infinitescroll" +) + +const ( + quotesFirstLoad = 12 + quotesPerScroll = 9 +) + +// render renders the quotes on user profiles. +func render(ctx *aero.Context, fetch func(userID string) []*arn.Quote) string { + nick := ctx.Get("nick") + index, _ := ctx.GetInt("index") + user := utils.GetUser(ctx) + viewUser, err := arn.GetUserByNick(nick) + + if err != nil { + return ctx.Error(http.StatusNotFound, "User not found", err) + } + + // Fetch all eligible quotes + allQuotes := fetch(viewUser.ID) + + // Sort the quotes by publication date + arn.SortQuotesLatestFirst(allQuotes) + + // Slice the part that we need + quotes := allQuotes[index:] + maxLength := quotesFirstLoad + + if index > 0 { + maxLength = quotesPerScroll + } + + if len(quotes) > maxLength { + quotes = quotes[:maxLength] + } + + // Next index + nextIndex := infinitescroll.NextIndex(ctx, len(allQuotes), maxLength, index) + + // In case we're scrolling, send quotes only (without the page frame) + if index > 0 { + return ctx.HTML(components.QuotesScrollable(quotes, user)) + } + + // Otherwise, send the full page + return ctx.HTML(components.ProfileQuotes(quotes, viewUser, nextIndex, user, ctx.URI())) +} diff --git a/tests.go b/tests.go index ab1efc4b..7dfaf72b 100644 --- a/tests.go +++ b/tests.go @@ -30,6 +30,22 @@ var routeTests = map[string][]string{ "/+Akyoto/soundtracks/liked/from/3", }, + "/user/:nick/quotes/added": []string{ + "/+Scott/quotes/added", + }, + + "/user/:nick/quotes/added/from/:index": []string{ + "/+Scott/quotes/added/from/3", + }, + + "/user/:nick/quotes/liked": []string{ + "/+Scott/quotes/liked", + }, + + "/user/:nick/quotes/liked/from/:index": []string{ + "/+Scott/quotes/liked/from/3", + }, + "/user/:nick/followers": []string{ "/+Akyoto/followers", },