diff --git a/pages/editlog/editlog.go b/pages/editlog/editlog.go index 77139487..a8640f8e 100644 --- a/pages/editlog/editlog.go +++ b/pages/editlog/editlog.go @@ -1,13 +1,6 @@ package editlog import ( - "net/http" - - "github.com/animenotifier/notify.moe/arn" - - "github.com/animenotifier/notify.moe/components" - "github.com/animenotifier/notify.moe/utils/infinitescroll" - "github.com/aerogo/aero" ) @@ -16,71 +9,12 @@ const ( entriesPerScroll = 40 ) -// All edit log. -func All(ctx aero.Context) error { - user := arn.GetUserFromContext(ctx) - index, _ := ctx.GetInt("index") - nick := ctx.Get("nick") - compact := ctx.Query("compact") == "true" - - if user == nil || (user.Role != "editor" && user.Role != "admin") { - return ctx.Error(http.StatusUnauthorized, "Not authorized") - } - - viewUser, err := arn.GetUserByNick(nick) - - if nick != "" && err != nil { - return ctx.Error(http.StatusNotFound, "User not found", err) - } - - 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) - - // Show only one entry for a single object in compact mode - if compact { - filteredEntries := make([]*arn.EditLogEntry, 0, len(allEntries)) - lastObjectID := "" - - for _, entry := range allEntries { - if lastObjectID == entry.ObjectID { - continue - } - - filteredEntries = append(filteredEntries, entry) - lastObjectID = entry.ObjectID - } - - allEntries = filteredEntries - } - - // 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)) +// Full edit log. +func Full(ctx aero.Context) error { + return render(ctx, false) +} + +// Compact edit log. +func Compact(ctx aero.Context) error { + return render(ctx, true) } diff --git a/pages/editlog/editlog.pixy b/pages/editlog/editlog.pixy index c02a0f9c..2464fd18 100644 --- a/pages/editlog/editlog.pixy +++ b/pages/editlog/editlog.pixy @@ -4,12 +4,13 @@ component EditLogPage(entries []*arn.EditLogEntry, nextIndex int, viewUser *arn. else h1.mountable Editor log - .corner-buttons - a.button(href="/log", title="Full list") - RawIcon("list") - - a.button(href="/log?compact=true", title="Compact list") - RawIcon("list-alt") + if viewUser == nil + .corner-buttons + a.button(href="/log", title="Full list") + RawIcon("list") + + a.button(href="/log/compact", title="Compact list") + RawIcon("list-alt") EditLog(entries, user) diff --git a/pages/editlog/render.go b/pages/editlog/render.go new file mode 100644 index 00000000..8917ed77 --- /dev/null +++ b/pages/editlog/render.go @@ -0,0 +1,78 @@ +package editlog + +import ( + "net/http" + + "github.com/aerogo/aero" + "github.com/animenotifier/notify.moe/arn" + "github.com/animenotifier/notify.moe/components" + "github.com/animenotifier/notify.moe/utils/infinitescroll" +) + +// render edit log. +func render(ctx aero.Context, compact bool) error { + user := arn.GetUserFromContext(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") + } + + viewUser, err := arn.GetUserByNick(nick) + + if nick != "" && err != nil { + return ctx.Error(http.StatusNotFound, "User not found", err) + } + + 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) + + // Show only one entry for a single object in compact mode + if compact { + filteredEntries := make([]*arn.EditLogEntry, 0, len(allEntries)) + lastObjectID := "" + + for _, entry := range allEntries { + if lastObjectID == entry.ObjectID { + continue + } + + filteredEntries = append(filteredEntries, entry) + lastObjectID = entry.ObjectID + } + + allEntries = filteredEntries + } + + // 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)) +} diff --git a/pages/index/staffroutes/staffroutes.go b/pages/index/staffroutes/staffroutes.go index 1cd5fe9c..f6819fb3 100644 --- a/pages/index/staffroutes/staffroutes.go +++ b/pages/index/staffroutes/staffroutes.go @@ -68,10 +68,14 @@ func Register(app *aero.Application) { page.Get(app, "/editor/jobs", jobs.Overview) // Log - page.Get(app, "/log", editlog.All) - page.Get(app, "/log/from/:index", editlog.All) - page.Get(app, "/user/:nick/log", editlog.All) - page.Get(app, "/user/:nick/log/from/:index", editlog.All) + page.Get(app, "/log", editlog.Full) + page.Get(app, "/log/from/:index", editlog.Full) + page.Get(app, "/log/compact", editlog.Compact) + page.Get(app, "/log/compact/from/:index", editlog.Compact) + page.Get(app, "/user/:nick/log", editlog.Full) + page.Get(app, "/user/:nick/log/from/:index", editlog.Full) + page.Get(app, "/user/:nick/log/compact", editlog.Compact) + page.Get(app, "/user/:nick/log/compact/from/:index", editlog.Compact) // Admin page.Get(app, "/admin", admin.Get)