Skip to content

Commit

Permalink
add default language admin ui functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
cblgh committed Apr 20, 2021
1 parent e941e10 commit c97b7d4
Show file tree
Hide file tree
Showing 15 changed files with 412 additions and 109 deletions.
2 changes: 2 additions & 0 deletions roomdb/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
type RoomConfig interface {
GetPrivacyMode(context.Context) (PrivacyMode, error)
SetPrivacyMode(context.Context, PrivacyMode) error
GetDefaultLanguage(context.Context) (string, error)
SetDefaultLanguage(context.Context, string) error
}

// AuthFallbackService allows password authentication which might be helpful for scenarios
Expand Down
155 changes: 155 additions & 0 deletions roomdb/mockdb/roomconfig.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions roomdb/sqlite/migrations/03-config.sql
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
-- +migrate Up
-- the configuration settings for this room, currently only privacy modes
-- the configuration settings for this room, currently privacy mode settings and the default translation for the room
CREATE TABLE config (
id integer NOT NULL PRIMARY KEY,
privacyMode integer NOT NULL, -- open, community, restricted
defaultLanguage TEXT NOT NULL, -- a language tag, e.g. en, sv, de

CHECK (id == 0) -- should only ever store one row
);

-- the config table will only ever contain one row: the rooms current settings
-- we update that row whenever the config changes.
-- to have something to update, we insert the first and only row at id 0
INSERT INTO config (id, privacyMode) VALUES (
0, -- the constant id we will query
2 -- community is the default mode unless overridden
INSERT INTO config (id, privacyMode, defaultLanguage) VALUES (
0, -- the constant id we will query
2, -- community is the default mode unless overridden
"en" -- english is the default language for all installs
);

-- +migrate Down
Expand Down
29 changes: 17 additions & 12 deletions roomdb/sqlite/models/config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 43 additions & 1 deletion roomdb/sqlite/roomconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func (c Config) SetPrivacyMode(ctx context.Context, pm roomdb.PrivacyMode) error
return err
}

// cblgh: a walkthrough of this step (again, now that i have some actual context) would be real good :)
err = transact(c.db, func(tx *sql.Tx) error {
// get the settings row
config, err := models.FindConfig(ctx, tx, configRowID)
Expand Down Expand Up @@ -68,3 +67,46 @@ func (c Config) SetPrivacyMode(ctx context.Context, pm roomdb.PrivacyMode) error

return nil // alles gut!!
}

// TODO: use proper language tag from "golang.org/x/text/language"?
func (c Config) GetDefaultLanguage(ctx context.Context) (string, error) {
config, err := models.FindConfig(ctx, c.db, configRowID)
if err != nil {
return "", err
}

return config.DefaultLanguage, nil
}

func (c Config) SetDefaultLanguage(ctx context.Context, langTag string) error {
if len(langTag) == 0 {
return fmt.Errorf("language tag cannot be empty")
}

err := transact(c.db, func(tx *sql.Tx) error {
// get the settings row
config, err := models.FindConfig(ctx, tx, configRowID)
if err != nil {
return err
}

// set the new language tag
config.DefaultLanguage = langTag
// issue update stmt
rowsAffected, err := config.Update(ctx, tx, boil.Infer())
if err != nil {
return err
}
if rowsAffected == 0 {
return fmt.Errorf("setting default language should have update the settings row, instead 0 rows were updated")
}

return nil
})

if err != nil {
return err
}

return nil // alles gut!!
}
5 changes: 4 additions & 1 deletion web/handlers/admin/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/ssb-ngi-pointer/go-ssb-room/web"
weberrors "github.com/ssb-ngi-pointer/go-ssb-room/web/errors"
"github.com/ssb-ngi-pointer/go-ssb-room/web/router"
"github.com/ssb-ngi-pointer/go-ssb-room/web/i18n"
)

// HTMLTemplates define the list of files the template system should load.
Expand Down Expand Up @@ -65,6 +66,7 @@ func Handler(
r *render.Renderer,
roomState *roomstate.Manager,
fh *weberrors.FlashHelper,
locHelper *i18n.Helper,
dbs Databases,
) http.Handler {
mux := &http.ServeMux{}
Expand All @@ -84,11 +86,12 @@ func Handler(
var sh = settingsHandler{
r: r,
urlTo: urlTo,

db: dbs.Config,
loc: locHelper,
}
mux.HandleFunc("/settings", r.HTML("admin/settings.tmpl", sh.overview))
mux.HandleFunc("/settings/set-privacy", sh.setPrivacy)
mux.HandleFunc("/settings/set-language", sh.setLanguage)

mux.HandleFunc("/menu", r.HTML("admin/menu.tmpl", func(w http.ResponseWriter, req *http.Request) (interface{}, error) {
return map[string]interface{}{}, nil
Expand Down
Loading

0 comments on commit c97b7d4

Please sign in to comment.