2017-09-03 04:50:31 +00:00
/ *
*
2017-09-13 15:09:13 +00:00
* Gosora Route Handlers
* Copyright Azareal 2016 - 2018
2017-09-03 04:50:31 +00:00
*
* /
2016-12-02 07:38:54 +00:00
package main
2017-05-11 13:04:43 +00:00
import (
"log"
2017-06-12 09:03:14 +00:00
//"fmt"
2017-05-11 13:04:43 +00:00
"bytes"
2017-09-03 04:50:31 +00:00
"html"
2017-05-11 13:04:43 +00:00
"io"
"net/http"
2017-09-03 04:50:31 +00:00
"strconv"
"strings"
"time"
2017-06-19 08:06:54 +00:00
2017-11-10 03:33:11 +00:00
"./common"
2017-06-19 08:06:54 +00:00
"./query_gen/lib"
2017-05-11 13:04:43 +00:00
)
2017-04-05 14:05:37 +00:00
2016-12-02 07:38:54 +00:00
// A blank list to fill out that parameter in Page for routes which don't use it
2016-12-18 12:56:06 +00:00
var tList [ ] interface { }
2017-09-03 04:50:31 +00:00
//var nList []string
var successJSONBytes = [ ] byte ( ` { "success":"1"} ` )
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
var cacheControlMaxAge = "max-age=" + strconv . Itoa ( day ) // TODO: Make this a config value
2016-12-02 07:38:54 +00:00
2017-09-18 17:03:52 +00:00
// HTTPSRedirect is a connection handler which redirects all HTTP requests to HTTPS
2017-09-10 16:57:22 +00:00
type HTTPSRedirect struct {
2017-08-13 11:22:34 +00:00
}
2017-09-10 16:57:22 +00:00
func ( red * HTTPSRedirect ) ServeHTTP ( w http . ResponseWriter , req * http . Request ) {
2017-08-13 11:22:34 +00:00
dest := "https://" + req . Host + req . URL . Path
if len ( req . URL . RawQuery ) > 0 {
dest += "?" + req . URL . RawQuery
}
2017-09-03 04:50:31 +00:00
http . Redirect ( w , req , dest , http . StatusTemporaryRedirect )
2017-08-13 11:22:34 +00:00
}
2016-12-02 07:38:54 +00:00
// GET functions
2017-09-11 10:24:03 +00:00
func routeStatic ( w http . ResponseWriter , r * http . Request ) {
2017-01-01 15:45:43 +00:00
//log.Print("Outputting static file '" + r.URL.Path + "'")
2017-09-03 04:50:31 +00:00
file , ok := staticFiles [ r . URL . Path ]
2017-01-01 15:45:43 +00:00
if ! ok {
2017-09-23 19:57:13 +00:00
if dev . DebugMode {
log . Print ( "Failed to find '" + r . URL . Path + "'" )
}
2017-01-01 15:45:43 +00:00
w . WriteHeader ( http . StatusNotFound )
return
}
2017-09-10 16:57:22 +00:00
h := w . Header ( )
2017-05-29 14:52:37 +00:00
2017-01-01 15:45:43 +00:00
// Surely, there's a more efficient way of doing this?
2017-09-10 16:57:22 +00:00
t , err := time . Parse ( http . TimeFormat , h . Get ( "If-Modified-Since" ) )
if err == nil && file . Info . ModTime ( ) . Before ( t . Add ( 1 * time . Second ) ) {
2016-12-05 07:21:17 +00:00
w . WriteHeader ( http . StatusNotModified )
return
}
2017-01-01 15:45:43 +00:00
h . Set ( "Last-Modified" , file . FormattedModTime )
h . Set ( "Content-Type" , file . Mimetype )
2017-08-17 11:13:49 +00:00
//Cache-Control: max-age=31536000
h . Set ( "Cache-Control" , cacheControlMaxAge )
2017-09-03 04:50:31 +00:00
h . Set ( "Vary" , "Accept-Encoding" )
2017-01-01 15:45:43 +00:00
//http.ServeContent(w,r,r.URL.Path,file.Info.ModTime(),file)
//w.Write(file.Data)
2017-09-10 16:57:22 +00:00
if strings . Contains ( h . Get ( "Accept-Encoding" ) , "gzip" ) {
2017-09-03 04:50:31 +00:00
h . Set ( "Content-Encoding" , "gzip" )
2017-02-16 06:47:55 +00:00
h . Set ( "Content-Length" , strconv . FormatInt ( file . GzipLength , 10 ) )
2017-02-10 13:39:13 +00:00
io . Copy ( w , bytes . NewReader ( file . GzipData ) ) // Use w.Write instead?
} else {
2017-02-16 06:47:55 +00:00
h . Set ( "Content-Length" , strconv . FormatInt ( file . Length , 10 ) ) // Avoid doing a type conversion every time?
2017-02-10 13:39:13 +00:00
io . Copy ( w , bytes . NewReader ( file . Data ) )
}
2017-10-30 09:57:08 +00:00
//io.CopyN(w, bytes.NewReader(file.Data), staticFiles[r.URL.Path].Length)
2016-12-05 07:21:17 +00:00
}
2017-06-05 11:57:27 +00:00
// Deprecated: Test route for stopping the server during a performance analysis
2017-10-30 09:57:08 +00:00
/ * func routeExit ( w http . ResponseWriter , r * http . Request , user User ) RouteError {
2017-01-17 07:55:46 +00:00
db . Close ( )
os . Exit ( 0 )
2017-04-12 13:39:03 +00:00
} * /
2016-12-05 07:21:17 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Make this a static file somehow? Is it possible for us to put this file somewhere else?
// TODO: Add a sitemap
2017-11-02 04:12:51 +00:00
// TODO: Add an API so that plugins can register disallowed areas. E.g. /guilds/join for plugin_guilds
2017-10-30 09:57:08 +00:00
func routeRobotsTxt ( w http . ResponseWriter , r * http . Request ) RouteError {
2017-09-03 04:50:31 +00:00
_ , _ = w . Write ( [ ] byte ( ` User - agent : *
2017-08-06 15:22:18 +00:00
Disallow : / panel /
Disallow : / topics / create /
Disallow : / user / edit /
Disallow : / accounts /
` ) )
2017-10-30 09:57:08 +00:00
return nil
2017-08-06 15:22:18 +00:00
}
2017-10-30 09:57:08 +00:00
func routeOverview ( w http . ResponseWriter , r * http . Request , user User ) RouteError {
headerVars , ferr := UserCheck ( w , r , & user )
if ferr != nil {
return ferr
2016-12-16 10:37:42 +00:00
}
2017-09-03 04:50:31 +00:00
BuildWidgets ( "overview" , nil , headerVars , r )
2017-06-28 12:05:26 +00:00
2017-11-10 03:33:11 +00:00
pi := common . Page { "Overview" , user , headerVars , tList , nil }
2017-09-03 04:50:31 +00:00
if preRenderHooks [ "pre_render_overview" ] != nil {
if runPreRenderHook ( "pre_render_overview" , w , r , & user , & pi ) {
2017-10-30 09:57:08 +00:00
return nil
2017-07-12 11:05:18 +00:00
}
}
2017-09-03 04:50:31 +00:00
err := templates . ExecuteTemplate ( w , "overview.html" , pi )
2017-05-29 14:52:37 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-05-29 14:52:37 +00:00
}
2017-10-30 09:57:08 +00:00
return nil
2016-12-02 07:38:54 +00:00
}
2017-10-30 09:57:08 +00:00
func routeCustomPage ( w http . ResponseWriter , r * http . Request , user User ) RouteError {
headerVars , ferr := UserCheck ( w , r , & user )
if ferr != nil {
return ferr
2016-12-16 10:37:42 +00:00
}
2017-06-28 12:05:26 +00:00
2016-12-09 13:46:29 +00:00
name := r . URL . Path [ len ( "/pages/" ) : ]
2017-09-03 04:50:31 +00:00
if templates . Lookup ( "page_" + name ) == nil {
2017-10-30 09:57:08 +00:00
return NotFound ( w , r )
2016-12-13 02:14:14 +00:00
}
2017-09-03 04:50:31 +00:00
BuildWidgets ( "custom_page" , name , headerVars , r )
2017-07-12 11:05:18 +00:00
2017-11-10 03:33:11 +00:00
pi := common . Page { "Page" , user , headerVars , tList , nil }
2017-09-03 04:50:31 +00:00
if preRenderHooks [ "pre_render_custom_page" ] != nil {
if runPreRenderHook ( "pre_render_custom_page" , w , r , & user , & pi ) {
2017-10-30 09:57:08 +00:00
return nil
2017-07-12 11:05:18 +00:00
}
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
err := templates . ExecuteTemplate ( w , "page_" + name , pi )
2016-12-08 14:11:18 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-02 07:38:54 +00:00
}
2017-10-30 09:57:08 +00:00
return nil
2016-12-02 07:38:54 +00:00
}
2017-05-29 14:52:37 +00:00
2017-10-30 09:57:08 +00:00
func routeTopics ( w http . ResponseWriter , r * http . Request , user User ) RouteError {
headerVars , ferr := UserCheck ( w , r , & user )
if ferr != nil {
return ferr
2016-12-16 10:37:42 +00:00
}
2017-09-03 04:50:31 +00:00
BuildWidgets ( "topics" , nil , headerVars , r )
2017-05-29 14:52:37 +00:00
2017-09-24 00:49:41 +00:00
// TODO: Add a function for the qlist stuff
2017-06-19 08:06:54 +00:00
var qlist string
2017-09-15 22:20:01 +00:00
group , err := gstore . Get ( user . Group )
if err != nil {
2017-10-30 09:57:08 +00:00
log . Printf ( "Group #%d doesn't exist despite being used by User #%d" , user . Group , user . ID )
return LocalError ( "Something weird happened" , w , r , user )
2017-09-15 22:20:01 +00:00
}
2017-09-28 22:16:34 +00:00
// TODO: Make CanSee a method on *Group with a canSee field?
2017-09-24 00:49:41 +00:00
var canSee [ ] int
if user . IsSuperAdmin {
canSee , err = fstore . GetAllVisibleIDs ( )
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-09-24 00:49:41 +00:00
}
} else {
canSee = group . CanSee
}
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
// We need a list of the visible forums for Quick Topic
var forumList [ ] Forum
2017-10-30 09:57:08 +00:00
var argList [ ] interface { }
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
2017-09-24 00:49:41 +00:00
for _ , fid := range canSee {
forum := fstore . DirtyGet ( fid )
if forum . Name != "" && forum . Active {
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
if forum . ParentType == "" || forum . ParentType == "forum" {
// Optimise Quick Topic away for guests
if user . Loggedin {
fcopy := forum . Copy ( )
2017-11-02 04:12:51 +00:00
// TODO: Add a hook here for plugin_guilds
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
forumList = append ( forumList , fcopy )
}
}
2017-11-02 04:12:51 +00:00
// ? - Should we be showing plugin_guilds posts on /topics/?
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
// ? - Would it be useful, if we could post in social groups from /topics/?
2017-10-30 09:57:08 +00:00
argList = append ( argList , strconv . Itoa ( fid ) )
2017-06-19 08:06:54 +00:00
qlist += "?,"
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
2017-02-05 14:41:53 +00:00
}
}
2017-09-24 00:49:41 +00:00
// ! Need an inline error not a page level error
if qlist == "" {
2017-10-30 09:57:08 +00:00
return NotFound ( w , r )
2017-09-24 00:49:41 +00:00
}
2017-09-03 04:50:31 +00:00
qlist = qlist [ 0 : len ( qlist ) - 1 ]
2017-05-29 14:52:37 +00:00
2017-10-30 09:57:08 +00:00
topicCountStmt , err := qgen . Builder . SimpleCount ( "topics" , "parentID IN(" + qlist + ")" , "" )
if err != nil {
return InternalError ( err , w , r )
}
var topicCount int
err = topicCountStmt . QueryRow ( argList ... ) . Scan ( & topicCount )
if err != nil {
return InternalError ( err , w , r )
}
// Get the current page
page , _ := strconv . Atoi ( r . FormValue ( "page" ) )
// Calculate the offset
var offset int
lastPage := ( topicCount / config . ItemsPerPage ) + 1
if page > 1 {
offset = ( config . ItemsPerPage * page ) - config . ItemsPerPage
} else if page == - 1 {
page = lastPage
offset = ( config . ItemsPerPage * page ) - config . ItemsPerPage
} else {
page = 1
}
2017-08-06 15:22:18 +00:00
var topicList [ ] * TopicsRow
2017-10-30 09:57:08 +00:00
stmt , err := qgen . Builder . SimpleSelect ( "topics" , "tid, title, content, createdBy, is_closed, sticky, createdAt, lastReplyAt, lastReplyBy, parentID, postCount, likeCount" , "parentID IN(" + qlist + ")" , "sticky DESC, lastReplyAt DESC, createdBy DESC" , "?,?" )
2017-06-19 08:06:54 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-06-19 08:06:54 +00:00
}
2017-10-30 09:57:08 +00:00
argList = append ( argList , offset )
argList = append ( argList , config . ItemsPerPage )
rows , err := stmt . Query ( argList ... )
2016-12-02 07:38:54 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-02 07:38:54 +00:00
}
2017-08-06 15:22:18 +00:00
defer rows . Close ( )
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
var reqUserList = make ( map [ int ] bool )
2016-12-02 07:38:54 +00:00
for rows . Next ( ) {
2017-08-06 15:22:18 +00:00
topicItem := TopicsRow { ID : 0 }
2017-09-03 04:50:31 +00:00
err := rows . Scan ( & topicItem . ID , & topicItem . Title , & topicItem . Content , & topicItem . CreatedBy , & topicItem . IsClosed , & topicItem . Sticky , & topicItem . CreatedAt , & topicItem . LastReplyAt , & topicItem . LastReplyBy , & topicItem . ParentID , & topicItem . PostCount , & topicItem . LikeCount )
2016-12-02 07:38:54 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-02 07:38:54 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
topicItem . Link = buildTopicURL ( nameToSlug ( topicItem . Title ) , topicItem . ID )
2017-05-29 14:52:37 +00:00
2017-07-12 11:05:18 +00:00
forum := fstore . DirtyGet ( topicItem . ParentID )
2017-10-30 09:57:08 +00:00
topicItem . ForumName = forum . Name
topicItem . ForumLink = forum . Link
2017-05-29 14:52:37 +00:00
2017-11-10 03:33:11 +00:00
//topicItem.CreatedAt = relativeTime(topicItem.CreatedAt)
2017-10-14 07:39:22 +00:00
topicItem . RelativeLastReplyAt = relativeTime ( topicItem . LastReplyAt )
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
if vhooks [ "topics_topic_row_assign" ] != nil {
runVhook ( "topics_topic_row_assign" , & topicItem , & forum )
2016-12-11 16:06:17 +00:00
}
2017-08-06 15:22:18 +00:00
topicList = append ( topicList , & topicItem )
reqUserList [ topicItem . CreatedBy ] = true
reqUserList [ topicItem . LastReplyBy ] = true
2016-12-02 07:38:54 +00:00
}
err = rows . Err ( )
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-02 07:38:54 +00:00
}
2017-08-06 15:22:18 +00:00
// Convert the user ID map to a slice, then bulk load the users
2017-09-03 04:50:31 +00:00
var idSlice = make ( [ ] int , len ( reqUserList ) )
2017-08-06 15:22:18 +00:00
var i int
2017-09-03 04:50:31 +00:00
for userID := range reqUserList {
2017-08-06 15:22:18 +00:00
idSlice [ i ] = userID
i ++
}
2017-09-10 16:57:22 +00:00
// TODO: What if a user is deleted via the Control Panel?
2017-09-15 22:20:01 +00:00
userList , err := users . BulkGetMap ( idSlice )
2017-08-06 15:22:18 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-08-06 15:22:18 +00:00
}
// Second pass to the add the user data
2017-09-10 16:57:22 +00:00
// TODO: Use a pointer to TopicsRow instead of TopicsRow itself?
2017-08-06 15:22:18 +00:00
for _ , topicItem := range topicList {
topicItem . Creator = userList [ topicItem . CreatedBy ]
topicItem . LastUser = userList [ topicItem . LastReplyBy ]
}
2017-05-29 14:52:37 +00:00
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
pi := TopicsPage { "All Topics" , user , headerVars , topicList , forumList , config . DefaultForum }
2017-09-03 04:50:31 +00:00
if preRenderHooks [ "pre_render_topic_list" ] != nil {
if runPreRenderHook ( "pre_render_topic_list" , w , r , & user , & pi ) {
2017-10-30 09:57:08 +00:00
return nil
2017-07-12 11:05:18 +00:00
}
}
2017-10-30 09:57:08 +00:00
err = RunThemeTemplate ( headerVars . ThemeName , "topics" , pi , w )
if err != nil {
return InternalError ( err , w , r )
}
return nil
2016-12-02 07:38:54 +00:00
}
2016-12-03 13:45:08 +00:00
2017-10-30 09:57:08 +00:00
func routeForum ( w http . ResponseWriter , r * http . Request , user User , sfid string ) RouteError {
2017-01-26 13:37:50 +00:00
page , _ := strconv . Atoi ( r . FormValue ( "page" ) )
2017-06-25 09:56:39 +00:00
// SEO URLs...
2017-09-03 04:50:31 +00:00
halves := strings . Split ( sfid , "." )
2017-06-25 09:56:39 +00:00
if len ( halves ) < 2 {
2017-09-03 04:50:31 +00:00
halves = append ( halves , halves [ 0 ] )
2017-06-25 09:56:39 +00:00
}
fid , err := strconv . Atoi ( halves [ 1 ] )
2016-12-03 13:45:08 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return PreError ( "The provided ForumID is not a valid number." , w , r )
2016-12-03 13:45:08 +00:00
}
2017-05-29 14:52:37 +00:00
2017-10-30 09:57:08 +00:00
headerVars , ferr := ForumUserCheck ( w , r , & user , fid )
if ferr != nil {
return ferr
2016-12-03 13:45:08 +00:00
}
2017-10-30 09:57:08 +00:00
2017-02-05 16:36:54 +00:00
if ! user . Perms . ViewTopic {
2017-10-30 09:57:08 +00:00
return NoPermissions ( w , r , user )
2016-12-21 02:30:32 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Fix this double-check
2017-09-15 22:20:01 +00:00
forum , err := fstore . Get ( fid )
2017-06-28 12:05:26 +00:00
if err == ErrNoRows {
2017-10-30 09:57:08 +00:00
return NotFound ( w , r )
2017-06-28 12:05:26 +00:00
} else if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-06-28 12:05:26 +00:00
}
2017-09-03 04:50:31 +00:00
BuildWidgets ( "view_forum" , forum , headerVars , r )
2017-06-28 12:05:26 +00:00
2017-01-26 13:37:50 +00:00
// Calculate the offset
var offset int
2017-10-30 09:57:08 +00:00
// TODO: Does forum.TopicCount take the deleted items into consideration for guests?
2017-09-03 04:50:31 +00:00
lastPage := ( forum . TopicCount / config . ItemsPerPage ) + 1
2017-01-26 13:37:50 +00:00
if page > 1 {
2017-07-17 10:23:42 +00:00
offset = ( config . ItemsPerPage * page ) - config . ItemsPerPage
2017-01-26 13:37:50 +00:00
} else if page == - 1 {
2017-09-03 04:50:31 +00:00
page = lastPage
2017-07-17 10:23:42 +00:00
offset = ( config . ItemsPerPage * page ) - config . ItemsPerPage
2017-01-26 13:37:50 +00:00
} else {
page = 1
}
2017-10-21 00:27:47 +00:00
// TODO: Move this to *Forum
2017-11-05 09:55:34 +00:00
rows , err := stmts . getForumTopicsOffset . Query ( fid , offset , config . ItemsPerPage )
2016-12-03 13:45:08 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-03 13:45:08 +00:00
}
2017-08-06 15:22:18 +00:00
defer rows . Close ( )
2017-05-29 14:52:37 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Use something other than TopicsRow as we don't need to store the forum name and link on each and every topic item?
2017-08-06 15:22:18 +00:00
var topicList [ ] * TopicsRow
2017-09-03 04:50:31 +00:00
var reqUserList = make ( map [ int ] bool )
2016-12-03 13:45:08 +00:00
for rows . Next ( ) {
2017-09-03 04:50:31 +00:00
var topicItem = TopicsRow { ID : 0 }
err := rows . Scan ( & topicItem . ID , & topicItem . Title , & topicItem . Content , & topicItem . CreatedBy , & topicItem . IsClosed , & topicItem . Sticky , & topicItem . CreatedAt , & topicItem . LastReplyAt , & topicItem . LastReplyBy , & topicItem . ParentID , & topicItem . PostCount , & topicItem . LikeCount )
2016-12-03 13:45:08 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-03 13:45:08 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
topicItem . Link = buildTopicURL ( nameToSlug ( topicItem . Title ) , topicItem . ID )
2017-10-14 07:39:22 +00:00
topicItem . RelativeLastReplyAt = relativeTime ( topicItem . LastReplyAt )
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
if vhooks [ "forum_trow_assign" ] != nil {
runVhook ( "forum_trow_assign" , & topicItem , & forum )
2016-12-11 16:06:17 +00:00
}
2017-08-06 15:22:18 +00:00
topicList = append ( topicList , & topicItem )
reqUserList [ topicItem . CreatedBy ] = true
reqUserList [ topicItem . LastReplyBy ] = true
2016-12-03 13:45:08 +00:00
}
err = rows . Err ( )
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-03 13:45:08 +00:00
}
2017-08-06 15:22:18 +00:00
// Convert the user ID map to a slice, then bulk load the users
2017-09-03 04:50:31 +00:00
var idSlice = make ( [ ] int , len ( reqUserList ) )
2017-08-06 15:22:18 +00:00
var i int
2017-09-03 04:50:31 +00:00
for userID := range reqUserList {
2017-08-06 15:22:18 +00:00
idSlice [ i ] = userID
i ++
}
2017-09-10 16:57:22 +00:00
// TODO: What if a user is deleted via the Control Panel?
2017-09-15 22:20:01 +00:00
userList , err := users . BulkGetMap ( idSlice )
2017-08-06 15:22:18 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-08-06 15:22:18 +00:00
}
// Second pass to the add the user data
2017-09-10 16:57:22 +00:00
// TODO: Use a pointer to TopicsRow instead of TopicsRow itself?
2017-08-06 15:22:18 +00:00
for _ , topicItem := range topicList {
topicItem . Creator = userList [ topicItem . CreatedBy ]
topicItem . LastUser = userList [ topicItem . LastReplyBy ]
}
2017-05-29 14:52:37 +00:00
2017-09-28 22:16:34 +00:00
pi := ForumPage { forum . Name , user , headerVars , topicList , forum , page , lastPage }
2017-09-03 04:50:31 +00:00
if preRenderHooks [ "pre_render_view_forum" ] != nil {
if runPreRenderHook ( "pre_render_view_forum" , w , r , & user , & pi ) {
2017-10-30 09:57:08 +00:00
return nil
2017-07-12 11:05:18 +00:00
}
}
2017-10-30 09:57:08 +00:00
err = RunThemeTemplate ( headerVars . ThemeName , "forum" , pi , w )
if err != nil {
return InternalError ( err , w , r )
}
return nil
2016-12-03 13:45:08 +00:00
}
2017-10-30 09:57:08 +00:00
func routeForums ( w http . ResponseWriter , r * http . Request , user User ) RouteError {
headerVars , ferr := UserCheck ( w , r , & user )
if ferr != nil {
return ferr
2016-12-16 10:37:42 +00:00
}
2017-09-03 04:50:31 +00:00
BuildWidgets ( "forums" , nil , headerVars , r )
2017-05-29 14:52:37 +00:00
2017-02-06 04:52:19 +00:00
var err error
2017-07-12 11:05:18 +00:00
var forumList [ ] Forum
var canSee [ ] int
2017-09-03 04:50:31 +00:00
if user . IsSuperAdmin {
2017-09-10 16:57:22 +00:00
canSee , err = fstore . GetAllVisibleIDs ( )
2017-07-12 11:05:18 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-07-12 11:05:18 +00:00
}
2017-10-30 09:57:08 +00:00
//log.Print("canSee ", canSee)
2017-07-12 11:05:18 +00:00
} else {
2017-09-15 22:20:01 +00:00
group , err := gstore . Get ( user . Group )
if err != nil {
2017-10-30 09:57:08 +00:00
log . Printf ( "Group #%d doesn't exist despite being used by User #%d" , user . Group , user . ID )
return LocalError ( "Something weird happened" , w , r , user )
2017-09-15 22:20:01 +00:00
}
2017-07-12 11:05:18 +00:00
canSee = group . CanSee
2017-10-30 09:57:08 +00:00
//log.Print("group.CanSee ", group.CanSee)
2017-07-12 11:05:18 +00:00
}
for _ , fid := range canSee {
2017-09-28 22:16:34 +00:00
// Avoid data races by copying the struct into something we can freely mold without worrying about breaking something somewhere else
var forum = fstore . DirtyGet ( fid ) . Copy ( )
2017-09-24 00:49:41 +00:00
if forum . ParentID == 0 && forum . Name != "" && forum . Active {
2017-02-06 04:52:19 +00:00
if forum . LastTopicID != 0 {
2017-09-28 22:16:34 +00:00
//topic, user := forum.GetLast()
//if topic.ID != 0 && user.ID != 0 {
if forum . LastTopic . ID != 0 && forum . LastReplyer . ID != 0 {
2017-10-16 07:32:58 +00:00
forum . LastTopicTime = relativeTime ( forum . LastTopic . LastReplyAt )
2017-09-28 22:16:34 +00:00
} else {
forum . LastTopicTime = ""
2017-02-06 04:52:19 +00:00
}
} else {
forum . LastTopicTime = ""
}
2017-07-12 11:05:18 +00:00
if hooks [ "forums_frow_assign" ] != nil {
2017-09-03 04:50:31 +00:00
runHook ( "forums_frow_assign" , & forum )
2017-07-12 11:05:18 +00:00
}
2017-02-06 04:52:19 +00:00
forumList = append ( forumList , forum )
2016-12-03 13:45:08 +00:00
}
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
pi := ForumsPage { "Forum List" , user , headerVars , forumList }
if preRenderHooks [ "pre_render_forum_list" ] != nil {
if runPreRenderHook ( "pre_render_forum_list" , w , r , & user , & pi ) {
2017-10-30 09:57:08 +00:00
return nil
2017-07-12 11:05:18 +00:00
}
}
2017-10-30 09:57:08 +00:00
err = RunThemeTemplate ( headerVars . ThemeName , "forums" , pi , w )
if err != nil {
return InternalError ( err , w , r )
}
return nil
2016-12-03 13:45:08 +00:00
}
2017-05-29 14:52:37 +00:00
2017-10-30 09:57:08 +00:00
func routeTopicID ( w http . ResponseWriter , r * http . Request , user User ) RouteError {
2017-04-06 17:37:32 +00:00
var err error
var page , offset int
2017-09-28 22:16:34 +00:00
var replyList [ ] ReplyUser
2017-05-29 14:52:37 +00:00
2017-01-21 18:16:27 +00:00
page , _ = strconv . Atoi ( r . FormValue ( "page" ) )
2017-06-28 12:05:26 +00:00
// SEO URLs...
2017-09-03 04:50:31 +00:00
halves := strings . Split ( r . URL . Path [ len ( "/topic/" ) : ] , "." )
2017-06-28 12:05:26 +00:00
if len ( halves ) < 2 {
2017-09-03 04:50:31 +00:00
halves = append ( halves , halves [ 0 ] )
2017-06-28 12:05:26 +00:00
}
tid , err := strconv . Atoi ( halves [ 1 ] )
2016-12-02 07:38:54 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return PreError ( "The provided TopicID is not a valid number." , w , r )
2016-12-02 07:38:54 +00:00
}
2017-05-29 14:52:37 +00:00
2017-04-02 13:00:40 +00:00
// Get the topic...
2017-09-28 22:16:34 +00:00
topic , err := getTopicUser ( tid )
2017-06-28 12:05:26 +00:00
if err == ErrNoRows {
2017-10-30 09:57:08 +00:00
return NotFound ( w , r )
2016-12-02 07:38:54 +00:00
} else if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-02 07:38:54 +00:00
}
2017-07-17 10:23:42 +00:00
topic . ClassName = ""
2017-09-18 17:03:52 +00:00
//log.Printf("topic: %+v\n", topic)
2017-05-29 14:52:37 +00:00
2017-10-30 09:57:08 +00:00
headerVars , ferr := ForumUserCheck ( w , r , & user , topic . ParentID )
if ferr != nil {
return ferr
2017-01-31 05:13:38 +00:00
}
2017-02-05 16:36:54 +00:00
if ! user . Perms . ViewTopic {
2017-08-13 11:22:34 +00:00
//log.Printf("user.Perms: %+v\n", user.Perms)
2017-10-30 09:57:08 +00:00
return NoPermissions ( w , r , user )
2017-01-31 05:13:38 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
BuildWidgets ( "view_topic" , & topic , headerVars , r )
2017-06-28 12:05:26 +00:00
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
topic . ContentHTML = parseMessage ( topic . Content , topic . ParentID , "forums" )
2017-09-03 04:50:31 +00:00
topic . ContentLines = strings . Count ( topic . Content , "\n" )
2017-05-29 14:52:37 +00:00
2017-01-10 06:51:28 +00:00
// We don't want users posting in locked topics...
2017-09-03 04:50:31 +00:00
if topic . IsClosed && ! user . IsMod {
2017-01-10 06:51:28 +00:00
user . Perms . CreateReply = false
2016-12-03 04:50:35 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-15 22:20:01 +00:00
postGroup , err := gstore . Get ( topic . Group )
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-09-15 22:20:01 +00:00
}
topic . Tag = postGroup . Tag
if postGroup . IsMod || postGroup . IsAdmin {
2017-09-18 17:03:52 +00:00
topic . ClassName = config . StaffCSS
2016-12-04 06:16:59 +00:00
}
2017-11-02 02:52:21 +00:00
topic . RelativeCreatedAt = relativeTime ( topic . CreatedAt )
2017-06-12 09:03:14 +00:00
2017-09-18 17:03:52 +00:00
// TODO: Make a function for this? Build a more sophisticated noavatar handling system?
if topic . Avatar != "" {
if topic . Avatar [ 0 ] == '.' {
topic . Avatar = "/uploads/avatar_" + strconv . Itoa ( topic . CreatedBy ) + topic . Avatar
}
} else {
topic . Avatar = strings . Replace ( config . Noavatar , "{id}" , strconv . Itoa ( topic . CreatedBy ) , 1 )
}
2017-01-21 18:16:27 +00:00
// Calculate the offset
2017-09-03 04:50:31 +00:00
lastPage := ( topic . PostCount / config . ItemsPerPage ) + 1
2017-01-21 18:16:27 +00:00
if page > 1 {
2017-07-17 10:23:42 +00:00
offset = ( config . ItemsPerPage * page ) - config . ItemsPerPage
2017-01-21 18:16:27 +00:00
} else if page == - 1 {
2017-09-03 04:50:31 +00:00
page = lastPage
2017-07-17 10:23:42 +00:00
offset = ( config . ItemsPerPage * page ) - config . ItemsPerPage
2017-01-21 18:16:27 +00:00
} else {
page = 1
}
2017-05-29 14:52:37 +00:00
2017-09-28 22:16:34 +00:00
tpage := TopicPage { topic . Title , user , headerVars , replyList , topic , page , lastPage }
2016-12-02 07:38:54 +00:00
// Get the replies..
2017-11-05 09:55:34 +00:00
rows , err := stmts . getTopicRepliesOffset . Query ( topic . ID , offset , config . ItemsPerPage )
2017-06-28 12:05:26 +00:00
if err == ErrNoRows {
2017-10-30 09:57:08 +00:00
return 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 )
2017-01-21 18:16:27 +00:00
} else if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-02 07:38:54 +00:00
}
2017-08-06 15:22:18 +00:00
defer rows . Close ( )
2017-05-29 14:52:37 +00:00
2017-09-28 22:16:34 +00:00
replyItem := ReplyUser { ClassName : "" }
2016-12-02 07:38:54 +00:00
for rows . Next ( ) {
2017-09-03 04:50:31 +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 . ActionType )
2016-12-02 07:38:54 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-02 07:38:54 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
replyItem . UserLink = buildProfileURL ( nameToSlug ( replyItem . CreatedByName ) , replyItem . CreatedBy )
2017-01-17 07:55:46 +00:00
replyItem . ParentID = topic . ID
Added Quick Topic.
Added Attachments.
Added Attachment Media Embeds.
Renamed a load of *Store and *Cache methods to reduce the amount of unneccesary typing.
Added petabytes as a unit and cleaned up a few of the friendly units.
Refactored the username change logic to make it easier to maintain.
Refactored the avatar change logic to make it easier to maintain.
Shadow now uses CSS Variables for most of it's colours. We have plans to transpile this to support older browsers later on!
Snuck some CSS Variables into Tempra Conflux.
Added the GroupCache interface to MemoryGroupStore.
Added the Length method to MemoryGroupStore.
Added support for a site short name.
Added the UploadFiles permission.
Renamed more functions.
Fixed the background for the left gutter on the postbit for Tempra Simple and Shadow.
Added support for if statements operating on int8, int16, int32, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64 for the template compiler.
Added support for if statements operating on slices and maps for the template compiler.
Fixed a security exploit in reply editing.
Fixed a bug in the URL detector in the parser where it couldn't find URLs with non-standard ports.
Fixed buttons having blue outlines on focus on Shadow.
Refactored the topic creation logic to make it easier to maintain.
Made a few responsive fixes, but there's still more to do in the following commits!
2017-10-05 10:20:28 +00:00
replyItem . ContentHtml = parseMessage ( replyItem . Content , topic . ParentID , "forums" )
2017-09-03 04:50:31 +00:00
replyItem . ContentLines = strings . Count ( replyItem . Content , "\n" )
2017-05-29 14:52:37 +00:00
2017-09-15 22:20:01 +00:00
postGroup , err = gstore . Get ( replyItem . Group )
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-09-15 22:20:01 +00:00
}
if postGroup . IsMod || postGroup . IsAdmin {
2017-09-18 17:03:52 +00:00
replyItem . ClassName = config . StaffCSS
2016-12-04 06:16:59 +00:00
} else {
2017-07-17 10:23:42 +00:00
replyItem . ClassName = ""
2016-12-04 06:16:59 +00:00
}
2017-05-29 14:52:37 +00:00
2017-11-02 02:52:21 +00:00
// TODO: Make a function for this? Build a more sophisticated noavatar handling system? Do bulk user loads and let the UserStore initialise this?
2017-01-17 07:55:46 +00:00
if replyItem . Avatar != "" {
if replyItem . Avatar [ 0 ] == '.' {
replyItem . Avatar = "/uploads/avatar_" + strconv . Itoa ( replyItem . CreatedBy ) + replyItem . Avatar
2016-12-07 09:34:09 +00:00
}
} else {
2017-09-03 04:50:31 +00:00
replyItem . Avatar = strings . Replace ( config . Noavatar , "{id}" , strconv . Itoa ( replyItem . CreatedBy ) , 1 )
2016-12-07 09:34:09 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-15 22:20:01 +00:00
replyItem . Tag = postGroup . Tag
2017-11-02 02:52:21 +00:00
replyItem . RelativeCreatedAt = relativeTime ( replyItem . CreatedAt )
2017-06-12 09:03:14 +00:00
2017-04-02 13:00:40 +00:00
// We really shouldn't have inline HTML, we should do something about this...
if replyItem . ActionType != "" {
2017-09-03 04:50:31 +00:00
switch replyItem . ActionType {
case "lock" :
replyItem . ActionType = "This topic has been locked by <a href='" + replyItem . UserLink + "'>" + replyItem . CreatedByName + "</a>"
replyItem . ActionIcon = "🔒︎"
case "unlock" :
replyItem . ActionType = "This topic has been reopened by <a href='" + replyItem . UserLink + "'>" + replyItem . CreatedByName + "</a>"
replyItem . ActionIcon = "🔓︎"
case "stick" :
replyItem . ActionType = "This topic has been pinned by <a href='" + replyItem . UserLink + "'>" + replyItem . CreatedByName + "</a>"
replyItem . ActionIcon = "📌︎"
case "unstick" :
replyItem . ActionType = "This topic has been unpinned by <a href='" + replyItem . UserLink + "'>" + replyItem . CreatedByName + "</a>"
replyItem . ActionIcon = "📌︎"
default :
replyItem . ActionType = replyItem . ActionType + " has happened"
replyItem . ActionIcon = ""
2017-04-02 13:00:40 +00:00
}
}
2017-02-10 13:39:13 +00:00
replyItem . Liked = false
2017-05-29 14:52:37 +00:00
2017-09-28 22:16:34 +00:00
if vhooks [ "topic_reply_row_assign" ] != nil {
runVhook ( "topic_reply_row_assign" , & tpage , & replyItem )
2016-12-11 16:06:17 +00:00
}
2016-12-18 12:56:06 +00:00
replyList = append ( replyList , replyItem )
2016-12-02 07:38:54 +00:00
}
err = rows . Err ( )
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-02 07:38:54 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-28 22:16:34 +00:00
tpage . ItemList = replyList
2017-09-03 04:50:31 +00:00
if preRenderHooks [ "pre_render_view_topic" ] != nil {
if runPreRenderHook ( "pre_render_view_topic" , w , r , & user , & tpage ) {
2017-10-30 09:57:08 +00:00
return nil
2017-07-12 11:05:18 +00:00
}
}
2017-10-30 09:57:08 +00:00
err = RunThemeTemplate ( headerVars . ThemeName , "topic" , tpage , w )
if err != nil {
return InternalError ( err , w , r )
}
return nil
2016-12-02 07:38:54 +00:00
}
2016-12-07 09:34:09 +00:00
2017-10-30 09:57:08 +00:00
func routeProfile ( w http . ResponseWriter , r * http . Request , user User ) RouteError {
headerVars , ferr := UserCheck ( w , r , & user )
if ferr != nil {
return ferr
2016-12-16 10:37:42 +00:00
}
2017-05-29 14:52:37 +00:00
2017-04-06 17:37:32 +00:00
var err error
2017-11-02 02:52:21 +00:00
var replyCreatedAt time . Time
var replyContent , replyCreatedByName , replyRelativeCreatedAt , replyAvatar , replyTag , replyClassName string
2017-04-06 17:37:32 +00:00
var rid , replyCreatedBy , replyLastEdit , replyLastEditBy , replyLines , replyGroup int
2017-09-28 22:16:34 +00:00
var replyList [ ] ReplyUser
2017-05-29 14:52:37 +00:00
2017-06-28 12:05:26 +00:00
// SEO URLs...
2017-09-03 04:50:31 +00:00
halves := strings . Split ( r . URL . Path [ len ( "/user/" ) : ] , "." )
2017-06-28 12:05:26 +00:00
if len ( halves ) < 2 {
2017-09-03 04:50:31 +00:00
halves = append ( halves , halves [ 0 ] )
2017-06-28 12:05:26 +00:00
}
pid , err := strconv . Atoi ( halves [ 1 ] )
2016-12-07 09:34:09 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return LocalError ( "The provided User ID is not a valid number." , w , r , user )
2016-12-07 09:34:09 +00:00
}
2017-05-29 14:52:37 +00:00
2017-02-15 10:49:30 +00:00
var puser * User
if pid == user . ID {
2017-09-03 04:50:31 +00:00
user . IsMod = true
2017-02-15 10:49:30 +00:00
puser = & user
2016-12-07 13:46:14 +00:00
} else {
// Fetch the user data
2017-09-15 22:20:01 +00:00
puser , err = users . Get ( pid )
2017-06-28 12:05:26 +00:00
if err == ErrNoRows {
2017-10-30 09:57:08 +00:00
return NotFound ( w , r )
2016-12-07 13:46:14 +00:00
} else if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-07 13:46:14 +00:00
}
2016-12-07 09:34:09 +00:00
}
2017-05-29 14:52:37 +00:00
2016-12-07 09:34:09 +00:00
// Get the replies..
2017-11-05 09:55:34 +00:00
rows , err := stmts . getProfileReplies . Query ( puser . ID )
2016-12-07 09:34:09 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-07 09:34:09 +00:00
}
defer rows . Close ( )
2017-05-29 14:52:37 +00:00
2016-12-07 09:34:09 +00:00
for rows . Next ( ) {
2017-02-11 14:51:16 +00:00
err := rows . Scan ( & rid , & replyContent , & replyCreatedBy , & replyCreatedAt , & replyLastEdit , & replyLastEditBy , & replyAvatar , & replyCreatedByName , & replyGroup )
2016-12-07 09:34:09 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-07 09:34:09 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-15 22:20:01 +00:00
group , err := gstore . Get ( replyGroup )
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-09-15 22:20:01 +00:00
}
2017-09-03 04:50:31 +00:00
replyLines = strings . Count ( replyContent , "\n" )
2017-09-15 22:20:01 +00:00
if group . IsMod || group . IsAdmin {
2017-09-18 17:03:52 +00:00
replyClassName = config . StaffCSS
2016-12-07 09:34:09 +00:00
} else {
2017-07-17 10:23:42 +00:00
replyClassName = ""
2016-12-07 09:34:09 +00:00
}
2017-11-02 02:52:21 +00:00
2016-12-07 09:34:09 +00:00
if replyAvatar != "" {
if replyAvatar [ 0 ] == '.' {
replyAvatar = "/uploads/avatar_" + strconv . Itoa ( replyCreatedBy ) + replyAvatar
}
} else {
2017-09-03 04:50:31 +00:00
replyAvatar = strings . Replace ( config . Noavatar , "{id}" , strconv . Itoa ( replyCreatedBy ) , 1 )
2016-12-07 09:34:09 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-15 22:20:01 +00:00
if group . Tag != "" {
replyTag = group . Tag
2016-12-07 09:34:09 +00:00
} else if puser . ID == replyCreatedBy {
replyTag = "Profile Owner"
} else {
replyTag = ""
}
2017-05-29 14:52:37 +00:00
2017-02-10 13:39:13 +00:00
replyLiked := false
replyLikeCount := 0
2017-11-02 02:52:21 +00:00
replyRelativeCreatedAt = relativeTime ( replyCreatedAt )
2017-05-29 14:52:37 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Add a hook here
2017-07-12 11:05:18 +00:00
2017-11-02 02:52:21 +00:00
replyList = append ( replyList , ReplyUser { rid , puser . ID , replyContent , parseMessage ( replyContent , 0 , "" ) , replyCreatedBy , buildProfileURL ( nameToSlug ( replyCreatedByName ) , replyCreatedBy ) , replyCreatedByName , replyGroup , replyCreatedAt , replyRelativeCreatedAt , replyLastEdit , replyLastEditBy , replyAvatar , replyClassName , replyLines , replyTag , "" , "" , "" , 0 , "" , replyLiked , replyLikeCount , "" , "" } )
2016-12-07 09:34:09 +00:00
}
err = rows . Err ( )
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-07 09:34:09 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
ppage := ProfilePage { puser . Name + "'s Profile" , user , headerVars , replyList , * puser }
if preRenderHooks [ "pre_render_profile" ] != nil {
if runPreRenderHook ( "pre_render_profile" , w , r , & user , & ppage ) {
2017-10-30 09:57:08 +00:00
return nil
2017-07-12 11:05:18 +00:00
}
}
2017-10-30 09:57:08 +00:00
err = template_profile_handle ( ppage , w )
if err != nil {
return InternalError ( err , w , r )
}
return nil
2016-12-07 09:34:09 +00:00
}
2017-10-30 09:57:08 +00:00
func routeLogin ( w http . ResponseWriter , r * http . Request , user User ) RouteError {
headerVars , ferr := UserCheck ( w , r , & user )
if ferr != nil {
return ferr
2016-12-16 10:37:42 +00:00
}
2016-12-02 07:38:54 +00:00
if user . Loggedin {
2017-10-30 09:57:08 +00:00
return LocalError ( "You're already logged in." , w , r , user )
2016-12-02 07:38:54 +00:00
}
2017-09-03 04:50:31 +00:00
pi := Page { "Login" , user , headerVars , tList , nil }
if preRenderHooks [ "pre_render_login" ] != nil {
if runPreRenderHook ( "pre_render_login" , w , r , & user , & pi ) {
2017-10-30 09:57:08 +00:00
return nil
2017-07-12 11:05:18 +00:00
}
}
2017-10-30 09:57:08 +00:00
err := templates . ExecuteTemplate ( w , "login.html" , pi )
if err != nil {
return InternalError ( err , w , r )
}
return nil
2016-12-02 07:38:54 +00:00
}
2017-09-10 16:57:22 +00:00
// TODO: Log failed attempted logins?
// TODO: Lock IPS out if they have too many failed attempts?
// TODO: Log unusual countries in comparison to the country a user usually logs in from? Alert the user about this?
2017-10-30 09:57:08 +00:00
func routeLoginSubmit ( w http . ResponseWriter , r * http . Request , user User ) RouteError {
2016-12-02 07:38:54 +00:00
if user . Loggedin {
2017-10-30 09:57:08 +00:00
return LocalError ( "You're already logged in." , w , r , user )
2016-12-02 07:38:54 +00:00
}
err := r . ParseForm ( )
2016-12-02 11:00:07 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return LocalError ( "Bad Form" , w , r , user )
2016-12-02 11:00:07 +00:00
}
2017-05-29 14:52:37 +00:00
2017-06-25 09:56:39 +00:00
uid , err := auth . Authenticate ( html . EscapeString ( r . PostFormValue ( "username" ) ) , r . PostFormValue ( "password" ) )
if err != nil {
2017-10-30 09:57:08 +00:00
return LocalError ( err . Error ( ) , w , r , user )
2016-12-02 07:38:54 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-15 22:20:01 +00:00
userPtr , err := users . Get ( uid )
2017-08-15 13:47:56 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return LocalError ( "Bad account" , w , r , user )
2017-08-15 13:47:56 +00:00
}
user = * userPtr
2017-06-25 09:56:39 +00:00
var session string
if user . Session == "" {
session , err = auth . CreateSession ( uid )
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-02 07:38:54 +00:00
}
2017-06-25 09:56:39 +00:00
} else {
session = user . Session
2016-12-02 07:38:54 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
auth . SetCookies ( w , uid , session )
if user . IsAdmin {
2017-08-15 13:47:56 +00:00
// Is this error check reundant? We already check for the error in PreRoute for the same IP
2017-10-30 09:57:08 +00:00
// TODO: Should we be logging this?
2017-11-10 00:16:15 +00:00
log . Printf ( "#%d has logged in with IP %s" , uid , user . LastIP )
2017-08-15 13:47:56 +00:00
}
2017-09-03 04:50:31 +00:00
http . Redirect ( w , r , "/" , http . StatusSeeOther )
2017-10-30 09:57:08 +00:00
return nil
2016-12-02 07:38:54 +00:00
}
2017-10-30 09:57:08 +00:00
func routeRegister ( w http . ResponseWriter , r * http . Request , user User ) RouteError {
headerVars , ferr := UserCheck ( w , r , & user )
if ferr != nil {
return ferr
2016-12-16 10:37:42 +00:00
}
2016-12-02 07:38:54 +00:00
if user . Loggedin {
2017-10-30 09:57:08 +00:00
return LocalError ( "You're already logged in." , w , r , user )
2016-12-02 07:38:54 +00:00
}
2017-09-03 04:50:31 +00:00
pi := Page { "Registration" , user , headerVars , tList , nil }
if preRenderHooks [ "pre_render_register" ] != nil {
if runPreRenderHook ( "pre_render_register" , w , r , & user , & pi ) {
2017-10-30 09:57:08 +00:00
return nil
2017-07-12 11:05:18 +00:00
}
}
2017-10-30 09:57:08 +00:00
err := templates . ExecuteTemplate ( w , "register.html" , pi )
if err != nil {
return InternalError ( err , w , r )
}
return nil
2016-12-02 07:38:54 +00:00
}
2017-10-30 09:57:08 +00:00
func routeRegisterSubmit ( w http . ResponseWriter , r * http . Request , user User ) RouteError {
2017-09-10 17:39:16 +00:00
headerLite , _ := SimpleUserCheck ( w , r , & user )
2017-08-20 09:39:02 +00:00
2016-12-02 07:38:54 +00:00
err := r . ParseForm ( )
2016-12-02 11:00:07 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return LocalError ( "Bad Form" , w , r , user )
2016-12-02 11:00:07 +00:00
}
2017-05-29 14:52:37 +00:00
2016-12-02 07:38:54 +00:00
username := html . EscapeString ( r . PostFormValue ( "username" ) )
2016-12-16 10:37:42 +00:00
if username == "" {
2017-10-30 09:57:08 +00:00
return LocalError ( "You didn't put in a username." , w , r , user )
2016-12-16 10:37:42 +00:00
}
email := html . EscapeString ( r . PostFormValue ( "email" ) )
if email == "" {
2017-10-30 09:57:08 +00:00
return LocalError ( "You didn't put in an email." , w , r , user )
2016-12-16 10:37:42 +00:00
}
2017-05-29 14:52:37 +00:00
2016-12-02 07:38:54 +00:00
password := r . PostFormValue ( "password" )
2016-12-16 10:37:42 +00:00
if password == "" {
2017-10-30 09:57:08 +00:00
return LocalError ( "You didn't put in a password." , w , r , user )
2016-12-16 10:37:42 +00:00
}
2017-06-19 08:06:54 +00:00
if password == username {
2017-10-30 09:57:08 +00:00
return LocalError ( "You can't use your username as your password." , w , r , user )
2017-06-19 08:06:54 +00:00
}
if password == email {
2017-10-30 09:57:08 +00:00
return LocalError ( "You can't use your email as your password." , w , r , user )
2017-06-19 08:06:54 +00:00
}
2017-09-03 04:50:31 +00:00
err = weakPassword ( password )
2017-06-19 08:06:54 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return LocalError ( err . Error ( ) , w , r , user )
2016-12-16 10:37:42 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-10 16:57:22 +00:00
confirmPassword := r . PostFormValue ( "confirm_password" )
2017-10-30 09:57:08 +00:00
log . Print ( "Registration Attempt! Username: " + username ) // TODO: Add more controls over what is logged when?
2017-05-29 14:52:37 +00:00
2016-12-02 07:38:54 +00:00
// Do the two inputted passwords match..?
2017-09-10 16:57:22 +00:00
if password != confirmPassword {
2017-10-30 09:57:08 +00:00
return LocalError ( "The two passwords don't match." , w , r , user )
2016-12-02 07:38:54 +00:00
}
2017-05-29 14:52:37 +00:00
2017-10-21 00:27:47 +00:00
var active bool
var group int
2017-08-20 09:39:02 +00:00
switch headerLite . Settings [ "activation_type" ] {
2017-09-03 04:50:31 +00:00
case 1 : // Activate All
2017-10-21 00:27:47 +00:00
active = true
2017-09-03 04:50:31 +00:00
group = config . DefaultGroup
default : // Anything else. E.g. Admin Activation or Email Activation.
group = config . ActivationGroup
2016-12-21 02:30:32 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-15 22:20:01 +00:00
uid , err := users . Create ( username , password , email , group , active )
2017-09-03 04:50:31 +00:00
if err == errAccountExists {
2017-10-30 09:57:08 +00:00
return LocalError ( "This username isn't available. Try another." , w , r , user )
2017-06-25 09:56:39 +00:00
} else if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2016-12-02 07:38:54 +00:00
}
2017-05-29 14:52:37 +00:00
2017-01-03 07:47:31 +00:00
// Check if this user actually owns this email, if email activation is on, automatically flip their account to active when the email is validated. Validation is also useful for determining whether this user should receive any alerts, etc. via email
2017-07-17 10:23:42 +00:00
if site . EnableEmails {
2017-01-03 07:47:31 +00:00
token , err := GenerateSafeString ( 80 )
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-01-03 07:47:31 +00:00
}
2017-11-05 09:55:34 +00:00
_ , err = stmts . addEmail . Exec ( email , uid , 0 , token )
2017-01-03 07:47:31 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-01-03 07:47:31 +00:00
}
2017-05-29 14:52:37 +00:00
2017-01-03 07:47:31 +00:00
if ! SendValidationEmail ( username , email , token ) {
2017-10-30 09:57:08 +00:00
return LocalError ( "We were unable to send the email for you to confirm that this email address belongs to you. You may not have access to some functionality until you do so. Please ask an administrator for assistance." , w , r , user )
2017-01-03 07:47:31 +00:00
}
}
2017-05-29 14:52:37 +00:00
2017-06-25 09:56:39 +00:00
session , err := auth . CreateSession ( uid )
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-06-25 09:56:39 +00:00
}
2017-09-03 04:50:31 +00:00
auth . SetCookies ( w , uid , session )
http . Redirect ( w , r , "/" , http . StatusSeeOther )
2017-10-30 09:57:08 +00:00
return nil
2016-12-02 07:38:54 +00:00
}
2017-02-28 09:27:28 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Set the cookie domain
2017-10-30 09:57:08 +00:00
func routeChangeTheme ( w http . ResponseWriter , r * http . Request , user User ) RouteError {
2017-09-10 17:39:16 +00:00
//headerLite, _ := SimpleUserCheck(w, r, &user)
2017-09-10 16:57:22 +00:00
err := r . ParseForm ( )
if err != nil {
2017-10-30 09:57:08 +00:00
return PreError ( "Bad Form" , w , r )
2017-09-10 16:57:22 +00:00
}
// TODO: Rename isJs to something else, just in case we rewrite the JS side in WebAssembly?
isJs := ( r . PostFormValue ( "isJs" ) == "1" )
newTheme := html . EscapeString ( r . PostFormValue ( "newTheme" ) )
theme , ok := themes [ newTheme ]
if ! ok || theme . HideFromThemes {
2017-10-30 09:57:08 +00:00
// TODO: Should we be logging this?
2017-09-10 16:57:22 +00:00
log . Print ( "Bad Theme: " , newTheme )
2017-10-30 09:57:08 +00:00
return LocalErrorJSQ ( "That theme doesn't exist" , w , r , user , isJs )
2017-09-10 16:57:22 +00:00
}
// TODO: Store the current theme in the user's account?
/ * if user . Loggedin {
2017-11-05 09:55:34 +00:00
_ , err = stmts . changeTheme . Exec ( newTheme , user . ID )
2017-09-10 16:57:22 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-09-10 16:57:22 +00:00
}
} * /
cookie := http . Cookie { Name : "current_theme" , Value : newTheme , Path : "/" , MaxAge : year }
http . SetCookie ( w , & cookie )
if ! isJs {
http . Redirect ( w , r , "/" , http . StatusSeeOther )
} else {
_ , _ = w . Write ( successJSONBytes )
}
2017-10-30 09:57:08 +00:00
return nil
2017-09-10 16:57:22 +00:00
}
// TODO: We don't need support XML here to support sitemaps, we could handle those elsewhere
var phraseLoginAlerts = [ ] byte ( ` { "msgs":[ { "msg":"Login to see your alerts","path":"/accounts/login"}]} ` )
2017-09-03 04:50:31 +00:00
2017-10-30 09:57:08 +00:00
func routeAPI ( w http . ResponseWriter , r * http . Request , user User ) RouteError {
2017-09-03 04:50:31 +00:00
w . Header ( ) . Set ( "Content-Type" , "application/json" )
2017-02-28 09:27:28 +00:00
err := r . ParseForm ( )
if err != nil {
2017-10-30 09:57:08 +00:00
return PreErrorJS ( "Bad Form" , w , r )
2017-02-28 09:27:28 +00:00
}
2017-05-29 14:52:37 +00:00
2017-02-28 09:27:28 +00:00
action := r . FormValue ( "action" )
if action != "get" && action != "set" {
2017-10-30 09:57:08 +00:00
return PreErrorJS ( "Invalid Action" , w , r )
2017-02-28 09:27:28 +00:00
}
2017-05-29 14:52:37 +00:00
2017-02-28 09:27:28 +00:00
module := r . FormValue ( "module" )
2017-09-03 04:50:31 +00:00
switch module {
case "dismiss-alert" :
asid , err := strconv . Atoi ( r . FormValue ( "asid" ) )
if err != nil {
2017-10-30 09:57:08 +00:00
return PreErrorJS ( "Invalid asid" , w , r )
2017-09-03 04:50:31 +00:00
}
2017-05-29 14:52:37 +00:00
2017-11-05 09:55:34 +00:00
_ , err = stmts . deleteActivityStreamMatch . Exec ( user . ID , asid )
2017-09-03 04:50:31 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalError ( err , w , r )
2017-09-03 04:50:31 +00:00
}
case "alerts" : // A feed of events tailored for a specific user
if ! user . Loggedin {
2017-09-10 16:57:22 +00:00
w . Write ( phraseLoginAlerts )
2017-10-30 09:57:08 +00:00
return nil
2017-09-03 04:50:31 +00:00
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
var msglist , event , elementType string
var asid , actorID , targetUserID , elementID int
var msgCount int
2017-06-10 07:58:15 +00:00
2017-11-05 09:55:34 +00:00
err = stmts . getActivityCountByWatcher . QueryRow ( user . ID ) . Scan ( & msgCount )
2017-09-03 04:50:31 +00:00
if err == ErrNoRows {
2017-10-30 09:57:08 +00:00
return PreErrorJS ( "Couldn't find the parent topic" , w , r )
2017-09-03 04:50:31 +00:00
} else if err != nil {
2017-10-30 09:57:08 +00:00
return InternalErrorJS ( err , w , r )
2017-09-03 04:50:31 +00:00
}
2017-11-05 09:55:34 +00:00
rows , err := stmts . getActivityFeedByWatcher . Query ( user . ID )
2017-09-03 04:50:31 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalErrorJS ( err , w , r )
2017-09-03 04:50:31 +00:00
}
defer rows . Close ( )
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
for rows . Next ( ) {
err = rows . Scan ( & asid , & actorID , & targetUserID , & event , & elementType , & elementID )
2017-02-28 09:27:28 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalErrorJS ( err , w , r )
2017-02-28 09:27:28 +00:00
}
2017-09-03 04:50:31 +00:00
res , err := buildAlert ( asid , event , elementType , actorID , targetUserID , elementID , user )
2017-02-28 09:27:28 +00:00
if err != nil {
2017-10-30 09:57:08 +00:00
return LocalErrorJS ( err . Error ( ) , w , r )
2017-02-28 09:27:28 +00:00
}
2017-09-03 04:50:31 +00:00
msglist += res + ","
}
2017-05-29 14:52:37 +00:00
2017-09-03 04:50:31 +00:00
err = rows . Err ( )
if err != nil {
2017-10-30 09:57:08 +00:00
return InternalErrorJS ( err , w , r )
2017-09-03 04:50:31 +00:00
}
if len ( msglist ) != 0 {
msglist = msglist [ 0 : len ( msglist ) - 1 ]
}
_ , _ = w . Write ( [ ] byte ( ` { "msgs":[ ` + msglist + ` ],"msgCount": ` + strconv . Itoa ( msgCount ) + ` } ` ) )
//log.Print(`{"msgs":[` + msglist + `],"msgCount":` + strconv.Itoa(msgCount) + `}`)
//case "topics":
//case "forums":
//case "users":
//case "pages":
// This might not be possible. We might need .xml paths for sitemaps
/ * case "sitemap" :
if format != "xml" {
PreError ( "You can only fetch sitemaps in the XML format!" , w , r )
return
} * /
default :
2017-10-30 09:57:08 +00:00
return PreErrorJS ( "Invalid Module" , w , r )
2017-02-28 09:27:28 +00:00
}
2017-10-30 09:57:08 +00:00
return nil
2017-02-28 09:27:28 +00:00
}