From e57e67610fe7b8a96d24ec8c16a0125649d89639 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 19 Jul 2017 13:08:12 +0200 Subject: [PATCH] Added user lists --- jobs/active-users/active-users.go | 84 +++++++++++++++++++++++++++---- main.go | 5 +- pages/profile/profile.pixy | 2 +- pages/users/users.go | 39 ++++++++++++-- pages/users/users.pixy | 17 +++++++ 5 files changed, 131 insertions(+), 16 deletions(-) diff --git a/jobs/active-users/active-users.go b/jobs/active-users/active-users.go index b804ec03..4719f262 100644 --- a/jobs/active-users/active-users.go +++ b/jobs/active-users/active-users.go @@ -11,16 +11,13 @@ import ( func main() { color.Yellow("Caching list of active users") - cache := arn.ListOfIDs{} - // Filter out active users with an avatar users, err := arn.FilterUsers(func(user *arn.User) bool { return user.IsActive() && user.Avatar.Extension != "" }) + fmt.Println(len(users)) - if err != nil { - panic(err) - } + arn.PanicOnError(err) // Sort sort.Slice(users, func(i, j int) bool { @@ -36,17 +33,82 @@ func main() { }) // Add users to list - for _, user := range users { - cache.IDList = append(cache.IDList, user.ID) + SaveInCache("active users", users) + + // 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 { - panic(err) + sort.Slice(staff, func(i, j int) bool { + 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.") } + +// 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 +} diff --git a/main.go b/main.go index 52f7afc4..87bd3aa3 100644 --- a/main.go +++ b/main.go @@ -84,7 +84,10 @@ func configure(app *aero.Application) *aero.Application { app.Ajax("/new/soundtrack", newsoundtrack.Get) app.Ajax("/settings", settings.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) // User profiles diff --git a/pages/profile/profile.pixy b/pages/profile/profile.pixy index 7f9672e8..ce329bf3 100644 --- a/pages/profile/profile.pixy +++ b/pages/profile/profile.pixy @@ -22,7 +22,7 @@ component ProfileHeader(viewUser *arn.User, user *arn.User, uri string) Icon("home") 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) + "%") Icon("trophy") a(href="https://osu.ppy.sh/u/" + viewUser.Accounts.Osu.Nick, target="_blank", rel="noopener")= toString(int(viewUser.Accounts.Osu.PP)) + " pp" diff --git a/pages/users/users.go b/pages/users/users.go index a81e9f39..09a3b989 100644 --- a/pages/users/users.go +++ b/pages/users/users.go @@ -8,9 +8,42 @@ import ( "github.com/animenotifier/notify.moe/components" ) -// Get ... -func Get(ctx *aero.Context) string { - users, err := arn.GetActiveUsersCached() +// Active ... +func Active(ctx *aero.Context) string { + 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 { return ctx.Error(http.StatusInternalServerError, "Could not fetch user data", err) diff --git a/pages/users/users.pixy b/pages/users/users.pixy index 6a25e9cb..a1f4b0c2 100644 --- a/pages/users/users.pixy +++ b/pages/users/users.pixy @@ -1,6 +1,23 @@ component Users(users []*arn.User) 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 each user in users Avatar(user) \ No newline at end of file