2017-12-26 07:17:26 +00:00
package common
2018-02-10 15:07:21 +00:00
import (
"strconv"
"sync"
2018-06-24 13:49:29 +00:00
"sync/atomic"
2018-02-10 15:07:21 +00:00
2018-10-27 03:21:02 +00:00
"github.com/Azareal/Gosora/query_gen"
2018-02-10 15:07:21 +00:00
)
var TopicList TopicListInt
type TopicListHolder struct {
List [ ] * TopicsRow
ForumList [ ] Forum
2018-04-22 12:33:56 +00:00
Paginator Paginator
2018-02-10 15:07:21 +00:00
}
type TopicListInt interface {
2018-09-26 07:46:30 +00:00
GetListByCanSee ( canSee [ ] int , page int , orderby string ) ( topicList [ ] * TopicsRow , forumList [ ] Forum , paginator Paginator , err error )
GetListByGroup ( group * Group , page int , orderby string ) ( topicList [ ] * TopicsRow , forumList [ ] Forum , paginator Paginator , err error )
GetList ( page int , orderby string ) ( topicList [ ] * TopicsRow , forumList [ ] Forum , paginator Paginator , err error )
2018-06-24 13:49:29 +00:00
RebuildPermTree ( ) error
2018-02-10 15:07:21 +00:00
}
type DefaultTopicList struct {
2018-06-24 13:49:29 +00:00
// TODO: Rewrite this to put permTree as the primary and put canSeeStr on each group?
2018-02-10 15:07:21 +00:00
oddGroups map [ int ] * TopicListHolder
evenGroups map [ int ] * TopicListHolder
oddLock sync . RWMutex
evenLock sync . RWMutex
2018-06-24 13:49:29 +00:00
permTree atomic . Value // [string(canSee)]canSee
//permTree map[string][]int // [string(canSee)]canSee
2018-02-10 15:07:21 +00:00
}
2018-06-24 13:49:29 +00:00
// We've removed the topic list cache cap as admins really shouldn't be abusing groups like this with plugin_guilds around and it was extremely fiddly.
// If this becomes a problem later on, then we can revisit this with a fresh perspective, particularly with regards to what people expect a group to really be
// Also, keep in mind that as-long as the groups don't all have unique sets of forums they can see, then we can optimise a large portion of the work away.
2018-02-10 15:07:21 +00:00
func NewDefaultTopicList ( ) ( * DefaultTopicList , error ) {
tList := & DefaultTopicList {
oddGroups : make ( map [ int ] * TopicListHolder ) ,
evenGroups : make ( map [ int ] * TopicListHolder ) ,
}
2018-06-24 13:49:29 +00:00
err := tList . RebuildPermTree ( )
2018-02-10 15:07:21 +00:00
if err != nil {
return nil , err
}
err = tList . Tick ( )
if err != nil {
return nil , err
}
2018-06-24 13:49:29 +00:00
2018-02-10 15:07:21 +00:00
AddScheduledHalfSecondTask ( tList . Tick )
//AddScheduledSecondTask(tList.GroupCountTick) // TODO: Dynamically change the groups in the short list to be optimised every second
return tList , nil
}
func ( tList * DefaultTopicList ) Tick ( ) error {
var oddLists = make ( map [ int ] * TopicListHolder )
var evenLists = make ( map [ int ] * TopicListHolder )
2018-06-24 13:49:29 +00:00
var addList = func ( gid int , holder * TopicListHolder ) {
2018-02-10 15:07:21 +00:00
if gid % 2 == 0 {
2018-06-24 13:49:29 +00:00
evenLists [ gid ] = holder
2018-02-10 15:07:21 +00:00
} else {
2018-06-24 13:49:29 +00:00
oddLists [ gid ] = holder
2018-02-10 15:07:21 +00:00
}
}
2018-06-24 13:49:29 +00:00
var canSeeHolders = make ( map [ string ] * TopicListHolder )
for name , canSee := range tList . permTree . Load ( ) . ( map [ string ] [ ] int ) {
2018-09-26 07:46:30 +00:00
topicList , forumList , paginator , err := tList . GetListByCanSee ( canSee , 1 , "" )
2018-06-24 13:49:29 +00:00
if err != nil {
return err
}
canSeeHolders [ name ] = & TopicListHolder { topicList , forumList , paginator }
2018-02-10 15:07:21 +00:00
}
2018-06-24 13:49:29 +00:00
allGroups , err := Groups . GetAll ( )
2018-02-10 15:07:21 +00:00
if err != nil {
return err
}
2018-06-24 13:49:29 +00:00
for _ , group := range allGroups {
// ? - Move the user count check to instance initialisation? Might require more book-keeping, particularly when a user moves into a zero user group
if group . UserCount == 0 && group . ID != GuestUser . Group {
2018-02-15 13:15:27 +00:00
continue
}
2018-06-24 13:49:29 +00:00
var canSee = make ( [ ] byte , len ( group . CanSee ) )
for i , item := range group . CanSee {
canSee [ i ] = byte ( item )
2018-02-10 15:07:21 +00:00
}
2018-06-24 13:49:29 +00:00
addList ( group . ID , canSeeHolders [ string ( canSee ) ] )
2018-02-10 15:07:21 +00:00
}
tList . oddLock . Lock ( )
tList . oddGroups = oddLists
tList . oddLock . Unlock ( )
tList . evenLock . Lock ( )
tList . evenGroups = evenLists
tList . evenLock . Unlock ( )
return nil
}
2018-06-24 13:49:29 +00:00
func ( tList * DefaultTopicList ) RebuildPermTree ( ) error {
// TODO: Do something more efficient than this
allGroups , err := Groups . GetAll ( )
if err != nil {
return err
}
var permTree = make ( map [ string ] [ ] int ) // [string(canSee)]canSee
for _ , group := range allGroups {
var canSee = make ( [ ] byte , len ( group . CanSee ) )
for i , item := range group . CanSee {
canSee [ i ] = byte ( item )
}
var canSeeInt = make ( [ ] int , len ( canSee ) )
copy ( canSeeInt , group . CanSee )
permTree [ string ( canSee ) ] = canSeeInt
}
tList . permTree . Store ( permTree )
return nil
}
2018-09-26 07:46:30 +00:00
func ( tList * DefaultTopicList ) GetListByGroup ( group * Group , page int , orderby string ) ( topicList [ ] * TopicsRow , forumList [ ] Forum , paginator Paginator , err error ) {
2018-08-27 09:06:00 +00:00
if page == 0 {
page = 1
}
2018-02-10 15:07:21 +00:00
// TODO: Cache the first three pages not just the first along with all the topics on this beaten track
2018-09-26 07:46:30 +00:00
if page == 1 && orderby == "" {
2018-02-10 15:07:21 +00:00
var holder * TopicListHolder
var ok bool
if group . ID % 2 == 0 {
tList . evenLock . RLock ( )
holder , ok = tList . evenGroups [ group . ID ]
tList . evenLock . RUnlock ( )
} else {
tList . oddLock . RLock ( )
holder , ok = tList . oddGroups [ group . ID ]
tList . oddLock . RUnlock ( )
}
if ok {
2018-04-22 12:33:56 +00:00
return holder . List , holder . ForumList , holder . Paginator , nil
2018-02-10 15:07:21 +00:00
}
}
// TODO: Make CanSee a method on *Group with a canSee field? Have a CanSee method on *User to cover the case of superadmins?
2018-08-27 09:06:00 +00:00
//log.Printf("deoptimising for %d on page %d\n", group.ID, page)
2018-09-26 07:46:30 +00:00
return tList . GetListByCanSee ( group . CanSee , page , orderby )
2018-06-24 13:49:29 +00:00
}
2018-02-10 15:07:21 +00:00
2018-09-26 07:46:30 +00:00
func ( tList * DefaultTopicList ) GetListByCanSee ( canSee [ ] int , page int , orderby string ) ( topicList [ ] * TopicsRow , forumList [ ] Forum , paginator Paginator , err error ) {
2018-02-10 15:07:21 +00:00
// We need a list of the visible forums for Quick Topic
// ? - Would it be useful, if we could post in social groups from /topics/?
for _ , fid := range canSee {
forum := Forums . DirtyGet ( fid )
if forum . Name != "" && forum . Active && ( forum . ParentType == "" || forum . ParentType == "forum" ) {
fcopy := forum . Copy ( )
// TODO: Add a hook here for plugin_guilds
forumList = append ( forumList , fcopy )
}
}
// ? - Should we be showing plugin_guilds posts on /topics/?
argList , qlist := ForumListToArgQ ( forumList )
if qlist == "" {
// We don't want to kill the page, so pass an empty slice and nil error
2018-04-22 12:33:56 +00:00
return topicList , forumList , Paginator { [ ] int { } , 1 , 1 } , nil
2018-02-10 15:07:21 +00:00
}
2018-09-26 07:46:30 +00:00
topicList , paginator , err = tList . getList ( page , orderby , argList , qlist )
2018-04-22 12:33:56 +00:00
return topicList , forumList , paginator , err
2018-02-10 15:07:21 +00:00
}
// TODO: Reduce the number of returns
2018-09-26 07:46:30 +00:00
func ( tList * DefaultTopicList ) GetList ( page int , orderby string ) ( topicList [ ] * TopicsRow , forumList [ ] Forum , paginator Paginator , err error ) {
2018-02-10 15:07:21 +00:00
// TODO: Make CanSee a method on *Group with a canSee field? Have a CanSee method on *User to cover the case of superadmins?
canSee , err := Forums . GetAllVisibleIDs ( )
if err != nil {
2018-04-22 12:33:56 +00:00
return nil , nil , Paginator { nil , 1 , 1 } , err
2018-02-10 15:07:21 +00:00
}
// We need a list of the visible forums for Quick Topic
// ? - Would it be useful, if we could post in social groups from /topics/?
for _ , fid := range canSee {
forum := Forums . DirtyGet ( fid )
if forum . Name != "" && forum . Active && ( forum . ParentType == "" || forum . ParentType == "forum" ) {
fcopy := forum . Copy ( )
// TODO: Add a hook here for plugin_guilds
forumList = append ( forumList , fcopy )
}
}
// ? - Should we be showing plugin_guilds posts on /topics/?
argList , qlist := ForumListToArgQ ( forumList )
if qlist == "" {
// If the super admin can't see anything, then things have gone terribly wrong
2018-04-22 12:33:56 +00:00
return topicList , forumList , Paginator { [ ] int { } , 1 , 1 } , err
2018-02-10 15:07:21 +00:00
}
2018-09-26 07:46:30 +00:00
topicList , paginator , err = tList . getList ( page , orderby , argList , qlist )
2018-04-22 12:33:56 +00:00
return topicList , forumList , paginator , err
2018-02-10 15:07:21 +00:00
}
// TODO: Rename this to TopicListStore and pass back a TopicList instance holding the pagination data and topic list rather than passing them back one argument at a time
2018-09-26 07:46:30 +00:00
func ( tList * DefaultTopicList ) getList ( page int , orderby string , argList [ ] interface { } , qlist string ) ( topicList [ ] * TopicsRow , paginator Paginator , err error ) {
2018-02-10 15:07:21 +00:00
topicCount , err := ArgQToTopicCount ( argList , qlist )
if err != nil {
2018-04-22 12:33:56 +00:00
return nil , Paginator { nil , 1 , 1 } , err
2018-02-10 15:07:21 +00:00
}
offset , page , lastPage := PageOffset ( topicCount , page , Config . ItemsPerPage )
2018-09-26 07:46:30 +00:00
var orderq string
if orderby == "most-viewed" {
orderq = "views DESC, lastReplyAt DESC, createdBy DESC"
} else {
orderq = "sticky DESC, lastReplyAt DESC, createdBy DESC"
}
2018-09-13 07:41:01 +00:00
// TODO: Prepare common qlist lengths to speed this up in common cases, prepared statements are prepared lazily anyway, so it probably doesn't matter if we do ten or so
2018-09-26 07:46:30 +00:00
stmt , err := qgen . Builder . SimpleSelect ( "topics" , "tid, title, content, createdBy, is_closed, sticky, createdAt, lastReplyAt, lastReplyBy, parentID, views, postCount, likeCount" , "parentID IN(" + qlist + ")" , orderq , "?,?" )
2018-02-10 15:07:21 +00:00
if err != nil {
2018-04-22 12:33:56 +00:00
return nil , Paginator { nil , 1 , 1 } , err
2018-02-10 15:07:21 +00:00
}
defer stmt . Close ( )
argList = append ( argList , offset )
argList = append ( argList , Config . ItemsPerPage )
rows , err := stmt . Query ( argList ... )
if err != nil {
2018-04-22 12:33:56 +00:00
return nil , Paginator { nil , 1 , 1 } , err
2018-02-10 15:07:21 +00:00
}
defer rows . Close ( )
var reqUserList = make ( map [ int ] bool )
for rows . Next ( ) {
2018-09-13 07:41:01 +00:00
// TODO: Embed Topic structs in TopicsRow to make it easier for us to reuse this work in the topic cache
2018-02-10 15:07:21 +00:00
topicItem := TopicsRow { ID : 0 }
2018-08-30 10:31:21 +00:00
err := rows . Scan ( & topicItem . ID , & topicItem . Title , & topicItem . Content , & topicItem . CreatedBy , & topicItem . IsClosed , & topicItem . Sticky , & topicItem . CreatedAt , & topicItem . LastReplyAt , & topicItem . LastReplyBy , & topicItem . ParentID , & topicItem . ViewCount , & topicItem . PostCount , & topicItem . LikeCount )
2018-02-10 15:07:21 +00:00
if err != nil {
2018-04-22 12:33:56 +00:00
return nil , Paginator { nil , 1 , 1 } , err
2018-02-10 15:07:21 +00:00
}
topicItem . Link = BuildTopicURL ( NameToSlug ( topicItem . Title ) , topicItem . ID )
2018-06-24 13:49:29 +00:00
// TODO: Pass forum to something like topicItem.Forum and use that instead of these two properties? Could be more flexible.
2018-02-10 15:07:21 +00:00
forum := Forums . DirtyGet ( topicItem . ParentID )
topicItem . ForumName = forum . Name
topicItem . ForumLink = forum . Link
2018-06-24 13:49:29 +00:00
//topicItem.RelativeCreatedAt = RelativeTime(topicItem.CreatedAt)
2018-02-10 15:07:21 +00:00
topicItem . RelativeLastReplyAt = RelativeTime ( topicItem . LastReplyAt )
// TODO: Rename this Vhook to better reflect moving the topic list from /routes/ to /common/
2018-10-21 13:54:32 +00:00
GetHookTable ( ) . Vhook ( "topics_topic_row_assign" , & topicItem , & forum )
2018-02-10 15:07:21 +00:00
topicList = append ( topicList , & topicItem )
reqUserList [ topicItem . CreatedBy ] = true
reqUserList [ topicItem . LastReplyBy ] = true
}
err = rows . Err ( )
if err != nil {
2018-04-22 12:33:56 +00:00
return nil , Paginator { nil , 1 , 1 } , err
2018-02-10 15:07:21 +00:00
}
// 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?
userList , err := Users . BulkGetMap ( idSlice )
if err != nil {
2018-04-22 12:33:56 +00:00
return nil , Paginator { nil , 1 , 1 } , err
2018-02-10 15:07:21 +00:00
}
// Second pass to the add the user data
// TODO: Use a pointer to TopicsRow instead of TopicsRow itself?
for _ , topicItem := range topicList {
topicItem . Creator = userList [ topicItem . CreatedBy ]
topicItem . LastUser = userList [ topicItem . LastReplyBy ]
}
2018-04-22 12:33:56 +00:00
pageList := Paginate ( topicCount , Config . ItemsPerPage , 5 )
return topicList , Paginator { pageList , page , lastPage } , nil
2018-02-10 15:07:21 +00:00
}
2017-12-26 07:17:26 +00:00
// Internal. Don't rely on it.
func ForumListToArgQ ( forums [ ] Forum ) ( argList [ ] interface { } , qlist string ) {
for _ , forum := range forums {
argList = append ( argList , strconv . Itoa ( forum . ID ) )
qlist += "?,"
}
if qlist != "" {
qlist = qlist [ 0 : len ( qlist ) - 1 ]
}
return argList , qlist
}
// Internal. Don't rely on it.
func ArgQToTopicCount ( argList [ ] interface { } , qlist string ) ( topicCount int , err error ) {
topicCountStmt , err := qgen . Builder . SimpleCount ( "topics" , "parentID IN(" + qlist + ")" , "" )
if err != nil {
return 0 , err
}
defer topicCountStmt . Close ( )
err = topicCountStmt . QueryRow ( argList ... ) . Scan ( & topicCount )
if err != nil && err != ErrNoRows {
return 0 , err
}
return topicCount , err
}
func TopicCountInForums ( forums [ ] Forum ) ( topicCount int , err error ) {
return ArgQToTopicCount ( ForumListToArgQ ( forums ) )
}