Split database.go's contents off into their own functions for reusability's sake.
This commit is contained in:
parent
fe2eabedd0
commit
8c1d8d5c64
|
@ -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 staff_css = " background-color: #ffeaff;"
|
||||
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/285/{id}@" + site_url + ".png"
|
||||
|
|
178
database.go
178
database.go
|
@ -2,7 +2,6 @@ package main
|
|||
|
||||
import "log"
|
||||
import "fmt"
|
||||
import "strconv"
|
||||
import "encoding/json"
|
||||
|
||||
func init_database() (err error) {
|
||||
|
@ -57,196 +56,31 @@ func init_database() (err error) {
|
|||
GuestPerms = groups[6].Perms
|
||||
|
||||
log.Print("Loading the forums.")
|
||||
log.Print("Adding the uncategorised forum")
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
i = 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)
|
||||
err = LoadForums()
|
||||
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()
|
||||
log.Print("Loading the forum permissions.")
|
||||
err = build_forum_permissions()
|
||||
if err != nil {
|
||||
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.")
|
||||
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()
|
||||
err = LoadSettings()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Print("Loading the plugins.")
|
||||
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()
|
||||
err = LoadPlugins()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Print("Loading the themes.")
|
||||
rows, err = get_themes_stmt.Query()
|
||||
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()
|
||||
err = LoadThemes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
30
extend.go
30
extend.go
|
@ -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 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
|
||||
{
|
||||
UName string
|
||||
|
|
54
forum.go
54
forum.go
|
@ -1,5 +1,6 @@
|
|||
package main
|
||||
|
||||
import "log"
|
||||
//import "fmt"
|
||||
import "sync"
|
||||
import "strconv"
|
||||
|
@ -40,6 +41,59 @@ type ForumSimple struct
|
|||
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
|
||||
func create_forum(forum_name string, forum_desc string, active bool, preset string) (int, error) {
|
||||
var fid int
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
// Code generated by. DO NOT EDIT.
|
||||
/* This file was generated by Gosora's Query Generator. The thing above is to tell GH this file is generated. */
|
||||
// Code generated by Gosora. More below:
|
||||
/* 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
|
||||
package main
|
||||
|
||||
|
|
|
@ -173,6 +173,7 @@ var activation_group = 5 // Should be a setting in the database
|
|||
var staff_css = " background-color: #ffeaff;"
|
||||
var uncategorised_forum_visible = 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/285/{id}@" + site_url + ".png"
|
||||
|
|
|
@ -192,6 +192,11 @@ CREATE TABLE `administration_logs`(
|
|||
`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`,`constraints`) VALUES ('activation_type','1','list','1-3');
|
||||
INSERT INTO settings(`name`,`content`,`type`) VALUES ('bigpost_min_words','250','int');
|
||||
|
|
|
@ -303,14 +303,18 @@ func permmap_to_query(permmap map[string]ForumPerms, fid int) error {
|
|||
}
|
||||
|
||||
func rebuild_forum_permissions(fid int) error {
|
||||
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)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
if debug {
|
||||
log.Print("Updating the forum permissions")
|
||||
}
|
||||
for rows.Next() {
|
||||
var gid int
|
||||
var perms []byte
|
||||
|
@ -332,7 +336,9 @@ func rebuild_forum_permissions(fid int) error {
|
|||
forum_perms[gid][fid] = pperms
|
||||
}
|
||||
for gid, _ := range groups {
|
||||
if debug {
|
||||
log.Print("Updating the forum permissions for Group #" + strconv.Itoa(gid))
|
||||
}
|
||||
var blank_list []ForumPerms
|
||||
var blank_int_list []int
|
||||
groups[gid].Forums = blank_list
|
||||
|
@ -357,14 +363,80 @@ func rebuild_forum_permissions(fid int) error {
|
|||
groups[gid].CanSee = append(groups[gid].CanSee, ffid)
|
||||
}
|
||||
}
|
||||
//fmt.Printf("%+v\n", groups[gid].CanSee)
|
||||
//fmt.Printf("%+v\n", groups[gid].Forums)
|
||||
//fmt.Println(len(groups[gid].Forums))
|
||||
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].Forums)",len(groups[gid].Forums))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -491,8 +491,8 @@ func (adapter *Mysql_Adapter) Write() error {
|
|||
`
|
||||
}
|
||||
|
||||
out := `// Code generated by. DO NOT EDIT.
|
||||
/* This file was generated by Gosora's Query Generator. The thing above is to tell GH this file is generated. */
|
||||
out := `// Code generated by Gosora. More below:
|
||||
/* 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
|
||||
package main
|
||||
|
||||
|
|
25
setting.go
25
setting.go
|
@ -24,6 +24,31 @@ func init() {
|
|||
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 {
|
||||
var err error
|
||||
if stype == "bool" {
|
||||
|
|
39
themes.go
39
themes.go
|
@ -52,6 +52,45 @@ type TemplateMapping struct
|
|||
//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() {
|
||||
themeFiles, err := ioutil.ReadDir("./themes")
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue