69 lines
1.5 KiB
Go
Raw Normal View History

2018-03-09 18:04:06 +00:00
package editlog
import (
"net/http"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2018-03-09 18:04:06 +00:00
"github.com/animenotifier/notify.moe/components"
2018-04-09 11:44:02 +00:00
"github.com/animenotifier/notify.moe/utils/infinitescroll"
2018-03-09 18:04:06 +00:00
"github.com/aerogo/aero"
)
2018-04-09 11:44:02 +00:00
const (
entriesFirstLoad = 120
entriesPerScroll = 40
2018-04-09 11:44:02 +00:00
)
2018-03-09 18:04:06 +00:00
// Get edit log.
2019-06-01 04:55:49 +00:00
func Get(ctx aero.Context) error {
2019-11-17 07:59:34 +00:00
user := arn.GetUserFromContext(ctx)
2018-04-09 11:44:02 +00:00
index, _ := ctx.GetInt("index")
nick := ctx.Get("nick")
2018-03-09 18:04:06 +00:00
if user == nil || (user.Role != "editor" && user.Role != "admin") {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusUnauthorized, "Not authorized")
2018-03-09 18:04:06 +00:00
}
2018-04-09 11:44:02 +00:00
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
})
2018-03-09 18:04:06 +00:00
// Sort by creation date
2018-04-09 11:44:02 +00:00
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)
2018-03-09 18:04:06 +00:00
2018-04-09 11:44:02 +00:00
// In case we're scrolling, send log entries only (without the page frame)
if index > 0 {
return ctx.HTML(components.EditLogScrollable(entries, user))
2018-03-09 18:04:06 +00:00
}
2018-04-09 11:44:02 +00:00
// Otherwise, send the full page
return ctx.HTML(components.EditLogPage(entries, nextIndex, viewUser, user))
2018-03-09 18:04:06 +00:00
}