2018-02-10 15:07:21 +00:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
2019-02-23 06:29:19 +00:00
|
|
|
"database/sql"
|
2018-02-10 15:07:21 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2019-02-10 05:52:26 +00:00
|
|
|
"strings"
|
2018-02-10 15:07:21 +00:00
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2018-11-01 06:43:56 +00:00
|
|
|
"github.com/Azareal/Gosora/common/phrases"
|
2018-02-10 15:07:21 +00:00
|
|
|
)
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
func wsTopicList(topicList []*c.TopicsRow, lastPage int) *c.WsTopicList {
|
|
|
|
wsTopicList := make([]*c.WsTopicsRow, len(topicList))
|
2019-02-10 05:52:26 +00:00
|
|
|
for i, topicRow := range topicList {
|
|
|
|
wsTopicList[i] = topicRow.WebSockets()
|
|
|
|
}
|
2019-04-19 06:36:26 +00:00
|
|
|
return &c.WsTopicList{wsTopicList, lastPage}
|
2019-02-10 05:52:26 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
func TopicList(w http.ResponseWriter, r *http.Request, user c.User, header *c.Header) c.RouteError {
|
2019-02-23 08:16:54 +00:00
|
|
|
return TopicListCommon(w, r, user, header, "lastupdated", "")
|
2019-02-23 06:29:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
func TopicListMostViewed(w http.ResponseWriter, r *http.Request, user c.User, header *c.Header) c.RouteError {
|
2019-02-23 08:16:54 +00:00
|
|
|
return TopicListCommon(w, r, user, header, "mostviewed", "most-viewed")
|
2019-02-23 06:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Implement search
|
2019-04-19 06:36:26 +00:00
|
|
|
func TopicListCommon(w http.ResponseWriter, r *http.Request, user c.User, header *c.Header, torder string, tsorder string) c.RouteError {
|
2019-02-24 01:29:06 +00:00
|
|
|
header.Title = phrases.GetTitlePhrase("topics")
|
2018-09-26 07:46:30 +00:00
|
|
|
header.Zone = "topics"
|
2018-10-14 05:08:44 +00:00
|
|
|
header.Path = "/topics/"
|
2018-09-26 07:46:30 +00:00
|
|
|
header.MetaDesc = header.Settings["meta_desc"].(string)
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
group, err := c.Groups.Get(user.Group)
|
2018-09-26 07:46:30 +00:00
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
log.Printf("Group #%d doesn't exist despite being used by c.User #%d", user.Group, user.ID)
|
|
|
|
return c.LocalError("Something weird happened", w, r, user)
|
2018-09-26 07:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current page
|
|
|
|
page, _ := strconv.Atoi(r.FormValue("page"))
|
2019-02-10 05:52:26 +00:00
|
|
|
sfids := r.FormValue("fids")
|
|
|
|
var fids []int
|
|
|
|
if sfids != "" {
|
|
|
|
for _, sfid := range strings.Split(sfids, ",") {
|
|
|
|
fid, err := strconv.Atoi(sfid)
|
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.LocalError("Invalid fid", w, r, user)
|
2019-02-10 05:52:26 +00:00
|
|
|
}
|
|
|
|
fids = append(fids, fid)
|
|
|
|
}
|
|
|
|
if len(fids) == 1 {
|
2019-04-19 06:36:26 +00:00
|
|
|
forum, err := c.Forums.Get(fids[0])
|
2019-02-10 05:52:26 +00:00
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.LocalError("Invalid fid forum", w, r, user)
|
2019-02-10 05:52:26 +00:00
|
|
|
}
|
|
|
|
header.Title = forum.Name
|
|
|
|
header.ZoneID = forum.ID
|
|
|
|
}
|
|
|
|
}
|
2018-09-26 07:46:30 +00:00
|
|
|
|
2019-02-23 06:29:19 +00:00
|
|
|
// TODO: Allow multiple forums in searches
|
|
|
|
// TODO: Simplify this block after initially landing search
|
2019-04-19 06:36:26 +00:00
|
|
|
var topicList []*c.TopicsRow
|
|
|
|
var forumList []c.Forum
|
|
|
|
var paginator c.Paginator
|
2019-02-23 06:29:19 +00:00
|
|
|
q := r.FormValue("q")
|
2019-04-19 06:36:26 +00:00
|
|
|
if q != "" && c.RepliesSearch != nil {
|
2019-02-23 06:29:19 +00:00
|
|
|
var canSee []int
|
|
|
|
if user.IsSuperAdmin {
|
2019-04-19 06:36:26 +00:00
|
|
|
canSee, err = c.Forums.GetAllVisibleIDs()
|
2019-02-23 06:29:19 +00:00
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2019-02-23 06:29:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
canSee = group.CanSee
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfids []int
|
|
|
|
if len(fids) > 0 {
|
|
|
|
var inSlice = func(haystack []int, needle int) bool {
|
|
|
|
for _, item := range haystack {
|
|
|
|
if needle == item {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, fid := range fids {
|
|
|
|
if inSlice(canSee, fid) {
|
2019-04-19 06:36:26 +00:00
|
|
|
forum := c.Forums.DirtyGet(fid)
|
2019-02-23 06:29:19 +00:00
|
|
|
if forum.Name != "" && forum.Active && (forum.ParentType == "" || forum.ParentType == "forum") {
|
|
|
|
// TODO: Add a hook here for plugin_guilds?
|
|
|
|
cfids = append(cfids, fid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cfids = canSee
|
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
tids, err := c.RepliesSearch.Query(q, cfids)
|
2019-02-23 06:29:19 +00:00
|
|
|
if err != nil && err != sql.ErrNoRows {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2019-02-23 06:29:19 +00:00
|
|
|
}
|
|
|
|
//fmt.Printf("tids %+v\n", tids)
|
|
|
|
// TODO: Handle the case where there aren't any items...
|
|
|
|
// TODO: Add a BulkGet method which returns a slice?
|
2019-04-19 06:36:26 +00:00
|
|
|
tMap, err := c.Topics.BulkGetMap(tids)
|
2019-02-23 06:29:19 +00:00
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2019-02-23 06:29:19 +00:00
|
|
|
}
|
|
|
|
var reqUserList = make(map[int]bool)
|
|
|
|
for _, topic := range tMap {
|
|
|
|
reqUserList[topic.CreatedBy] = true
|
|
|
|
reqUserList[topic.LastReplyBy] = true
|
|
|
|
topicList = append(topicList, topic.TopicsRow())
|
|
|
|
}
|
|
|
|
//fmt.Printf("reqUserList %+v\n", reqUserList)
|
|
|
|
|
|
|
|
// 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?
|
|
|
|
//fmt.Printf("idSlice %+v\n", idSlice)
|
2019-04-19 06:36:26 +00:00
|
|
|
userList, err := c.Users.BulkGetMap(idSlice)
|
2019-02-23 06:29:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil // TODO: Implement this!
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: De-dupe this logic in common/topic_list.go?
|
|
|
|
for _, topic := range topicList {
|
2019-04-19 06:36:26 +00:00
|
|
|
topic.Link = c.BuildTopicURL(c.NameToSlug(topic.Title), topic.ID)
|
2019-02-23 06:29:19 +00:00
|
|
|
// TODO: Pass forum to something like topic.Forum and use that instead of these two properties? Could be more flexible.
|
2019-04-19 06:36:26 +00:00
|
|
|
forum := c.Forums.DirtyGet(topic.ParentID)
|
2019-02-23 06:29:19 +00:00
|
|
|
topic.ForumName = forum.Name
|
|
|
|
topic.ForumLink = forum.Link
|
|
|
|
|
|
|
|
// TODO: Create a specialised function with a bit less overhead for getting the last page for a post count
|
2019-04-19 06:36:26 +00:00
|
|
|
_, _, lastPage := c.PageOffset(topic.PostCount, 1, c.Config.ItemsPerPage)
|
2019-02-23 06:29:19 +00:00
|
|
|
topic.LastPage = lastPage
|
|
|
|
topic.Creator = userList[topic.CreatedBy]
|
|
|
|
topic.LastUser = userList[topic.LastReplyBy]
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Reduce the amount of boilerplate here
|
|
|
|
if r.FormValue("js") == "1" {
|
|
|
|
outBytes, err := wsTopicList(topicList, paginator.LastPage).MarshalJSON()
|
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2019-02-23 06:29:19 +00:00
|
|
|
}
|
|
|
|
w.Write(outBytes)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-23 07:47:42 +00:00
|
|
|
header.Title = phrases.GetTitlePhrase("topics_search")
|
2019-04-19 06:36:26 +00:00
|
|
|
pi := c.TopicListPage{header, topicList, forumList, c.Config.DefaultForum, c.TopicListSort{torder, false}, paginator}
|
2019-02-23 06:29:19 +00:00
|
|
|
return renderTemplate("topics", w, r, header, pi)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Pass a struct back rather than passing back so many variables
|
2018-09-26 07:46:30 +00:00
|
|
|
if user.IsSuperAdmin {
|
2019-04-19 06:36:26 +00:00
|
|
|
topicList, forumList, paginator, err = c.TopicList.GetList(page, tsorder, fids)
|
2018-09-26 07:46:30 +00:00
|
|
|
} else {
|
2019-04-19 06:36:26 +00:00
|
|
|
topicList, forumList, paginator, err = c.TopicList.GetListByGroup(group, page, tsorder, fids)
|
2018-09-26 07:46:30 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.InternalError(err, w, r)
|
2018-09-26 07:46:30 +00:00
|
|
|
}
|
|
|
|
// ! Need an inline error not a page level error
|
|
|
|
if len(topicList) == 0 {
|
2019-04-19 06:36:26 +00:00
|
|
|
return c.NotFound(w, r, header)
|
2018-09-26 07:46:30 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 05:52:26 +00:00
|
|
|
// TODO: Reduce the amount of boilerplate here
|
|
|
|
if r.FormValue("js") == "1" {
|
|
|
|
outBytes, err := wsTopicList(topicList, paginator.LastPage).MarshalJSON()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-19 06:36:26 +00:00
|
|
|
pi := c.TopicListPage{header, topicList, forumList, c.Config.DefaultForum, c.TopicListSort{torder, false}, paginator}
|
2018-11-12 09:23:36 +00:00
|
|
|
return renderTemplate("topics", w, r, header, pi)
|
2018-09-26 07:46:30 +00:00
|
|
|
}
|