Improved editor logs

This commit is contained in:
Eduard Urbach 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/arn"
"github.com/animenotifier/notify.moe/components" "github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils/infinitescroll"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/notify.moe/utils" "github.com/animenotifier/notify.moe/utils"
) )
const maxEntries = 120 const (
entriesFirstLoad = 120
entriesPerScroll = 30
)
// Get edit log. // Get edit log.
func Get(ctx *aero.Context) string { func Get(ctx *aero.Context) string {
user := utils.GetUser(ctx) user := utils.GetUser(ctx)
index, _ := ctx.GetInt("index")
nick := ctx.Get("nick")
if user == nil || (user.Role != "editor" && user.Role != "admin") { if user == nil || (user.Role != "editor" && user.Role != "admin") {
return ctx.Error(http.StatusUnauthorized, "Not authorized", nil) return ctx.Error(http.StatusUnauthorized, "Not authorized", nil)
} }
entries := arn.AllEditLogEntries() 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 // Sort by creation date
arn.SortEditLogEntriesLatestFirst(entries) arn.SortEditLogEntriesLatestFirst(allEntries)
// Limit results // Slice the part that we need
if len(entries) > maxEntries { entries := allEntries[index:]
entries = entries[:maxEntries] maxLength := entriesFirstLoad
if index > 0 {
maxLength = entriesPerScroll
} }
return ctx.HTML(components.EditLogPage(entries, user)) 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

@ -1,7 +1,15 @@
component EditLogPage(entries []*arn.EditLogEntry, user *arn.User) component EditLogPage(entries []*arn.EditLogEntry, nextIndex int, viewUser *arn.User, user *arn.User)
if viewUser != nil
h1.mountable= "Editor log: " + viewUser.Nick
else
h1.mountable Editor log h1.mountable Editor log
EditLog(entries, user) EditLog(entries, user)
if nextIndex != -1
.buttons
LoadMore(nextIndex)
component EditLog(entries []*arn.EditLogEntry, user *arn.User) component EditLog(entries []*arn.EditLogEntry, user *arn.User)
table.edit-log table.edit-log
thead thead
@ -13,7 +21,10 @@ component EditLog(entries []*arn.EditLogEntry, user *arn.User)
th Old th Old
th New th New
th Date th Date
tbody tbody#load-more-target
EditLogScrollable(entries, user)
component EditLogScrollable(entries []*arn.EditLogEntry, user *arn.User)
each entry in entries each entry in entries
tr.mountable tr.mountable
td td

View File

@ -305,6 +305,9 @@ func Configure(app *aero.Application) {
// Log // Log
l.Page("/log", editlog.Get) l.Page("/log", editlog.Get)
l.Page("/log/from/:index", editlog.Get)
l.Page("/user/:nick/log", editlog.Get)
l.Page("/user/:nick/log/from/:index", editlog.Get)
// Mixed // Mixed
// l.Page("/database", database.Get) // l.Page("/database", database.Get)

View File

@ -126,3 +126,8 @@ component ProfileHead(viewUser *arn.User, user *arn.User, uri string)
Icon("archive") Icon("archive")
span Recomms span Recomms
if user != nil && (user.Role == "editor" || user.Role == "admin")
a.button.profile-action(href="/+" + viewUser.Nick + "/log")
Icon("edit")
span Log

View File

@ -1,3 +1,5 @@
import Diff from "./Diff"
export default class InfiniteScroller { export default class InfiniteScroller {
container: HTMLElement container: HTMLElement
threshold: number threshold: number
@ -6,10 +8,21 @@ export default class InfiniteScroller {
this.container = container this.container = container
this.threshold = threshold this.threshold = threshold
this.container.addEventListener("scroll", e => { let check = () => {
if(this.container.scrollTop + this.container.clientHeight >= this.container.scrollHeight - threshold) { if(this.container.scrollTop + this.container.clientHeight >= this.container.scrollHeight - threshold) {
this.loadMore() this.loadMore()
} }
}
this.container.addEventListener("scroll", e => {
// Wait for mutations to finish before checking if we need infinite scroll to trigger.
if(Diff.mutations.mutations.length > 0) {
Diff.mutations.wait(() => check())
return
}
// Otherwise, check immediately.
check()
}) })
} }