gosora/common/forum_perms_store.go

256 lines
6.6 KiB
Go
Raw Normal View History

package common
import (
2022-02-21 03:53:13 +00:00
"database/sql"
"encoding/json"
"sync"
2022-02-21 03:53:13 +00:00
qgen "git.tuxpa.in/a/gosora/query_gen"
)
Added the AboutSegment feature, you can see this in use on Cosora, it's a little raw right now, but I'm planning to polish it in the next commit. Refactored the code to use switches instead of if blocks in some places. Refactored the Dashboard to make it easier to add icons to it like I did with Cosora. You can now use maps in transpiled templates. Made progress on Cosora's footer. Swapped out the ThemeName property in the HeaderVars struct for a more general and flexible Theme property. Added the colstack CSS class to make it easier to style the layouts for the Control Panel and profile. Renamed the FStore variable to Forums. Renamed the Fpstore variable to FPStore. Renamed the Gstore variable to Groups. Split the MemoryTopicStore into DefaultTopicStore and MemoryTopicCache. Split the MemoryUserStore into DefaultUserStore and MemoryUserCache. Removed the NullUserStore, SQLUserStore, and SQLTopicStore. Added the NullTopicCache and NullUserCache. Moved the Reload method out of the TopicCache interface and into the TopicStore one. Moved the Reload method out of the UserCache interface and into the UserStore one. Added the SetCache and GetCache methods to the TopicStore and UserStore. Added the BypassGetAll method to the WordFilterMap type. Renamed routePanelSetting to routePanelSettingEdit. Renamed routePanelSettingEdit to routePanelSettingEditSubmit. Moved the page titles into the english language pack. Split main() into main and afterDBInit to avoid code duplication in general_test.go Added the ReqIsJson method so that we don't have to sniff the headers every time. Added the LogStore interface. Added the SQLModLogStore and the SQLAdminLogStore. Refactored the phrase system to use getPhrasePlaceholder instead of hard-coding the string to return in a bunch of functions. Removed a redundant rank check. Added the GuildStore to plugin_guilds. Added the about_segment_title and about_segment_body settings. Refactored the setting system to use predefined errors to make it easier for an upstream caller to filter out sensitive error messages as opposed to safe errors. Added the BypassGetAll method to the SettingMap type. Added the Update method to the SettingMap type. BulkGet is now exposed via the MemoryUserCache. Refactored more logs in the template transpiler to reduce the amount of indentation. Refactored the tests to take up fewer lines. Further improved the Cosora theme's colours, padding, and profiles. Added styling for the Control Panel Dashboard to the Cosora Theme. Reduced the amount of code duplication in the installer query generator and opened the door to certain types of auto-migrations. Refactored the Control Panel Dashboard to reduce the amount of code duplication. Refactored the modlog route to reduce the amount of code duplication and string concatenation.
2017-11-23 05:37:08 +00:00
var FPStore ForumPermsStore
type ForumPermsStore interface {
2022-02-21 03:32:53 +00:00
Init() error
GetAllMap() (bigMap map[int]map[int]*ForumPerms)
Get(fid, gid int) (fp *ForumPerms, err error)
GetCopy(fid, gid int) (fp ForumPerms, err error)
ReloadAll() error
Reload(id int) error
}
type ForumPermsCache interface {
}
type MemoryForumPermsStore struct {
2022-02-21 03:32:53 +00:00
getByForum *sql.Stmt
getByForumGroup *sql.Stmt
2022-02-21 03:32:53 +00:00
evenForums map[int]map[int]*ForumPerms
oddForums map[int]map[int]*ForumPerms // [fid][gid]*ForumPerms
evenLock sync.RWMutex
oddLock sync.RWMutex
}
func NewMemoryForumPermsStore() (*MemoryForumPermsStore, error) {
2022-02-21 03:32:53 +00:00
acc := qgen.NewAcc()
fp := "forums_permissions"
return &MemoryForumPermsStore{
getByForum: acc.Select(fp).Columns("gid,permissions").Where("fid=?").Orderby("gid ASC").Prepare(),
getByForumGroup: acc.Select(fp).Columns("permissions").Where("fid=? AND gid=?").Prepare(),
evenForums: make(map[int]map[int]*ForumPerms),
oddForums: make(map[int]map[int]*ForumPerms),
}, acc.FirstError()
}
func (s *MemoryForumPermsStore) Init() error {
2022-02-21 03:32:53 +00:00
DebugLog("Initialising the forum perms store")
return s.ReloadAll()
}
// TODO: Optimise this?
func (s *MemoryForumPermsStore) ReloadAll() error {
2022-02-21 03:32:53 +00:00
DebugLog("Reloading the forum perms")
fids, e := Forums.GetAllIDs()
if e != nil {
return e
}
for _, fid := range fids {
if e := s.reload(fid); e != nil {
return e
}
}
if e := s.recalcCanSeeAll(); e != nil {
return e
}
TopicListThaw.Thaw()
return nil
}
WIP forum action code. Currently disabled. Add Http Conn Count tracking. Move more panel phrases into the panel namespace. Use a string builder in hookgen. Use Countf() in a couple of places to eliminate boilerplate. Reduce prepared stmt boilerplate in forum store with a lambda. Reduce prepared stmt boilerplate in topic.go with a lambda. Reduce prepared stmt boilerplate in group.go with a lambda. Add TestSetCreatedAt method to *Topic. Add DateOlderThanQ method to *accDeleteBuilder and *accUpdateBuilder. Add Stmt method to *accUpdateBuilder and *AccSelectBuilder. Add AccBuilder interface. Shorten variable names. Shorten extractPerm name to ep. Add avatar_visibility setting stub. Implementation coming in a later commit. Don't set an IP for installer generated posts. Add counters_perf_tick_row hook. Add avatar_visibility phrase. Add avatar_visibility_label phrase. Rename forums_no_description to forums_no_desc. Rename panel.forums_create_description_label to panel.forums_create_desc_label. Rename panel.forums_create_description to panel.forums_create_desc. Rename panel_forum_description to panel.forum_desc. Rename panel_forum_description_placeholder to panel.forum_desc_placeholder. Add panel_debug_http_conns_label phrase. Add panel.forum_actions_head phrase. Add panel.forum_actions_create_head phrase. Add panel.forum_action_run_on_topic_creation phrase. Add panel.forum_action_run_days_after_topic_creation phrase. Add panel.forum_action_run_days_after_topic_last_reply phrase. Add panel.forum_action_action phrase. Add panel.forum_action_action_delete phrase. Add panel.forum_action_action_lock phrase. Add panel.forum_action_action_unlock phrase. Add panel.forum_action_action_move phrase. Add panel.forum_action_extra phrase. Add panel.forum_action_create_button phrase. You will need to run the patcher / updater for this commit.
2021-04-07 14:23:11 +00:00
func (s *MemoryForumPermsStore) parseForumPerm(perms []byte) (pperms *ForumPerms, e error) {
2022-02-21 03:32:53 +00:00
DebugDetail("perms: ", string(perms))
pperms = BlankForumPerms()
e = json.Unmarshal(perms, &pperms)
pperms.ExtData = make(map[string]bool)
pperms.Overrides = true
return pperms, e
}
func (s *MemoryForumPermsStore) Reload(fid int) error {
2022-02-21 03:32:53 +00:00
e := s.reload(fid)
if e != nil {
return e
}
if e = s.recalcCanSeeAll(); e != nil {
return e
}
TopicListThaw.Thaw()
return nil
}
// TODO: Need a more thread-safe way of doing this. Possibly with sync.Map?
func (s *MemoryForumPermsStore) reload(fid int) error {
2022-02-21 03:32:53 +00:00
DebugLogf("Reloading the forum permissions for forum #%d", fid)
rows, err := s.getByForum.Query(fid)
if err != nil {
return err
}
defer rows.Close()
forumPerms := make(map[int]*ForumPerms)
for rows.Next() {
var gid int
var perms []byte
err := rows.Scan(&gid, &perms)
if err != nil {
return err
}
DebugLog("gid:", gid)
DebugLogf("perms: %+v\n", perms)
pperms, err := s.parseForumPerm(perms)
if err != nil {
return err
}
DebugLogf("pperms: %+v\n", pperms)
forumPerms[gid] = pperms
}
DebugLogf("forumPerms: %+v\n", forumPerms)
if fid%2 == 0 {
s.evenLock.Lock()
s.evenForums[fid] = forumPerms
s.evenLock.Unlock()
} else {
s.oddLock.Lock()
s.oddForums[fid] = forumPerms
s.oddLock.Unlock()
}
return nil
}
func (s *MemoryForumPermsStore) recalcCanSeeAll() error {
2022-02-21 03:32:53 +00:00
groups, err := Groups.GetAll()
if err != nil {
return err
}
fids, err := Forums.GetAllIDs()
if err != nil {
return err
}
gc, ok := Groups.(GroupCache)
if !ok {
TopicListThaw.Thaw()
return nil
}
// A separate loop to avoid contending on the odd-even locks as much
fForumPerms := make(map[int]map[int]*ForumPerms)
for _, fid := range fids {
var forumPerms map[int]*ForumPerms
var ok bool
if fid%2 == 0 {
s.evenLock.RLock()
forumPerms, ok = s.evenForums[fid]
s.evenLock.RUnlock()
} else {
s.oddLock.RLock()
forumPerms, ok = s.oddForums[fid]
s.oddLock.RUnlock()
}
if ok {
fForumPerms[fid] = forumPerms
}
}
// TODO: Can we recalculate CanSee without calculating every other forum?
for _, g := range groups {
DebugLogf("Updating the forum permissions for Group #%d", g.ID)
canSee := []int{}
for _, fid := range fids {
DebugDetailf("Forum #%+v\n", fid)
forumPerms, ok := fForumPerms[fid]
if !ok {
continue
}
fp, ok := forumPerms[g.ID]
if !ok {
if g.Perms.ViewTopic {
canSee = append(canSee, fid)
}
continue
}
if fp.Overrides {
if fp.ViewTopic {
canSee = append(canSee, fid)
}
} else if g.Perms.ViewTopic {
canSee = append(canSee, fid)
}
//DebugDetail("g.ID: ", g.ID)
DebugDetailf("forumPerm: %+v\n", fp)
DebugDetail("canSee: ", canSee)
}
DebugDetailf("canSee (length %d): %+v \n", len(canSee), canSee)
gc.SetCanSee(g.ID, canSee)
}
return nil
}
// ! Throughput on this might be bad due to the excessive locking
func (s *MemoryForumPermsStore) GetAllMap() (bigMap map[int]map[int]*ForumPerms) {
2022-02-21 03:32:53 +00:00
bigMap = make(map[int]map[int]*ForumPerms)
s.evenLock.RLock()
for fid, subMap := range s.evenForums {
bigMap[fid] = subMap
}
s.evenLock.RUnlock()
s.oddLock.RLock()
for fid, subMap := range s.oddForums {
bigMap[fid] = subMap
}
s.oddLock.RUnlock()
return bigMap
}
// TODO: Add a hook here and have plugin_guilds use it
// TODO: Check if the forum exists?
// TODO: Fix the races
// TODO: Return BlankForumPerms() when the forum permission set doesn't exist?
func (s *MemoryForumPermsStore) Get(fid, gid int) (fp *ForumPerms, err error) {
2022-02-21 03:32:53 +00:00
var fmap map[int]*ForumPerms
var ok bool
if fid%2 == 0 {
s.evenLock.RLock()
fmap, ok = s.evenForums[fid]
s.evenLock.RUnlock()
} else {
s.oddLock.RLock()
fmap, ok = s.oddForums[fid]
s.oddLock.RUnlock()
}
if !ok {
return fp, ErrNoRows
}
fp, ok = fmap[gid]
if !ok {
return fp, ErrNoRows
}
return fp, nil
}
// TODO: Check if the forum exists?
// TODO: Fix the races
WIP forum action code. Currently disabled. Add Http Conn Count tracking. Move more panel phrases into the panel namespace. Use a string builder in hookgen. Use Countf() in a couple of places to eliminate boilerplate. Reduce prepared stmt boilerplate in forum store with a lambda. Reduce prepared stmt boilerplate in topic.go with a lambda. Reduce prepared stmt boilerplate in group.go with a lambda. Add TestSetCreatedAt method to *Topic. Add DateOlderThanQ method to *accDeleteBuilder and *accUpdateBuilder. Add Stmt method to *accUpdateBuilder and *AccSelectBuilder. Add AccBuilder interface. Shorten variable names. Shorten extractPerm name to ep. Add avatar_visibility setting stub. Implementation coming in a later commit. Don't set an IP for installer generated posts. Add counters_perf_tick_row hook. Add avatar_visibility phrase. Add avatar_visibility_label phrase. Rename forums_no_description to forums_no_desc. Rename panel.forums_create_description_label to panel.forums_create_desc_label. Rename panel.forums_create_description to panel.forums_create_desc. Rename panel_forum_description to panel.forum_desc. Rename panel_forum_description_placeholder to panel.forum_desc_placeholder. Add panel_debug_http_conns_label phrase. Add panel.forum_actions_head phrase. Add panel.forum_actions_create_head phrase. Add panel.forum_action_run_on_topic_creation phrase. Add panel.forum_action_run_days_after_topic_creation phrase. Add panel.forum_action_run_days_after_topic_last_reply phrase. Add panel.forum_action_action phrase. Add panel.forum_action_action_delete phrase. Add panel.forum_action_action_lock phrase. Add panel.forum_action_action_unlock phrase. Add panel.forum_action_action_move phrase. Add panel.forum_action_extra phrase. Add panel.forum_action_create_button phrase. You will need to run the patcher / updater for this commit.
2021-04-07 14:23:11 +00:00
func (s *MemoryForumPermsStore) GetCopy(fid, gid int) (fp ForumPerms, e error) {
2022-02-21 03:32:53 +00:00
fPermsPtr, e := s.Get(fid, gid)
if e != nil {
return fp, e
}
return *fPermsPtr, nil
}