2018-01-18 12:31:25 +00:00
package routes
import (
2018-01-22 08:15:45 +00:00
"crypto/sha256"
2018-01-18 12:31:25 +00:00
"database/sql"
2018-01-22 08:15:45 +00:00
"encoding/hex"
2018-01-20 06:50:29 +00:00
"encoding/json"
2018-12-27 05:42:41 +00:00
"errors"
2018-01-22 08:15:45 +00:00
"io"
2018-01-20 06:50:29 +00:00
"log"
2018-01-18 12:31:25 +00:00
"net/http"
2018-01-22 08:15:45 +00:00
"os"
"regexp"
2018-01-18 12:31:25 +00:00
"strconv"
2018-01-22 08:15:45 +00:00
"strings"
2018-01-18 12:31:25 +00:00
2018-10-27 03:21:02 +00:00
"github.com/Azareal/Gosora/common"
"github.com/Azareal/Gosora/common/counters"
2018-11-01 06:43:56 +00:00
"github.com/Azareal/Gosora/common/phrases"
2018-10-27 03:21:02 +00:00
"github.com/Azareal/Gosora/query_gen"
2018-01-18 12:31:25 +00:00
)
2018-02-10 15:07:21 +00:00
type TopicStmts struct {
2018-03-31 05:25:27 +00:00
getReplies * sql . Stmt
getLikedTopic * sql . Stmt
2018-12-27 05:42:41 +00:00
updateAttachs * sql . Stmt
2018-02-10 15:07:21 +00:00
}
var topicStmts TopicStmts
// TODO: Move these DbInits into a TopicList abstraction
func init ( ) {
common . DbInits . Add ( func ( acc * qgen . Accumulator ) error {
topicStmts = TopicStmts {
2018-12-31 09:03:49 +00:00
getReplies : acc . SimpleLeftJoin ( "replies" , "users" , "replies.rid, replies.content, replies.createdBy, replies.createdAt, replies.lastEdit, replies.lastEditBy, users.avatar, users.name, users.group, users.url_prefix, users.url_name, users.level, replies.ipaddress, replies.likeCount, replies.attachCount, replies.actionType" , "replies.createdBy = users.uid" , "replies.tid = ?" , "replies.rid ASC" , "?,?" ) ,
2018-03-31 05:25:27 +00:00
getLikedTopic : acc . Select ( "likes" ) . Columns ( "targetItem" ) . Where ( "sentBy = ? && targetItem = ? && targetType = 'topics'" ) . Prepare ( ) ,
2018-12-27 05:42:41 +00:00
// TODO: Less race-y attachment count updates
updateAttachs : acc . Update ( "topics" ) . Set ( "attachCount = ?" ) . Where ( "tid = ?" ) . Prepare ( ) ,
2018-02-10 15:07:21 +00:00
}
return acc . FirstError ( )
} )
}
2018-11-12 09:23:36 +00:00
func ViewTopic ( w http . ResponseWriter , r * http . Request , user common . User , header * common . Header , urlBit string ) common . RouteError {
2018-02-03 05:47:14 +00:00
page , _ := strconv . Atoi ( r . FormValue ( "page" ) )
2018-11-12 09:23:36 +00:00
_ , tid , err := ParseSEOURL ( urlBit )
2018-02-03 05:47:14 +00:00
if err != nil {
2018-11-01 06:43:56 +00:00
return common . PreError ( phrases . GetErrorPhrase ( "url_id_must_be_integer" ) , w , r )
2018-02-03 05:47:14 +00:00
}
// Get the topic...
2018-11-22 07:21:43 +00:00
topic , err := common . GetTopicUser ( & user , tid )
2018-02-03 05:47:14 +00:00
if err == sql . ErrNoRows {
2019-04-18 01:00:39 +00:00
return common . NotFound ( w , r , nil ) // TODO: Can we add a simplified invocation of header here? This is likely to be an extremely common NotFound
2018-02-03 05:47:14 +00:00
} else if err != nil {
return common . InternalError ( err , w , r )
}
2018-11-12 09:23:36 +00:00
ferr := common . ForumUserCheck ( header , w , r , & user , topic . ParentID )
2018-02-03 05:47:14 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic {
return common . NoPermissions ( w , r , user )
}
2018-05-27 09:36:35 +00:00
header . Title = topic . Title
2018-10-14 05:08:44 +00:00
header . Path = common . BuildTopicURL ( common . NameToSlug ( topic . Title ) , topic . ID )
2018-02-03 05:47:14 +00:00
2018-12-27 05:42:41 +00:00
// TODO: Cache ContentHTML when possible?
2018-02-03 05:47:14 +00:00
topic . ContentHTML = common . ParseMessage ( topic . Content , topic . ParentID , "forums" )
topic . ContentLines = strings . Count ( topic . Content , "\n" )
2019-02-24 08:02:00 +00:00
header . OGDesc = topic . Content
if len ( header . OGDesc ) > 200 {
header . OGDesc = header . OGDesc [ : 197 ] + "..."
}
2018-02-03 05:47:14 +00:00
postGroup , err := common . Groups . Get ( topic . Group )
if err != nil {
return common . InternalError ( err , w , r )
}
topic . Tag = postGroup . Tag
2018-06-24 13:49:29 +00:00
if postGroup . IsMod {
2018-02-03 05:47:14 +00:00
topic . ClassName = common . Config . StaffCSS
}
2018-08-30 10:31:21 +00:00
forum , err := common . Forums . Get ( topic . ParentID )
if err != nil {
return common . InternalError ( err , w , r )
}
2018-02-03 05:47:14 +00:00
var poll common . Poll
if topic . Poll != 0 {
pPoll , err := common . Polls . Get ( topic . Poll )
if err != nil {
log . Print ( "Couldn't find the attached poll for topic " + strconv . Itoa ( topic . ID ) )
return common . InternalError ( err , w , r )
}
poll = pPoll . Copy ( )
2018-08-13 12:37:53 +00:00
header . AddSheet ( "chartist/chartist.min.css" )
header . AddScript ( "chartist/chartist.min.js" )
2018-02-03 05:47:14 +00:00
}
2018-03-31 05:25:27 +00:00
if topic . LikeCount > 0 && user . Liked > 0 {
var disp int // Discard this value
err = topicStmts . getLikedTopic . QueryRow ( user . ID , topic . ID ) . Scan ( & disp )
if err == nil {
topic . Liked = true
} else if err != nil && err != sql . ErrNoRows {
return common . InternalError ( err , w , r )
}
}
2018-12-27 05:42:41 +00:00
if topic . AttachCount > 0 {
2018-12-31 09:03:49 +00:00
attachs , err := common . Attachments . MiniGetList ( "topics" , topic . ID )
2018-12-27 05:42:41 +00:00
if err != nil {
// TODO: We might want to be a little permissive here in-case of a desync?
return common . InternalError ( err , w , r )
}
topic . Attachments = attachs
}
2018-02-03 05:47:14 +00:00
// Calculate the offset
offset , page , lastPage := common . PageOffset ( topic . PostCount , page , common . Config . ItemsPerPage )
2018-12-17 04:58:55 +00:00
pageList := common . Paginate ( topic . PostCount , common . Config . ItemsPerPage , 5 )
tpage := common . TopicPage { header , [ ] common . ReplyUser { } , topic , forum , poll , common . Paginator { pageList , page , lastPage } }
2018-02-15 13:15:27 +00:00
// Get the replies if we have any...
if topic . PostCount > 0 {
2019-02-24 08:02:00 +00:00
var pFrag int
if strings . HasPrefix ( r . URL . Fragment , "post-" ) {
pFrag , _ = strconv . Atoi ( strings . TrimPrefix ( r . URL . Fragment , "post-" ) )
}
2018-12-31 09:03:49 +00:00
var likedMap map [ int ] int
if user . Liked > 0 {
likedMap = make ( map [ int ] int )
}
2018-03-31 05:25:27 +00:00
var likedQueryList = [ ] int { user . ID }
2018-12-31 09:03:49 +00:00
var attachMap map [ int ] int
if user . Perms . EditReply {
attachMap = make ( map [ int ] int )
}
var attachQueryList = [ ] int { }
2018-02-15 13:15:27 +00:00
rows , err := topicStmts . getReplies . Query ( topic . ID , offset , common . Config . ItemsPerPage )
if err == sql . ErrNoRows {
return common . LocalError ( "Bad Page. Some of the posts may have been deleted or you got here by directly typing in the page number." , w , r , user )
} else if err != nil {
2018-02-03 05:47:14 +00:00
return common . InternalError ( err , w , r )
}
2018-02-15 13:15:27 +00:00
defer rows . Close ( )
2018-02-03 05:47:14 +00:00
2018-07-28 12:52:23 +00:00
// TODO: Factor the user fields out and embed a user struct instead
2018-02-15 13:15:27 +00:00
replyItem := common . ReplyUser { ClassName : "" }
for rows . Next ( ) {
2018-12-31 09:03:49 +00:00
err := rows . Scan ( & replyItem . ID , & replyItem . Content , & replyItem . CreatedBy , & replyItem . CreatedAt , & replyItem . LastEdit , & replyItem . LastEditBy , & replyItem . Avatar , & replyItem . CreatedByName , & replyItem . Group , & replyItem . URLPrefix , & replyItem . URLName , & replyItem . Level , & replyItem . IPAddress , & replyItem . LikeCount , & replyItem . AttachCount , & replyItem . ActionType )
2018-02-15 13:15:27 +00:00
if err != nil {
return common . InternalError ( err , w , r )
}
2018-02-03 05:47:14 +00:00
2018-02-15 13:15:27 +00:00
replyItem . UserLink = common . BuildProfileURL ( common . NameToSlug ( replyItem . CreatedByName ) , replyItem . CreatedBy )
replyItem . ParentID = topic . ID
replyItem . ContentHtml = common . ParseMessage ( replyItem . Content , topic . ParentID , "forums" )
replyItem . ContentLines = strings . Count ( replyItem . Content , "\n" )
2018-02-03 05:47:14 +00:00
2019-02-24 08:02:00 +00:00
if replyItem . ID == pFrag {
header . OGDesc = replyItem . Content
if len ( header . OGDesc ) > 200 {
header . OGDesc = header . OGDesc [ : 197 ] + "..."
}
}
2018-02-15 13:15:27 +00:00
postGroup , err = common . Groups . Get ( replyItem . Group )
if err != nil {
return common . InternalError ( err , w , r )
}
2018-02-03 05:47:14 +00:00
2018-06-24 13:49:29 +00:00
if postGroup . IsMod {
2018-02-15 13:15:27 +00:00
replyItem . ClassName = common . Config . StaffCSS
} else {
replyItem . ClassName = ""
2018-02-03 05:47:14 +00:00
}
2018-02-15 13:15:27 +00:00
// TODO: Make a function for this? Build a more sophisticated noavatar handling system? Do bulk user loads and let the common.UserStore initialise this?
2018-07-28 12:52:23 +00:00
replyItem . Avatar , replyItem . MicroAvatar = common . BuildAvatar ( replyItem . CreatedBy , replyItem . Avatar )
2018-02-15 13:15:27 +00:00
replyItem . Tag = postGroup . Tag
// We really shouldn't have inline HTML, we should do something about this...
if replyItem . ActionType != "" {
2018-12-27 05:42:41 +00:00
var action string
2019-04-17 10:12:35 +00:00
aarr := strings . Split ( replyItem . ActionType , "-" )
switch aarr [ 0 ] {
2018-02-15 13:15:27 +00:00
case "lock" :
2018-12-27 05:42:41 +00:00
action = "lock"
2018-02-15 13:15:27 +00:00
replyItem . ActionIcon = "🔒︎"
case "unlock" :
2018-12-27 05:42:41 +00:00
action = "unlock"
2018-02-15 13:15:27 +00:00
replyItem . ActionIcon = "🔓︎"
case "stick" :
2018-12-27 05:42:41 +00:00
action = "stick"
2018-02-15 13:15:27 +00:00
replyItem . ActionIcon = "📌︎"
case "unstick" :
2018-12-27 05:42:41 +00:00
action = "unstick"
2018-02-15 13:15:27 +00:00
replyItem . ActionIcon = "📌︎"
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 {
replyItem . ActionType = phrases . GetTmplPhrasef ( "topic.action_topic_move_dest" , forum . Link , forum . Name , replyItem . UserLink , replyItem . CreatedByName )
} else {
action = "move"
}
} else {
action = "move"
}
2018-12-27 05:42:41 +00:00
replyItem . ActionIcon = ""
2019-04-17 10:12:35 +00:00
default :
2018-12-27 05:42:41 +00:00
// TODO: Only fire this off if a corresponding phrase for the ActionType doesn't exist? Or maybe have some sort of action registry?
2018-11-12 09:23:36 +00:00
replyItem . ActionType = phrases . GetTmplPhrasef ( "topic.action_topic_default" , replyItem . ActionType )
2018-02-15 13:15:27 +00:00
replyItem . ActionIcon = ""
}
2019-04-17 10:12:35 +00:00
if action != "" {
replyItem . ActionType = phrases . GetTmplPhrasef ( "topic.action_topic_" + action , replyItem . UserLink , replyItem . CreatedByName )
}
2018-02-15 13:15:27 +00:00
}
2018-03-31 05:25:27 +00:00
2018-12-27 05:42:41 +00:00
if replyItem . LikeCount > 0 && user . Liked > 0 {
2018-03-31 05:25:27 +00:00
likedMap [ replyItem . ID ] = len ( tpage . ItemList )
likedQueryList = append ( likedQueryList , replyItem . ID )
}
2018-12-31 09:03:49 +00:00
if user . Perms . EditReply && replyItem . AttachCount > 0 {
attachMap [ replyItem . ID ] = len ( tpage . ItemList )
attachQueryList = append ( attachQueryList , replyItem . ID )
}
2018-02-15 13:15:27 +00:00
2018-10-21 13:54:32 +00:00
header . Hooks . VhookNoRet ( "topic_reply_row_assign" , & tpage , & replyItem )
2018-02-15 13:15:27 +00:00
// TODO: Use a pointer instead to make it easier to abstract this loop? What impact would this have on escape analysis?
tpage . ItemList = append ( tpage . ItemList , replyItem )
2019-04-17 10:12:35 +00:00
//log.Printf("r: %d-%d", replyItem.ID, len(tpage.ItemList)-1)
2018-02-15 13:15:27 +00:00
}
err = rows . Err ( )
if err != nil {
return common . InternalError ( err , w , r )
2018-02-03 05:47:14 +00:00
}
2018-03-31 05:25:27 +00:00
// TODO: Add a config setting to disable the liked query for a burst of extra speed
if user . Liked > 0 && len ( likedQueryList ) > 1 /*&& user.LastLiked <= time.Now()*/ {
2018-12-27 05:42:41 +00:00
// TODO: Abstract this
2018-08-04 11:46:36 +00:00
rows , err := qgen . NewAcc ( ) . Select ( "likes" ) . Columns ( "targetItem" ) . Where ( "sentBy = ? AND targetType = 'replies'" ) . In ( "targetItem" , likedQueryList [ 1 : ] ) . Query ( user . ID )
2018-03-31 05:25:27 +00:00
if err != nil && err != sql . ErrNoRows {
return common . InternalError ( err , w , r )
}
defer rows . Close ( )
for rows . Next ( ) {
var likeRid int
err := rows . Scan ( & likeRid )
if err != nil {
return common . InternalError ( err , w , r )
}
tpage . ItemList [ likedMap [ likeRid ] ] . Liked = true
}
err = rows . Err ( )
if err != nil {
return common . InternalError ( err , w , r )
}
}
2018-12-31 09:03:49 +00:00
if user . Perms . EditReply && len ( attachQueryList ) > 0 {
2019-04-13 11:54:22 +00:00
//log.Printf("attachQueryList: %+v\n", attachQueryList)
2018-12-31 09:03:49 +00:00
amap , err := common . Attachments . BulkMiniGetList ( "replies" , attachQueryList )
if err != nil && err != sql . ErrNoRows {
return common . InternalError ( err , w , r )
}
2019-04-13 11:54:22 +00:00
//log.Printf("amap: %+v\n", amap)
//log.Printf("attachMap: %+v\n", attachMap)
2018-12-31 09:03:49 +00:00
for id , attach := range amap {
2019-04-17 10:12:35 +00:00
//log.Print("id:", id)
2018-12-31 09:03:49 +00:00
tpage . ItemList [ attachMap [ id ] ] . Attachments = attach
2019-04-13 11:54:22 +00:00
/ * for _ , a := range attach {
log . Printf ( "a: %+v\n" , a )
} * /
2018-12-31 09:03:49 +00:00
}
}
2018-02-03 05:47:14 +00:00
}
2019-01-21 12:27:59 +00:00
header . Zone = "view_topic"
header . ZoneID = topic . ID
header . ZoneData = topic
2018-10-27 03:21:02 +00:00
rerr := renderTemplate ( "topic" , w , r , header , tpage )
2018-02-19 04:26:01 +00:00
counters . TopicViewCounter . Bump ( topic . ID ) // TODO: Move this into the router?
2018-02-22 02:27:17 +00:00
counters . ForumViewCounter . Bump ( topic . ParentID )
2018-10-27 03:21:02 +00:00
return rerr
2018-02-03 05:47:14 +00:00
}
2018-12-27 05:42:41 +00:00
// TODO: Avoid uploading this again if the attachment already exists? They'll resolve to the same hash either way, but we could save on some IO / bandwidth here
// TODO: Enforce the max request limit on all of this topic's attachments
// TODO: Test this route
func AddAttachToTopicSubmit ( w http . ResponseWriter , r * http . Request , user common . User , stid string ) common . RouteError {
tid , err := strconv . Atoi ( stid )
if err != nil {
return common . LocalErrorJS ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r )
}
topic , err := common . Topics . Get ( tid )
if err != nil {
return common . NotFoundJS ( w , r )
}
_ , ferr := common . SimpleForumUserCheck ( w , r , & user , topic . ParentID )
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic || ! user . Perms . EditTopic || ! user . Perms . UploadFiles {
return common . NoPermissionsJS ( w , r , user )
}
if topic . IsClosed && ! user . Perms . CloseTopic {
return common . NoPermissionsJS ( w , r , user )
}
// Handle the file attachments
2019-04-13 11:54:22 +00:00
pathMap , rerr := uploadAttachment ( w , r , user , topic . ParentID , "forums" , tid , "topics" , "" )
2018-12-27 05:42:41 +00:00
if rerr != nil {
// TODO: This needs to be a JS error...
return rerr
}
if len ( pathMap ) == 0 {
return common . InternalErrorJS ( errors . New ( "no paths for attachment add" ) , w , r )
}
var elemStr string
for path , aids := range pathMap {
elemStr += "\"" + path + "\":\"" + aids + "\","
}
if len ( elemStr ) > 1 {
elemStr = elemStr [ : len ( elemStr ) - 1 ]
}
w . Write ( [ ] byte ( ` { "success":"1","elems":[ { ` + elemStr + ` }]} ` ) )
return nil
}
func RemoveAttachFromTopicSubmit ( w http . ResponseWriter , r * http . Request , user common . User , stid string ) common . RouteError {
tid , err := strconv . Atoi ( stid )
if err != nil {
return common . LocalErrorJS ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r )
}
topic , err := common . Topics . Get ( tid )
if err != nil {
return common . NotFoundJS ( w , r )
}
_ , ferr := common . SimpleForumUserCheck ( w , r , & user , topic . ParentID )
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic || ! user . Perms . EditTopic {
return common . NoPermissionsJS ( w , r , user )
}
if topic . IsClosed && ! user . Perms . CloseTopic {
return common . NoPermissionsJS ( w , r , user )
}
for _ , said := range strings . Split ( r . PostFormValue ( "aids" ) , "," ) {
aid , err := strconv . Atoi ( said )
if err != nil {
return common . LocalErrorJS ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r )
}
rerr := deleteAttachment ( w , r , user , aid , true )
if rerr != nil {
// TODO: This needs to be a JS error...
return rerr
}
}
w . Write ( successJSONBytes )
return nil
}
2018-01-21 11:17:43 +00:00
// ? - Should we add a new permission or permission zone (like per-forum permissions) specifically for profile comment creation
// ? - Should we allow banned users to make reports? How should we handle report abuse?
// TODO: Add a permission to stop certain users from using custom avatars
// ? - Log username changes and put restrictions on this?
2018-06-01 05:02:29 +00:00
// TODO: Test this
2018-10-21 13:54:32 +00:00
// TODO: Revamp this route
2018-11-12 09:23:36 +00:00
func CreateTopic ( w http . ResponseWriter , r * http . Request , user common . User , header * common . Header , sfid string ) common . RouteError {
2018-01-21 11:17:43 +00:00
var fid int
var err error
if sfid != "" {
fid , err = strconv . Atoi ( sfid )
if err != nil {
2018-11-01 06:43:56 +00:00
return common . LocalError ( phrases . GetErrorPhrase ( "url_id_must_be_integer" ) , w , r , user )
2018-01-21 11:17:43 +00:00
}
}
if fid == 0 {
fid = common . Config . DefaultForum
}
2018-11-12 09:23:36 +00:00
ferr := common . ForumUserCheck ( header , w , r , & user , fid )
2018-01-21 11:17:43 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic || ! user . Perms . CreateTopic {
return common . NoPermissions ( w , r , user )
}
2018-06-01 05:02:29 +00:00
// TODO: Add a phrase for this
2018-11-01 06:43:56 +00:00
header . Title = phrases . GetTitlePhrase ( "create_topic" )
2018-06-01 05:02:29 +00:00
header . Zone = "create_topic"
2018-01-21 11:17:43 +00:00
// Lock this to the forum being linked?
// Should we always put it in strictmode when it's linked from another forum? Well, the user might end up changing their mind on what forum they want to post in and it would be a hassle, if they had to switch pages, even if it is a single click for many (exc. mobile)
var strictmode bool
2018-10-21 13:54:32 +00:00
header . Hooks . VhookNoRet ( "topic_create_pre_loop" , w , r , fid , & header , & user , & strictmode )
2018-01-21 11:17:43 +00:00
// TODO: Re-add support for plugin_guilds
var forumList [ ] common . Forum
var canSee [ ] int
if user . IsSuperAdmin {
canSee , err = common . Forums . GetAllVisibleIDs ( )
if err != nil {
return common . InternalError ( err , w , r )
}
} else {
group , err := common . Groups . Get ( user . Group )
if err != nil {
// TODO: Refactor this
common . LocalError ( "Something weird happened behind the scenes" , w , r , user )
log . Printf ( "Group #%d doesn't exist, but it's set on common.User #%d" , user . Group , user . ID )
return nil
}
canSee = group . CanSee
}
// TODO: plugin_superadmin needs to be able to override this loop. Skip flag on topic_create_pre_loop?
for _ , ffid := range canSee {
// TODO: Surely, there's a better way of doing this. I've added it in for now to support plugin_guilds, but we really need to clean this up
if strictmode && ffid != fid {
continue
}
// Do a bulk forum fetch, just in case it's the SqlForumStore?
forum := common . Forums . DirtyGet ( ffid )
if forum . Name != "" && forum . Active {
fcopy := forum . Copy ( )
2018-02-19 04:26:01 +00:00
// TODO: Abstract this
2018-10-21 13:54:32 +00:00
if header . Hooks . HookSkippable ( "topic_create_frow_assign" , & fcopy ) {
continue
2018-01-21 11:17:43 +00:00
}
forumList = append ( forumList , fcopy )
}
}
2018-06-01 05:02:29 +00:00
ctpage := common . CreateTopicPage { header , forumList , fid }
2018-10-27 03:21:02 +00:00
return renderTemplate ( "create_topic" , w , r , header , ctpage )
2018-01-21 11:17:43 +00:00
}
2018-01-22 08:15:45 +00:00
func CreateTopicSubmit ( w http . ResponseWriter , r * http . Request , user common . User ) common . RouteError {
fid , err := strconv . Atoi ( r . PostFormValue ( "topic-board" ) )
if err != nil {
return common . LocalError ( "The provided ForumID is not a valid number." , w , r , user )
}
// TODO: Add hooks to make use of headerLite
2019-04-06 01:08:49 +00:00
lite , ferr := common . SimpleForumUserCheck ( w , r , & user , fid )
2018-01-22 08:15:45 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic || ! user . Perms . CreateTopic {
return common . NoPermissions ( w , r , user )
}
2018-05-31 06:51:31 +00:00
topicName := common . SanitiseSingleLine ( r . PostFormValue ( "topic-name" ) )
2018-01-22 08:15:45 +00:00
content := common . PreparseMessage ( r . PostFormValue ( "topic-content" ) )
// TODO: Fully parse the post and store it in the parsed column
tid , err := common . Topics . Create ( fid , topicName , content , user . ID , user . LastIP )
if err != nil {
switch err {
case common . ErrNoRows :
return common . LocalError ( "Something went wrong, perhaps the forum got deleted?" , w , r , user )
case common . ErrNoTitle :
return common . LocalError ( "This topic doesn't have a title" , w , r , user )
2018-03-17 08:16:43 +00:00
case common . ErrLongTitle :
return common . LocalError ( "The length of the title is too long, max: " + strconv . Itoa ( common . Config . MaxTopicTitleLength ) , w , r , user )
2018-01-22 08:15:45 +00:00
case common . ErrNoBody :
return common . LocalError ( "This topic doesn't have a body" , w , r , user )
2018-01-26 05:53:34 +00:00
}
return common . InternalError ( err , w , r )
}
topic , err := common . Topics . Get ( tid )
if err != nil {
return common . LocalError ( "Unable to load the topic" , w , r , user )
}
if r . PostFormValue ( "has_poll" ) == "1" {
var maxPollOptions = 10
var pollInputItems = make ( map [ int ] string )
for key , values := range r . Form {
for _ , value := range values {
if strings . HasPrefix ( key , "pollinputitem[" ) {
halves := strings . Split ( key , "[" )
if len ( halves ) != 2 {
return common . LocalError ( "Malformed pollinputitem" , w , r , user )
}
halves [ 1 ] = strings . TrimSuffix ( halves [ 1 ] , "]" )
index , err := strconv . Atoi ( halves [ 1 ] )
if err != nil {
return common . LocalError ( "Malformed pollinputitem" , w , r , user )
}
// If there are duplicates, then something has gone horribly wrong, so let's ignore them, this'll likely happen during an attack
_ , exists := pollInputItems [ index ]
2018-05-31 06:51:31 +00:00
// TODO: Should we use SanitiseBody instead to keep the newlines?
if ! exists && len ( common . SanitiseSingleLine ( value ) ) != 0 {
pollInputItems [ index ] = common . SanitiseSingleLine ( value )
2018-01-26 05:53:34 +00:00
if len ( pollInputItems ) >= maxPollOptions {
break
}
}
}
}
}
2018-01-27 07:30:44 +00:00
// Make sure the indices are sequential to avoid out of bounds issues
var seqPollInputItems = make ( map [ int ] string )
for i := 0 ; i < len ( pollInputItems ) ; i ++ {
seqPollInputItems [ i ] = pollInputItems [ i ]
}
2018-01-26 05:53:34 +00:00
pollType := 0 // Basic single choice
2018-01-27 07:30:44 +00:00
_ , err := common . Polls . Create ( topic , pollType , seqPollInputItems )
2018-01-26 05:53:34 +00:00
if err != nil {
return common . LocalError ( "Failed to add poll to topic" , w , r , user ) // TODO: Might need to be an internal error as it could leave phantom polls?
2018-01-22 08:15:45 +00:00
}
}
err = common . Subscriptions . Add ( user . ID , tid , "topic" )
if err != nil {
return common . InternalError ( err , w , r )
}
err = user . IncreasePostStats ( common . WordCount ( content ) , true )
if err != nil {
return common . InternalError ( err , w , r )
}
// Handle the file attachments
if user . Perms . UploadFiles {
2019-04-13 11:54:22 +00:00
_ , rerr := uploadAttachment ( w , r , user , fid , "forums" , tid , "topics" , "" )
2018-12-27 05:42:41 +00:00
if rerr != nil {
return rerr
}
}
2018-01-22 08:15:45 +00:00
2018-12-27 05:42:41 +00:00
counters . PostCounter . Bump ( )
counters . TopicCounter . Bump ( )
2019-04-06 01:08:49 +00:00
// TODO: Pass more data to this hook?
skip , rerr := lite . Hooks . VhookSkippable ( "action_end_create_topic" , tid )
if skip || rerr != nil {
return rerr
}
2018-12-27 05:42:41 +00:00
http . Redirect ( w , r , "/topic/" + strconv . Itoa ( tid ) , http . StatusSeeOther )
return nil
}
2018-01-22 08:15:45 +00:00
2018-12-27 05:42:41 +00:00
func uploadFilesWithHash ( w http . ResponseWriter , r * http . Request , user common . User , dir string ) ( filenames [ ] string , rerr common . RouteError ) {
files , ok := r . MultipartForm . File [ "upload_files" ]
if ! ok {
return nil , nil
}
if len ( files ) > 5 {
return nil , common . LocalError ( "You can't attach more than five files" , w , r , user )
}
2018-01-22 08:15:45 +00:00
2018-12-27 05:42:41 +00:00
for _ , file := range files {
if file . Filename == "" {
continue
}
//common.DebugLog("file.Filename ", file.Filename)
2018-01-22 08:15:45 +00:00
2018-12-27 05:42:41 +00:00
extarr := strings . Split ( file . Filename , "." )
if len ( extarr ) < 2 {
return nil , common . LocalError ( "Bad file" , w , r , user )
}
ext := extarr [ len ( extarr ) - 1 ]
2018-01-22 08:15:45 +00:00
2018-12-27 05:42:41 +00:00
// TODO: Can we do this without a regex?
reg , err := regexp . Compile ( "[^A-Za-z0-9]+" )
if err != nil {
return nil , common . LocalError ( "Bad file extension" , w , r , user )
}
ext = strings . ToLower ( reg . ReplaceAllString ( ext , "" ) )
if ! common . AllowedFileExts . Contains ( ext ) {
return nil , common . LocalError ( "You're not allowed to upload files with this extension" , w , r , user )
}
2018-01-22 08:15:45 +00:00
2018-12-27 05:42:41 +00:00
infile , err := file . Open ( )
if err != nil {
return nil , common . LocalError ( "Upload failed" , w , r , user )
}
defer infile . Close ( )
2018-01-22 08:15:45 +00:00
2018-12-27 05:42:41 +00:00
hasher := sha256 . New ( )
_ , err = io . Copy ( hasher , infile )
if err != nil {
return nil , common . LocalError ( "Upload failed [Hashing Failed]" , w , r , user )
}
infile . Close ( )
2018-01-22 08:15:45 +00:00
2018-12-27 05:42:41 +00:00
checksum := hex . EncodeToString ( hasher . Sum ( nil ) )
filename := checksum + "." + ext
outfile , err := os . Create ( dir + filename )
if err != nil {
return nil , common . LocalError ( "Upload failed [File Creation Failed]" , w , r , user )
}
defer outfile . Close ( )
infile , err = file . Open ( )
if err != nil {
return nil , common . LocalError ( "Upload failed" , w , r , user )
}
defer infile . Close ( )
_ , err = io . Copy ( outfile , infile )
if err != nil {
return nil , common . LocalError ( "Upload failed [Copy Failed]" , w , r , user )
}
filenames = append ( filenames , filename )
}
return filenames , nil
}
2018-01-18 12:31:25 +00:00
// TODO: Update the stats after edits so that we don't under or over decrement stats during deletes
// TODO: Disable stat updates in posts handled by plugin_guilds
func EditTopicSubmit ( w http . ResponseWriter , r * http . Request , user common . User , stid string ) common . RouteError {
isJs := ( r . PostFormValue ( "js" ) == "1" )
tid , err := strconv . Atoi ( stid )
if err != nil {
2018-11-01 06:43:56 +00:00
return common . PreErrorJSQ ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r , isJs )
2018-01-18 12:31:25 +00:00
}
topic , err := common . Topics . Get ( tid )
if err == sql . ErrNoRows {
return common . PreErrorJSQ ( "The topic you tried to edit doesn't exist." , w , r , isJs )
} else if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
// TODO: Add hooks to make use of headerLite
2019-04-19 01:02:33 +00:00
lite , ferr := common . SimpleForumUserCheck ( w , r , & user , topic . ParentID )
2018-01-18 12:31:25 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic || ! user . Perms . EditTopic {
return common . NoPermissionsJSQ ( w , r , user , isJs )
}
2018-06-01 05:02:29 +00:00
if topic . IsClosed && ! user . Perms . CloseTopic {
return common . NoPermissionsJSQ ( w , r , user , isJs )
}
2018-01-18 12:31:25 +00:00
err = topic . Update ( r . PostFormValue ( "topic_name" ) , r . PostFormValue ( "topic_content" ) )
2018-03-17 08:16:43 +00:00
// TODO: Avoid duplicating this across this route and the topic creation route
2018-01-18 12:31:25 +00:00
if err != nil {
2018-03-17 08:16:43 +00:00
switch err {
case common . ErrNoTitle :
return common . LocalErrorJSQ ( "This topic doesn't have a title" , w , r , user , isJs )
case common . ErrLongTitle :
return common . LocalErrorJSQ ( "The length of the title is too long, max: " + strconv . Itoa ( common . Config . MaxTopicTitleLength ) , w , r , user , isJs )
case common . ErrNoBody :
return common . LocalErrorJSQ ( "This topic doesn't have a body" , w , r , user , isJs )
}
2018-01-18 12:31:25 +00:00
return common . InternalErrorJSQ ( err , w , r , isJs )
}
err = common . Forums . UpdateLastTopic ( topic . ID , user . ID , topic . ParentID )
if err != nil && err != sql . ErrNoRows {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
2018-12-28 11:13:06 +00:00
// TODO: Avoid the load to get this faster?
topic , err = common . Topics . Get ( topic . ID )
if err == sql . ErrNoRows {
return common . PreErrorJSQ ( "The updated topic doesn't exist." , w , r , isJs )
} else if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
2019-04-19 01:02:33 +00:00
skip , rerr := lite . Hooks . VhookSkippable ( "action_end_edit_topic" , topic . ID , & user )
if skip || rerr != nil {
return rerr
}
2018-01-18 12:31:25 +00:00
if ! isJs {
http . Redirect ( w , r , "/topic/" + strconv . Itoa ( tid ) , http . StatusSeeOther )
} else {
2018-12-28 11:13:06 +00:00
outBytes , err := json . Marshal ( JsonReply { common . ParseMessage ( topic . Content , topic . ParentID , "forums" ) } )
if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
w . Write ( outBytes )
2018-01-18 12:31:25 +00:00
}
return nil
}
2018-01-20 06:50:29 +00:00
// TODO: Add support for soft-deletion and add a permission for hard delete in addition to the usual
// TODO: Disable stat updates in posts handled by plugin_guilds
func DeleteTopicSubmit ( w http . ResponseWriter , r * http . Request , user common . User ) common . RouteError {
// TODO: Move this to some sort of middleware
var tids [ ] int
var isJs = false
if common . ReqIsJson ( r ) {
if r . Body == nil {
return common . PreErrorJS ( "No request body" , w , r )
}
err := json . NewDecoder ( r . Body ) . Decode ( & tids )
if err != nil {
return common . PreErrorJS ( "We weren't able to parse your data" , w , r )
}
isJs = true
} else {
tid , err := strconv . Atoi ( r . URL . Path [ len ( "/topic/delete/submit/" ) : ] )
if err != nil {
return common . PreError ( "The provided TopicID is not a valid number." , w , r )
}
tids = append ( tids , tid )
}
if len ( tids ) == 0 {
return common . LocalErrorJSQ ( "You haven't provided any IDs" , w , r , user , isJs )
}
for _ , tid := range tids {
topic , err := common . Topics . Get ( tid )
if err == sql . ErrNoRows {
return common . PreErrorJSQ ( "The topic you tried to delete doesn't exist." , w , r , isJs )
} else if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
// TODO: Add hooks to make use of headerLite
2019-04-19 01:02:33 +00:00
lite , ferr := common . SimpleForumUserCheck ( w , r , & user , topic . ParentID )
2018-01-20 06:50:29 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic || ! user . Perms . DeleteTopic {
return common . NoPermissionsJSQ ( w , r , user , isJs )
}
// We might be able to handle this err better
err = topic . Delete ( )
if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
err = common . ModLogs . Create ( "delete" , tid , "topic" , user . LastIP , user . ID )
if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
// ? - We might need to add soft-delete before we can do an action reply for this
/ * _ , err = stmts . createActionReply . Exec ( tid , "delete" , ipaddress , user . ID )
if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
} * /
2019-04-19 01:02:33 +00:00
// 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
}
2018-02-26 09:07:00 +00:00
log . Printf ( "Topic #%d was deleted by UserID #%d" , tid , user . ID )
2018-01-20 06:50:29 +00:00
}
http . Redirect ( w , r , "/" , http . StatusSeeOther )
return nil
}
func StickTopicSubmit ( w http . ResponseWriter , r * http . Request , user common . User , stid string ) common . RouteError {
2019-04-19 01:02:33 +00:00
topic , lite , rerr := topicActionPre ( stid , "pin" , w , r , user )
2018-11-01 06:43:56 +00:00
if rerr != nil {
return rerr
}
if ! user . Perms . ViewTopic || ! user . Perms . PinTopic {
return common . NoPermissions ( w , r , user )
}
2019-04-19 01:02:33 +00:00
return topicActionPost ( topic . Stick ( ) , "stick" , w , r , lite , topic , user )
2018-11-01 06:43:56 +00:00
}
2019-04-19 01:02:33 +00:00
func topicActionPre ( stid string , action string , w http . ResponseWriter , r * http . Request , user common . User ) ( * common . Topic , * common . HeaderLite , common . RouteError ) {
2018-01-20 06:50:29 +00:00
tid , err := strconv . Atoi ( stid )
if err != nil {
2019-04-19 01:02:33 +00:00
return nil , nil , common . PreError ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r )
2018-01-20 06:50:29 +00:00
}
topic , err := common . Topics . Get ( tid )
if err == sql . ErrNoRows {
2019-04-19 01:02:33 +00:00
return nil , nil , common . PreError ( "The topic you tried to " + action + " doesn't exist." , w , r )
2018-01-20 06:50:29 +00:00
} else if err != nil {
2019-04-19 01:02:33 +00:00
return nil , nil , common . InternalError ( err , w , r )
2018-01-20 06:50:29 +00:00
}
// TODO: Add hooks to make use of headerLite
2019-04-19 01:02:33 +00:00
lite , ferr := common . SimpleForumUserCheck ( w , r , & user , topic . ParentID )
2018-01-20 06:50:29 +00:00
if ferr != nil {
2019-04-19 01:02:33 +00:00
return nil , nil , ferr
2018-01-20 06:50:29 +00:00
}
2019-04-19 01:02:33 +00:00
return topic , lite , nil
2018-11-01 06:43:56 +00:00
}
2019-04-19 01:02:33 +00:00
func topicActionPost ( err error , action string , w http . ResponseWriter , r * http . Request , lite * common . HeaderLite , topic * common . Topic , user common . User ) common . RouteError {
2018-01-20 06:50:29 +00:00
if err != nil {
return common . InternalError ( err , w , r )
}
2018-11-01 06:43:56 +00:00
err = addTopicAction ( action , topic , user )
2018-01-20 06:50:29 +00:00
if err != nil {
return common . InternalError ( err , w , r )
}
2019-04-19 01:02:33 +00:00
skip , rerr := lite . Hooks . VhookSkippable ( "action_end_" + action + "_topic" , topic . ID , & user )
if skip || rerr != nil {
return rerr
}
2018-11-01 06:43:56 +00:00
http . Redirect ( w , r , "/topic/" + strconv . Itoa ( topic . ID ) , http . StatusSeeOther )
2018-01-20 06:50:29 +00:00
return nil
}
func UnstickTopicSubmit ( w http . ResponseWriter , r * http . Request , user common . User , stid string ) common . RouteError {
2019-04-19 01:02:33 +00:00
topic , lite , rerr := topicActionPre ( stid , "unpin" , w , r , user )
2018-11-01 06:43:56 +00:00
if rerr != nil {
return rerr
2018-01-20 06:50:29 +00:00
}
if ! user . Perms . ViewTopic || ! user . Perms . PinTopic {
return common . NoPermissions ( w , r , user )
}
2019-04-19 01:02:33 +00:00
return topicActionPost ( topic . Unstick ( ) , "unstick" , w , r , lite , topic , user )
2018-01-20 06:50:29 +00:00
}
func LockTopicSubmit ( w http . ResponseWriter , r * http . Request , user common . User ) common . RouteError {
// TODO: Move this to some sort of middleware
var tids [ ] int
var isJs = false
if common . ReqIsJson ( r ) {
if r . Body == nil {
return common . PreErrorJS ( "No request body" , w , r )
}
err := json . NewDecoder ( r . Body ) . Decode ( & tids )
if err != nil {
return common . PreErrorJS ( "We weren't able to parse your data" , w , r )
}
isJs = true
} else {
tid , err := strconv . Atoi ( r . URL . Path [ len ( "/topic/lock/submit/" ) : ] )
if err != nil {
return common . PreError ( "The provided TopicID is not a valid number." , w , r )
}
tids = append ( tids , tid )
}
if len ( tids ) == 0 {
return common . LocalErrorJSQ ( "You haven't provided any IDs" , w , r , user , isJs )
}
for _ , tid := range tids {
topic , err := common . Topics . Get ( tid )
if err == sql . ErrNoRows {
return common . PreErrorJSQ ( "The topic you tried to lock doesn't exist." , w , r , isJs )
} else if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
// TODO: Add hooks to make use of headerLite
2019-04-19 01:02:33 +00:00
lite , ferr := common . SimpleForumUserCheck ( w , r , & user , topic . ParentID )
2018-01-20 06:50:29 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic || ! user . Perms . CloseTopic {
return common . NoPermissionsJSQ ( w , r , user , isJs )
}
err = topic . Lock ( )
if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
2018-01-21 11:17:43 +00:00
err = addTopicAction ( "lock" , topic , user )
2018-01-20 06:50:29 +00:00
if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
2019-04-19 01:02:33 +00:00
// 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
}
2018-01-20 06:50:29 +00:00
}
if len ( tids ) == 1 {
http . Redirect ( w , r , "/topic/" + strconv . Itoa ( tids [ 0 ] ) , http . StatusSeeOther )
}
return nil
}
func UnlockTopicSubmit ( w http . ResponseWriter , r * http . Request , user common . User , stid string ) common . RouteError {
2019-04-19 01:02:33 +00:00
topic , lite , rerr := topicActionPre ( stid , "unlock" , w , r , user )
2018-11-01 06:43:56 +00:00
if rerr != nil {
return rerr
2018-01-20 06:50:29 +00:00
}
if ! user . Perms . ViewTopic || ! user . Perms . CloseTopic {
return common . NoPermissions ( w , r , user )
}
2019-04-19 01:02:33 +00:00
return topicActionPost ( topic . Unlock ( ) , "unlock" , w , r , lite , topic , user )
2018-01-20 06:50:29 +00:00
}
// ! JS only route
// TODO: Figure a way to get this route to work without JS
func MoveTopicSubmit ( w http . ResponseWriter , r * http . Request , user common . User , sfid string ) common . RouteError {
fid , err := strconv . Atoi ( sfid )
if err != nil {
2018-11-01 06:43:56 +00:00
return common . PreErrorJS ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r )
2018-01-20 06:50:29 +00:00
}
// TODO: Move this to some sort of middleware
var tids [ ] int
if r . Body == nil {
return common . PreErrorJS ( "No request body" , w , r )
}
err = json . NewDecoder ( r . Body ) . Decode ( & tids )
if err != nil {
return common . PreErrorJS ( "We weren't able to parse your data" , w , r )
}
if len ( tids ) == 0 {
return common . LocalErrorJS ( "You haven't provided any IDs" , w , r )
}
for _ , tid := range tids {
topic , err := common . Topics . Get ( tid )
if err == sql . ErrNoRows {
return common . PreErrorJS ( "The topic you tried to move doesn't exist." , w , r )
} else if err != nil {
return common . InternalErrorJS ( err , w , r )
}
// TODO: Add hooks to make use of headerLite
_ , ferr := common . SimpleForumUserCheck ( w , r , & user , topic . ParentID )
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic || ! user . Perms . MoveTopic {
return common . NoPermissionsJS ( w , r , user )
}
2019-04-19 01:02:33 +00:00
lite , ferr := common . SimpleForumUserCheck ( w , r , & user , fid )
2018-01-20 06:50:29 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic || ! user . Perms . MoveTopic {
return common . NoPermissionsJS ( w , r , user )
}
err = topic . MoveTo ( fid )
if err != nil {
return common . InternalErrorJS ( err , w , r )
}
2019-04-17 10:12:35 +00:00
// ? - Is there a better way of doing this?
err = addTopicAction ( "move-" + strconv . Itoa ( fid ) , topic , user )
2018-01-20 06:50:29 +00:00
if err != nil {
return common . InternalErrorJS ( err , w , r )
}
2019-04-19 01:02:33 +00:00
// 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
}
2018-01-20 06:50:29 +00:00
}
if len ( tids ) == 1 {
http . Redirect ( w , r , "/topic/" + strconv . Itoa ( tids [ 0 ] ) , http . StatusSeeOther )
}
return nil
}
2018-01-21 11:17:43 +00:00
func addTopicAction ( action string , topic * common . Topic , user common . User ) error {
err := common . ModLogs . Create ( action , topic . ID , "topic" , user . LastIP , user . ID )
if err != nil {
return err
}
2018-10-08 04:15:06 +00:00
return topic . CreateActionReply ( action , user . LastIP , user . ID )
2018-01-21 11:17:43 +00:00
}
2018-05-15 05:59:52 +00:00
// TODO: Refactor this
func LikeTopicSubmit ( w http . ResponseWriter , r * http . Request , user common . User , stid string ) common . RouteError {
isJs := ( r . PostFormValue ( "isJs" ) == "1" )
tid , err := strconv . Atoi ( stid )
if err != nil {
2018-11-01 06:43:56 +00:00
return common . PreErrorJSQ ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r , isJs )
2018-05-15 05:59:52 +00:00
}
topic , err := common . Topics . Get ( tid )
if err == sql . ErrNoRows {
return common . PreErrorJSQ ( "The requested topic doesn't exist." , w , r , isJs )
} else if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
// TODO: Add hooks to make use of headerLite
2019-04-19 01:02:33 +00:00
lite , ferr := common . SimpleForumUserCheck ( w , r , & user , topic . ParentID )
2018-05-15 05:59:52 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic || ! user . Perms . LikeItem {
return common . NoPermissionsJSQ ( w , r , user , isJs )
}
if topic . CreatedBy == user . ID {
return common . LocalErrorJSQ ( "You can't like your own topics" , w , r , user , isJs )
}
_ , err = common . Users . Get ( topic . CreatedBy )
if err != nil && err == sql . ErrNoRows {
return common . LocalErrorJSQ ( "The target user doesn't exist" , w , r , user , isJs )
} else if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
score := 1
err = topic . Like ( score , user . ID )
if err == common . ErrAlreadyLiked {
return common . LocalErrorJSQ ( "You already liked this" , w , r , user , isJs )
} else if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
2018-11-22 07:21:43 +00:00
// ! Be careful about leaking per-route permission state with &user
alert := common . Alert { 0 , user . ID , topic . CreatedBy , "like" , "topic" , tid , & user }
err = common . AddActivityAndNotifyTarget ( alert )
2018-05-15 05:59:52 +00:00
if err != nil {
return common . InternalErrorJSQ ( err , w , r , isJs )
}
2019-04-19 01:02:33 +00:00
skip , rerr := lite . Hooks . VhookSkippable ( "action_end_like_topic" , topic . ID , & user )
if skip || rerr != nil {
return rerr
}
2018-05-15 05:59:52 +00:00
if ! isJs {
http . Redirect ( w , r , "/topic/" + strconv . Itoa ( tid ) , http . StatusSeeOther )
} else {
_ , _ = w . Write ( successJSONBytes )
}
return nil
}