2018-05-27 09:36:35 +00:00
|
|
|
package panel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2019-04-18 01:00:39 +00:00
|
|
|
"github.com/Azareal/Gosora/common/phrases"
|
2018-05-27 09:36:35 +00:00
|
|
|
)
|
|
|
|
|
2018-08-30 05:53:21 +00:00
|
|
|
// TODO: Link the usernames for successful registrations to the profiles
|
2019-04-19 07:25:49 +00:00
|
|
|
func LogsRegs(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError {
|
2018-06-17 07:28:18 +00:00
|
|
|
basePage, ferr := buildBasePage(w, r, &user, "registration_logs", "logs")
|
2018-05-27 09:36:35 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
logCount := c.RegLogs.Count()
|
2018-05-27 09:36:35 +00:00
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
2019-06-04 11:51:15 +00:00
|
|
|
perPage := 12
|
2019-04-19 07:25:49 +00:00
|
|
|
offset, page, lastPage := c.PageOffset(logCount, page, perPage)
|
2018-05-27 09:36:35 +00:00
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
logs, err := c.RegLogs.GetOffset(offset, perPage)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
2019-04-19 07:25:49 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-04-19 07:25:49 +00:00
|
|
|
var llist = make([]c.PageRegLogItem, len(logs))
|
2018-05-27 09:36:35 +00:00
|
|
|
for index, log := range logs {
|
2019-04-19 07:25:49 +00:00
|
|
|
llist[index] = c.PageRegLogItem{log, strings.Replace(strings.TrimSuffix(log.FailureReason, "|"), "|", " | ", -1)}
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 05:48:12 +00:00
|
|
|
pageList := c.Paginate(page, lastPage, 5)
|
2019-04-19 07:25:49 +00:00
|
|
|
pi := c.PanelRegLogsPage{basePage, llist, c.Paginator{pageList, page, lastPage}}
|
2019-05-03 08:34:18 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage,"","","panel_reglogs", pi})
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Log errors when something really screwy is going on?
|
2019-04-18 01:00:39 +00:00
|
|
|
// TODO: Base the slugs on the localised usernames?
|
2019-04-19 07:25:49 +00:00
|
|
|
func handleUnknownUser(user *c.User, err error) *c.User {
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
2019-04-19 07:25:49 +00:00
|
|
|
return &c.User{Name: phrases.GetTmplPhrase("user_unknown"), Link: c.BuildProfileURL("unknown", 0)}
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
2019-04-19 07:25:49 +00:00
|
|
|
func handleUnknownTopic(topic *c.Topic, err error) *c.Topic {
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
2019-04-19 07:25:49 +00:00
|
|
|
return &c.Topic{Title: phrases.GetTmplPhrase("topic_unknown"), Link: c.BuildTopicURL("unknown", 0)}
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
return topic
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Move the log building logic into /common/ and it's own abstraction
|
2019-04-19 07:25:49 +00:00
|
|
|
func topicElementTypeAction(action string, elementType string, elementID int, actor *c.User, topic *c.Topic) (out string) {
|
2018-05-27 09:36:35 +00:00
|
|
|
if action == "delete" {
|
2019-04-18 01:00:39 +00:00
|
|
|
return phrases.GetTmplPhrasef("panel_logs_moderation_action_topic_delete", elementID, actor.Link, actor.Name)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-04-18 01:00:39 +00:00
|
|
|
var tbit string
|
2019-04-17 10:12:35 +00:00
|
|
|
aarr := strings.Split(action, "-")
|
|
|
|
switch aarr[0] {
|
2019-04-18 01:00:39 +00:00
|
|
|
case "lock","unlock","stick","unstick":
|
|
|
|
tbit = aarr[0]
|
2018-05-27 09:36:35 +00:00
|
|
|
case "move":
|
2019-04-17 10:12:35 +00:00
|
|
|
if len(aarr) == 2 {
|
|
|
|
fid, _ := strconv.Atoi(aarr[1])
|
2019-04-19 07:25:49 +00:00
|
|
|
forum, err := c.Forums.Get(fid)
|
2019-04-17 10:12:35 +00:00
|
|
|
if err == nil {
|
2019-04-18 01:00:39 +00:00
|
|
|
return phrases.GetTmplPhrasef("panel_logs_moderation_action_topic_move_dest", topic.Link, topic.Title, forum.Link, forum.Name, actor.Link, actor.Name)
|
2019-04-17 10:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-18 01:00:39 +00:00
|
|
|
tbit = "move"
|
2018-05-27 09:36:35 +00:00
|
|
|
default:
|
2019-04-18 01:00:39 +00:00
|
|
|
return phrases.GetTmplPhrasef("panel_logs_moderation_action_topic_unknown", action, elementType, actor.Link, actor.Name)
|
|
|
|
}
|
|
|
|
if tbit != "" {
|
|
|
|
return phrases.GetTmplPhrasef("panel_logs_moderation_action_topic_"+tbit, topic.Link, topic.Title, actor.Link, actor.Name)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
return fmt.Sprintf(out, topic.Link, topic.Title, actor.Link, actor.Name)
|
|
|
|
}
|
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
func modlogsElementType(action string, elementType string, elementID int, actor *c.User) (out string) {
|
2018-05-27 09:36:35 +00:00
|
|
|
switch elementType {
|
|
|
|
case "topic":
|
2019-04-19 07:25:49 +00:00
|
|
|
topic := handleUnknownTopic(c.Topics.Get(elementID))
|
2018-05-27 09:36:35 +00:00
|
|
|
out = topicElementTypeAction(action, elementType, elementID, actor, topic)
|
|
|
|
case "user":
|
2019-04-19 07:25:49 +00:00
|
|
|
targetUser := handleUnknownUser(c.Users.Get(elementID))
|
2019-04-18 01:00:39 +00:00
|
|
|
out = phrases.GetTmplPhrasef("panel_logs_moderation_action_user_"+action, targetUser.Link, targetUser.Name, actor.Link, actor.Name)
|
2018-05-27 09:36:35 +00:00
|
|
|
case "reply":
|
|
|
|
if action == "delete" {
|
2019-04-19 07:25:49 +00:00
|
|
|
topic := handleUnknownTopic(c.TopicByReplyID(elementID))
|
2019-04-18 01:00:39 +00:00
|
|
|
out = phrases.GetTmplPhrasef("panel_logs_moderation_action_reply_delete", topic.Link, topic.Title, actor.Link, actor.Name)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if out == "" {
|
2019-04-18 01:00:39 +00:00
|
|
|
out = phrases.GetTmplPhrasef("panel_logs_moderation_action_unknown", action, elementType, actor.Link, actor.Name)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
func LogsMod(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError {
|
2018-06-17 07:28:18 +00:00
|
|
|
basePage, ferr := buildBasePage(w, r, &user, "mod_logs", "logs")
|
2018-05-27 09:36:35 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
logCount := c.ModLogs.Count()
|
2018-05-27 09:36:35 +00:00
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
2019-06-04 11:51:15 +00:00
|
|
|
perPage := 12
|
2019-04-19 07:25:49 +00:00
|
|
|
offset, page, lastPage := c.PageOffset(logCount, page, perPage)
|
2018-05-27 09:36:35 +00:00
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
logs, err := c.ModLogs.GetOffset(offset, perPage)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
2019-04-19 07:25:49 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-04-19 07:25:49 +00:00
|
|
|
var llist = make([]c.PageLogItem, len(logs))
|
2018-05-27 09:36:35 +00:00
|
|
|
for index, log := range logs {
|
2019-04-19 07:25:49 +00:00
|
|
|
actor := handleUnknownUser(c.Users.Get(log.ActorID))
|
2018-05-27 09:36:35 +00:00
|
|
|
action := modlogsElementType(log.Action, log.ElementType, log.ElementID, actor)
|
2019-04-19 07:25:49 +00:00
|
|
|
llist[index] = c.PageLogItem{Action: template.HTML(action), IPAddress: log.IPAddress, DoneAt: log.DoneAt}
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 05:48:12 +00:00
|
|
|
pageList := c.Paginate(page, lastPage, 5)
|
2019-04-19 07:25:49 +00:00
|
|
|
pi := c.PanelLogsPage{basePage, llist, c.Paginator{pageList, page, lastPage}}
|
2019-05-03 08:34:18 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage,"","","panel_modlogs", pi})
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
func LogsAdmin(w http.ResponseWriter, r *http.Request, user c.User) c.RouteError {
|
2018-06-17 07:28:18 +00:00
|
|
|
basePage, ferr := buildBasePage(w, r, &user, "admin_logs", "logs")
|
2018-05-27 09:36:35 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
|
2019-06-01 12:31:48 +00:00
|
|
|
logCount := c.ModLogs.Count()
|
2018-05-27 09:36:35 +00:00
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
2019-06-04 11:51:15 +00:00
|
|
|
perPage := 12
|
2019-04-19 07:25:49 +00:00
|
|
|
offset, page, lastPage := c.PageOffset(logCount, page, perPage)
|
2018-05-27 09:36:35 +00:00
|
|
|
|
2019-04-19 07:25:49 +00:00
|
|
|
logs, err := c.AdminLogs.GetOffset(offset, perPage)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
2019-04-19 07:25:49 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
2019-04-19 07:25:49 +00:00
|
|
|
var llist = make([]c.PageLogItem, len(logs))
|
2018-05-27 09:36:35 +00:00
|
|
|
for index, log := range logs {
|
2019-04-19 07:25:49 +00:00
|
|
|
actor := handleUnknownUser(c.Users.Get(log.ActorID))
|
2018-05-27 09:36:35 +00:00
|
|
|
action := modlogsElementType(log.Action, log.ElementType, log.ElementID, actor)
|
2019-04-19 07:25:49 +00:00
|
|
|
llist[index] = c.PageLogItem{Action: template.HTML(action), IPAddress: log.IPAddress, DoneAt: log.DoneAt}
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 05:48:12 +00:00
|
|
|
pageList := c.Paginate(page, lastPage, 5)
|
2019-04-19 07:25:49 +00:00
|
|
|
pi := c.PanelLogsPage{basePage, llist, c.Paginator{pageList, page, lastPage}}
|
2019-05-03 08:34:18 +00:00
|
|
|
return renderTemplate("panel", w, r, basePage.Header, c.Panel{basePage,"","","panel_adminlogs", pi})
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|