d9acf27c5b
Added AJAX Pagination for the Topic List and Forum Page. A new log file pair is now created every-time Gosora starts up. Added proper per-theme template overrides. Added EasyJSON to make JSON serialisation faster. Moved a bit of boilerplate into paginator.html Improved paginator.html with a richer template with first, last and symbols instead of text. Phased out direct access to Templates.ExecuteTemplate across the software. Fixed the Live Topic List so it should work again. Added MicroAvatar to WsJSONUser for topic list JSON requests. An instance of the plugin is now passed to plugin handlers rather than having the plugins manipulate the globals directly. Added the pre_render_panel_forum_edit and pre_render_panel_forum_edit_perms hooks to replace pre_render_panel_edit_forum. Renamed the pre_render_panel_edit_user hook to pre_render_panel_user_edit Reduced the amount of noise from fsnotify. Added RawPrepare() to qgen.Accumulator. Added a temporary phrase whitelist to the phrase endpoint. Moved the location of the zone data assignments in the topic list to reduce the chances of security issues in the future. Changed the signature of routes/panel/renderTemplate() requiring some changes across the panel routes. Removed bits of boilerplate in some of the panel routes with renderTemplate() Added a BenchmarkTopicsGuestJSRouteParallelWithRouter benchmark. Removed a fair bit of boilerplate for each page struct by generating a couple of interface casts for each template file instead. Added the profile_comments_row_alt template. Added the topics_quick_topic template to reuse part of the quick topic logic for both the topic list and forum page. Tweaked the CSS for the Online Users Widget. Tweaked the CSS for Widgets in every theme with a sidebar. Refactored the template initialisers to hopefully reduce the amount of boilerplate and make things easier to maintain and follow. Add genIntTmpl in the template initialiser file to reduce the amount of boilerplate needed for the fallback template bindings. Removed the topics_head phrase. Moved the paginator_ phrases into the paginator. namespace and renamed them accordingly. Added the paginator.first_page phrase. Added the paginator.first_page_aria phrase. Added the paginator.last_page phrase. Added the paginator.last_page_aria phrase. Added the panel_forum_delete_are_you_sure phrase. Fixed a data race in LogWarning()
152 lines
4.5 KiB
Go
152 lines
4.5 KiB
Go
package routes
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/Azareal/Gosora/common"
|
|
"github.com/Azareal/Gosora/common/phrases"
|
|
)
|
|
|
|
// TODO: Implement search
|
|
func TopicList(w http.ResponseWriter, r *http.Request, user common.User, header *common.Header) common.RouteError {
|
|
group, err := common.Groups.Get(user.Group)
|
|
if err != nil {
|
|
log.Printf("Group #%d doesn't exist despite being used by common.User #%d", user.Group, user.ID)
|
|
return common.LocalError("Something weird happened", w, r, user)
|
|
}
|
|
|
|
// Get the current page
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
|
sfids := r.FormValue("fids")
|
|
var fids []int
|
|
if sfids != "" {
|
|
for _, sfid := range strings.Split(sfids, ",") {
|
|
fid, err := strconv.Atoi(sfid)
|
|
if err != nil {
|
|
return common.LocalError("Invalid fid", w, r, user)
|
|
}
|
|
fids = append(fids, fid)
|
|
}
|
|
}
|
|
|
|
// TODO: Pass a struct back rather than passing back so many variables
|
|
var topicList []*common.TopicsRow
|
|
var forumList []common.Forum
|
|
var paginator common.Paginator
|
|
if user.IsSuperAdmin {
|
|
topicList, forumList, paginator, err = common.TopicList.GetList(page, "", fids)
|
|
} else {
|
|
topicList, forumList, paginator, err = common.TopicList.GetListByGroup(group, page, "", fids)
|
|
}
|
|
if err != nil {
|
|
return common.InternalError(err, w, r)
|
|
}
|
|
// ! Need an inline error not a page level error
|
|
if len(topicList) == 0 {
|
|
return common.NotFound(w, r, header)
|
|
}
|
|
|
|
// TODO: Reduce the amount of boilerplate here
|
|
if r.FormValue("js") == "1" {
|
|
outBytes, err := wsTopicList(topicList, paginator.LastPage).MarshalJSON()
|
|
if err != nil {
|
|
return common.InternalError(err, w, r)
|
|
}
|
|
w.Write(outBytes)
|
|
return nil
|
|
}
|
|
|
|
header.Title = phrases.GetTitlePhrase("topics")
|
|
header.Zone = "topics"
|
|
header.Path = "/topics/"
|
|
header.MetaDesc = header.Settings["meta_desc"].(string)
|
|
if len(fids) == 1 {
|
|
forum, err := common.Forums.Get(fids[0])
|
|
if err != nil {
|
|
return common.LocalError("Invalid fid forum", w, r, user)
|
|
}
|
|
header.Title = forum.Name
|
|
header.ZoneID = forum.ID
|
|
}
|
|
|
|
pi := common.TopicListPage{header, topicList, forumList, common.Config.DefaultForum, common.TopicListSort{"lastupdated", false}, paginator}
|
|
return renderTemplate("topics", w, r, header, pi)
|
|
}
|
|
|
|
func wsTopicList(topicList []*common.TopicsRow, lastPage int) *common.WsTopicList {
|
|
wsTopicList := make([]*common.WsTopicsRow, len(topicList))
|
|
for i, topicRow := range topicList {
|
|
wsTopicList[i] = topicRow.WebSockets()
|
|
}
|
|
return &common.WsTopicList{wsTopicList, lastPage}
|
|
}
|
|
|
|
func TopicListMostViewed(w http.ResponseWriter, r *http.Request, user common.User, header *common.Header) common.RouteError {
|
|
header.Title = phrases.GetTitlePhrase("topics")
|
|
header.Zone = "topics"
|
|
header.Path = "/topics/"
|
|
header.MetaDesc = header.Settings["meta_desc"].(string)
|
|
|
|
group, err := common.Groups.Get(user.Group)
|
|
if err != nil {
|
|
log.Printf("Group #%d doesn't exist despite being used by common.User #%d", user.Group, user.ID)
|
|
return common.LocalError("Something weird happened", w, r, user)
|
|
}
|
|
|
|
// Get the current page
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
|
sfids := r.FormValue("fids")
|
|
var fids []int
|
|
if sfids != "" {
|
|
for _, sfid := range strings.Split(sfids, ",") {
|
|
fid, err := strconv.Atoi(sfid)
|
|
if err != nil {
|
|
return common.LocalError("Invalid fid", w, r, user)
|
|
}
|
|
fids = append(fids, fid)
|
|
}
|
|
if len(fids) == 1 {
|
|
forum, err := common.Forums.Get(fids[0])
|
|
if err != nil {
|
|
return common.LocalError("Invalid fid forum", w, r, user)
|
|
}
|
|
header.Title = forum.Name
|
|
header.ZoneID = forum.ID
|
|
}
|
|
}
|
|
|
|
// TODO: Pass a struct back rather than passing back so many variables
|
|
var topicList []*common.TopicsRow
|
|
var forumList []common.Forum
|
|
var paginator common.Paginator
|
|
if user.IsSuperAdmin {
|
|
topicList, forumList, paginator, err = common.TopicList.GetList(page, "most-viewed", fids)
|
|
} else {
|
|
topicList, forumList, paginator, err = common.TopicList.GetListByGroup(group, page, "most-viewed", fids)
|
|
}
|
|
if err != nil {
|
|
return common.InternalError(err, w, r)
|
|
}
|
|
// ! Need an inline error not a page level error
|
|
if len(topicList) == 0 {
|
|
return common.NotFound(w, r, header)
|
|
}
|
|
|
|
//MarshalJSON() ([]byte, error)
|
|
// TODO: Reduce the amount of boilerplate here
|
|
if r.FormValue("js") == "1" {
|
|
outBytes, err := wsTopicList(topicList, paginator.LastPage).MarshalJSON()
|
|
if err != nil {
|
|
return common.InternalError(err, w, r)
|
|
}
|
|
w.Write(outBytes)
|
|
return nil
|
|
}
|
|
|
|
pi := common.TopicListPage{header, topicList, forumList, common.Config.DefaultForum, common.TopicListSort{"mostviewed", false}, paginator}
|
|
return renderTemplate("topics", w, r, header, pi)
|
|
}
|