From 802273b7c35e015abc5e3163866af1b6d431d024 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 16 Apr 2018 15:17:23 +0200 Subject: [PATCH] Added user registrations page --- pages/admin/admin.pixy | 1 + pages/admin/registrations.go | 56 ++++++++++++++++++++++++++++++++++ pages/admin/registrations.pixy | 11 +++++++ pages/editor/editor.pixy | 3 ++ pages/index.go | 1 + utils/YearRegistrations.go | 7 +++++ 6 files changed, 79 insertions(+) create mode 100644 pages/admin/registrations.go create mode 100644 pages/admin/registrations.pixy create mode 100644 utils/YearRegistrations.go diff --git a/pages/admin/admin.pixy b/pages/admin/admin.pixy index f274e0b2..71bd9c4e 100644 --- a/pages/admin/admin.pixy +++ b/pages/admin/admin.pixy @@ -2,6 +2,7 @@ component AdminTabs .tabs Tab("Server", "server", "/admin") Tab("WebDev", "html5", "/admin/webdev") + Tab("Registrations", "user-plus", "/admin/registrations") Tab("Purchases", "shopping-cart", "/admin/purchases") .corner-buttons diff --git a/pages/admin/registrations.go b/pages/admin/registrations.go new file mode 100644 index 00000000..a18e1088 --- /dev/null +++ b/pages/admin/registrations.go @@ -0,0 +1,56 @@ +package admin + +import ( + "net/http" + "sort" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" + "github.com/animenotifier/notify.moe/components" + "github.com/animenotifier/notify.moe/utils" + "github.com/fatih/color" +) + +// UserRegistrations ... +func UserRegistrations(ctx *aero.Context) string { + user := utils.GetUser(ctx) + + if user == nil { + return ctx.Error(http.StatusUnauthorized, "Not logged in", nil) + } + + if user.Role != "admin" { + return ctx.Error(http.StatusUnauthorized, "Not authorized", nil) + } + + yearInfo := map[int]utils.YearRegistrations{} + years := []int{} + + for user := range arn.StreamUsers() { + if user.Registered == "" { + color.Red("%s %s", user.ID, user.Nick) + user.Registered = user.LastLogin + user.Save() + } + + registered := user.RegisteredTime() + year := registered.Year() + yearRegistrations := yearInfo[year] + yearRegistrations.Total++ + + if yearRegistrations.Months == nil { + yearRegistrations.Months = map[int]int{} + } + + yearRegistrations.Months[int(registered.Month())]++ + yearInfo[year] = yearRegistrations + } + + for year := range yearInfo { + years = append(years, year) + } + + sort.Ints(years) + + return ctx.HTML(components.UserRegistrations(years, yearInfo)) +} diff --git a/pages/admin/registrations.pixy b/pages/admin/registrations.pixy new file mode 100644 index 00000000..71541666 --- /dev/null +++ b/pages/admin/registrations.pixy @@ -0,0 +1,11 @@ +component UserRegistrations(years []int, yearInfo map[int]utils.YearRegistrations) + AdminTabs + h1.page-title User registrations + + for _, year := range years + h3.mountable= year + h4.mountable= strconv.Itoa(yearInfo[year].Total) + " registrations" + for month := 1; month <= 12; month++ + p.mountable + strong= time.Month(month).String() + ": " + span= strconv.Itoa(yearInfo[year].Months[month]) \ No newline at end of file diff --git a/pages/editor/editor.pixy b/pages/editor/editor.pixy index 2c39204c..544ebfde 100644 --- a/pages/editor/editor.pixy +++ b/pages/editor/editor.pixy @@ -27,6 +27,9 @@ component Editor(url string, score int, scoreTitle string, scoreTypes map[string a.footer-element(href="/editor/mal/diff/anime" + user.Settings().Editor.Filter.Suffix()) MALdiff a.footer-element(href="/editor/kitsu/new/anime") Kitsu + if user.Role == "admin" + a.footer-element(href="/admin") Admin + component EditorTabs(url string, user *arn.User) .corner-buttons-left a.button(href="/editor") diff --git a/pages/index.go b/pages/index.go index 2962bcb9..cf10f90f 100644 --- a/pages/index.go +++ b/pages/index.go @@ -270,6 +270,7 @@ func Configure(app *aero.Application) { // Admin l.Page("/admin", admin.Get) l.Page("/admin/webdev", admin.WebDev) + l.Page("/admin/registrations", admin.UserRegistrations) l.Page("/admin/purchases", admin.PurchaseHistory) // Editor diff --git a/utils/YearRegistrations.go b/utils/YearRegistrations.go new file mode 100644 index 00000000..3d6f2879 --- /dev/null +++ b/utils/YearRegistrations.go @@ -0,0 +1,7 @@ +package utils + +// YearRegistrations describes how many user registrations happened in a year. +type YearRegistrations struct { + Total int + Months map[int]int +}