2022-07-16 21:09:43 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2022-07-17 08:36:10 +02:00
|
|
|
"fmt"
|
2022-07-21 19:09:50 +02:00
|
|
|
|
|
|
|
"github.com/samber/lo"
|
2022-07-17 08:36:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func settingNameWithBlog(blog, name string) string {
|
|
|
|
return fmt.Sprintf("%s---%s", blog, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2022-07-21 19:09:50 +02:00
|
|
|
defaultSectionSetting = "defaultsection"
|
|
|
|
hideOldContentWarningSetting = "hideoldcontentwarning"
|
2022-10-07 17:08:37 +02:00
|
|
|
hideShareButtonSetting = "hidesharebutton"
|
|
|
|
hideTranslateButtonSetting = "hidetranslatebutton"
|
2022-11-27 07:20:49 +01:00
|
|
|
userNickSetting = "usernick"
|
|
|
|
userNameSetting = "username"
|
2022-12-14 14:56:27 +01:00
|
|
|
addReplyTitleSetting = "addreplytitle"
|
2022-12-26 09:23:52 +01:00
|
|
|
addReplyContextSetting = "addreplycontext"
|
2022-12-14 14:56:27 +01:00
|
|
|
addLikeTitleSetting = "addliketitle"
|
2022-12-26 09:23:52 +01:00
|
|
|
addLikeContextSetting = "addlikecontext"
|
2022-07-16 21:09:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (a *goBlog) getSettingValue(name string) (string, error) {
|
2022-08-09 17:25:22 +02:00
|
|
|
row, err := a.db.QueryRow("select value from settings where name = @name", sql.Named("name", name))
|
2022-07-16 21:09:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return "",
|
|
|
|
err
|
|
|
|
}
|
|
|
|
var value string
|
|
|
|
err = row.Scan(&value)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return "", nil
|
|
|
|
} else if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return value, nil
|
|
|
|
}
|
|
|
|
|
2022-07-21 19:09:50 +02:00
|
|
|
func (a *goBlog) getBooleanSettingValue(name string, defaultValue bool) (bool, error) {
|
|
|
|
stringValue, err := a.getSettingValue(name)
|
|
|
|
if err != nil {
|
|
|
|
return defaultValue, err
|
|
|
|
}
|
|
|
|
if stringValue == "" {
|
|
|
|
return defaultValue, nil
|
|
|
|
}
|
|
|
|
return stringValue == "1", nil
|
|
|
|
}
|
|
|
|
|
2022-07-16 21:09:43 +02:00
|
|
|
func (a *goBlog) saveSettingValue(name, value string) error {
|
2022-08-09 17:25:22 +02:00
|
|
|
_, err := a.db.Exec(
|
2022-07-16 21:09:43 +02:00
|
|
|
"insert into settings (name, value) values (@name, @value) on conflict (name) do update set value = @value2",
|
|
|
|
sql.Named("name", name),
|
|
|
|
sql.Named("value", value),
|
|
|
|
sql.Named("value2", value),
|
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-21 19:09:50 +02:00
|
|
|
func (a *goBlog) saveBooleanSettingValue(name string, value bool) error {
|
|
|
|
return a.saveSettingValue(name, lo.If(value, "1").Else("0"))
|
|
|
|
}
|
|
|
|
|
2022-07-16 21:09:43 +02:00
|
|
|
func (a *goBlog) loadSections() error {
|
|
|
|
for blog, bc := range a.cfg.Blogs {
|
|
|
|
sections, err := a.getSections(blog)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
bc.Sections = sections
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) getSections(blog string) (map[string]*configSection, error) {
|
2022-08-09 17:25:22 +02:00
|
|
|
rows, err := a.db.Query("select name, title, description, pathtemplate, showfull from sections where blog = @blog", sql.Named("blog", blog))
|
2022-07-16 21:09:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sections := map[string]*configSection{}
|
|
|
|
for rows.Next() {
|
|
|
|
section := &configSection{}
|
|
|
|
err = rows.Scan(§ion.Name, §ion.Title, §ion.Description, §ion.PathTemplate, §ion.ShowFull)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sections[section.Name] = section
|
|
|
|
}
|
|
|
|
return sections, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) saveAllSections() error {
|
|
|
|
for blog, bc := range a.cfg.Blogs {
|
|
|
|
for k, s := range bc.Sections {
|
|
|
|
s.Name = k
|
2022-07-17 08:36:10 +02:00
|
|
|
if err := a.saveSection(blog, s); err != nil {
|
2022-07-16 21:09:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-17 08:36:10 +02:00
|
|
|
func (a *goBlog) saveSection(blog string, section *configSection) error {
|
2022-08-09 17:25:22 +02:00
|
|
|
_, err := a.db.Exec(
|
2022-07-17 08:36:10 +02:00
|
|
|
`
|
|
|
|
insert into sections (blog, name, title, description, pathtemplate, showfull) values (@blog, @name, @title, @description, @pathtemplate, @showfull)
|
|
|
|
on conflict (blog, name) do update set title = @title2, description = @description2, pathtemplate = @pathtemplate2, showfull = @showfull2
|
|
|
|
`,
|
2022-07-16 21:09:43 +02:00
|
|
|
sql.Named("blog", blog),
|
|
|
|
sql.Named("name", section.Name),
|
|
|
|
sql.Named("title", section.Title),
|
|
|
|
sql.Named("description", section.Description),
|
|
|
|
sql.Named("pathtemplate", section.PathTemplate),
|
|
|
|
sql.Named("showfull", section.ShowFull),
|
2022-07-17 08:36:10 +02:00
|
|
|
sql.Named("title2", section.Title),
|
|
|
|
sql.Named("description2", section.Description),
|
|
|
|
sql.Named("pathtemplate2", section.PathTemplate),
|
|
|
|
sql.Named("showfull2", section.ShowFull),
|
2022-07-16 21:09:43 +02:00
|
|
|
)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *goBlog) deleteSection(blog string, name string) error {
|
2022-08-09 17:25:22 +02:00
|
|
|
_, err := a.db.Exec("delete from sections where blog = @blog and name = @name", sql.Named("blog", blog), sql.Named("name", name))
|
2022-07-16 21:09:43 +02:00
|
|
|
return err
|
|
|
|
}
|