Split database.go's contents off into their own functions for reusability's sake.

This commit is contained in:
Azareal 2017-06-14 10:55:47 +01:00
parent fe2eabedd0
commit 8c1d8d5c64
11 changed files with 244 additions and 183 deletions

View File

@ -37,7 +37,8 @@ var default_group = 3 // Should be a setting in the database
var activation_group = 5 // Should be a setting in the database var activation_group = 5 // Should be a setting in the database
var staff_css = " background-color: #ffeaff;" var staff_css = " background-color: #ffeaff;"
var uncategorised_forum_visible = true var uncategorised_forum_visible = true
var minify_templates = true var minify_templates = false
var multi_server = false // Experimental: Enable Cross-Server Synchronisation and several other features
//var noavatar = "https://api.adorable.io/avatars/{width}/{id}@{site_url}.png" //var noavatar = "https://api.adorable.io/avatars/{width}/{id}@{site_url}.png"
var noavatar = "https://api.adorable.io/avatars/285/{id}@" + site_url + ".png" var noavatar = "https://api.adorable.io/avatars/285/{id}@" + site_url + ".png"

View File

@ -2,7 +2,6 @@ package main
import "log" import "log"
import "fmt" import "fmt"
import "strconv"
import "encoding/json" import "encoding/json"
func init_database() (err error) { func init_database() (err error) {
@ -57,196 +56,31 @@ func init_database() (err error) {
GuestPerms = groups[6].Perms GuestPerms = groups[6].Perms
log.Print("Loading the forums.") log.Print("Loading the forums.")
log.Print("Adding the uncategorised forum") err = LoadForums()
forums = append(forums, Forum{0,"Uncategorised","",uncategorised_forum_visible,"all",0,"",0,"",0,""})
//rows, err = db.Query("SELECT fid, name, active, lastTopic, lastTopicID, lastReplyer, lastReplyerID, lastTopicTime FROM forums")
//rows, err = db.Query("select `fid`, `name`, `desc`, `active`, `preset`, `topicCount`, `lastTopic`, `lastTopicID`, `lastReplyer`, `lastReplyerID`, `lastTopicTime` from forums order by fid asc")
rows, err = get_forums_stmt.Query()
if err != nil { if err != nil {
return err return err
} }
defer rows.Close()
i = 1 log.Print("Loading the forum permissions.")
for ;rows.Next();i++ { err = build_forum_permissions()
forum := Forum{ID:0,Name:"",Active:true,Preset:"all"}
err := rows.Scan(&forum.ID, &forum.Name, &forum.Desc, &forum.Active, &forum.Preset, &forum.TopicCount, &forum.LastTopic, &forum.LastTopicID, &forum.LastReplyer, &forum.LastReplyerID, &forum.LastTopicTime)
if err != nil {
return err
}
// Ugh, you really shouldn't physically delete these items, it makes a big mess of things
if forum.ID != i {
log.Print("Stop physically deleting forums. You are messing up the IDs. Use the Forum Manager or delete_forum() instead x.x")
fill_forum_id_gap(i, forum.ID)
}
if forum.Name == "" {
if debug {
log.Print("Adding a placeholder forum")
}
} else {
log.Print("Adding the " + forum.Name + " forum")
}
forums = append(forums,forum)
}
err = rows.Err()
if err != nil { if err != nil {
return err return err
} }
forumCapCount = i
//log.Print("Adding the reports forum")
//forums[-1] = Forum{-1,"Reports",false,0,"",0,"",0,""}
log.Print("Loading the forum permissions")
rows, err = get_forums_permissions_stmt.Query()
if err != nil {
return err
}
defer rows.Close()
if debug {
log.Print("Adding the forum permissions")
}
// Temporarily store the forum perms in a map before transferring it to a much faster and thread-safe slice
forum_perms = make(map[int]map[int]ForumPerms)
for rows.Next() {
var gid, fid int
var perms []byte
var pperms ForumPerms
err := rows.Scan(&gid, &fid, &perms)
if err != nil {
return err
}
err = json.Unmarshal(perms, &pperms)
if err != nil {
return err
}
pperms.ExtData = make(map[string]bool)
pperms.Overrides = true
_, ok := forum_perms[gid]
if !ok {
forum_perms[gid] = make(map[int]ForumPerms)
}
forum_perms[gid][fid] = pperms
}
for gid, _ := range groups {
if debug {
log.Print("Adding the forum permissions for Group #" + strconv.Itoa(gid) + " - " + groups[gid].Name)
}
//groups[gid].Forums = append(groups[gid].Forums,BlankForumPerms) // GID 0. I sometimes wish MySQL's AUTO_INCREMENT would start at zero
for fid, _ := range forums {
forum_perm, ok := forum_perms[gid][fid]
if ok {
// Override group perms
//log.Print("Overriding permissions for forum #" + strconv.Itoa(fid))
groups[gid].Forums = append(groups[gid].Forums,forum_perm)
} else {
// Inherit from Group
//log.Print("Inheriting from default for forum #" + strconv.Itoa(fid))
forum_perm = BlankForumPerms
groups[gid].Forums = append(groups[gid].Forums,forum_perm)
}
if forum_perm.Overrides {
if forum_perm.ViewTopic {
groups[gid].CanSee = append(groups[gid].CanSee, fid)
}
} else if groups[gid].Perms.ViewTopic {
groups[gid].CanSee = append(groups[gid].CanSee, fid)
}
}
//fmt.Printf("%+v\n", groups[gid].CanSee)
//fmt.Printf("%+v\n", groups[gid].Forums)
//fmt.Println(len(groups[gid].CanSee))
//fmt.Println(len(groups[gid].Forums))
}
log.Print("Loading the settings.") log.Print("Loading the settings.")
rows, err = get_full_settings_stmt.Query() err = LoadSettings()
if err != nil {
return err
}
defer rows.Close()
var sname, scontent, stype, sconstraints string
for rows.Next() {
err := rows.Scan(&sname, &scontent, &stype, &sconstraints)
if err != nil {
return err
}
errmsg := parseSetting(sname, scontent, stype, sconstraints)
if errmsg != "" {
return err
}
}
err = rows.Err()
if err != nil { if err != nil {
return err return err
} }
log.Print("Loading the plugins.") log.Print("Loading the plugins.")
rows, err = get_plugins_stmt.Query() err = LoadPlugins()
if err != nil {
return err
}
defer rows.Close()
var uname string
var active bool
for rows.Next() {
err := rows.Scan(&uname, &active)
if err != nil {
return err
}
// Was the plugin deleted at some point?
plugin, ok := plugins[uname]
if !ok {
continue
}
plugin.Active = active
plugins[uname] = plugin
}
err = rows.Err()
if err != nil { if err != nil {
return err return err
} }
log.Print("Loading the themes.") log.Print("Loading the themes.")
rows, err = get_themes_stmt.Query() err = LoadThemes()
if err != nil {
return err
}
defer rows.Close()
var defaultThemeSwitch bool
for rows.Next() {
err := rows.Scan(&uname, &defaultThemeSwitch)
if err != nil {
return err
}
// Was the theme deleted at some point?
theme, ok := themes[uname]
if !ok {
continue
}
if defaultThemeSwitch {
log.Print("Loading the theme '" + theme.Name + "'")
theme.Active = true
defaultTheme = uname
add_theme_static_files(uname)
map_theme_templates(theme)
} else {
theme.Active = false
}
themes[uname] = theme
}
err = rows.Err()
if err != nil { if err != nil {
return err return err
} }

View File

@ -6,6 +6,36 @@ var plugins map[string]*Plugin = make(map[string]*Plugin)
var hooks map[string][]func(interface{})interface{} = make(map[string][]func(interface{})interface{}) var hooks map[string][]func(interface{})interface{} = make(map[string][]func(interface{})interface{})
var vhooks map[string]func(...interface{})interface{} = make(map[string]func(...interface{})interface{}) var vhooks map[string]func(...interface{})interface{} = make(map[string]func(...interface{})interface{})
func LoadPlugins() error {
rows, err := get_plugins_stmt.Query()
if err != nil {
return err
}
defer rows.Close()
var uname string
var active bool
for rows.Next() {
err = rows.Scan(&uname, &active)
if err != nil {
return err
}
// Was the plugin deleted at some point?
plugin, ok := plugins[uname]
if !ok {
continue
}
plugin.Active = active
plugins[uname] = plugin
}
err = rows.Err()
if err != nil {
return err
}
return nil
}
type Plugin struct type Plugin struct
{ {
UName string UName string

View File

@ -1,5 +1,6 @@
package main package main
import "log"
//import "fmt" //import "fmt"
import "sync" import "sync"
import "strconv" import "strconv"
@ -40,6 +41,59 @@ type ForumSimple struct
Preset string Preset string
} }
/*type ForumStore interface
{
Get(int) (*Forum, error)
CascadeGet(int) (*Forum, error)
Update(Forum) error
CascadeUpdate(Forum) error
Delete(int) error
CascadeDelete(int) error
}*/
func LoadForums() error {
//if debug {
log.Print("Adding the uncategorised forum")
//}
forums = append(forums, Forum{0,"Uncategorised","",uncategorised_forum_visible,"all",0,"",0,"",0,""})
rows, err := get_forums_stmt.Query()
if err != nil {
return err
}
defer rows.Close()
var i int = 1
for ;rows.Next();i++ {
forum := Forum{ID:0,Name:"",Active:true,Preset:"all"}
err = rows.Scan(&forum.ID, &forum.Name, &forum.Desc, &forum.Active, &forum.Preset, &forum.TopicCount, &forum.LastTopic, &forum.LastTopicID, &forum.LastReplyer, &forum.LastReplyerID, &forum.LastTopicTime)
if err != nil {
return err
}
// Ugh, you really shouldn't physically delete these items, it makes a big mess of things
if forum.ID != i {
log.Print("Stop physically deleting forums. You are messing up the IDs. Use the Forum Manager or delete_forum() instead x.x")
fill_forum_id_gap(i, forum.ID)
}
if forum.Name == "" {
if debug {
log.Print("Adding a placeholder forum")
}
} else {
log.Print("Adding the " + forum.Name + " forum")
}
forums = append(forums,forum)
}
err = rows.Err()
if err != nil {
return err
}
forumCapCount = i
return nil
}
var forum_update_mutex sync.Mutex var forum_update_mutex sync.Mutex
func create_forum(forum_name string, forum_desc string, active bool, preset string) (int, error) { func create_forum(forum_name string, forum_desc string, active bool, preset string) (int, error) {
var fid int var fid int

View File

@ -1,5 +1,5 @@
// Code generated by. DO NOT EDIT. // Code generated by Gosora. More below:
/* This file was generated by Gosora's Query Generator. The thing above is to tell GH this file is generated. */ /* This file was generated by Gosora's Query Generator. Please try to avoid modifying this file, as it might change at any time. */
// +build !pgsql !sqlite !mssql // +build !pgsql !sqlite !mssql
package main package main

View File

@ -173,6 +173,7 @@ var activation_group = 5 // Should be a setting in the database
var staff_css = " background-color: #ffeaff;" var staff_css = " background-color: #ffeaff;"
var uncategorised_forum_visible = true var uncategorised_forum_visible = true
var minify_templates = true var minify_templates = true
var multi_server = false // Experimental: Enable Cross-Server Synchronisation and several other features
//var noavatar = "https://api.adorable.io/avatars/{width}/{id}@{site_url}.png" //var noavatar = "https://api.adorable.io/avatars/{width}/{id}@{site_url}.png"
var noavatar = "https://api.adorable.io/avatars/285/{id}@" + site_url + ".png" var noavatar = "https://api.adorable.io/avatars/285/{id}@" + site_url + ".png"

View File

@ -192,6 +192,11 @@ CREATE TABLE `administration_logs`(
`doneAt` datetime not null `doneAt` datetime not null
); );
CREATE TABLE `sync`(
`last_update` int not null,
`node_id` int not null
);
INSERT INTO settings(`name`,`content`,`type`) VALUES ('url_tags','1','bool'); INSERT INTO settings(`name`,`content`,`type`) VALUES ('url_tags','1','bool');
INSERT INTO settings(`name`,`content`,`type`,`constraints`) VALUES ('activation_type','1','list','1-3'); INSERT INTO settings(`name`,`content`,`type`,`constraints`) VALUES ('activation_type','1','list','1-3');
INSERT INTO settings(`name`,`content`,`type`) VALUES ('bigpost_min_words','250','int'); INSERT INTO settings(`name`,`content`,`type`) VALUES ('bigpost_min_words','250','int');

View File

@ -303,14 +303,18 @@ func permmap_to_query(permmap map[string]ForumPerms, fid int) error {
} }
func rebuild_forum_permissions(fid int) error { func rebuild_forum_permissions(fid int) error {
log.Print("Loading the forum permissions") if debug {
log.Print("Loading the forum permissions")
}
rows, err := db.Query("select gid, permissions from forums_permissions where fid = ? order by gid asc", fid) rows, err := db.Query("select gid, permissions from forums_permissions where fid = ? order by gid asc", fid)
if err != nil { if err != nil {
return err return err
} }
defer rows.Close() defer rows.Close()
log.Print("Updating the forum permissions") if debug {
log.Print("Updating the forum permissions")
}
for rows.Next() { for rows.Next() {
var gid int var gid int
var perms []byte var perms []byte
@ -332,7 +336,9 @@ func rebuild_forum_permissions(fid int) error {
forum_perms[gid][fid] = pperms forum_perms[gid][fid] = pperms
} }
for gid, _ := range groups { for gid, _ := range groups {
log.Print("Updating the forum permissions for Group #" + strconv.Itoa(gid)) if debug {
log.Print("Updating the forum permissions for Group #" + strconv.Itoa(gid))
}
var blank_list []ForumPerms var blank_list []ForumPerms
var blank_int_list []int var blank_int_list []int
groups[gid].Forums = blank_list groups[gid].Forums = blank_list
@ -357,14 +363,80 @@ func rebuild_forum_permissions(fid int) error {
groups[gid].CanSee = append(groups[gid].CanSee, ffid) groups[gid].CanSee = append(groups[gid].CanSee, ffid)
} }
} }
//fmt.Printf("%+v\n", groups[gid].CanSee) if super_debug {
//fmt.Printf("%+v\n", groups[gid].Forums) fmt.Printf("groups[gid].CanSee %+v\n", groups[gid].CanSee)
//fmt.Println(len(groups[gid].Forums)) fmt.Printf("groups[gid].Forums %+v\n", groups[gid].Forums)
fmt.Println("len(groups[gid].Forums)",len(groups[gid].Forums))
}
} }
return nil return nil
} }
func build_forum_permissions() error { func build_forum_permissions() error {
rows, err := get_forums_permissions_stmt.Query()
if err != nil {
return err
}
defer rows.Close()
if debug {
log.Print("Adding the forum permissions")
}
// Temporarily store the forum perms in a map before transferring it to a much faster and thread-safe slice
forum_perms = make(map[int]map[int]ForumPerms)
for rows.Next() {
var gid, fid int
var perms []byte
var pperms ForumPerms
err = rows.Scan(&gid, &fid, &perms)
if err != nil {
return err
}
err = json.Unmarshal(perms, &pperms)
if err != nil {
return err
}
pperms.ExtData = make(map[string]bool)
pperms.Overrides = true
_, ok := forum_perms[gid]
if !ok {
forum_perms[gid] = make(map[int]ForumPerms)
}
forum_perms[gid][fid] = pperms
}
for gid, _ := range groups {
if debug {
log.Print("Adding the forum permissions for Group #" + strconv.Itoa(gid) + " - " + groups[gid].Name)
}
//groups[gid].Forums = append(groups[gid].Forums,BlankForumPerms) // GID 0. No longer needed now that Uncategorised occupies that slot
for fid, _ := range forums {
forum_perm, ok := forum_perms[gid][fid]
if ok {
// Override group perms
//log.Print("Overriding permissions for forum #" + strconv.Itoa(fid))
groups[gid].Forums = append(groups[gid].Forums,forum_perm)
} else {
// Inherit from Group
//log.Print("Inheriting from default for forum #" + strconv.Itoa(fid))
forum_perm = BlankForumPerms
groups[gid].Forums = append(groups[gid].Forums,forum_perm)
}
if forum_perm.Overrides {
if forum_perm.ViewTopic {
groups[gid].CanSee = append(groups[gid].CanSee, fid)
}
} else if groups[gid].Perms.ViewTopic {
groups[gid].CanSee = append(groups[gid].CanSee, fid)
}
}
if super_debug {
//fmt.Printf("groups[gid].CanSee %+v\n", groups[gid].CanSee)
//fmt.Printf("groups[gid].Forums %+v\n", groups[gid].Forums)
//fmt.Println("len(groups[gid].CanSee)",len(groups[gid].CanSee))
//fmt.Println("len(groups[gid].Forums)",len(groups[gid].Forums))
}
}
return nil return nil
} }

