Skip to content

Commit

Permalink
persist privacy mode in sqlite :>
Browse files Browse the repository at this point in the history
  • Loading branch information
cblgh committed Mar 31, 2021
1 parent 9905e11 commit bb4ed93
Show file tree
Hide file tree
Showing 7 changed files with 852 additions and 8 deletions.
8 changes: 5 additions & 3 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ func initFlags() {

flag.BoolVar(&flagPrintVersion, "version", false, "print version number and build date")

/* cblgh: TODO change this to use a db.Config.SetPrivacyMode function; guessing database is not loaded yet tho.
* maybe we remove this flag entirely / replace with a small cli tool? idk*/
flag.Func("mode", "the privacy mode (values: open, community, restricted) determining room access controls", func(val string) error {
privacyMode := roomdb.ParsePrivacyMode(val)
err := privacyMode.IsValid()
Expand Down Expand Up @@ -224,7 +226,7 @@ func runroomsrv() error {

r := repo.New(repoDir)

// open the sqlite version of the admindb
// open the sqlite version of the roomdb
db, err := sqlite.Open(r)
if err != nil {
return fmt.Errorf("failed to initiate database: %w", err)
Expand All @@ -234,7 +236,7 @@ func runroomsrv() error {
roomsrv, err := mksrv.New(
db.Members,
db.Aliases,
config,
db.Config,
httpsDomain,
opts...)
if err != nil {
Expand Down Expand Up @@ -279,7 +281,7 @@ func runroomsrv() error {
handlers.Databases{
Aliases: db.Aliases,
AuthFallback: db.AuthFallback,
Config: config,
Config: db.Config,
DeniedKeys: db.DeniedKeys,
Invites: db.Invites,
Notices: db.Notices,
Expand Down
45 changes: 45 additions & 0 deletions roomdb/sqlite/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: MIT

package sqlite

import (
"context"
"database/sql"
"fmt"

"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb/sqlite/models"
)

// cblgh: ask cryptix about the details of the syntax of the "compiler assertion" below
// why two parens? this does not look like a typical type assertion? e.g. <var>.(type)
// hm-maybe this is a type conversion, forcing "nil" to be a *Aliases?
var _ roomdb.AliasesService = (*Aliases)(nil)

// the database will only ever store one row, which contains all the room settings
const configRowID = 0

/* Config basically enables long-term memory for the server when it comes to storing settings. Currently, the only
* stored settings is the privacy mode of the room.
*/
type Config struct {
db *sql.DB
}

// cblgh questions:
// * is storing the entire config in a single row really ugly? ._.
func (c Config) GetPrivacyMode(ctx context.Context) (roomdb.PrivacyMode, error) {
config, err := models.FindConfig(ctx, c.db, configRowID)
if err != nil {
return roomdb.ModeUnknown, err
}

// use a type conversion to tell compiler the returned value is a roomdb.PrivacyMode
pm := (roomdb.PrivacyMode)(config.PrivacyMode)
err = pm.IsValid()
if err != nil {
return roomdb.ModeUnknown, err
}

return pm, nil
}
14 changes: 14 additions & 0 deletions roomdb/sqlite/migrations/03-config.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- +migrate Up
-- the configuration settings for this room, currently only privacy modes
CREATE TABLE config (
id integer NOT NULL PRIMARY KEY,
privacyMode integer NOT NULL -- open, community, restricted
);

-- 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
1 -- community is the default mode unless overridden
);
2 changes: 2 additions & 0 deletions roomdb/sqlite/models/boil_table_names.go

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

Loading

0 comments on commit bb4ed93

Please sign in to comment.