github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/routes/forum.go (about)

     1  package routes
     2  
     3  import (
     4  	"database/sql"
     5  	"net/http"
     6  	"strconv"
     7  
     8  	c "github.com/Azareal/Gosora/common"
     9  	co "github.com/Azareal/Gosora/common/counters"
    10  	p "github.com/Azareal/Gosora/common/phrases"
    11  )
    12  
    13  // TODO: Retire this in favour of an alias for /topics/?
    14  func ViewForum(w http.ResponseWriter, r *http.Request, u *c.User, h *c.Header, sfid string) c.RouteError {
    15  	page, _ := strconv.Atoi(r.FormValue("page"))
    16  	_, fid, err := ParseSEOURL(sfid)
    17  	if err != nil {
    18  		return c.SimpleError(p.GetErrorPhrase("url_id_must_be_integer"), w, r, h)
    19  	}
    20  
    21  	ferr := c.ForumUserCheck(h, w, r, u, fid)
    22  	if ferr != nil {
    23  		return ferr
    24  	}
    25  	if !u.Perms.ViewTopic {
    26  		return c.NoPermissions(w, r, u)
    27  	}
    28  	h.Path = "/forums/"
    29  
    30  	// TODO: Fix this double-check
    31  	forum, err := c.Forums.Get(fid)
    32  	if err == sql.ErrNoRows {
    33  		return c.NotFound(w, r, h)
    34  	} else if err != nil {
    35  		return c.InternalError(err, w, r)
    36  	}
    37  	h.Title = forum.Name
    38  	h.OGDesc = forum.Desc
    39  
    40  	topicList, pagi, err := c.TopicList.GetListByForum(forum, page, 0)
    41  	if err != nil {
    42  		return c.InternalError(err, w, r)
    43  	}
    44  	h.Zone = "view_forum"
    45  	h.ZoneID = forum.ID
    46  
    47  	// TODO: Reduce the amount of boilerplate here
    48  	if r.FormValue("js") == "1" {
    49  		outBytes, err := wsTopicList(topicList, pagi.LastPage).MarshalJSON()
    50  		if err != nil {
    51  			return c.InternalError(err, w, r)
    52  		}
    53  		w.Write(outBytes)
    54  		return nil
    55  	}
    56  
    57  	topicList2 := make([]c.TopicsRowMut, len(topicList))
    58  	canMod := u.Perms.CloseTopic || u.Perms.MoveTopic
    59  	for i, t := range topicList {
    60  		topicList2[i] = c.TopicsRowMut{t, t.CreatedBy == u.ID || canMod}
    61  	}
    62  
    63  	//pageList := c.Paginate(page, lastPage, 5)
    64  	pi := c.ForumPage{h, topicList2, forum, u.Perms.CloseTopic, u.Perms.MoveTopic, pagi}
    65  	tmpl := forum.Tmpl
    66  	if tmpl == "" {
    67  		ferr = renderTemplate("forum", w, r, h, pi)
    68  	} else {
    69  		tmpl = "forum_" + tmpl
    70  		err = renderTemplate3(tmpl, tmpl, w, r, h, pi)
    71  		if err != nil {
    72  			ferr = renderTemplate("forum", w, r, h, pi)
    73  		}
    74  	}
    75  	co.ForumViewCounter.Bump(forum.ID)
    76  	return ferr
    77  }