2018-02-22 02:27:17 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2019-02-10 05:52:26 +00:00
|
|
|
"encoding/json"
|
2018-02-22 02:27:17 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/Gosora/common"
|
|
|
|
"github.com/Azareal/Gosora/common/counters"
|
2018-11-12 09:23:36 +00:00
|
|
|
"github.com/Azareal/Gosora/common/phrases"
|
2018-10-27 03:21:02 +00:00
|
|
|
"github.com/Azareal/Gosora/query_gen"
|
2018-02-22 02:27:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ForumStmts struct {
|
|
|
|
getTopics *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
var forumStmts ForumStmts
|
|
|
|
|
|
|
|
// TODO: Move these DbInits into *Forum as Topics()
|
|
|
|
func init() {
|
|
|
|
common.DbInits.Add(func(acc *qgen.Accumulator) error {
|
|
|
|
forumStmts = ForumStmts{
|
2018-12-27 05:42:41 +00:00
|
|
|
getTopics: acc.Select("topics").Columns("tid, title, content, createdBy, is_closed, sticky, createdAt, lastReplyAt, lastReplyBy, lastReplyID, parentID, views, postCount, likeCount").Where("parentID = ?").Orderby("sticky DESC, lastReplyAt DESC, createdBy DESC").Limit("?,?").Prepare(),
|
2018-02-22 02:27:17 +00:00
|
|
|
}
|
|
|
|
return acc.FirstError()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
// TODO: Retire this in favour of an alias for /topics/?
|
2018-11-12 09:23:36 +00:00
|
|
|
func ViewForum(w http.ResponseWriter, r *http.Request, user common.User, header *common.Header, sfid string) common.RouteError {
|
2018-02-22 02:27:17 +00:00
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
2018-11-12 09:23:36 +00:00
|
|
|
_, fid, err := ParseSEOURL(sfid)
|
2018-02-22 02:27:17 +00:00
|
|
|
if err != nil {
|
2018-11-12 09:23:36 +00:00
|
|
|
return common.PreError(phrases.GetErrorPhrase("url_id_must_be_integer"), w, r)
|
2018-02-22 02:27:17 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 09:23:36 +00:00
|
|
|
ferr := common.ForumUserCheck(header, w, r, &user, fid)
|
2018-02-22 02:27:17 +00:00
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !user.Perms.ViewTopic {
|
|
|
|
return common.NoPermissions(w, r, user)
|
|
|
|
}
|
2018-12-08 06:49:14 +00:00
|
|
|
header.Path = "/forums/"
|
2018-02-22 02:27:17 +00:00
|
|
|
|
|
|
|
// TODO: Fix this double-check
|
|
|
|
forum, err := common.Forums.Get(fid)
|
|
|
|
if err == sql.ErrNoRows {
|
2018-04-22 12:33:56 +00:00
|
|
|
return common.NotFound(w, r, header)
|
2018-02-22 02:27:17 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
2018-05-27 09:36:35 +00:00
|
|
|
header.Title = forum.Name
|
2018-02-22 02:27:17 +00:00
|
|
|
|
|
|
|
// TODO: Does forum.TopicCount take the deleted items into consideration for guests? We don't have soft-delete yet, only hard-delete
|
|
|
|
offset, page, lastPage := common.PageOffset(forum.TopicCount, page, common.Config.ItemsPerPage)
|
|
|
|
|
|
|
|
// TODO: Move this to *Forum
|
|
|
|
rows, err := forumStmts.getTopics.Query(fid, offset, common.Config.ItemsPerPage)
|
|
|
|
if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
// TODO: Use something other than TopicsRow as we don't need to store the forum name and link on each and every topic item?
|
|
|
|
var topicList []*common.TopicsRow
|
|
|
|
var reqUserList = make(map[int]bool)
|
|
|
|
for rows.Next() {
|
|
|
|
var topicItem = common.TopicsRow{ID: 0}
|
2018-12-27 05:42:41 +00:00
|
|
|
err := rows.Scan(&topicItem.ID, &topicItem.Title, &topicItem.Content, &topicItem.CreatedBy, &topicItem.IsClosed, &topicItem.Sticky, &topicItem.CreatedAt, &topicItem.LastReplyAt, &topicItem.LastReplyBy, &topicItem.LastReplyID, &topicItem.ParentID, &topicItem.ViewCount, &topicItem.PostCount, &topicItem.LikeCount)
|
2018-02-22 02:27:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
topicItem.Link = common.BuildTopicURL(common.NameToSlug(topicItem.Title), topicItem.ID)
|
2018-12-17 04:58:55 +00:00
|
|
|
// TODO: Create a specialised function with a bit less overhead for getting the last page for a post count
|
|
|
|
_, _, lastPage := common.PageOffset(topicItem.PostCount, 1, common.Config.ItemsPerPage)
|
|
|
|
topicItem.LastPage = lastPage
|
2018-02-22 02:27:17 +00:00
|
|
|
|
2018-10-21 13:54:32 +00:00
|
|
|
header.Hooks.VhookNoRet("forum_trow_assign", &topicItem, &forum)
|
2018-02-22 02:27:17 +00:00
|
|
|
topicList = append(topicList, &topicItem)
|
|
|
|
reqUserList[topicItem.CreatedBy] = true
|
|
|
|
reqUserList[topicItem.LastReplyBy] = true
|
|
|
|
}
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the user ID map to a slice, then bulk load the users
|
|
|
|
var idSlice = make([]int, len(reqUserList))
|
|
|
|
var i int
|
|
|
|
for userID := range reqUserList {
|
|
|
|
idSlice[i] = userID
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: What if a user is deleted via the Control Panel?
|
|
|
|
userList, err := common.Users.BulkGetMap(idSlice)
|
|
|
|
if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Second pass to the add the user data
|
|
|
|
// TODO: Use a pointer to TopicsRow instead of TopicsRow itself?
|
|
|
|
for _, topicItem := range topicList {
|
|
|
|
topicItem.Creator = userList[topicItem.CreatedBy]
|
|
|
|
topicItem.LastUser = userList[topicItem.LastReplyBy]
|
|
|
|
}
|
2019-02-10 05:52:26 +00:00
|
|
|
header.Zone = "view_forum"
|
|
|
|
header.ZoneID = forum.ID
|
|
|
|
|
|
|
|
// TODO: Reduce the amount of boilerplate here
|
|
|
|
if r.FormValue("js") == "1" {
|
|
|
|
outBytes, err := json.Marshal(wsTopicList(topicList, lastPage))
|
|
|
|
if err != nil {
|
|
|
|
return common.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
w.Write(outBytes)
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-22 02:27:17 +00:00
|
|
|
|
|
|
|
pageList := common.Paginate(forum.TopicCount, common.Config.ItemsPerPage, 5)
|
2018-05-27 09:36:35 +00:00
|
|
|
pi := common.ForumPage{header, topicList, forum, common.Paginator{pageList, page, lastPage}}
|
2018-11-12 09:23:36 +00:00
|
|
|
ferr = renderTemplate("forum", w, r, header, pi)
|
2018-02-22 02:27:17 +00:00
|
|
|
counters.ForumViewCounter.Bump(forum.ID)
|
2018-11-12 09:23:36 +00:00
|
|
|
return ferr
|
2018-02-22 02:27:17 +00:00
|
|
|
}
|