Improved editor logs

This commit is contained in:
2018-04-09 13:44:02 +02:00
parent 66bd51f603
commit 719c04f3d5
5 changed files with 120 additions and 54 deletions

View File

@ -6,30 +6,64 @@ import (
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils/infinitescroll"
"github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/utils"
)
const maxEntries = 120
const (
entriesFirstLoad = 120
entriesPerScroll = 30
)
// Get edit log.
func Get(ctx *aero.Context) string {
user := utils.GetUser(ctx)
index, _ := ctx.GetInt("index")
nick := ctx.Get("nick")
if user == nil || (user.Role != "editor" && user.Role != "admin") {
return ctx.Error(http.StatusUnauthorized, "Not authorized", nil)
}
entries := arn.AllEditLogEntries()
viewUser, err := arn.GetUserByNick(nick)
// Sort by creation date
arn.SortEditLogEntriesLatestFirst(entries)
// Limit results
if len(entries) > maxEntries {
entries = entries[:maxEntries]
if nick != "" && err != nil {
return ctx.Error(http.StatusNotFound, "User not found", err)
}
return ctx.HTML(components.EditLogPage(entries, user))
allEntries := arn.FilterEditLogEntries(func(entry *arn.EditLogEntry) bool {
if viewUser != nil {
return entry.UserID == viewUser.ID
}
return true
})
// Sort by creation date
arn.SortEditLogEntriesLatestFirst(allEntries)
// Slice the part that we need
entries := allEntries[index:]
maxLength := entriesFirstLoad
if index > 0 {
maxLength = entriesPerScroll
}
if len(entries) > maxLength {
entries = entries[:maxLength]
}
// Next index
nextIndex := infinitescroll.NextIndex(ctx, len(allEntries), maxLength, index)
// In case we're scrolling, send log entries only (without the page frame)
if index > 0 {
return ctx.HTML(components.EditLogScrollable(entries, user))
}
// Otherwise, send the full page
return ctx.HTML(components.EditLogPage(entries, nextIndex, viewUser, user))
}