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/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)
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(entries)
arn.SortEditLogEntriesLatestFirst(allEntries)
// Limit results
if len(entries) > maxEntries {
entries = entries[:maxEntries]
// Slice the part that we need
entries := allEntries[index:]
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
EditLog(entries, user)
if nextIndex != -1
.buttons
LoadMore(nextIndex)
component EditLog(entries []*arn.EditLogEntry, user *arn.User)
table.edit-log
thead
@ -13,7 +21,10 @@ component EditLog(entries []*arn.EditLogEntry, user *arn.User)
th Old
th New
th Date
tbody
tbody#load-more-target
EditLogScrollable(entries, user)
component EditLogScrollable(entries []*arn.EditLogEntry, user *arn.User)
each entry in entries
tr.mountable
td

View File

@ -305,6 +305,9 @@ func Configure(app *aero.Application) {
// Log
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
// l.Page("/database", database.Get)

View File

@ -126,3 +126,8 @@ component ProfileHead(viewUser *arn.User, user *arn.User, uri string)
Icon("archive")
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 {
container: HTMLElement
threshold: number
@ -6,10 +8,21 @@ export default class InfiniteScroller {
this.container = container
this.threshold = threshold
this.container.addEventListener("scroll", e => {
let check = () => {
if(this.container.scrollTop + this.container.clientHeight >= this.container.scrollHeight - threshold) {
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()
})
}