View File

@ -491,8 +491,8 @@ func (adapter *Mysql_Adapter) Write() error {
` `
} }
out := `// Code generated by. DO NOT EDIT. out := `// Code generated by Gosora. More below:
/* This file was generated by Gosora's Query Generator. The thing above is to tell GH this file is generated. */ /* This file was generated by Gosora's Query Generator. Please try to avoid modifying this file, as it might change at any time. */
// +build !pgsql !sqlite !mssql // +build !pgsql !sqlite !mssql
package main package main

View File

@ -24,6 +24,31 @@ func init() {
settingLabels["activation_type"] = "Activate All,Email Activation,Admin Approval" settingLabels["activation_type"] = "Activate All,Email Activation,Admin Approval"
} }
func LoadSettings() error {
rows, err := get_full_settings_stmt.Query()
if err != nil {
return err
}
defer rows.Close()
var sname, scontent, stype, sconstraints string
for rows.Next() {
err = rows.Scan(&sname, &scontent, &stype, &sconstraints)
if err != nil {
return err
}
errmsg := parseSetting(sname, scontent, stype, sconstraints)
if errmsg != "" {
return err
}
}
err = rows.Err()
if err != nil {
return err
}
return nil
}
func parseSetting(sname string, scontent string, stype string, constraint string) string { func parseSetting(sname string, scontent string, stype string, constraint string) string {
var err error var err error
if stype == "bool" { if stype == "bool" {

View File

@ -52,6 +52,45 @@ type TemplateMapping struct
//When string //When string
} }
func LoadThemes() error {
rows, err := get_themes_stmt.Query()
if err != nil {
return err
}
defer rows.Close()
var uname string
var defaultThemeSwitch bool
for rows.Next() {
err = rows.Scan(&uname, &defaultThemeSwitch)
if err != nil {
return err
}
// Was the theme deleted at some point?
theme, ok := themes[uname]
if !ok {
continue
}
if defaultThemeSwitch {
log.Print("Loading the theme '" + theme.Name + "'")
theme.Active = true
defaultTheme = uname
add_theme_static_files(uname)
map_theme_templates(theme)
} else {
theme.Active = false
}
themes[uname] = theme
}
err = rows.Err()
if err != nil {
return err
}
return nil
}
func init_themes() { func init_themes() {
themeFiles, err := ioutil.ReadDir("./themes") themeFiles, err := ioutil.ReadDir("./themes")
if err != nil { if err != nil {