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"
2019-10-06 11:32:00 +00:00
2019-06-01 12:31:48 +00:00
//"fmt"
2019-12-08 03:40:56 +00:00
"image"
"image/gif"
"image/jpeg"
"image/png"
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
2019-12-31 21:57:54 +00:00
"golang.org/x/image/tiff"
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-11-01 06:43:56 +00:00
"github.com/Azareal/Gosora/common/phrases"
2019-10-06 11:32:00 +00:00
qgen "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
getLikedTopic * sql . Stmt
2018-02-10 15:07:21 +00:00
}
var topicStmts TopicStmts
// TODO: Move these DbInits into a TopicList abstraction
func init ( ) {
2019-04-19 06:36:26 +00:00
c . DbInits . Add ( func ( acc * qgen . Accumulator ) error {
2018-02-10 15:07:21 +00:00
topicStmts = TopicStmts {
2020-02-09 10:00:08 +00:00
getLikedTopic : acc . Select ( "likes" ) . Columns ( "targetItem" ) . Where ( "sentBy=? && targetItem=? && targetType='topics'" ) . Prepare ( ) ,
2018-02-10 15:07:21 +00:00
}
return acc . FirstError ( )
} )
}
2019-04-19 06:36:26 +00:00
func ViewTopic ( w http . ResponseWriter , r * http . Request , user c . User , header * c . Header , urlBit string ) c . 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 {
2019-10-06 11:32:00 +00:00
return c . SimpleError ( phrases . GetErrorPhrase ( "url_id_must_be_integer" ) , w , r , header )
2018-02-03 05:47:14 +00:00
}
// Get the topic...
2019-04-19 06:36:26 +00:00
topic , err := c . GetTopicUser ( & user , tid )
2018-02-03 05:47:14 +00:00
if err == sql . ErrNoRows {
2019-04-19 06:36:26 +00:00
return c . 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 {
2019-04-19 06:36:26 +00:00
return c . InternalError ( err , w , r )
2018-02-03 05:47:14 +00:00
}
2019-04-19 06:36:26 +00:00
ferr := c . ForumUserCheck ( header , w , r , & user , topic . ParentID )
2018-02-03 05:47:14 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic {
2019-04-19 06:36:26 +00:00
return c . NoPermissions ( w , r , user )
2018-02-03 05:47:14 +00:00
}
2018-05-27 09:36:35 +00:00
header . Title = topic . Title
2019-04-19 06:36:26 +00:00
header . Path = c . BuildTopicURL ( c . NameToSlug ( topic . Title ) , topic . ID )
2018-02-03 05:47:14 +00:00
2020-03-09 07:11:58 +00:00
postGroup , err := c . Groups . Get ( topic . Group )
if err != nil {
return c . InternalError ( err , w , r )
}
2018-02-03 05:47:14 +00:00
topic . ContentLines = strings . Count ( topic . Content , "\n" )
2019-12-08 03:40:56 +00:00
if len ( topic . Content ) > 200 {
header . OGDesc = topic . Content [ : 197 ] + "..."
} else {
header . OGDesc = topic . Content
2019-02-24 08:02:00 +00:00
}
2020-02-04 11:47:03 +00:00
var parseSettings * c . ParseSettings
if ! postGroup . Perms . AutoEmbed && ( user . ParseSettings == nil || ! user . ParseSettings . NoEmbed ) {
parseSettings = c . DefaultParseSettings . CopyPtr ( )
parseSettings . NoEmbed = true
} else {
parseSettings = user . ParseSettings
}
// TODO: Cache ContentHTML when possible?
topic . ContentHTML = c . ParseMessage ( topic . Content , topic . ParentID , "forums" , parseSettings , & user )
// TODO: Do this more efficiently by avoiding the allocations entirely in ParseMessage, if there's nothing to do.
if topic . ContentHTML == topic . Content {
topic . ContentHTML = topic . Content
}
2020-02-09 10:00:08 +00:00
2018-02-03 05:47:14 +00:00
topic . Tag = postGroup . Tag
2018-06-24 13:49:29 +00:00
if postGroup . IsMod {
2019-04-19 06:36:26 +00:00
topic . ClassName = c . Config . StaffCSS
2018-02-03 05:47:14 +00:00
}
2019-10-06 11:32:00 +00:00
topic . Deletable = user . Perms . DeleteTopic || topic . CreatedBy == user . ID
2018-02-03 05:47:14 +00:00
2019-04-19 06:36:26 +00:00
forum , err := c . Forums . Get ( topic . ParentID )
2018-08-30 10:31:21 +00:00
if err != nil {
2019-04-19 06:36:26 +00:00
return c . InternalError ( err , w , r )
2018-08-30 10:31:21 +00:00
}
2019-04-19 06:36:26 +00:00
var poll c . Poll
2018-02-03 05:47:14 +00:00
if topic . Poll != 0 {
2019-04-19 06:36:26 +00:00
pPoll , err := c . Polls . Get ( topic . Poll )
2018-02-03 05:47:14 +00:00
if err != nil {
log . Print ( "Couldn't find the attached poll for topic " + strconv . Itoa ( topic . ID ) )
2019-04-19 06:36:26 +00:00
return c . InternalError ( err , w , r )
2018-02-03 05:47:14 +00:00
}
poll = pPoll . Copy ( )
}
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 {
2019-04-19 06:36:26 +00:00
return c . InternalError ( err , w , r )
2018-03-31 05:25:27 +00:00
}
}
2018-12-27 05:42:41 +00:00
if topic . AttachCount > 0 {
2019-04-19 06:36:26 +00:00
attachs , err := c . Attachments . MiniGetList ( "topics" , topic . ID )
2020-02-19 10:32:26 +00:00
if err != nil && err != sql . ErrNoRows {
2018-12-27 05:42:41 +00:00
// TODO: We might want to be a little permissive here in-case of a desync?
2019-04-19 06:36:26 +00:00
return c . InternalError ( err , w , r )
2018-12-27 05:42:41 +00:00
}
topic . Attachments = attachs
}
2018-02-03 05:47:14 +00:00
// Calculate the offset
2019-04-19 06:36:26 +00:00
offset , page , lastPage := c . PageOffset ( topic . PostCount , page , c . Config . ItemsPerPage )
2019-06-04 05:48:12 +00:00
pageList := c . Paginate ( page , lastPage , 5 )
2019-05-17 08:40:41 +00:00
tpage := c . TopicPage { header , nil , topic , forum , poll , c . 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-" ) )
}
2019-05-17 08:40:41 +00:00
rlist , ogdesc , err := topic . Replies ( offset , pFrag , & user )
2018-02-15 13:15:27 +00:00
if err == sql . ErrNoRows {
2019-04-19 06:36:26 +00:00
return c . 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 )
2018-02-15 13:15:27 +00:00
} else if err != nil {
2019-04-19 06:36:26 +00:00
return c . InternalError ( err , w , r )
2018-02-03 05:47:14 +00:00
}
2019-05-17 08:40:41 +00:00
header . OGDesc = ogdesc
tpage . ItemList = rlist
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
2019-06-01 12:31:48 +00:00
var rerr c . RouteError
tmpl := forum . Tmpl
2020-03-11 03:26:33 +00:00
if r . FormValue ( "i" ) == "1" {
2020-03-13 02:56:23 +00:00
if tpage . Poll . ID != 0 {
header . AddXRes ( "chartist/chartist.min.css" , "chartist/chartist.min.js" )
}
2020-03-11 03:26:33 +00:00
if tmpl == "" {
rerr = renderTemplate ( "topic_mini" , w , r , header , tpage )
} else {
tmpl = "topic_mini" + tmpl
err = renderTemplate3 ( tmpl , tmpl , w , r , header , tpage )
if err != nil {
rerr = renderTemplate ( "topic_mini" , w , r , header , tpage )
}
}
2019-06-01 12:31:48 +00:00
} else {
2020-03-13 02:56:23 +00:00
if tpage . Poll . ID != 0 {
header . AddSheet ( "chartist/chartist.min.css" )
header . AddScript ( "chartist/chartist.min.js" )
}
2020-03-11 03:26:33 +00:00
if tmpl == "" {
2019-06-01 12:31:48 +00:00
rerr = renderTemplate ( "topic" , w , r , header , tpage )
2020-03-11 03:26:33 +00:00
} else {
tmpl = "topic_" + tmpl
err = renderTemplate3 ( tmpl , tmpl , w , r , header , tpage )
if err != nil {
rerr = renderTemplate ( "topic" , w , r , header , tpage )
}
2019-06-01 12:31:48 +00:00
}
}
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
2019-04-19 06:36:26 +00:00
func AddAttachToTopicSubmit ( w http . ResponseWriter , r * http . Request , user c . User , stid string ) c . RouteError {
2018-12-27 05:42:41 +00:00
tid , err := strconv . Atoi ( stid )
if err != nil {
2019-04-19 06:36:26 +00:00
return c . LocalErrorJS ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r )
2018-12-27 05:42:41 +00:00
}
2019-04-19 06:36:26 +00:00
topic , err := c . Topics . Get ( tid )
2018-12-27 05:42:41 +00:00
if err != nil {
2019-04-19 06:36:26 +00:00
return c . NotFoundJS ( w , r )
2018-12-27 05:42:41 +00:00
}
2019-04-19 06:36:26 +00:00
_ , ferr := c . SimpleForumUserCheck ( w , r , & user , topic . ParentID )
2018-12-27 05:42:41 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic || ! user . Perms . EditTopic || ! user . Perms . UploadFiles {
2019-04-19 06:36:26 +00:00
return c . NoPermissionsJS ( w , r , user )
2018-12-27 05:42:41 +00:00
}
if topic . IsClosed && ! user . Perms . CloseTopic {
2019-04-19 06:36:26 +00:00
return c . NoPermissionsJS ( w , r , user )
2018-12-27 05:42:41 +00:00
}
// 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 {
2019-04-19 06:36:26 +00:00
return c . InternalErrorJS ( errors . New ( "no paths for attachment add" ) , w , r )
2018-12-27 05:42:41 +00:00
}
var elemStr string
for path , aids := range pathMap {
elemStr += "\"" + path + "\":\"" + aids + "\","
}
if len ( elemStr ) > 1 {
elemStr = elemStr [ : len ( elemStr ) - 1 ]
}
2019-10-27 23:55:48 +00:00
w . Write ( [ ] byte ( ` { "success":1,"elems":[ { ` + elemStr + ` }]} ` ) )
2018-12-27 05:42:41 +00:00
return nil
}
2019-04-19 06:36:26 +00:00
func RemoveAttachFromTopicSubmit ( w http . ResponseWriter , r * http . Request , user c . User , stid string ) c . RouteError {
2018-12-27 05:42:41 +00:00
tid , err := strconv . Atoi ( stid )
if err != nil {
2019-04-19 06:36:26 +00:00
return c . LocalErrorJS ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r )
2018-12-27 05:42:41 +00:00
}
2019-04-19 06:36:26 +00:00
topic , err := c . Topics . Get ( tid )
2018-12-27 05:42:41 +00:00
if err != nil {
2019-04-19 06:36:26 +00:00
return c . NotFoundJS ( w , r )
2018-12-27 05:42:41 +00:00
}
2019-04-19 06:36:26 +00:00
_ , ferr := c . SimpleForumUserCheck ( w , r , & user , topic . ParentID )
2018-12-27 05:42:41 +00:00
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic || ! user . Perms . EditTopic {
2019-04-19 06:36:26 +00:00
return c . NoPermissionsJS ( w , r , user )
2018-12-27 05:42:41 +00:00
}
if topic . IsClosed && ! user . Perms . CloseTopic {
2019-04-19 06:36:26 +00:00
return c . NoPermissionsJS ( w , r , user )
2018-12-27 05:42:41 +00:00
}
for _ , said := range strings . Split ( r . PostFormValue ( "aids" ) , "," ) {
aid , err := strconv . Atoi ( said )
if err != nil {
2019-04-19 06:36:26 +00:00
return c . LocalErrorJS ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r )
2018-12-27 05:42:41 +00:00
}
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
2019-04-19 06:36:26 +00:00
func CreateTopic ( w http . ResponseWriter , r * http . Request , user c . User , header * c . Header , sfid string ) c . 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 {
2019-04-19 06:36:26 +00:00
return c . LocalError ( phrases . GetErrorPhrase ( "url_id_must_be_integer" ) , w , r , user )
2018-01-21 11:17:43 +00:00
}
}
if fid == 0 {
2019-04-19 06:36:26 +00:00
fid = c . Config . DefaultForum
2018-01-21 11:17:43 +00:00
}
2019-04-19 06:36:26 +00:00
ferr := c . 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 {
2019-04-19 06:36:26 +00:00
return c . NoPermissions ( w , r , user )
2018-01-21 11:17:43 +00:00
}
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)
2019-10-01 21:06:22 +00:00
var strict bool
header . Hooks . VhookNoRet ( "topic_create_pre_loop" , w , r , fid , & header , & user , & strict )
2018-01-21 11:17:43 +00:00
// TODO: Re-add support for plugin_guilds
2019-04-19 06:36:26 +00:00
var forumList [ ] c . Forum
2018-01-21 11:17:43 +00:00
var canSee [ ] int
if user . IsSuperAdmin {
2019-04-19 06:36:26 +00:00
canSee , err = c . Forums . GetAllVisibleIDs ( )
2018-01-21 11:17:43 +00:00
if err != nil {
2019-04-19 06:36:26 +00:00
return c . InternalError ( err , w , r )
2018-01-21 11:17:43 +00:00
}
} else {
2019-04-19 06:36:26 +00:00
group , err := c . Groups . Get ( user . Group )
2018-01-21 11:17:43 +00:00
if err != nil {
// TODO: Refactor this
2019-04-19 06:36:26 +00:00
c . LocalError ( "Something weird happened behind the scenes" , w , r , user )
log . Printf ( "Group #%d doesn't exist, but it's set on c.User #%d" , user . Group , user . ID )
2018-01-21 11:17:43 +00:00
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
2019-10-01 21:06:22 +00:00
if strict && ffid != fid {
2018-01-21 11:17:43 +00:00
continue
}
// Do a bulk forum fetch, just in case it's the SqlForumStore?
2020-02-09 10:00:08 +00:00
f := c . Forums . DirtyGet ( ffid )
if f . Name != "" && f . Active {
fcopy := f . 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 )
}
}
2019-05-06 04:04:00 +00:00
return renderTemplate ( "create_topic" , w , r , header , c . CreateTopicPage { header , forumList , fid } )
2018-01-21 11:17:43 +00:00
}
2019-04-19 06:36:26 +00:00
func CreateTopicSubmit ( w http . ResponseWriter , r * http . Request , user c . User ) c . RouteError {
2019-12-31 21:57:54 +00:00
fid , err := strconv . Atoi ( r . PostFormValue ( "board" ) )
2018-01-22 08:15:45 +00:00
if err != nil {
2019-10-06 11:32:00 +00:00
return c . LocalError ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r , user )
2018-01-22 08:15:45 +00:00
}
// TODO: Add hooks to make use of headerLite
2019-04-19 06:36:26 +00:00
lite , ferr := c . 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 {
2019-04-19 06:36:26 +00:00
return c . NoPermissions ( w , r , user )
2018-01-22 08:15:45 +00:00
}
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
name := c . SanitiseSingleLine ( r . PostFormValue ( "name" ) )
2019-12-31 21:57:54 +00:00
content := c . PreparseMessage ( r . PostFormValue ( "content" ) )
2018-01-22 08:15:45 +00:00
// TODO: Fully parse the post and store it in the parsed column
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
tid , err := c . Topics . Create ( fid , name , content , user . ID , user . GetIP ( ) )
2018-01-22 08:15:45 +00:00
if err != nil {
switch err {
2019-04-19 06:36:26 +00:00
case c . ErrNoRows :
return c . LocalError ( "Something went wrong, perhaps the forum got deleted?" , w , r , user )
case c . ErrNoTitle :
return c . LocalError ( "This topic doesn't have a title" , w , r , user )
case c . ErrLongTitle :
return c . LocalError ( "The length of the title is too long, max: " + strconv . Itoa ( c . Config . MaxTopicTitleLength ) , w , r , user )
case c . ErrNoBody :
return c . LocalError ( "This topic doesn't have a body" , w , r , user )
2018-01-26 05:53:34 +00:00
}
2019-04-19 06:36:26 +00:00
return c . InternalError ( err , w , r )
2018-01-26 05:53:34 +00:00
}
2019-04-19 06:36:26 +00:00
topic , err := c . Topics . Get ( tid )
2018-01-26 05:53:34 +00:00
if err != nil {
2019-04-19 06:36:26 +00:00
return c . LocalError ( "Unable to load the topic" , w , r , user )
2018-01-26 05:53:34 +00:00
}
if r . PostFormValue ( "has_poll" ) == "1" {
2019-10-01 21:06:22 +00:00
maxPollOptions := 10
pollInputItems := make ( map [ int ] string )
2018-01-26 05:53:34 +00:00
for key , values := range r . Form {
for _ , value := range values {
2019-06-15 12:26:37 +00:00
if ! strings . HasPrefix ( key , "pollinputitem[" ) {
continue
}
2019-07-28 04:58:01 +00:00
halves := strings . Split ( key , "[" )
if len ( halves ) != 2 {
return c . LocalError ( "Malformed pollinputitem" , w , r , user )
}
halves [ 1 ] = strings . TrimSuffix ( halves [ 1 ] , "]" )
2018-01-26 05:53:34 +00:00
2019-07-28 04:58:01 +00:00
index , err := strconv . Atoi ( halves [ 1 ] )
if err != nil {
return c . LocalError ( "Malformed pollinputitem" , w , r , user )
}
2018-01-26 05:53:34 +00:00
2019-07-28 04:58:01 +00:00
// 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 ]
// TODO: Should we use SanitiseBody instead to keep the newlines?
if ! exists && len ( c . SanitiseSingleLine ( value ) ) != 0 {
pollInputItems [ index ] = c . SanitiseSingleLine ( value )
if len ( pollInputItems ) >= maxPollOptions {
break
2018-01-26 05:53:34 +00:00
}
2019-07-28 04:58:01 +00:00
}
2018-01-26 05:53:34 +00:00
}
}
2019-06-15 12:26:37 +00:00
if len ( pollInputItems ) > 0 {
// Make sure the indices are sequential to avoid out of bounds issues
2019-10-01 21:06:22 +00:00
seqPollInputItems := make ( map [ int ] string )
2019-06-15 12:26:37 +00:00
for i := 0 ; i < len ( pollInputItems ) ; i ++ {
seqPollInputItems [ i ] = pollInputItems [ i ]
}
2018-01-27 07:30:44 +00:00
2019-06-15 12:26:37 +00:00
pollType := 0 // Basic single choice
_ , err := c . Polls . Create ( topic , pollType , seqPollInputItems )
if err != nil {
return c . 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
}
}
2019-04-19 06:36:26 +00:00
err = c . Subscriptions . Add ( user . ID , tid , "topic" )
2018-01-22 08:15:45 +00:00
if err != nil {
2019-04-19 06:36:26 +00:00
return c . InternalError ( err , w , r )
2018-01-22 08:15:45 +00:00
}
2019-04-19 06:36:26 +00:00
err = user . IncreasePostStats ( c . WordCount ( content ) , true )
2018-01-22 08:15:45 +00:00
if err != nil {
2019-04-19 06:36:26 +00:00
return c . InternalError ( err , w , r )
2018-01-22 08:15:45 +00:00
}
// 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?
2019-04-25 06:02:51 +00:00
skip , rerr := lite . Hooks . VhookSkippable ( "action_end_create_topic" , tid , & user )
2019-04-06 01:08:49 +00:00
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
2020-02-19 10:32:26 +00:00
// TODO: Move this function
2019-04-19 06:36:26 +00:00
func uploadFilesWithHash ( w http . ResponseWriter , r * http . Request , user c . User , dir string ) ( filenames [ ] string , rerr c . RouteError ) {
2018-12-27 05:42:41 +00:00
files , ok := r . MultipartForm . File [ "upload_files" ]
if ! ok {
return nil , nil
}
if len ( files ) > 5 {
2019-04-19 06:36:26 +00:00
return nil , c . LocalError ( "You can't attach more than five files" , w , r , user )
2018-12-27 05:42:41 +00:00
}
2019-10-30 19:25:45 +00:00
disableEncode := r . PostFormValue ( "ko" ) == "1"
2018-01-22 08:15:45 +00:00
2018-12-27 05:42:41 +00:00
for _ , file := range files {
if file . Filename == "" {
continue
}
2019-04-19 06:36:26 +00:00
//c.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 {
2019-04-19 06:36:26 +00:00
return nil , c . LocalError ( "Bad file" , w , r , user )
2018-12-27 05:42:41 +00:00
}
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 {
2019-04-19 06:36:26 +00:00
return nil , c . LocalError ( "Bad file extension" , w , r , user )
2018-12-27 05:42:41 +00:00
}
ext = strings . ToLower ( reg . ReplaceAllString ( ext , "" ) )
2019-04-19 06:36:26 +00:00
if ! c . AllowedFileExts . Contains ( ext ) {
return nil , c . LocalError ( "You're not allowed to upload files with this extension" , w , r , user )
2018-12-27 05:42:41 +00:00
}
2018-01-22 08:15:45 +00:00
2019-10-27 23:55:48 +00:00
inFile , err := file . Open ( )
2018-12-27 05:42:41 +00:00
if err != nil {
2019-04-19 06:36:26 +00:00
return nil , c . LocalError ( "Upload failed" , w , r , user )
2018-12-27 05:42:41 +00:00
}
2019-10-27 23:55:48 +00:00
defer inFile . Close ( )
2018-01-22 08:15:45 +00:00
2018-12-27 05:42:41 +00:00
hasher := sha256 . New ( )
2019-10-27 23:55:48 +00:00
_ , err = io . Copy ( hasher , inFile )
2018-12-27 05:42:41 +00:00
if err != nil {
2019-04-19 06:36:26 +00:00
return nil , c . LocalError ( "Upload failed [Hashing Failed]" , w , r , user )
2018-12-27 05:42:41 +00:00
}
2019-10-27 23:55:48 +00:00
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
2019-10-27 23:55:48 +00:00
inFile , err = file . Open ( )
2018-12-27 05:42:41 +00:00
if err != nil {
2019-04-19 06:36:26 +00:00
return nil , c . LocalError ( "Upload failed" , w , r , user )
2018-12-27 05:42:41 +00:00
}
2019-10-27 23:55:48 +00:00
defer inFile . Close ( )
2018-12-27 05:42:41 +00:00
2019-10-30 19:25:45 +00:00
if disableEncode || ( ext != "jpg" && ext != "jpeg" && ext != "png" && ext != "gif" && ext != "tiff" && ext != "tif" ) {
2019-10-27 23:55:48 +00:00
outFile , err := os . Create ( dir + filename )
if err != nil {
return nil , c . LocalError ( "Upload failed [File Creation Failed]" , w , r , user )
}
defer outFile . Close ( )
_ , err = io . Copy ( outFile , inFile )
if err != nil {
return nil , c . LocalError ( "Upload failed [Copy Failed]" , w , r , user )
}
} else {
img , _ , err := image . Decode ( inFile )
if err != nil {
2019-12-08 03:40:56 +00:00
return nil , c . LocalError ( "Upload failed [Image Decoding Failed]" , w , r , user )
2019-10-27 23:55:48 +00:00
}
outFile , err := os . Create ( dir + filename )
if err != nil {
return nil , c . LocalError ( "Upload failed [File Creation Failed]" , w , r , user )
}
defer outFile . Close ( )
2019-12-08 03:40:56 +00:00
2019-10-27 23:55:48 +00:00
switch ext {
case "gif" :
err = gif . Encode ( outFile , img , nil )
case "png" :
err = png . Encode ( outFile , img )
2019-12-08 03:40:56 +00:00
case "tiff" , "tif" :
err = tiff . Encode ( outFile , img , nil )
2019-10-27 23:55:48 +00:00
default :
err = jpeg . Encode ( outFile , img , nil )
}
if err != nil {
2019-12-08 03:40:56 +00:00
return nil , c . LocalError ( "Upload failed [Image Encoding Failed]" , w , r , user )
2019-10-27 23:55:48 +00:00
}
2018-12-27 05:42:41 +00:00
}
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
2019-04-19 06:36:26 +00:00
func EditTopicSubmit ( w http . ResponseWriter , r * http . Request , user c . User , stid string ) c . RouteError {
2019-09-30 10:15:50 +00:00
js := ( r . PostFormValue ( "js" ) == "1" )
2018-01-18 12:31:25 +00:00
tid , err := strconv . Atoi ( stid )
if err != nil {
2019-09-30 10:15:50 +00:00
return c . PreErrorJSQ ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r , js )
2018-01-18 12:31:25 +00:00
}
2019-04-19 06:36:26 +00:00
topic , err := c . Topics . Get ( tid )
2018-01-18 12:31:25 +00:00
if err == sql . ErrNoRows {
2019-09-30 10:15:50 +00:00
return c . PreErrorJSQ ( "The topic you tried to edit doesn't exist." , w , r , js )
2018-01-18 12:31:25 +00:00
} else if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-01-18 12:31:25 +00:00
}
// TODO: Add hooks to make use of headerLite
2019-04-19 06:36:26 +00:00
lite , ferr := c . 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 {
2019-09-30 10:15:50 +00:00
return c . NoPermissionsJSQ ( w , r , user , js )
2018-01-18 12:31:25 +00:00
}
2018-06-01 05:02:29 +00:00
if topic . IsClosed && ! user . Perms . CloseTopic {
2019-09-30 10:15:50 +00:00
return c . NoPermissionsJSQ ( w , r , user , js )
2018-06-01 05:02:29 +00:00
}
2018-01-18 12:31:25 +00:00
2020-03-11 03:26:33 +00:00
err = topic . Update ( r . PostFormValue ( "name" ) , r . PostFormValue ( "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 {
2019-04-19 06:36:26 +00:00
case c . ErrNoTitle :
2019-09-30 10:15:50 +00:00
return c . LocalErrorJSQ ( "This topic doesn't have a title" , w , r , user , js )
2019-04-19 06:36:26 +00:00
case c . ErrLongTitle :
2019-09-30 10:15:50 +00:00
return c . LocalErrorJSQ ( "The length of the title is too long, max: " + strconv . Itoa ( c . Config . MaxTopicTitleLength ) , w , r , user , js )
2019-04-19 06:36:26 +00:00
case c . ErrNoBody :
2019-09-30 10:15:50 +00:00
return c . LocalErrorJSQ ( "This topic doesn't have a body" , w , r , user , js )
2018-03-17 08:16:43 +00:00
}
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-01-18 12:31:25 +00:00
}
2019-04-19 06:36:26 +00:00
err = c . Forums . UpdateLastTopic ( topic . ID , user . ID , topic . ParentID )
2018-01-18 12:31:25 +00:00
if err != nil && err != sql . ErrNoRows {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-01-18 12:31:25 +00:00
}
2018-12-28 11:13:06 +00:00
// TODO: Avoid the load to get this faster?
2019-04-19 06:36:26 +00:00
topic , err = c . Topics . Get ( topic . ID )
2018-12-28 11:13:06 +00:00
if err == sql . ErrNoRows {
2019-09-30 10:15:50 +00:00
return c . PreErrorJSQ ( "The updated topic doesn't exist." , w , r , js )
2018-12-28 11:13:06 +00:00
} else if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-12-28 11:13:06 +00:00
}
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
}
2019-09-30 10:15:50 +00:00
if ! js {
2018-01-18 12:31:25 +00:00
http . Redirect ( w , r , "/topic/" + strconv . Itoa ( tid ) , http . StatusSeeOther )
} else {
2020-02-04 11:47:03 +00:00
outBytes , err := json . Marshal ( JsonReply { c . ParseMessage ( topic . Content , topic . ParentID , "forums" , user . ParseSettings , & user ) } )
2018-12-28 11:13:06 +00:00
if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-12-28 11:13:06 +00:00
}
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
2019-04-19 06:36:26 +00:00
func DeleteTopicSubmit ( w http . ResponseWriter , r * http . Request , user c . User ) c . RouteError {
2018-01-20 06:50:29 +00:00
// TODO: Move this to some sort of middleware
var tids [ ] int
2019-09-30 10:15:50 +00:00
js := false
2019-04-19 06:36:26 +00:00
if c . ReqIsJson ( r ) {
2018-01-20 06:50:29 +00:00
if r . Body == nil {
2019-04-19 06:36:26 +00:00
return c . PreErrorJS ( "No request body" , w , r )
2018-01-20 06:50:29 +00:00
}
err := json . NewDecoder ( r . Body ) . Decode ( & tids )
if err != nil {
2019-04-19 06:36:26 +00:00
return c . PreErrorJS ( "We weren't able to parse your data" , w , r )
2018-01-20 06:50:29 +00:00
}
2019-09-30 10:15:50 +00:00
js = true
2018-01-20 06:50:29 +00:00
} else {
tid , err := strconv . Atoi ( r . URL . Path [ len ( "/topic/delete/submit/" ) : ] )
if err != nil {
2019-04-19 06:36:26 +00:00
return c . PreError ( "The provided TopicID is not a valid number." , w , r )
2018-01-20 06:50:29 +00:00
}
2019-12-31 21:57:54 +00:00
tids = [ ] int { tid }
2018-01-20 06:50:29 +00:00
}
if len ( tids ) == 0 {
2019-09-30 10:15:50 +00:00
return c . LocalErrorJSQ ( "You haven't provided any IDs" , w , r , user , js )
2018-01-20 06:50:29 +00:00
}
for _ , tid := range tids {
2019-04-19 06:36:26 +00:00
topic , err := c . Topics . Get ( tid )
2018-01-20 06:50:29 +00:00
if err == sql . ErrNoRows {
2019-09-30 10:15:50 +00:00
return c . PreErrorJSQ ( "The topic you tried to delete doesn't exist." , w , r , js )
2018-01-20 06:50:29 +00:00
} else if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-01-20 06:50:29 +00:00
}
// TODO: Add hooks to make use of headerLite
2019-04-19 06:36:26 +00:00
lite , ferr := c . SimpleForumUserCheck ( w , r , & user , topic . ParentID )
2018-01-20 06:50:29 +00:00
if ferr != nil {
return ferr
}
2019-10-06 11:32:00 +00:00
if topic . CreatedBy != user . ID {
if ! user . Perms . ViewTopic || ! user . Perms . DeleteTopic {
return c . NoPermissionsJSQ ( w , r , user , js )
}
2018-01-20 06:50:29 +00:00
}
// We might be able to handle this err better
err = topic . Delete ( )
if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-01-20 06:50:29 +00:00
}
2019-12-31 21:57:54 +00:00
err = c . ModLogs . Create ( "delete" , tid , "topic" , user . GetIP ( ) , user . ID )
2018-01-20 06:50:29 +00:00
if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-01-20 06:50:29 +00:00
}
// ? - We might need to add soft-delete before we can do an action reply for this
2019-12-31 21:57:54 +00:00
/ * _ , err = stmts . createActionReply . Exec ( tid , "delete" , ip , user . ID )
2018-01-20 06:50:29 +00:00
if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-01-20 06:50:29 +00:00
} * /
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
}
2019-04-19 06:36:26 +00:00
func StickTopicSubmit ( w http . ResponseWriter , r * http . Request , user c . User , stid string ) c . RouteError {
2019-05-17 08:40:41 +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 {
2019-04-19 06:36:26 +00:00
return c . NoPermissions ( w , r , user )
2018-11-01 06:43:56 +00:00
}
2019-05-17 08:40:41 +00:00
return topicActionPost ( topic . Stick ( ) , "stick" , w , r , lite , topic , user )
2018-11-01 06:43:56 +00:00
}
2020-02-09 10:00:08 +00:00
func topicActionPre ( stid , action string , w http . ResponseWriter , r * http . Request , user c . User ) ( * c . Topic , * c . HeaderLite , c . RouteError ) {
2018-01-20 06:50:29 +00:00
tid , err := strconv . Atoi ( stid )
if err != nil {
2019-05-17 08:40:41 +00:00
return nil , nil , c . PreError ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r )
2018-01-20 06:50:29 +00:00
}
2019-10-01 21:06:22 +00:00
t , err := c . Topics . Get ( tid )
2018-01-20 06:50:29 +00:00
if err == sql . ErrNoRows {
2019-05-17 08:40:41 +00:00
return nil , nil , c . 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-05-17 08:40:41 +00:00
return nil , nil , c . InternalError ( err , w , r )
2018-01-20 06:50:29 +00:00
}
// TODO: Add hooks to make use of headerLite
2019-10-01 21:06:22 +00:00
lite , ferr := c . SimpleForumUserCheck ( w , r , & user , t . ParentID )
2018-01-20 06:50:29 +00:00
if ferr != nil {
2019-05-17 08:40:41 +00:00
return nil , nil , ferr
2018-01-20 06:50:29 +00:00
}
2019-10-01 21:06:22 +00:00
return t , lite , nil
2018-11-01 06:43:56 +00:00
}
2019-04-19 06:36:26 +00:00
func topicActionPost ( err error , action string , w http . ResponseWriter , r * http . Request , lite * c . HeaderLite , topic * c . Topic , user c . User ) c . RouteError {
2018-01-20 06:50:29 +00:00
if err != nil {
2019-04-19 06:36:26 +00:00
return c . InternalError ( err , w , r )
2018-01-20 06:50:29 +00:00
}
2018-11-01 06:43:56 +00:00
err = addTopicAction ( action , topic , user )
2018-01-20 06:50:29 +00:00
if err != nil {
2019-04-19 06:36:26 +00:00
return c . InternalError ( err , w , r )
2018-01-20 06:50:29 +00:00
}
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
}
2019-10-01 21:06:22 +00:00
func UnstickTopicSubmit ( w http . ResponseWriter , r * http . Request , u c . User , stid string ) c . RouteError {
t , lite , rerr := topicActionPre ( stid , "unpin" , w , r , u )
2018-11-01 06:43:56 +00:00
if rerr != nil {
return rerr
2018-01-20 06:50:29 +00:00
}
2019-10-01 21:06:22 +00:00
if ! u . Perms . ViewTopic || ! u . Perms . PinTopic {
return c . NoPermissions ( w , r , u )
2018-01-20 06:50:29 +00:00
}
2019-10-01 21:06:22 +00:00
return topicActionPost ( t . Unstick ( ) , "unstick" , w , r , lite , t , u )
2018-01-20 06:50:29 +00:00
}
2019-04-19 06:36:26 +00:00
func LockTopicSubmit ( w http . ResponseWriter , r * http . Request , user c . User ) c . RouteError {
2018-01-20 06:50:29 +00:00
// TODO: Move this to some sort of middleware
var tids [ ] int
2019-09-30 10:15:50 +00:00
js := false
2019-04-19 06:36:26 +00:00
if c . ReqIsJson ( r ) {
2018-01-20 06:50:29 +00:00
if r . Body == nil {
2019-04-19 06:36:26 +00:00
return c . PreErrorJS ( "No request body" , w , r )
2018-01-20 06:50:29 +00:00
}
err := json . NewDecoder ( r . Body ) . Decode ( & tids )
if err != nil {
2019-04-19 06:36:26 +00:00
return c . PreErrorJS ( "We weren't able to parse your data" , w , r )
2018-01-20 06:50:29 +00:00
}
2019-09-30 10:15:50 +00:00
js = true
2018-01-20 06:50:29 +00:00
} else {
tid , err := strconv . Atoi ( r . URL . Path [ len ( "/topic/lock/submit/" ) : ] )
if err != nil {
2019-04-19 06:36:26 +00:00
return c . PreError ( "The provided TopicID is not a valid number." , w , r )
2018-01-20 06:50:29 +00:00
}
tids = append ( tids , tid )
}
if len ( tids ) == 0 {
2019-09-30 10:15:50 +00:00
return c . LocalErrorJSQ ( "You haven't provided any IDs" , w , r , user , js )
2018-01-20 06:50:29 +00:00
}
for _ , tid := range tids {
2019-04-19 06:36:26 +00:00
topic , err := c . Topics . Get ( tid )
2018-01-20 06:50:29 +00:00
if err == sql . ErrNoRows {
2019-09-30 10:15:50 +00:00
return c . PreErrorJSQ ( "The topic you tried to lock doesn't exist." , w , r , js )
2018-01-20 06:50:29 +00:00
} else if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-01-20 06:50:29 +00:00
}
// TODO: Add hooks to make use of headerLite
2019-04-19 06:36:26 +00:00
lite , ferr := c . 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 {
2019-09-30 10:15:50 +00:00
return c . NoPermissionsJSQ ( w , r , user , js )
2018-01-20 06:50:29 +00:00
}
err = topic . Lock ( )
if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-01-20 06:50:29 +00:00
}
2018-01-21 11:17:43 +00:00
err = addTopicAction ( "lock" , topic , user )
2018-01-20 06:50:29 +00:00
if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-01-20 06:50:29 +00:00
}
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
}
2019-10-01 21:06:22 +00:00
func UnlockTopicSubmit ( w http . ResponseWriter , r * http . Request , u c . User , stid string ) c . RouteError {
t , lite , rerr := topicActionPre ( stid , "unlock" , w , r , u )
2018-11-01 06:43:56 +00:00
if rerr != nil {
return rerr
2018-01-20 06:50:29 +00:00
}
2019-10-01 21:06:22 +00:00
if ! u . Perms . ViewTopic || ! u . Perms . CloseTopic {
return c . NoPermissions ( w , r , u )
2018-01-20 06:50:29 +00:00
}
2019-10-01 21:06:22 +00:00
return topicActionPost ( t . Unlock ( ) , "unlock" , w , r , lite , t , u )
2018-01-20 06:50:29 +00:00
}
// ! JS only route
// TODO: Figure a way to get this route to work without JS
2019-04-19 06:36:26 +00:00
func MoveTopicSubmit ( w http . ResponseWriter , r * http . Request , user c . User , sfid string ) c . RouteError {
2018-01-20 06:50:29 +00:00
fid , err := strconv . Atoi ( sfid )
if err != nil {
2019-04-19 06:36:26 +00:00
return c . 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 {
2019-04-19 06:36:26 +00:00
return c . PreErrorJS ( "No request body" , w , r )
2018-01-20 06:50:29 +00:00
}
err = json . NewDecoder ( r . Body ) . Decode ( & tids )
if err != nil {
2019-04-19 06:36:26 +00:00
return c . PreErrorJS ( "We weren't able to parse your data" , w , r )
2018-01-20 06:50:29 +00:00
}
if len ( tids ) == 0 {
2019-04-19 06:36:26 +00:00
return c . LocalErrorJS ( "You haven't provided any IDs" , w , r )
2018-01-20 06:50:29 +00:00
}
for _ , tid := range tids {
2019-04-19 06:36:26 +00:00
topic , err := c . Topics . Get ( tid )
2018-01-20 06:50:29 +00:00
if err == sql . ErrNoRows {
2019-04-19 06:36:26 +00:00
return c . PreErrorJS ( "The topic you tried to move doesn't exist." , w , r )
2018-01-20 06:50:29 +00:00
} else if err != nil {
2019-04-19 06:36:26 +00:00
return c . InternalErrorJS ( err , w , r )
2018-01-20 06:50:29 +00:00
}
// TODO: Add hooks to make use of headerLite
2019-04-19 06:36:26 +00:00
_ , ferr := c . 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 . MoveTopic {
2019-04-19 06:36:26 +00:00
return c . NoPermissionsJS ( w , r , user )
2018-01-20 06:50:29 +00:00
}
2019-04-19 06:36:26 +00:00
lite , ferr := c . 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 {
2019-04-19 06:36:26 +00:00
return c . NoPermissionsJS ( w , r , user )
2018-01-20 06:50:29 +00:00
}
err = topic . MoveTo ( fid )
if err != nil {
2019-04-19 06:36:26 +00:00
return c . InternalErrorJS ( err , w , r )
2018-01-20 06:50:29 +00:00
}
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 {
2019-04-19 06:36:26 +00:00
return c . InternalErrorJS ( err , w , r )
2018-01-20 06:50:29 +00:00
}
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
2019-10-01 21:06:22 +00:00
func addTopicAction ( action string , t * c . Topic , u c . User ) error {
2019-12-31 21:57:54 +00:00
err := c . ModLogs . Create ( action , t . ID , "topic" , u . GetIP ( ) , u . ID )
2018-01-21 11:17:43 +00:00
if err != nil {
return err
}
2019-12-31 21:57:54 +00:00
return t . CreateActionReply ( action , u . GetIP ( ) , u . ID )
2018-01-21 11:17:43 +00:00
}
2018-05-15 05:59:52 +00:00
// TODO: Refactor this
2019-04-19 06:36:26 +00:00
func LikeTopicSubmit ( w http . ResponseWriter , r * http . Request , user c . User , stid string ) c . RouteError {
2019-10-06 11:32:00 +00:00
js := r . PostFormValue ( "js" ) == "1"
2018-05-15 05:59:52 +00:00
tid , err := strconv . Atoi ( stid )
if err != nil {
2019-09-30 10:15:50 +00:00
return c . PreErrorJSQ ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r , js )
2018-05-15 05:59:52 +00:00
}
2019-04-19 06:36:26 +00:00
topic , err := c . Topics . Get ( tid )
2018-05-15 05:59:52 +00:00
if err == sql . ErrNoRows {
2019-09-30 10:15:50 +00:00
return c . PreErrorJSQ ( "The requested topic doesn't exist." , w , r , js )
2018-05-15 05:59:52 +00:00
} else if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-05-15 05:59:52 +00:00
}
// TODO: Add hooks to make use of headerLite
2019-04-19 06:36:26 +00:00
lite , ferr := c . 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 {
2019-09-30 10:15:50 +00:00
return c . NoPermissionsJSQ ( w , r , user , js )
2018-05-15 05:59:52 +00:00
}
if topic . CreatedBy == user . ID {
2019-09-30 10:15:50 +00:00
return c . LocalErrorJSQ ( "You can't like your own topics" , w , r , user , js )
2018-05-15 05:59:52 +00:00
}
2019-04-19 06:36:26 +00:00
_ , err = c . Users . Get ( topic . CreatedBy )
2018-05-15 05:59:52 +00:00
if err != nil && err == sql . ErrNoRows {
2019-09-30 10:15:50 +00:00
return c . LocalErrorJSQ ( "The target user doesn't exist" , w , r , user , js )
2018-05-15 05:59:52 +00:00
} else if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-05-15 05:59:52 +00:00
}
score := 1
err = topic . Like ( score , user . ID )
2019-04-19 06:36:26 +00:00
if err == c . ErrAlreadyLiked {
2019-09-30 10:15:50 +00:00
return c . LocalErrorJSQ ( "You already liked this" , w , r , user , js )
2018-05-15 05:59:52 +00:00
} else if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-05-15 05:59:52 +00:00
}
2018-11-22 07:21:43 +00:00
// ! Be careful about leaking per-route permission state with &user
2019-05-11 23:07:24 +00:00
alert := c . Alert { ActorID : user . ID , TargetUserID : topic . CreatedBy , Event : "like" , ElementType : "topic" , ElementID : tid , Actor : & user }
2019-04-19 06:36:26 +00:00
err = c . AddActivityAndNotifyTarget ( alert )
2018-05-15 05:59:52 +00:00
if err != nil {
2019-09-30 10:15:50 +00:00
return c . InternalErrorJSQ ( err , w , r , js )
2018-05-15 05:59:52 +00:00
}
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
}
2019-09-30 10:15:50 +00:00
if ! js {
2018-05-15 05:59:52 +00:00
http . Redirect ( w , r , "/topic/" + strconv . Itoa ( tid ) , http . StatusSeeOther )
} else {
_ , _ = w . Write ( successJSONBytes )
}
return nil
}
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
func UnlikeTopicSubmit ( w http . ResponseWriter , r * http . Request , user c . User , stid string ) c . RouteError {
js := r . PostFormValue ( "js" ) == "1"
tid , err := strconv . Atoi ( stid )
if err != nil {
return c . PreErrorJSQ ( phrases . GetErrorPhrase ( "id_must_be_integer" ) , w , r , js )
}
topic , err := c . Topics . Get ( tid )
if err == sql . ErrNoRows {
return c . PreErrorJSQ ( "The requested topic doesn't exist." , w , r , js )
} else if err != nil {
return c . InternalErrorJSQ ( err , w , r , js )
}
// TODO: Add hooks to make use of headerLite
lite , ferr := c . SimpleForumUserCheck ( w , r , & user , topic . ParentID )
if ferr != nil {
return ferr
}
if ! user . Perms . ViewTopic || ! user . Perms . LikeItem {
return c . NoPermissionsJSQ ( w , r , user , js )
}
_ , err = c . Users . Get ( topic . CreatedBy )
if err != nil && err == sql . ErrNoRows {
return c . LocalErrorJSQ ( "The target user doesn't exist" , w , r , user , js )
} else if err != nil {
return c . InternalErrorJSQ ( err , w , r , js )
}
err = topic . Unlike ( user . ID )
if err != nil {
return c . InternalErrorJSQ ( err , w , r , js )
}
2020-02-01 06:56:04 +00:00
// TODO: Better coupling between the two params queries
aids , err := c . Activity . AidsByParams ( "like" , topic . ID , "topic" )
if err != nil {
return c . InternalErrorJSQ ( err , w , r , js )
}
for _ , aid := range aids {
c . DismissAlert ( topic . CreatedBy , aid )
}
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
err = c . Activity . DeleteByParams ( "like" , topic . ID , "topic" )
if err != nil {
return c . InternalErrorJSQ ( err , w , r , js )
}
skip , rerr := lite . Hooks . VhookSkippable ( "action_end_unlike_topic" , topic . ID , & user )
if skip || rerr != nil {
return rerr
}
if ! js {
http . Redirect ( w , r , "/topic/" + strconv . Itoa ( tid ) , http . StatusSeeOther )
} else {
_ , _ = w . Write ( successJSONBytes )
}
return nil
}