60 lines
1.2 KiB
Go
Raw Normal View History

2018-04-16 13:17:23 +00:00
package admin
import (
"net/http"
"sort"
"github.com/aerogo/aero"
2019-04-23 05:52:55 +00:00
"github.com/akyoto/color"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2018-04-16 13:17:23 +00:00
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
// UserRegistrations ...
2019-06-01 04:55:49 +00:00
func UserRegistrations(ctx aero.Context) error {
2019-11-17 07:59:34 +00:00
user := arn.GetUserFromContext(ctx)
2018-04-16 13:17:23 +00:00
if user == nil {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusUnauthorized, "Not logged in")
2018-04-16 13:17:23 +00:00
}
if user.Role != "admin" {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusUnauthorized, "Not authorized")
2018-04-16 13:17:23 +00:00
}
2018-04-16 13:21:42 +00:00
total := 0
2018-04-16 13:17:23 +00:00
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
2018-04-16 13:21:42 +00:00
total++
2018-04-16 13:17:23 +00:00
}
for year := range yearInfo {
years = append(years, year)
}
sort.Ints(years)
2018-04-16 13:21:42 +00:00
return ctx.HTML(components.UserRegistrations(total, years, yearInfo))
2018-04-16 13:17:23 +00:00
}