2018-05-27 09:36:35 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/Gosora/common/counters"
|
2018-05-27 09:36:35 +00:00
|
|
|
)
|
|
|
|
|
2020-03-15 07:06:44 +00:00
|
|
|
func ReportSubmit(w http.ResponseWriter, r *http.Request, user *c.User, sItemID string) c.RouteError {
|
|
|
|
headerLite, ferr := c.SimpleUserCheck(w, r, user)
|
2018-05-27 09:36:35 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
2019-12-07 06:27:01 +00:00
|
|
|
js := r.PostFormValue("js") == "1"
|
2018-05-27 09:36:35 +00:00
|
|
|
|
2020-02-09 10:00:08 +00:00
|
|
|
itemID, err := strconv.Atoi(sItemID)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.LocalError("Bad ID", w, r, user)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
itemType := r.FormValue("type")
|
|
|
|
|
2018-06-06 00:21:22 +00:00
|
|
|
// TODO: Localise these titles and bodies
|
2018-05-27 09:36:35 +00:00
|
|
|
var title, content string
|
2019-08-14 10:39:04 +00:00
|
|
|
switch itemType {
|
|
|
|
case "reply":
|
2019-04-19 06:36:26 +00:00
|
|
|
reply, err := c.Rstore.Get(itemID)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.LocalError("We were unable to find the reported post", w, r, user)
|
2018-05-27 09:36:35 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
topic, err := c.Topics.Get(reply.ParentID)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.LocalError("We weren't able to find the topic the reported post is supposed to be in", w, r, user)
|
2018-05-27 09:36:35 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
title = "Reply: " + topic.Title
|
|
|
|
content = reply.Content + "\n\nOriginal Post: #rid-" + strconv.Itoa(itemID)
|
2019-08-14 10:39:04 +00:00
|
|
|
case "user-reply":
|
2019-04-19 06:36:26 +00:00
|
|
|
userReply, err := c.Prstore.Get(itemID)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.LocalError("We weren't able to find the reported post", w, r, user)
|
2018-05-27 09:36:35 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
profileOwner, err := c.Users.Get(userReply.ParentID)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.LocalError("We weren't able to find the profile the reported post is supposed to be on", w, r, user)
|
2018-05-27 09:36:35 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
title = "Profile: " + profileOwner.Name
|
|
|
|
content = userReply.Content + "\n\nOriginal Post: @" + strconv.Itoa(userReply.ParentID)
|
2019-08-14 10:39:04 +00:00
|
|
|
case "topic":
|
2019-04-19 06:36:26 +00:00
|
|
|
topic, err := c.Topics.Get(itemID)
|
2018-05-27 09:36:35 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.NotFound(w, r, nil)
|
2018-05-27 09:36:35 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
title = "Topic: " + topic.Title
|
|
|
|
content = topic.Content + "\n\nOriginal Post: #tid-" + strconv.Itoa(itemID)
|
2019-08-14 10:39:04 +00:00
|
|
|
case "convo-reply":
|
|
|
|
post := &c.ConversationPost{ID: itemID}
|
|
|
|
err := post.Fetch()
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return c.NotFound(w, r, nil)
|
|
|
|
} else if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
post, err = c.ConvoPostProcess.OnLoad(post)
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
user, err := c.Users.Get(post.CreatedBy)
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
2020-03-15 07:06:44 +00:00
|
|
|
title = "Convo: " + user.Name
|
2019-08-14 10:39:04 +00:00
|
|
|
content = post.Body + "\n\nOriginal Post: #cpid-" + strconv.Itoa(itemID)
|
|
|
|
default:
|
2018-10-21 13:54:32 +00:00
|
|
|
_, hasHook := headerLite.Hooks.VhookNeedHook("report_preassign", &itemID, &itemType)
|
2018-05-27 09:36:35 +00:00
|
|
|
if hasHook {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't try to guess the type
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.LocalError("Unknown type", w, r, user)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Repost attachments in the reports forum, so that the mods can see them
|
2020-03-15 07:06:44 +00:00
|
|
|
_, err = c.Reports.Create(title, content, user, itemType, itemID)
|
2019-04-19 06:36:26 +00:00
|
|
|
if err == c.ErrAlreadyReported {
|
|
|
|
return c.LocalError("Someone has already reported this!", w, r, user)
|
2018-05-27 09:36:35 +00:00
|
|
|
}
|
|
|
|
counters.PostCounter.Bump()
|
|
|
|
|
2019-09-30 10:15:50 +00:00
|
|
|
if !js {
|
2018-05-27 09:36:35 +00:00
|
|
|
// TODO: Redirect back to where we came from
|
|
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
|
|
} else {
|
|
|
|
_, _ = w.Write(successJSONBytes)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|