Added user lists

This commit is contained in:
Eduard Urbach 2017-07-19 13:08:12 +02:00
parent cd6641cc06
commit e57e67610f
5 changed files with 131 additions and 16 deletions

View File

@ -11,16 +11,13 @@ import (
func main() { func main() {
color.Yellow("Caching list of active users") color.Yellow("Caching list of active users")
cache := arn.ListOfIDs{}
// Filter out active users with an avatar // Filter out active users with an avatar
users, err := arn.FilterUsers(func(user *arn.User) bool { users, err := arn.FilterUsers(func(user *arn.User) bool {
return user.IsActive() && user.Avatar.Extension != "" return user.IsActive() && user.Avatar.Extension != ""
}) })
fmt.Println(len(users))
if err != nil { arn.PanicOnError(err)
panic(err)
}
// Sort // Sort
sort.Slice(users, func(i, j int) bool { sort.Slice(users, func(i, j int) bool {
@ -36,17 +33,82 @@ func main() {
}) })
// Add users to list // Add users to list
for _, user := range users { SaveInCache("active users", users)
cache.IDList = append(cache.IDList, user.ID)
// Sort by osu rank
osuUsers := users[:]
sort.Slice(osuUsers, func(i, j int) bool {
return osuUsers[i].Accounts.Osu.PP > osuUsers[j].Accounts.Osu.PP
})
// Cut off users with 0 pp
for index, user := range osuUsers {
if user.Accounts.Osu.PP == 0 {
osuUsers = osuUsers[:index]
break
}
} }
fmt.Println(len(cache.IDList), "users") // Save osu users
SaveInCache("active osu users", osuUsers)
err = arn.DB.Set("Cache", "active users", cache) // Sort by role
staff := users[:]
if err != nil { sort.Slice(staff, func(i, j int) bool {
panic(err) if staff[i].Role == "" {
return false
}
if staff[j].Role == "" {
return true
}
return staff[i].Role == "admin"
})
// Cut off non-staff
for index, user := range staff {
if user.Role == "" {
staff = staff[:index]
break
}
} }
// Save staff users
SaveInCache("active staff users", staff)
// Sort by anime watching list length
watching := users[:]
sort.Slice(watching, func(i, j int) bool {
return len(watching[i].AnimeList().FilterStatus(arn.AnimeListStatusWatching).Items) > len(watching[j].AnimeList().FilterStatus(arn.AnimeListStatusWatching).Items)
})
// Save watching users
SaveInCache("active anime watching users", watching)
color.Green("Finished.") color.Green("Finished.")
} }
// SaveInCache ...
func SaveInCache(key string, users []*arn.User) {
cache := arn.ListOfIDs{
IDList: GenerateIDList(users),
}
fmt.Println(len(cache.IDList), key)
arn.PanicOnError(arn.DB.Set("Cache", key, cache))
}
// GenerateIDList generates an ID list from a slice of users.
func GenerateIDList(users []*arn.User) []string {
list := []string{}
for _, user := range users {
list = append(list, user.ID)
}
return list
}

View File

@ -84,7 +84,10 @@ func configure(app *aero.Application) *aero.Application {
app.Ajax("/new/soundtrack", newsoundtrack.Get) app.Ajax("/new/soundtrack", newsoundtrack.Get)
app.Ajax("/settings", settings.Get) app.Ajax("/settings", settings.Get)
app.Ajax("/soundtracks", music.Get) app.Ajax("/soundtracks", music.Get)
app.Ajax("/users", users.Get) app.Ajax("/users", users.Active)
app.Ajax("/users/osu", users.Osu)
app.Ajax("/users/staff", users.Staff)
app.Ajax("/users/anime/watching", users.AnimeWatching)
app.Ajax("/login", login.Get) app.Ajax("/login", login.Get)
// User profiles // User profiles

View File

@ -22,7 +22,7 @@ component ProfileHeader(viewUser *arn.User, user *arn.User, uri string)
Icon("home") Icon("home")
a(href=viewUser.WebsiteURL(), target="_blank", rel="nofollow")= viewUser.WebsiteShortURL() a(href=viewUser.WebsiteURL(), target="_blank", rel="nofollow")= viewUser.WebsiteShortURL()
if viewUser.Accounts.Osu.Nick != "" && viewUser.Accounts.Osu.PP >= 1000 if viewUser.Accounts.Osu.Nick != "" && viewUser.Accounts.Osu.PP >= 100
p.profile-field.osu(title="osu! Level " + toString(int(viewUser.Accounts.Osu.Level)) + " | Accuracy: " + fmt.Sprintf("%.1f", viewUser.Accounts.Osu.Accuracy) + "%") p.profile-field.osu(title="osu! Level " + toString(int(viewUser.Accounts.Osu.Level)) + " | Accuracy: " + fmt.Sprintf("%.1f", viewUser.Accounts.Osu.Accuracy) + "%")
Icon("trophy") Icon("trophy")
a(href="https://osu.ppy.sh/u/" + viewUser.Accounts.Osu.Nick, target="_blank", rel="noopener")= toString(int(viewUser.Accounts.Osu.PP)) + " pp" a(href="https://osu.ppy.sh/u/" + viewUser.Accounts.Osu.Nick, target="_blank", rel="noopener")= toString(int(viewUser.Accounts.Osu.PP)) + " pp"

View File

@ -8,9 +8,42 @@ import (
"github.com/animenotifier/notify.moe/components" "github.com/animenotifier/notify.moe/components"
) )
// Get ... // Active ...
func Get(ctx *aero.Context) string { func Active(ctx *aero.Context) string {
users, err := arn.GetActiveUsersCached() users, err := arn.GetListOfUsersCached("active users")
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not fetch user data", err)
}
return ctx.HTML(components.Users(users))
}
// Osu ...
func Osu(ctx *aero.Context) string {
users, err := arn.GetListOfUsersCached("active osu users")
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not fetch user data", err)
}
return ctx.HTML(components.Users(users))
}
// Staff ...
func Staff(ctx *aero.Context) string {
users, err := arn.GetListOfUsersCached("active staff users")
if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not fetch user data", err)
}
return ctx.HTML(components.Users(users))
}
// AnimeWatching ...
func AnimeWatching(ctx *aero.Context) string {
users, err := arn.GetListOfUsersCached("active anime watching users")
if err != nil { if err != nil {
return ctx.Error(http.StatusInternalServerError, "Could not fetch user data", err) return ctx.Error(http.StatusInternalServerError, "Could not fetch user data", err)

View File

@ -1,6 +1,23 @@
component Users(users []*arn.User) component Users(users []*arn.User)
h1.page-title Users h1.page-title Users
.buttons.tabs
a.button.tab.action(href="/users", data-action="diff", data-trigger="click")
Icon("users")
span Active
a.button.tab.action(href="/users/anime/watching", data-action="diff", data-trigger="click")
Icon("tv")
span Watching
a.button.tab.action(href="/users/osu", data-action="diff", data-trigger="click")
Icon("gamepad")
span Osu
a.button.tab.action(href="/users/staff", data-action="diff", data-trigger="click")
Icon("user-secret")
span Staff
.user-avatars .user-avatars
each user in users each user in users
Avatar(user) Avatar(user)