2018-02-22 02:27:17 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
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"
|
2019-10-01 21:06:22 +00:00
|
|
|
p "github.com/Azareal/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/?
|
2019-04-19 06:36:26 +00:00
|
|
|
func ViewForum(w http.ResponseWriter, r *http.Request, user c.User, header *c.Header, sfid string) c.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 {
|
2019-10-01 21:06:22 +00:00
|
|
|
return c.SimpleError(p.GetErrorPhrase("url_id_must_be_integer"), w, r, header)
|
2018-02-22 02:27:17 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
ferr := c.ForumUserCheck(header, w, r, &user, fid)
|
2018-02-22 02:27:17 +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-22 02:27:17 +00:00
|
|
|
}
|
2018-12-08 06:49:14 +00:00
|
|
|
header.Path = "/forums/"
|
2018-02-22 02:27:17 +00:00
|
|
|
|
|
|
|
// TODO: Fix this double-check
|
2019-04-19 06:36:26 +00:00
|
|
|
forum, err := c.Forums.Get(fid)
|
2018-02-22 02:27:17 +00:00
|
|
|
if err == sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.NotFound(w, r, header)
|
2018-02-22 02:27:17 +00:00
|
|
|
} else if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-02-22 02:27:17 +00:00
|
|
|
}
|
2018-05-27 09:36:35 +00:00
|
|
|
header.Title = forum.Name
|
2019-02-24 08:02:00 +00:00
|
|
|
header.OGDesc = forum.Desc
|
2018-02-22 02:27:17 +00:00
|
|
|
|
2020-03-01 06:22:43 +00:00
|
|
|
topicList, pagi, err := c.TopicList.GetListByForum(forum, page, "")
|
2018-02-22 02:27:17 +00:00
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-02-22 02:27:17 +00:00
|
|
|
}
|
|
|
|
|
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" {
|
2020-03-01 06:22:43 +00:00
|
|
|
outBytes, err := wsTopicList(topicList, pagi.LastPage).MarshalJSON()
|
2019-02-10 05:52:26 +00:00
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2019-02-10 05:52:26 +00:00
|
|
|
}
|
|
|
|
w.Write(outBytes)
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-22 02:27:17 +00:00
|
|
|
|
2020-03-01 06:22:43 +00:00
|
|
|
//pageList := c.Paginate(page, lastPage, 5)
|
|
|
|
pi := c.ForumPage{header, topicList, forum, pagi}
|
2019-06-01 12:31:48 +00:00
|
|
|
tmpl := forum.Tmpl
|
2019-05-27 09:00:40 +00:00
|
|
|
if tmpl == "" {
|
2019-06-01 12:31:48 +00:00
|
|
|
ferr = renderTemplate("forum", w, r, header, pi)
|
|
|
|
} else {
|
2019-10-01 21:06:22 +00:00
|
|
|
tmpl = "forum_" + tmpl
|
|
|
|
err = renderTemplate3(tmpl, tmpl, w, r, header, pi)
|
2019-06-01 12:31:48 +00:00
|
|
|
if err != nil {
|
|
|
|
ferr = renderTemplate("forum", w, r, header, pi)
|
|
|
|
}
|
2019-05-27 09:00:40 +00:00
|
|
|
}
|
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
|
|
|
}
|