From 29d14a4a4a5540b35a82e34d9ad39bd8990806b2 Mon Sep 17 00:00:00 2001 From: Azareal Date: Fri, 19 Apr 2019 11:02:33 +1000 Subject: [PATCH] Added ten new plugin hooks and split topic_unknown out of the user_unknown phrase. Added the user parameter to the action hooks to give plugins more contextual information. Added the action_end_edit_topic plugin hook. Added the action_end_delete_topic plugin hook. Added the action_end_lock_topic plugin hook. Added the action_end_unlock_topic plugin hook. Added the action_end_stick_topic plugin hook. Added the action_end_unstick_topic plugin hook. Added the action_end_move_topic plugin hook. Added the action_end_like_topic plugin hook. Added the action_end_like_reply plugin hook. Added the tasks_tick_topic_list plugin hook. --- common/extend.go | 15 +++++++++- common/topic_list.go | 3 ++ langs/english.json | 1 + routes/panel/logs.go | 2 +- routes/reply.go | 17 +++++++---- routes/topic.go | 70 ++++++++++++++++++++++++++++++++------------ 6 files changed, 81 insertions(+), 27 deletions(-) diff --git a/common/extend.go b/common/extend.go index 52995712..16d8e94e 100644 --- a/common/extend.go +++ b/common/extend.go @@ -1,7 +1,7 @@ /* * * Gosora Plugin System -* Copyright Azareal 2016 - 2019 +* Copyright Azareal 2016 - 2020 * */ package common @@ -84,14 +84,27 @@ var hookTable = &HookTable{ "forum_check_pre_perms": nil, "action_end_create_topic": nil, + "action_end_edit_topic":nil, + "action_end_delete_topic":nil, + "action_end_lock_topic":nil, + "action_end_unlock_topic": nil, + "action_end_stick_topic": nil, + "action_end_unstick_topic": nil, + "action_end_move_topic": nil, + "action_end_like_topic":nil, + "action_end_create_reply": nil, "action_end_edit_reply": nil, "action_end_delete_reply": nil, "action_end_add_attach_to_reply": nil, "action_end_remove_attach_from_reply": nil, + "action_end_like_reply":nil, + "router_after_filters": nil, "router_pre_route": nil, + + "tasks_tick_topic_list": nil, }, map[string][]func(string) string{ "preparse_preassign": nil, diff --git a/common/topic_list.go b/common/topic_list.go index 596f47a7..99f272fa 100644 --- a/common/topic_list.go +++ b/common/topic_list.go @@ -112,6 +112,9 @@ func (tList *DefaultTopicList) Tick() error { tList.evenGroups = evenLists tList.evenLock.Unlock() + hTbl := GetHookTable() + _, _ = hTbl.VhookSkippable("tasks_tick_topic_list", tList) + return nil } diff --git a/langs/english.json b/langs/english.json index 252ced77..b04e481b 100644 --- a/langs/english.json +++ b/langs/english.json @@ -920,6 +920,7 @@ "panel_logs_moderation_action_unknown":"Unknown action '%s' on elementType '%s' by %s", "user_unknown":"Unknown", + "topic_unknown":"Unknown", "panel_logs_administration_head":"Admin Action Logs", diff --git a/routes/panel/logs.go b/routes/panel/logs.go index 699d0c58..7bf008d1 100644 --- a/routes/panel/logs.go +++ b/routes/panel/logs.go @@ -47,7 +47,7 @@ func handleUnknownUser(user *common.User, err error) *common.User { } func handleUnknownTopic(topic *common.Topic, err error) *common.Topic { if err != nil { - return &common.Topic{Title: phrases.GetTmplPhrase("user_unknown"), Link: common.BuildTopicURL("unknown", 0)} + return &common.Topic{Title: phrases.GetTmplPhrase("topic_unknown"), Link: common.BuildTopicURL("unknown", 0)} } return topic } diff --git a/routes/reply.go b/routes/reply.go index 9004e23d..7218d3bc 100644 --- a/routes/reply.go +++ b/routes/reply.go @@ -189,7 +189,7 @@ func CreateReplySubmit(w http.ResponseWriter, r *http.Request, user common.User) } counters.PostCounter.Bump() - skip, rerr := lite.Hooks.VhookSkippable("action_end_create_reply", reply.ID) + skip, rerr := lite.Hooks.VhookSkippable("action_end_create_reply", reply.ID, &user) if skip || rerr != nil { return rerr } @@ -261,7 +261,7 @@ func ReplyEditSubmit(w http.ResponseWriter, r *http.Request, user common.User, s return common.InternalErrorJSQ(err, w, r, js) } - skip, rerr := lite.Hooks.VhookSkippable("action_end_edit_reply", reply.ID) + skip, rerr := lite.Hooks.VhookSkippable("action_end_edit_reply", reply.ID, &user) if skip || rerr != nil { return rerr } @@ -316,7 +316,7 @@ func ReplyDeleteSubmit(w http.ResponseWriter, r *http.Request, user common.User, return common.InternalErrorJSQ(err, w, r, isJs) } - skip, rerr := lite.Hooks.VhookSkippable("action_end_delete_reply", reply.ID) + skip, rerr := lite.Hooks.VhookSkippable("action_end_delete_reply", reply.ID, &user) if skip || rerr != nil { return rerr } @@ -389,7 +389,7 @@ func AddAttachToReplySubmit(w http.ResponseWriter, r *http.Request, user common. return common.InternalErrorJS(errors.New("no paths for attachment add"), w, r) } - skip, rerr := lite.Hooks.VhookSkippable("action_end_add_attach_to_reply", reply.ID) + skip, rerr := lite.Hooks.VhookSkippable("action_end_add_attach_to_reply", reply.ID, &user) if skip || rerr != nil { return rerr } @@ -452,7 +452,7 @@ func RemoveAttachFromReplySubmit(w http.ResponseWriter, r *http.Request, user co } } - skip, rerr := lite.Hooks.VhookSkippable("action_end_remove_attach_from_reply", reply.ID) + skip, rerr := lite.Hooks.VhookSkippable("action_end_remove_attach_from_reply", reply.ID, &user) if skip || rerr != nil { return rerr } @@ -597,7 +597,7 @@ func ReplyLikeSubmit(w http.ResponseWriter, r *http.Request, user common.User, s } // TODO: Add hooks to make use of headerLite - _, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) + lite, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) if ferr != nil { return ferr } @@ -629,6 +629,11 @@ func ReplyLikeSubmit(w http.ResponseWriter, r *http.Request, user common.User, s return common.InternalErrorJSQ(err, w, r, isJs) } + skip, rerr := lite.Hooks.VhookSkippable("action_end_like_reply", reply.ID, &user) + if skip || rerr != nil { + return rerr + } + if !isJs { http.Redirect(w, r, "/topic/"+strconv.Itoa(reply.ParentID), http.StatusSeeOther) } else { diff --git a/routes/topic.go b/routes/topic.go index 234e0910..43d17238 100644 --- a/routes/topic.go +++ b/routes/topic.go @@ -651,7 +651,7 @@ func EditTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User, s } // TODO: Add hooks to make use of headerLite - _, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) + lite, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) if ferr != nil { return ferr } @@ -689,6 +689,11 @@ func EditTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User, s return common.InternalErrorJSQ(err, w, r, isJs) } + skip, rerr := lite.Hooks.VhookSkippable("action_end_edit_topic", topic.ID, &user) + if skip || rerr != nil { + return rerr + } + if !isJs { http.Redirect(w, r, "/topic/"+strconv.Itoa(tid), http.StatusSeeOther) } else { @@ -736,7 +741,7 @@ func DeleteTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User) } // TODO: Add hooks to make use of headerLite - _, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) + lite, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) if ferr != nil { return ferr } @@ -761,6 +766,12 @@ func DeleteTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User) return common.InternalErrorJSQ(err,w,r,isJs) }*/ + // TODO: Do a bulk delete action hook? + skip, rerr := lite.Hooks.VhookSkippable("action_end_delete_topic", topic.ID, &user) + if skip || rerr != nil { + return rerr + } + log.Printf("Topic #%d was deleted by UserID #%d", tid, user.ID) } http.Redirect(w, r, "/", http.StatusSeeOther) @@ -768,39 +779,39 @@ func DeleteTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User) } func StickTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User, stid string) common.RouteError { - topic, rerr := topicActionPre(stid, "pin", w, r, user) + topic, lite,rerr := topicActionPre(stid, "pin", w, r, user) if rerr != nil { return rerr } if !user.Perms.ViewTopic || !user.Perms.PinTopic { return common.NoPermissions(w, r, user) } - return topicActionPost(topic.Stick(), "stick", w, r, topic, user) + return topicActionPost(topic.Stick(), "stick", w, r, lite,topic, user) } -func topicActionPre(stid string, action string, w http.ResponseWriter, r *http.Request, user common.User) (*common.Topic, common.RouteError) { +func topicActionPre(stid string, action string, w http.ResponseWriter, r *http.Request, user common.User) (*common.Topic, *common.HeaderLite, common.RouteError) { tid, err := strconv.Atoi(stid) if err != nil { - return nil, common.PreError(phrases.GetErrorPhrase("id_must_be_integer"), w, r) + return nil, nil,common.PreError(phrases.GetErrorPhrase("id_must_be_integer"), w, r) } topic, err := common.Topics.Get(tid) if err == sql.ErrNoRows { - return nil, common.PreError("The topic you tried to "+action+" doesn't exist.", w, r) + return nil, nil,common.PreError("The topic you tried to "+action+" doesn't exist.", w, r) } else if err != nil { - return nil, common.InternalError(err, w, r) + return nil, nil,common.InternalError(err, w, r) } // TODO: Add hooks to make use of headerLite - _, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) + lite, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) if ferr != nil { - return nil, ferr + return nil, nil,ferr } - return topic, nil + return topic, lite, nil } -func topicActionPost(err error, action string, w http.ResponseWriter, r *http.Request, topic *common.Topic, user common.User) common.RouteError { +func topicActionPost(err error, action string, w http.ResponseWriter, r *http.Request, lite *common.HeaderLite, topic *common.Topic, user common.User) common.RouteError { if err != nil { return common.InternalError(err, w, r) } @@ -808,19 +819,23 @@ func topicActionPost(err error, action string, w http.ResponseWriter, r *http.Re if err != nil { return common.InternalError(err, w, r) } + skip, rerr := lite.Hooks.VhookSkippable("action_end_"+action+"_topic", topic.ID, &user) + if skip || rerr != nil { + return rerr + } http.Redirect(w, r, "/topic/"+strconv.Itoa(topic.ID), http.StatusSeeOther) return nil } func UnstickTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User, stid string) common.RouteError { - topic, rerr := topicActionPre(stid, "unpin", w, r, user) + topic, lite, rerr := topicActionPre(stid, "unpin", w, r, user) if rerr != nil { return rerr } if !user.Perms.ViewTopic || !user.Perms.PinTopic { return common.NoPermissions(w, r, user) } - return topicActionPost(topic.Unstick(), "unstick", w, r, topic, user) + return topicActionPost(topic.Unstick(), "unstick", w, r, lite,topic, user) } func LockTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User) common.RouteError { @@ -856,7 +871,7 @@ func LockTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User) c } // TODO: Add hooks to make use of headerLite - _, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) + lite, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) if ferr != nil { return ferr } @@ -873,6 +888,12 @@ func LockTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User) c if err != nil { return common.InternalErrorJSQ(err, w, r, isJs) } + + // TODO: Do a bulk lock action hook? + skip, rerr := lite.Hooks.VhookSkippable("action_end_lock_topic", topic.ID, &user) + if skip || rerr != nil { + return rerr + } } if len(tids) == 1 { @@ -882,14 +903,14 @@ func LockTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User) c } func UnlockTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User, stid string) common.RouteError { - topic, rerr := topicActionPre(stid, "unlock", w, r, user) + topic, lite,rerr := topicActionPre(stid, "unlock", w, r, user) if rerr != nil { return rerr } if !user.Perms.ViewTopic || !user.Perms.CloseTopic { return common.NoPermissions(w, r, user) } - return topicActionPost(topic.Unlock(), "unlock", w, r, topic, user) + return topicActionPost(topic.Unlock(), "unlock", w, r, lite,topic, user) } // ! JS only route @@ -929,7 +950,7 @@ func MoveTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User, s if !user.Perms.ViewTopic || !user.Perms.MoveTopic { return common.NoPermissionsJS(w, r, user) } - _, ferr = common.SimpleForumUserCheck(w, r, &user, fid) + lite, ferr := common.SimpleForumUserCheck(w, r, &user, fid) if ferr != nil { return ferr } @@ -947,6 +968,12 @@ func MoveTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User, s if err != nil { return common.InternalErrorJS(err, w, r) } + + // TODO: Do a bulk move action hook? + skip, rerr := lite.Hooks.VhookSkippable("action_end_move_topic", topic.ID, &user) + if skip || rerr != nil { + return rerr + } } if len(tids) == 1 { @@ -979,7 +1006,7 @@ func LikeTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User, s } // TODO: Add hooks to make use of headerLite - _, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) + lite, ferr := common.SimpleForumUserCheck(w, r, &user, topic.ParentID) if ferr != nil { return ferr } @@ -1012,6 +1039,11 @@ func LikeTopicSubmit(w http.ResponseWriter, r *http.Request, user common.User, s return common.InternalErrorJSQ(err, w, r, isJs) } + skip, rerr := lite.Hooks.VhookSkippable("action_end_like_topic", topic.ID, &user) + if skip || rerr != nil { + return rerr + } + if !isJs { http.Redirect(w, r, "/topic/"+strconv.Itoa(tid), http.StatusSeeOther) } else {