github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/routes/reports.go (about)

     1  package routes
     2  
     3  import (
     4  	"database/sql"
     5  	"net/http"
     6  	"strconv"
     7  
     8  	c "github.com/Azareal/Gosora/common"
     9  	"github.com/Azareal/Gosora/common/counters"
    10  )
    11  
    12  func ReportSubmit(w http.ResponseWriter, r *http.Request, user *c.User, sItemID string) c.RouteError {
    13  	headerLite, ferr := c.SimpleUserCheck(w, r, user)
    14  	if ferr != nil {
    15  		return ferr
    16  	}
    17  	js := r.PostFormValue("js") == "1"
    18  
    19  	itemID, err := strconv.Atoi(sItemID)
    20  	if err != nil {
    21  		return c.LocalError("Bad ID", w, r, user)
    22  	}
    23  	itemType := r.FormValue("type")
    24  
    25  	// TODO: Localise these titles and bodies
    26  	var title, content string
    27  	switch itemType {
    28  	case "reply":
    29  		reply, err := c.Rstore.Get(itemID)
    30  		if err == sql.ErrNoRows {
    31  			return c.LocalError("We were unable to find the reported post", w, r, user)
    32  		} else if err != nil {
    33  			return c.InternalError(err, w, r)
    34  		}
    35  
    36  		topic, err := c.Topics.Get(reply.ParentID)
    37  		if err == sql.ErrNoRows {
    38  			return c.LocalError("We weren't able to find the topic the reported post is supposed to be in", w, r, user)
    39  		} else if err != nil {
    40  			return c.InternalError(err, w, r)
    41  		}
    42  
    43  		title = "Reply: " + topic.Title
    44  		content = reply.Content + "\n\nOriginal Post: #rid-" + strconv.Itoa(itemID)
    45  	case "user-reply":
    46  		userReply, err := c.Prstore.Get(itemID)
    47  		if err == sql.ErrNoRows {
    48  			return c.LocalError("We weren't able to find the reported post", w, r, user)
    49  		} else if err != nil {
    50  			return c.InternalError(err, w, r)
    51  		}
    52  
    53  		profileOwner, err := c.Users.Get(userReply.ParentID)
    54  		if err == sql.ErrNoRows {
    55  			return c.LocalError("We weren't able to find the profile the reported post is supposed to be on", w, r, user)
    56  		} else if err != nil {
    57  			return c.InternalError(err, w, r)
    58  		}
    59  		title = "Profile: " + profileOwner.Name
    60  		content = userReply.Content + "\n\nOriginal Post: @" + strconv.Itoa(userReply.ParentID)
    61  	case "topic":
    62  		topic, err := c.Topics.Get(itemID)
    63  		if err == sql.ErrNoRows {
    64  			return c.NotFound(w, r, nil)
    65  		} else if err != nil {
    66  			return c.InternalError(err, w, r)
    67  		}
    68  		title = "Topic: " + topic.Title
    69  		content = topic.Content + "\n\nOriginal Post: #tid-" + strconv.Itoa(itemID)
    70  	case "convo-reply":
    71  		post := &c.ConversationPost{ID: itemID}
    72  		err := post.Fetch()
    73  		if err == sql.ErrNoRows {
    74  			return c.NotFound(w, r, nil)
    75  		} else if err != nil {
    76  			return c.InternalError(err, w, r)
    77  		}
    78  
    79  		post, err = c.ConvoPostProcess.OnLoad(post)
    80  		if err != nil {
    81  			return c.InternalError(err, w, r)
    82  		}
    83  		user, err := c.Users.Get(post.CreatedBy)
    84  		if err != nil {
    85  			return c.InternalError(err, w, r)
    86  		}
    87  
    88  		title = "Convo: " + user.Name
    89  		content = post.Body + "\n\nOriginal Post: #cpid-" + strconv.Itoa(itemID)
    90  	default:
    91  		_, hasHook := headerLite.Hooks.VhookNeedHook("report_preassign", &itemID, &itemType)
    92  		if hasHook {
    93  			return nil
    94  		}
    95  
    96  		// Don't try to guess the type
    97  		return c.LocalError("Unknown type", w, r, user)
    98  	}
    99  
   100  	// TODO: Repost attachments in the reports forum, so that the mods can see them
   101  	_, err = c.Reports.Create(title, content, user, itemType, itemID)
   102  	if err == c.ErrAlreadyReported {
   103  		return c.LocalError("Someone has already reported this!", w, r, user)
   104  	}
   105  	counters.PostCounter.Bump()
   106  	// TODO: Redirect back to where we came from
   107  	return actionSuccess(w, r, "/", js)
   108  }