Improved compact mode log

This commit is contained in:
Eduard Urbach 2020-02-10 17:58:35 +09:00
parent 70849553ef
commit 3cf7697184
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
4 changed files with 101 additions and 84 deletions

View File

@ -1,13 +1,6 @@
package editlog package editlog
import ( 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" "github.com/aerogo/aero"
) )
@ -16,71 +9,12 @@ const (
entriesPerScroll = 40 entriesPerScroll = 40
) )
// All edit log. // Full edit log.
func All(ctx aero.Context) error { func Full(ctx aero.Context) error {
user := arn.GetUserFromContext(ctx) return render(ctx, false)
index, _ := ctx.GetInt("index") }
nick := ctx.Get("nick")
compact := ctx.Query("compact") == "true" // Compact edit log.
func Compact(ctx aero.Context) error {
if user == nil || (user.Role != "editor" && user.Role != "admin") { return render(ctx, true)
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))
} }

View File

@ -4,12 +4,13 @@ component EditLogPage(entries []*arn.EditLogEntry, nextIndex int, viewUser *arn.
else else
h1.mountable Editor log h1.mountable Editor log
.corner-buttons if viewUser == nil
a.button(href="/log", title="Full list") .corner-buttons
RawIcon("list") a.button(href="/log", title="Full list")
RawIcon("list")
a.button(href="/log?compact=true", title="Compact list")
RawIcon("list-alt") a.button(href="/log/compact", title="Compact list")
RawIcon("list-alt")
EditLog(entries, user) EditLog(entries, user)

78
pages/editlog/render.go Normal file
View File

@ -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))
}

View File

@ -68,10 +68,14 @@ func Register(app *aero.Application) {
page.Get(app, "/editor/jobs", jobs.Overview) page.Get(app, "/editor/jobs", jobs.Overview)
// Log // Log
page.Get(app, "/log", editlog.All) page.Get(app, "/log", editlog.Full)
page.Get(app, "/log/from/:index", editlog.All) page.Get(app, "/log/from/:index", editlog.Full)
page.Get(app, "/user/:nick/log", editlog.All) page.Get(app, "/log/compact", editlog.Compact)
page.Get(app, "/user/:nick/log/from/:index", editlog.All) 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 // Admin
page.Get(app, "/admin", admin.Get) page.Get(app, "/admin", admin.Get)