85 lines
2.5 KiB
Go
85 lines
2.5 KiB
Go
|
|
package tenant
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (q *Queries) GetSettings(ctx context.Context) (Settings, error) {
|
||
|
|
rows, err := q.db.QueryContext(ctx, `SELECT key, value FROM site_settings`)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
defer rows.Close()
|
||
|
|
|
||
|
|
settings := make(Settings)
|
||
|
|
for rows.Next() {
|
||
|
|
var key, value string
|
||
|
|
if err := rows.Scan(&key, &value); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
settings[key] = value
|
||
|
|
}
|
||
|
|
return settings, rows.Err()
|
||
|
|
}
|
||
|
|
|
||
|
|
func (q *Queries) GetSetting(ctx context.Context, key string) (string, error) {
|
||
|
|
var value string
|
||
|
|
err := q.db.QueryRowContext(ctx, `SELECT value FROM site_settings WHERE key = ?`, key).Scan(&value)
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
return value, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (q *Queries) SetSetting(ctx context.Context, key, value string) error {
|
||
|
|
_, err := q.db.ExecContext(ctx, `INSERT INTO site_settings (key, value, updated_at)
|
||
|
|
VALUES (?, ?, CURRENT_TIMESTAMP)
|
||
|
|
ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = CURRENT_TIMESTAMP`, key, value)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (q *Queries) SetSettings(ctx context.Context, settings Settings) error {
|
||
|
|
for key, value := range settings {
|
||
|
|
if err := q.SetSetting(ctx, key, value); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (q *Queries) DeleteSetting(ctx context.Context, key string) error {
|
||
|
|
_, err := q.db.ExecContext(ctx, `DELETE FROM site_settings WHERE key = ?`, key)
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
var defaultSettings = map[string]string{
|
||
|
|
"comments_enabled": "true",
|
||
|
|
"reactions_enabled": "true",
|
||
|
|
"reaction_mode": "upvote",
|
||
|
|
"reaction_emojis": "👍,❤️,😂,😮,😢",
|
||
|
|
"upvote_icon": "👍",
|
||
|
|
"reactions_require_auth": "false",
|
||
|
|
}
|
||
|
|
|
||
|
|
func (q *Queries) GetSettingWithDefault(ctx context.Context, key string) string {
|
||
|
|
value, err := q.GetSetting(ctx, key)
|
||
|
|
if err != nil || value == "" {
|
||
|
|
if def, ok := defaultSettings[key]; ok {
|
||
|
|
return def
|
||
|
|
}
|
||
|
|
return ""
|
||
|
|
}
|
||
|
|
return value
|
||
|
|
}
|
||
|
|
|
||
|
|
func (q *Queries) GetInteractionConfig(ctx context.Context) map[string]any {
|
||
|
|
return map[string]any{
|
||
|
|
"comments_enabled": q.GetSettingWithDefault(ctx, "comments_enabled") == "true",
|
||
|
|
"reactions_enabled": q.GetSettingWithDefault(ctx, "reactions_enabled") == "true",
|
||
|
|
"reaction_mode": q.GetSettingWithDefault(ctx, "reaction_mode"),
|
||
|
|
"reaction_emojis": q.GetSettingWithDefault(ctx, "reaction_emojis"),
|
||
|
|
"upvote_icon": q.GetSettingWithDefault(ctx, "upvote_icon"),
|
||
|
|
"reactions_require_auth": q.GetSettingWithDefault(ctx, "reactions_require_auth") == "true",
|
||
|
|
}
|
||
|
|
}
|