Merge pull request #105 from soulcramer/Add_quotes_profile_page

 Add quotes to user profile.
This commit is contained in:
Eduard Urbach 2018-03-19 17:33:15 +01:00 committed by GitHub
commit 3d810f3b1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 141 additions and 0 deletions

View File

@ -44,6 +44,7 @@ import (
"github.com/animenotifier/notify.moe/pages/popular" "github.com/animenotifier/notify.moe/pages/popular"
"github.com/animenotifier/notify.moe/pages/posts" "github.com/animenotifier/notify.moe/pages/posts"
"github.com/animenotifier/notify.moe/pages/profile" "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/profile/profiletracks"
"github.com/animenotifier/notify.moe/pages/quote" "github.com/animenotifier/notify.moe/pages/quote"
"github.com/animenotifier/notify.moe/pages/quotes" "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/added/from/:index", profiletracks.Added)
l.Page("/user/:nick/soundtracks/liked", profiletracks.Liked) l.Page("/user/:nick/soundtracks/liked", profiletracks.Liked)
l.Page("/user/:nick/soundtracks/liked/from/:index", 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/stats", profile.GetStatsByUser)
l.Page("/user/:nick/followers", profile.GetFollowers) l.Page("/user/:nick/followers", profile.GetFollowers)
l.Page("/user/:nick/animelist/anime/:id", animelistitem.Get) l.Page("/user/:nick/animelist/anime/:id", animelistitem.Get)

View File

@ -23,6 +23,7 @@ component ProfileTabs(viewUser *arn.User, uri string)
//- Tab("Collection", "list", "/+" + viewUser.Nick + "/animelist/watching") //- Tab("Collection", "list", "/+" + viewUser.Nick + "/animelist/watching")
Tab("Forum", "comment", "/+" + viewUser.Nick + "/forum/threads") Tab("Forum", "comment", "/+" + viewUser.Nick + "/forum/threads")
Tab("Tracks", "music", "/+" + viewUser.Nick + "/soundtracks/liked") Tab("Tracks", "music", "/+" + viewUser.Nick + "/soundtracks/liked")
Tab("Quotes", "quote-left", "/+" + viewUser.Nick + "/quotes/liked")
Tab("Stats", "area-chart", "/+" + viewUser.Nick + "/stats") Tab("Stats", "area-chart", "/+" + viewUser.Nick + "/stats")
Tab("Followers", "users", "/+" + viewUser.Nick + "/followers") 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("Liked", "heart", "/+" + viewUser.Nick + "/soundtracks/liked")
Tab("Added", "music", "/+" + viewUser.Nick + "/soundtracks/added") 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) component ProfileHeader(viewUser *arn.User, user *arn.User, uri string)
ProfileHead(viewUser, user, uri) ProfileHead(viewUser, user, uri)
ProfileTabs(viewUser, uri) ProfileTabs(viewUser, uri)

View File

@ -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
})
}

View File

@ -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)
})
}

View File

@ -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)

View File

@ -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()))
}

View File

@ -30,6 +30,22 @@ var routeTests = map[string][]string{
"/+Akyoto/soundtracks/liked/from/3", "/+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{ "/user/:nick/followers": []string{
"/+Akyoto/followers", "/+Akyoto/followers",
}, },