From 8d0479f4b2f6006901466b4ceb9a4e90a65a56a2 Mon Sep 17 00:00:00 2001 From: Azareal Date: Mon, 13 Nov 2017 09:23:43 +0000 Subject: [PATCH] Renamed setting.go to settings.go Added the BypassGet method to SettingMap and refactored the Control Panel to use it. Refactored the query builder. More work on Cosora. --- common/forum_store.go | 1 - common/pages.go | 2 +- common/{setting.go => settings.go} | 21 ++- gen_mssql.go | 16 --- gen_mysql.go | 14 -- panel_routes.go | 17 +-- query_gen/lib/builder.go | 67 +++------- query_gen/main.go | 4 - routes.go | 6 - template_list.go | 1 - templates/account-own-edit-avatar.html | 42 +++--- templates/account-own-edit-email.html | 32 ++--- templates/account-own-edit-username.html | 40 +++--- templates/account-own-edit.html | 48 +++---- templates/forum.html | 162 +++++++++++------------ templates/ip-search.html | 38 +++--- templates/login.html | 40 +++--- templates/register.html | 54 ++++---- themes/cosora/public/main.css | 50 ++++++- 19 files changed, 320 insertions(+), 335 deletions(-) rename common/{setting.go => settings.go} (80%) diff --git a/common/forum_store.go b/common/forum_store.go index e35e2eea..0e83702f 100644 --- a/common/forum_store.go +++ b/common/forum_store.go @@ -56,7 +56,6 @@ type ForumCache interface { type MemoryForumStore struct { forums sync.Map // map[int]*Forum forumView atomic.Value // []*Forum - //fids []int get *sql.Stmt getAll *sql.Stmt diff --git a/common/pages.go b/common/pages.go index 62413d08..64ec5114 100644 --- a/common/pages.go +++ b/common/pages.go @@ -12,7 +12,7 @@ type HeaderVars struct { Stylesheets []string Widgets PageWidgets Site *site - Settings map[string]interface{} + Settings SettingMap Themes map[string]Theme // TODO: Use a slice containing every theme instead of the main map for speed ThemeName string //TemplateName string // TODO: Use this to move template calls to the router rather than duplicating them over and over and over? diff --git a/common/setting.go b/common/settings.go similarity index 80% rename from common/setting.go rename to common/settings.go index dc9f2aeb..b7e0f3a6 100644 --- a/common/setting.go +++ b/common/settings.go @@ -9,10 +9,15 @@ import ( "../query_gen/lib" ) +var SettingBox atomic.Value // An atomic value pointing to a SettingBox + // SettingMap 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 SettingMap map[string]interface{} -var SettingBox atomic.Value // An atomic value pointing to a SettingBox +type SettingStore interface { + ParseSetting(sname string, scontent string, stype string, sconstraint string) string + BypassGet(name string) (*Setting, error) +} type OptionLabel struct { Label string @@ -28,7 +33,8 @@ type Setting struct { } type SettingStmts struct { - getFull *sql.Stmt + getAll *sql.Stmt + get *sql.Stmt } var settingStmts SettingStmts @@ -37,14 +43,15 @@ func init() { SettingBox.Store(SettingMap(make(map[string]interface{}))) DbInits.Add(func(acc *qgen.Accumulator) error { settingStmts = SettingStmts{ - getFull: acc.Select("settings").Columns("name, content, type, constraints").Prepare(), + getAll: acc.Select("settings").Columns("name, content, type, constraints").Prepare(), + get: acc.Select("settings").Columns("content, type, constraints").Where("name = ?").Prepare(), } return acc.FirstError() }) } func LoadSettings() error { - rows, err := settingStmts.getFull.Query() + rows, err := settingStmts.getAll.Query() if err != nil { return err } @@ -113,3 +120,9 @@ func (sBox SettingMap) ParseSetting(sname string, scontent string, stype string, } return "" } + +func (sBox SettingMap) BypassGet(name string) (*Setting, error) { + setting := &Setting{Name: name} + err := settingStmts.get.QueryRow(name).Scan(&setting.Content, &setting.Type, &setting.Constraint) + return setting, err +} diff --git a/gen_mssql.go b/gen_mssql.go index 5d3aa854..afba5918 100644 --- a/gen_mssql.go +++ b/gen_mssql.go @@ -11,8 +11,6 @@ import "./common" type Stmts struct { getPassword *sql.Stmt getSettings *sql.Stmt - getSetting *sql.Stmt - getFullSetting *sql.Stmt isPluginActive *sql.Stmt getUsersOffset *sql.Stmt isThemeDefault *sql.Stmt @@ -99,20 +97,6 @@ func _gen_mssql() (err error) { return err } - log.Print("Preparing getSetting statement.") - stmts.getSetting, err = db.Prepare("SELECT [content],[type] FROM [settings] WHERE [name] = ?1") - if err != nil { - log.Print("Bad Query: ","SELECT [content],[type] FROM [settings] WHERE [name] = ?1") - return err - } - - log.Print("Preparing getFullSetting statement.") - stmts.getFullSetting, err = db.Prepare("SELECT [name],[type],[constraints] FROM [settings] WHERE [name] = ?1") - if err != nil { - log.Print("Bad Query: ","SELECT [name],[type],[constraints] FROM [settings] WHERE [name] = ?1") - return err - } - log.Print("Preparing isPluginActive statement.") stmts.isPluginActive, err = db.Prepare("SELECT [active] FROM [plugins] WHERE [uname] = ?1") if err != nil { diff --git a/gen_mysql.go b/gen_mysql.go index 6936eea5..4c5df0d6 100644 --- a/gen_mysql.go +++ b/gen_mysql.go @@ -13,8 +13,6 @@ import "./common" type Stmts struct { getPassword *sql.Stmt getSettings *sql.Stmt - getSetting *sql.Stmt - getFullSetting *sql.Stmt isPluginActive *sql.Stmt getUsersOffset *sql.Stmt isThemeDefault *sql.Stmt @@ -99,18 +97,6 @@ func _gen_mysql() (err error) { return err } - log.Print("Preparing getSetting statement.") - stmts.getSetting, err = db.Prepare("SELECT `content`,`type` FROM `settings` WHERE `name` = ?") - if err != nil { - return err - } - - log.Print("Preparing getFullSetting statement.") - stmts.getFullSetting, err = db.Prepare("SELECT `name`,`type`,`constraints` FROM `settings` WHERE `name` = ?") - if err != nil { - return err - } - log.Print("Preparing isPluginActive statement.") stmts.isPluginActive, err = db.Prepare("SELECT `active` FROM `plugins` WHERE `uname` = ?") if err != nil { diff --git a/panel_routes.go b/panel_routes.go index 209a8e22..557596b5 100644 --- a/panel_routes.go +++ b/panel_routes.go @@ -455,7 +455,6 @@ func routePanelSettings(w http.ResponseWriter, r *http.Request, user common.User return common.NoPermissions(w, r, user) } - //log.Print("headerVars.Settings",headerVars.Settings) var settingList = make(map[string]interface{}) rows, err := stmts.getSettings.Query() if err != nil { @@ -515,9 +514,8 @@ func routePanelSetting(w http.ResponseWriter, r *http.Request, user common.User, if !user.Perms.EditSettings { return common.NoPermissions(w, r, user) } - setting := common.Setting{sname, "", "", ""} - err := stmts.getSetting.QueryRow(setting.Name).Scan(&setting.Content, &setting.Type) + setting, err := headerVars.Settings.BypassGet(sname) if err == ErrNoRows { return common.LocalError("The setting you want to edit doesn't exist.", w, r, user) } else if err != nil { @@ -532,8 +530,7 @@ func routePanelSetting(w http.ResponseWriter, r *http.Request, user common.User, return common.LocalError("The value of this setting couldn't be converted to an integer", w, r, user) } - labels := strings.Split(llist, ",") - for index, label := range labels { + for index, label := range strings.Split(llist, ",") { itemList = append(itemList, common.OptionLabel{ Label: label, Value: index + 1, @@ -564,17 +561,15 @@ func routePanelSettingEdit(w http.ResponseWriter, r *http.Request, user common.U return common.NoPermissions(w, r, user) } - var stype, sconstraints string scontent := r.PostFormValue("setting-value") - - err := stmts.getFullSetting.QueryRow(sname).Scan(&sname, &stype, &sconstraints) + setting, err := headerLite.Settings.BypassGet(sname) if err == ErrNoRows { return common.LocalError("The setting you want to edit doesn't exist.", w, r, user) } else if err != nil { return common.InternalError(err, w, r) } - if stype == "bool" { + if setting.Type == "bool" { if scontent == "on" || scontent == "1" { scontent = "1" } else { @@ -588,10 +583,11 @@ func routePanelSettingEdit(w http.ResponseWriter, r *http.Request, user common.U return common.InternalError(err, w, r) } - errmsg := headerLite.Settings.ParseSetting(sname, scontent, stype, sconstraints) + errmsg := headerLite.Settings.ParseSetting(sname, scontent, setting.Type, setting.Constraint) if errmsg != "" { return common.LocalError(errmsg, w, r, user) } + // TODO: Do a reload instead? common.SettingBox.Store(headerLite.Settings) http.Redirect(w, r, "/panel/settings/", http.StatusSeeOther) @@ -649,6 +645,7 @@ func routePanelWordFiltersCreate(w http.ResponseWriter, r *http.Request, user co } common.AddWordFilter(int(lastID), find, replacement) + if !isJs { http.Redirect(w, r, "/panel/settings/word-filters/", http.StatusSeeOther) } else { diff --git a/query_gen/lib/builder.go b/query_gen/lib/builder.go index e664c712..f39f2359 100644 --- a/query_gen/lib/builder.go +++ b/query_gen/lib/builder.go @@ -110,100 +110,71 @@ func (build *builder) Purge(table string) (stmt *sql.Stmt, err error) { return build.prepare(build.adapter.Purge("_builder", table)) } +func (build *builder) prepareTx(tx *sql.Tx, res string, err error) (*sql.Stmt, error) { + if err != nil { + return nil, err + } + return tx.Prepare(res) +} + // These ones support transactions func (build *builder) SimpleSelectTx(tx *sql.Tx, table string, columns string, where string, orderby string, limit string) (stmt *sql.Stmt, err error) { res, err := build.adapter.SimpleSelect("_builder", table, columns, where, orderby, limit) - if err != nil { - return stmt, err - } - return tx.Prepare(res) + return build.prepareTx(tx, res, err) } func (build *builder) SimpleCountTx(tx *sql.Tx, table string, where string, limit string) (stmt *sql.Stmt, err error) { res, err := build.adapter.SimpleCount("_builder", table, where, limit) - if err != nil { - return stmt, err - } - return tx.Prepare(res) + return build.prepareTx(tx, res, err) } func (build *builder) SimpleLeftJoinTx(tx *sql.Tx, table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) (stmt *sql.Stmt, err error) { res, err := build.adapter.SimpleLeftJoin("_builder", table1, table2, columns, joiners, where, orderby, limit) - if err != nil { - return stmt, err - } - return tx.Prepare(res) + return build.prepareTx(tx, res, err) } func (build *builder) SimpleInnerJoinTx(tx *sql.Tx, table1 string, table2 string, columns string, joiners string, where string, orderby string, limit string) (stmt *sql.Stmt, err error) { res, err := build.adapter.SimpleInnerJoin("_builder", table1, table2, columns, joiners, where, orderby, limit) - if err != nil { - return stmt, err - } - return tx.Prepare(res) + return build.prepareTx(tx, res, err) } func (build *builder) CreateTableTx(tx *sql.Tx, table string, charset string, collation string, columns []DBTableColumn, keys []DBTableKey) (stmt *sql.Stmt, err error) { res, err := build.adapter.CreateTable("_builder", table, charset, collation, columns, keys) - if err != nil { - return stmt, err - } - return tx.Prepare(res) + return build.prepareTx(tx, res, err) } func (build *builder) SimpleInsertTx(tx *sql.Tx, table string, columns string, fields string) (stmt *sql.Stmt, err error) { res, err := build.adapter.SimpleInsert("_builder", table, columns, fields) - if err != nil { - return stmt, err - } - return tx.Prepare(res) + return build.prepareTx(tx, res, err) } func (build *builder) SimpleInsertSelectTx(tx *sql.Tx, ins DBInsert, sel DBSelect) (stmt *sql.Stmt, err error) { res, err := build.adapter.SimpleInsertSelect("_builder", ins, sel) - if err != nil { - return stmt, err - } - return tx.Prepare(res) + return build.prepareTx(tx, res, err) } func (build *builder) SimpleInsertLeftJoinTx(tx *sql.Tx, ins DBInsert, sel DBJoin) (stmt *sql.Stmt, err error) { res, err := build.adapter.SimpleInsertLeftJoin("_builder", ins, sel) - if err != nil { - return stmt, err - } - return tx.Prepare(res) + return build.prepareTx(tx, res, err) } func (build *builder) SimpleInsertInnerJoinTx(tx *sql.Tx, ins DBInsert, sel DBJoin) (stmt *sql.Stmt, err error) { res, err := build.adapter.SimpleInsertInnerJoin("_builder", ins, sel) - if err != nil { - return stmt, err - } - return tx.Prepare(res) + return build.prepareTx(tx, res, err) } func (build *builder) SimpleUpdateTx(tx *sql.Tx, table string, set string, where string) (stmt *sql.Stmt, err error) { res, err := build.adapter.SimpleUpdate("_builder", table, set, where) - if err != nil { - return stmt, err - } - return tx.Prepare(res) + return build.prepareTx(tx, res, err) } func (build *builder) SimpleDeleteTx(tx *sql.Tx, table string, where string) (stmt *sql.Stmt, err error) { res, err := build.adapter.SimpleDelete("_builder", table, where) - if err != nil { - return stmt, err - } - return tx.Prepare(res) + return build.prepareTx(tx, res, err) } // I don't know why you need this, but here it is x.x func (build *builder) PurgeTx(tx *sql.Tx, table string) (stmt *sql.Stmt, err error) { res, err := build.adapter.Purge("_builder", table) - if err != nil { - return stmt, err - } - return tx.Prepare(res) + return build.prepareTx(tx, res, err) } diff --git a/query_gen/main.go b/query_gen/main.go index 10d7f2c9..1d79e07b 100644 --- a/query_gen/main.go +++ b/query_gen/main.go @@ -228,10 +228,6 @@ func writeSelects(adapter qgen.Adapter) error { build.Select("getSettings").Table("settings").Columns("name, content, type").Parse() - build.Select("getSetting").Table("settings").Columns("content, type").Where("name = ?").Parse() - - build.Select("getFullSetting").Table("settings").Columns("name, type, constraints").Where("name = ?").Parse() - build.Select("isPluginActive").Table("plugins").Columns("active").Where("uname = ?").Parse() //build.Select("isPluginInstalled").Table("plugins").Columns("installed").Where("uname = ?").Parse() diff --git a/routes.go b/routes.go index 808d1bd2..7d740253 100644 --- a/routes.go +++ b/routes.go @@ -1013,12 +1013,6 @@ func routeAPI(w http.ResponseWriter, r *http.Request, user common.User) common.R //case "forums": //case "users": //case "pages": - // This might not be possible. We might need .xml paths for sitemaps - /*case "sitemap": - if format != "xml" { - PreError("You can only fetch sitemaps in the XML format!",w,r) - return - }*/ default: return common.PreErrorJS("Invalid Module", w, r) } diff --git a/template_list.go b/template_list.go index 1a7c456d..dad04b44 100644 --- a/template_list.go +++ b/template_list.go @@ -882,7 +882,6 @@ var forum_7 = []byte(`">>`) var forum_8 = []byte(`
-
-
-

Edit Avatar

-
- {{if .CurrentUser.Avatar}} -
-
-
- {{end}} -
-
- -
-
-
-
+
+ {{template "account-menu.html" . }} +
+
+

Edit Avatar

+
+ {{if .CurrentUser.Avatar}} +
+
+
+ {{end}} +
+
+ +
+
+
+
+
{{template "footer.html" . }} diff --git a/templates/account-own-edit-email.html b/templates/account-own-edit-email.html index d0f5c69e..72184f1b 100644 --- a/templates/account-own-edit-email.html +++ b/templates/account-own-edit-email.html @@ -1,20 +1,22 @@ {{template "header.html" . }} -{{template "account-menu.html" . }} -
-
-

Emails

-
-
- - {{range .ItemList}} -
- {{.Email}} - - {{if .Primary}}Primary{{else}}Secondary{{end}} - {{if .Validated}}Verified{{else}}Resend Verification Email{{end}} - +
+ {{template "account-menu.html" . }} +
+
+

Emails

+
+
+ + {{range .ItemList}} +
+ {{.Email}} + + {{if .Primary}}Primary{{else}}Secondary{{end}} + {{if .Validated}}Verified{{else}}Resend Verification Email{{end}} + +
+ {{end}}
- {{end}}
{{template "footer.html" . }} diff --git a/templates/account-own-edit-username.html b/templates/account-own-edit-username.html index 3441add1..a24803c4 100644 --- a/templates/account-own-edit-username.html +++ b/templates/account-own-edit-username.html @@ -1,23 +1,25 @@ {{template "header.html" . }} -{{template "account-menu.html" . }} -
-
-

Edit Username

-
-
-
-
- -
{{.CurrentUser.Name}}
-
-
- -
-
-
-
-
-
+
+ {{template "account-menu.html" . }} +
+
+

Edit Username

+
+
+
+
+ +
{{.CurrentUser.Name}}
+
+
+ +
+
+
+
+
+
+
{{template "footer.html" . }} diff --git a/templates/account-own-edit.html b/templates/account-own-edit.html index ee58d43f..eefaf8db 100644 --- a/templates/account-own-edit.html +++ b/templates/account-own-edit.html @@ -1,27 +1,29 @@ {{template "header.html" . }} -{{template "account-menu.html" . }} -
-
-

Edit Password

-
-
-
- -
- -
-
- -
-
-
-
+
+ {{template "account-menu.html" . }} +
+
+

Edit Password

+
+
+
+ +
+ +
+
+ +
+
+
+
+
{{template "footer.html" . }} diff --git a/templates/forum.html b/templates/forum.html index 72fde3d1..44fc8966 100644 --- a/templates/forum.html +++ b/templates/forum.html @@ -5,96 +5,94 @@ {{end}}
- -
-

{{.Title}}

+
+
+

{{.Title}}

+
+ {{if ne .CurrentUser.ID 0}} + {{if .CurrentUser.Perms.CreateTopic}} +
+
+ {{/** TODO: Add a permissions check for this **/}} +
+ +
+ {{else}}
{{end}} +
+ {{end}}
{{if ne .CurrentUser.ID 0}} - {{if .CurrentUser.Perms.CreateTopic}} -
-
- {{/** TODO: Add a permissions check for this **/}} -
- -
- {{else}}
{{end}} -
- {{end}} -
-{{if ne .CurrentUser.ID 0}} -
-
-
- What do you want to do with these 18 topics? +
+ +
+ What do you want to do with these 18 topics? +
+
+ + +
+
-
- - -
- -
- -{{if .CurrentUser.Perms.CreateTopic}} -
{{template "footer.html" . }} diff --git a/templates/ip-search.html b/templates/ip-search.html index 3b5e50c1..5c696994 100644 --- a/templates/ip-search.html +++ b/templates/ip-search.html @@ -1,28 +1,24 @@ {{template "header.html" . }}
- -
-
-

IP Search

+
+
+

IP Search

+
-
- -
-
-
- - +
+
+
+ + +
-
- -{{if .IP}} -
- {{range .ItemList}}
- {{.Name}} + {{if .IP}} +
+ {{range .ItemList}}
+ {{.Name}} +
+ {{else}}
No users found.
{{end}}
- {{else}}
No users found.
{{end}} -
-{{end}} - + {{end}}
{{template "footer.html" . }} diff --git a/templates/login.html b/templates/login.html index d54e788f..b4b95720 100644 --- a/templates/login.html +++ b/templates/login.html @@ -1,21 +1,23 @@ {{template "header.html" . }} -
-

Login

-
-
-
-
- -
-
-
- -
-
-
-
-
Don't have an account?
-
-
-
+
+
+

Login

+
+
+
+
+ +
+
+
+ +
+
+
+
+
Don't have an account?
+
+
+
+
{{template "footer.html" . }} diff --git a/templates/register.html b/templates/register.html index 247ad396..10832ad3 100644 --- a/templates/register.html +++ b/templates/register.html @@ -1,32 +1,30 @@ {{template "header.html" . }}
- -
-

Create Account

-
-
-
-
- -
-
-
- -
-
-
- -
-
- -
-
-
- -
- +
+

Create Account

+
+
+
+
+ +
+
+
+ +
+
+
+ +
+
+ +
+
+
+ +
{{template "footer.html" . }} diff --git a/themes/cosora/public/main.css b/themes/cosora/public/main.css index fec85e7e..f2e76a40 100644 --- a/themes/cosora/public/main.css +++ b/themes/cosora/public/main.css @@ -157,7 +157,7 @@ ul { display: none; } -.rowblock, .colstack_item { +.rowblock, .colstack_head { margin-bottom: 12px; border: 1px solid var(--header-border-color); border-bottom: 2px solid var(--header-border-color); @@ -182,6 +182,16 @@ ul { display: inline-block; } +.colstack { + display: flex; +} +#main .colstack_left { + width: 300px; +} +#main .colstack_right { + width: calc(90% - 300px); +} + .extra_little_row_avatar { height: 38px; width: 38px; @@ -735,7 +745,7 @@ select, input, textarea { #profile_left_pane .profileName { font-size: 19px; } -#profile_left_pane .passiveBlock .passive { +.rowmenu .passive { border: 1px solid var(--element-border-color); border-bottom: 2px solid var(--element-border-color); margin-top: 6px; @@ -743,14 +753,48 @@ select, input, textarea { padding-top: 10px; padding-bottom: 10px; } +.rowmenu { + padding-left: 12px; + padding-right: 12px; +} +.rowmenu .passive:hover { + margin-left: 4px; +} +#profile_left_pane .passiveBlock .passive { + padding-left: 12px; +} #profile_right_lane { width: 100%; margin-right: 12px; } -#profile_right_lane .colstack_item { +#profile_right_lane .colstack_item, .colstack_right .colstack_item { border: 1px solid var(--element-border-color); border-bottom: 2px solid var(--element-border-color); + margin-left: 16px; +} + +.form_item { + display: flex; +} +.form_item form, .colstack_item .formrow { + display: contents; +} +.colstack_right .formrow { + padding-left: 16px; + padding-right: 16px; + padding-bottom: 4px; +} +.colstack_right .formrow:first-child { + padding-top: 16px; +} +.colstack_right .formrow:last-child { + padding-bottom: 16px; +} +.colstack_item:not(#profile_right_lane) .formrow .formlabel { + width: min-content; + margin-right: 12px; + white-space: nowrap; } .footer {