2018-05-27 09:36:35 +00:00
|
|
|
package panel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
"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
|
2018-05-27 09:36:35 +00:00
|
|
|
func LogsRegs(w http.ResponseWriter, r *http.Request, user common.User) common.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
|
|
|
|
}
|
|
|
|
|
|
|
|
logCount := common.RegLogs.GlobalCount()
|
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
|
|
|
perPage := 10
|
|
|
|
offset, page, lastPage := common.PageOffset(logCount, page, perPage)
|
|
|
|
|
|
|
|
logs, err := common.RegLogs.GetOffset(offset, perPage)
|
|
|
|
if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
var llist = make([]common.PageRegLogItem, len(logs))
|
|
|
|
for index, log := range logs {
|
|
|
|
llist[index] = common.PageRegLogItem{log, strings.Replace(strings.TrimSuffix(log.FailureReason, "|"), "|", " | ", -1)}
|
|
|
|
}
|
|
|
|
|
|
|
|
pageList := common.Paginate(logCount, perPage, 5)
|
2018-06-17 07:28:18 +00:00
|
|
|
pi := common.PanelRegLogsPage{basePage, llist, common.Paginator{pageList, page, lastPage}}
|
2019-02-10 05:52:26 +00:00
|
|
|
return renderTemplate("panel_reglogs", w, r, basePage.Header, &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?
|
2018-05-27 09:36:35 +00:00
|
|
|
func handleUnknownUser(user *common.User, err error) *common.User {
|
|
|
|
if err != nil {
|
2019-04-18 01:00:39 +00:00
|
|
|
return &common.User{Name: phrases.GetTmplPhrase("user_unknown"), Link: common.BuildProfileURL("unknown", 0)}
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
func handleUnknownTopic(topic *common.Topic, err error) *common.Topic {
|
|
|
|
if err != nil {
|
2019-04-19 01:02:33 +00:00
|
|
|
return &common.Topic{Title: phrases.GetTmplPhrase("topic_unknown"), Link: common.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
|
|
|
|
func topicElementTypeAction(action string, elementType string, elementID int, actor *common.User, topic *common.Topic) (out string) {
|
|
|
|
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])
|
|
|
|
forum, err := common.Forums.Get(fid)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func modlogsElementType(action string, elementType string, elementID int, actor *common.User) (out string) {
|
|
|
|
switch elementType {
|
|
|
|
case "topic":
|
|
|
|
topic := handleUnknownTopic(common.Topics.Get(elementID))
|
|
|
|
out = topicElementTypeAction(action, elementType, elementID, actor, topic)
|
|
|
|
case "user":
|
|
|
|
targetUser := handleUnknownUser(common.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" {
|
|
|
|
topic := handleUnknownTopic(common.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
|
|
|
|
}
|
|
|
|
|
|
|
|
func LogsMod(w http.ResponseWriter, r *http.Request, user common.User) common.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
|
|
|
|
}
|
|
|
|
|
|
|
|
logCount := common.ModLogs.GlobalCount()
|
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
|
|
|
perPage := 10
|
|
|
|
offset, page, lastPage := common.PageOffset(logCount, page, perPage)
|
|
|
|
|
|
|
|
logs, err := common.ModLogs.GetOffset(offset, perPage)
|
|
|
|
if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
var llist = make([]common.PageLogItem, len(logs))
|
|
|
|
for index, log := range logs {
|
|
|
|
actor := handleUnknownUser(common.Users.Get(log.ActorID))
|
|
|
|
action := modlogsElementType(log.Action, log.ElementType, log.ElementID, actor)
|
|
|
|
llist[index] = common.PageLogItem{Action: template.HTML(action), IPAddress: log.IPAddress, DoneAt: log.DoneAt}
|
|
|
|
}
|
|
|
|
|
|
|
|
pageList := common.Paginate(logCount, perPage, 5)
|
2018-06-17 07:28:18 +00:00
|
|
|
pi := common.PanelLogsPage{basePage, llist, common.Paginator{pageList, page, lastPage}}
|
2019-02-10 05:52:26 +00:00
|
|
|
return renderTemplate("panel_modlogs", w, r, basePage.Header, &pi)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func LogsAdmin(w http.ResponseWriter, r *http.Request, user common.User) common.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
|
|
|
|
}
|
|
|
|
|
|
|
|
logCount := common.ModLogs.GlobalCount()
|
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
|
|
|
perPage := 10
|
|
|
|
offset, page, lastPage := common.PageOffset(logCount, page, perPage)
|
|
|
|
|
|
|
|
logs, err := common.AdminLogs.GetOffset(offset, perPage)
|
|
|
|
if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
var llist = make([]common.PageLogItem, len(logs))
|
|
|
|
for index, log := range logs {
|
|
|
|
actor := handleUnknownUser(common.Users.Get(log.ActorID))
|
|
|
|
action := modlogsElementType(log.Action, log.ElementType, log.ElementID, actor)
|
|
|
|
llist[index] = common.PageLogItem{Action: template.HTML(action), IPAddress: log.IPAddress, DoneAt: log.DoneAt}
|
|
|
|
}
|
|
|
|
|
|
|
|
pageList := common.Paginate(logCount, perPage, 5)
|
2018-06-17 07:28:18 +00:00
|
|
|
pi := common.PanelLogsPage{basePage, llist, common.Paginator{pageList, page, lastPage}}
|
2019-02-10 05:52:26 +00:00
|
|
|
return renderTemplate("panel_adminlogs", w, r, basePage.Header, &pi)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|