2018-02-22 02:27:17 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2022-02-21 03:53:13 +00:00
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2018-02-22 02:27:17 +00:00
|
|
|
|
2022-02-21 03:53:13 +00:00
|
|
|
c "git.tuxpa.in/a/gosora/common"
|
|
|
|
co "git.tuxpa.in/a/gosora/common/counters"
|
|
|
|
p "git.tuxpa.in/a/gosora/common/phrases"
|
2018-02-22 02:27:17 +00:00
|
|
|
)
|
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
// TODO: Retire this in favour of an alias for /topics/?
|
2020-04-09 08:35:43 +00:00
|
|
|
func ViewForum(w http.ResponseWriter, r *http.Request, u *c.User, h *c.Header, sfid string) c.RouteError {
|
2022-02-21 03:32:53 +00:00
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
|
|
|
_, fid, err := ParseSEOURL(sfid)
|
|
|
|
if err != nil {
|
|
|
|
return c.SimpleError(p.GetErrorPhrase("url_id_must_be_integer"), w, r, h)
|
|
|
|
}
|
2018-02-22 02:27:17 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
ferr := c.ForumUserCheck(h, w, r, u, fid)
|
|
|
|
if ferr != nil {
|
|
|
|
return ferr
|
|
|
|
}
|
|
|
|
if !u.Perms.ViewTopic {
|
|
|
|
return c.NoPermissions(w, r, u)
|
|
|
|
}
|
|
|
|
h.Path = "/forums/"
|
2018-02-22 02:27:17 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
// TODO: Fix this double-check
|
|
|
|
forum, err := c.Forums.Get(fid)
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return c.NotFound(w, r, h)
|
|
|
|
} else if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
h.Title = forum.Name
|
|
|
|
h.OGDesc = forum.Desc
|
2018-02-22 02:27:17 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
topicList, pagi, err := c.TopicList.GetListByForum(forum, page, 0)
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
h.Zone = "view_forum"
|
|
|
|
h.ZoneID = forum.ID
|
2019-02-10 05:52:26 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
// TODO: Reduce the amount of boilerplate here
|
|
|
|
if r.FormValue("js") == "1" {
|
|
|
|
outBytes, err := wsTopicList(topicList, pagi.LastPage).MarshalJSON()
|
|
|
|
if err != nil {
|
|
|
|
return c.InternalError(err, w, r)
|
|
|
|
}
|
|
|
|
w.Write(outBytes)
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-22 02:27:17 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
topicList2 := make([]c.TopicsRowMut, len(topicList))
|
|
|
|
canMod := u.Perms.CloseTopic || u.Perms.MoveTopic
|
|
|
|
for i, t := range topicList {
|
|
|
|
topicList2[i] = c.TopicsRowMut{t, t.CreatedBy == u.ID || canMod}
|
|
|
|
}
|
2020-07-19 03:26:56 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
//pageList := c.Paginate(page, lastPage, 5)
|
|
|
|
pi := c.ForumPage{h, topicList2, forum, u.Perms.CloseTopic, u.Perms.MoveTopic, pagi}
|
|
|
|
tmpl := forum.Tmpl
|
|
|
|
if tmpl == "" {
|
|
|
|
ferr = renderTemplate("forum", w, r, h, pi)
|
|
|
|
} else {
|
|
|
|
tmpl = "forum_" + tmpl
|
|
|
|
err = renderTemplate3(tmpl, tmpl, w, r, h, pi)
|
|
|
|
if err != nil {
|
|
|
|
ferr = renderTemplate("forum", w, r, h, pi)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
co.ForumViewCounter.Bump(forum.ID)
|
|
|
|
return ferr
|
2018-02-22 02:27:17 +00:00
|
|
|
}
|