91f70d2a4a
Added basic support for server sync. Re-added a few missing defers. Renamed TO-DO to TODO across the entire codebase. Renamed StaticForumStore to MemoryForumStore. The ForumStore is now built on a sync.Map with a view slice for generating /forums rather than a slice. Renamed many more functions and variables to satisfy the linter. increase_post_user_stats() and decrease_post_user_stats() are now methods on the User struct. We also fix a bug where they take the moderator's score rather than the target user's into account when recalculating their level after a post / topic is deleted. Transitioned the topic list to CSS Grid for Tempra Simple, with a float fallback. Cosmo and Cosmo Conflux are now hidden from the theme list. Fixed more data races. Added more debug data to the template compiler logs.
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
package main
|
|
|
|
import "strconv"
|
|
import "strings"
|
|
import "sync/atomic"
|
|
|
|
// SettingBox is a map type specifically for holding the various settings admins set to toggle features on and off or to otherwise alter Gosora's behaviour from the Control Panel
|
|
type SettingBox map[string]interface{}
|
|
|
|
var settingBox atomic.Value // An atomic value pointing to a SettingBox
|
|
|
|
type OptionLabel struct {
|
|
Label string
|
|
Value int
|
|
Selected bool
|
|
}
|
|
|
|
type Setting struct {
|
|
Name string
|
|
Content string
|
|
Type string
|
|
Constraint string
|
|
}
|
|
|
|
func init() {
|
|
settingBox.Store(SettingBox(make(map[string]interface{})))
|
|
}
|
|
|
|
func LoadSettings() error {
|
|
rows, err := get_full_settings_stmt.Query()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var sBox = SettingBox(make(map[string]interface{}))
|
|
var sname, scontent, stype, sconstraints string
|
|
for rows.Next() {
|
|
err = rows.Scan(&sname, &scontent, &stype, &sconstraints)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
errmsg := sBox.ParseSetting(sname, scontent, stype, sconstraints)
|
|
if errmsg != "" {
|
|
return err
|
|
}
|
|
}
|
|
err = rows.Err()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
settingBox.Store(sBox)
|
|
return nil
|
|
}
|
|
|
|
// TODO: Add better support for HTML attributes (html-attribute). E.g. Meta descriptions.
|
|
func (sBox SettingBox) ParseSetting(sname string, scontent string, stype string, constraint string) string {
|
|
var err error
|
|
var ssBox = map[string]interface{}(sBox)
|
|
if stype == "bool" {
|
|
ssBox[sname] = (scontent == "1")
|
|
} else if stype == "int" {
|
|
ssBox[sname], err = strconv.Atoi(scontent)
|
|
if err != nil {
|
|
return "You were supposed to enter an integer x.x\nType mismatch in " + sname
|
|
}
|
|
} else if stype == "int64" {
|
|
ssBox[sname], err = strconv.ParseInt(scontent, 10, 64)
|
|
if err != nil {
|
|
return "You were supposed to enter an integer x.x\nType mismatch in " + sname
|
|
}
|
|
} else if stype == "list" {
|
|
cons := strings.Split(constraint, "-")
|
|
if len(cons) < 2 {
|
|
return "Invalid constraint! The second field wasn't set!"
|
|
}
|
|
|
|
con1, err := strconv.Atoi(cons[0])
|
|
if err != nil {
|
|
return "Invalid contraint! The constraint field wasn't an integer!"
|
|
}
|
|
con2, err := strconv.Atoi(cons[1])
|
|
if err != nil {
|
|
return "Invalid contraint! The constraint field wasn't an integer!"
|
|
}
|
|
|
|
value, err := strconv.Atoi(scontent)
|
|
if err != nil {
|
|
return "Only integers are allowed in this setting x.x\nType mismatch in " + sname
|
|
}
|
|
|
|
if value < con1 || value > con2 {
|
|
return "Only integers between a certain range are allowed in this setting"
|
|
}
|
|
ssBox[sname] = value
|
|
} else {
|
|
ssBox[sname] = scontent
|
|
}
|
|
return ""
|
|
}
|