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

View File

@ -1,7 +1,15 @@
component EditLogPage(entries []*arn.EditLogEntry, user *arn.User)
h1.mountable Editor log
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,48 +21,51 @@ component EditLog(entries []*arn.EditLogEntry, user *arn.User)
th Old
th New
th Date
tbody
each entry in entries
tr.mountable
td
.edit-log-icon(title=entry.Action)
if entry.Action == "create"
.edit-log-create
RawIcon("plus")
else if entry.Action == "delete"
.edit-log-delete
RawIcon("minus")
else if entry.Action == "edit" || entry.Action == "arrayAppend" || entry.Action == "arrayRemove"
.edit-log-change
RawIcon("pencil")
if entry.Action == "arrayAppend"
.edit-log-sub-icon.edit-log-add
RawIcon("plus-square")
else if entry.Action == "arrayRemove"
.edit-log-sub-icon.edit-log-remove
RawIcon("minus-square")
else
if entry.OldValue == "" && entry.NewValue != ""
.edit-log-sub-icon.edit-log-add
RawIcon("plus-circle")
else if entry.OldValue != "" && entry.NewValue == ""
.edit-log-sub-icon.edit-log-remove
RawIcon("minus-circle")
tbody#load-more-target
EditLogScrollable(entries, user)
td
.edit-log-user
Avatar(entry.User())
td.edit-log-object
if strings.HasPrefix(arn.GetObjectTitle(entry.ObjectType, entry.ObjectID), "<not found:")
span= arn.GetObjectTitle(entry.ObjectType, entry.ObjectID)
component EditLogScrollable(entries []*arn.EditLogEntry, user *arn.User)
each entry in entries
tr.mountable
td
.edit-log-icon(title=entry.Action)
if entry.Action == "create"
.edit-log-create
RawIcon("plus")
else if entry.Action == "delete"
.edit-log-delete
RawIcon("minus")
else if entry.Action == "edit" || entry.Action == "arrayAppend" || entry.Action == "arrayRemove"
.edit-log-change
RawIcon("pencil")
if entry.Action == "arrayAppend"
.edit-log-sub-icon.edit-log-add
RawIcon("plus-square")
else if entry.Action == "arrayRemove"
.edit-log-sub-icon.edit-log-remove
RawIcon("minus-square")
else
a(href="/" + strings.ToLower(entry.ObjectType) + "/" + entry.ObjectID, target="_blank")= arn.GetObjectTitle(entry.ObjectType, entry.ObjectID)
td.edit-log-key
span= entry.ObjectType
if entry.OldValue == "" && entry.NewValue != ""
.edit-log-sub-icon.edit-log-add
RawIcon("plus-circle")
else if entry.OldValue != "" && entry.NewValue == ""
.edit-log-sub-icon.edit-log-remove
RawIcon("minus-circle")
if entry.Key != ""
span= "." + entry.Key
td.edit-log-value(title=entry.OldValue)= entry.OldValue
td.edit-log-value(title=entry.NewValue)= entry.NewValue
td.edit-log-date.utc-date(data-date=entry.Created)
td
.edit-log-user
Avatar(entry.User())
td.edit-log-object
if strings.HasPrefix(arn.GetObjectTitle(entry.ObjectType, entry.ObjectID), "<not found:")
span= arn.GetObjectTitle(entry.ObjectType, entry.ObjectID)
else
a(href="/" + strings.ToLower(entry.ObjectType) + "/" + entry.ObjectID, target="_blank")= arn.GetObjectTitle(entry.ObjectType, entry.ObjectID)
td.edit-log-key
span= entry.ObjectType
if entry.Key != ""
span= "." + entry.Key
td.edit-log-value(title=entry.OldValue)= entry.OldValue
td.edit-log-value(title=entry.NewValue)= entry.NewValue
td.edit-log-date.utc-date(data-date=entry.Created)

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

@ -125,4 +125,9 @@ component ProfileHead(viewUser *arn.User, user *arn.User, uri string)
a.button.profile-action(href="/+" + viewUser.Nick + "/recommended/anime")
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()
})
}