Skip to content

Commit

Permalink
implement SetPrivacyMode, rework -mode flag to use
Browse files Browse the repository at this point in the history
  • Loading branch information
cblgh committed Apr 6, 2021
1 parent 9b4b1a2 commit bd7a4e5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 30 deletions.
32 changes: 8 additions & 24 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var (
logToFile string
repoDir string

config roomdb.RoomConfig
privacyMode = roomdb.ModeUnknown

// helper
log kitlog.Logger
Expand Down Expand Up @@ -84,23 +84,9 @@ func checkAndLog(err error) {
}
}

type Config struct {
// open, community, restricted
privacyMode roomdb.PrivacyMode
}

func (c Config) GetPrivacyMode(ctx context.Context) (roomdb.PrivacyMode, error) {
err := c.privacyMode.IsValid()
if err != nil {
return roomdb.ModeUnknown, err
}
return c.privacyMode, nil
}

func initFlags() {
u, err := user.Current()
checkFatal(err)
config = Config{privacyMode: roomdb.ModeCommunity} // set default privacy mode to community

flag.StringVar(&appKey, "shscap", "1KHLiKZvAvjbY1ziZEHMXawbCEIM6qwjCDm3VYRan/s=", "secret-handshake app-key (or capability)")

Expand All @@ -118,15 +104,13 @@ 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()
pm := roomdb.ParsePrivacyMode(val)
err := pm.IsValid()
if err != nil {
return fmt.Errorf("%s, valid values are open, community, restricted", err)
}
config = Config{privacyMode: privacyMode}
privacyMode = pm
return nil
})

Expand Down Expand Up @@ -237,6 +221,10 @@ func runroomsrv() error {
}

bridge := signinwithssb.NewSignalBridge()
// the privacy mode flag was passed => update it in the database
if privacyMode != roomdb.ModeUnknown {
db.Config.SetPrivacyMode(ctx, privacyMode)
}

// create the shs+muxrpc server
roomsrv, err := mksrv.New(
Expand Down Expand Up @@ -293,12 +281,8 @@ func runroomsrv() error {
handlers.Databases{
Aliases: db.Aliases,
AuthFallback: db.AuthFallback,
<<<<<<< HEAD
AuthWithSSB: db.AuthWithSSB,
Config: config,
=======
Config: db.Config,
>>>>>>> a66b343 (persist privacy mode in sqlite :>)
DeniedKeys: db.DeniedKeys,
Invites: db.Invites,
Notices: db.Notices,
Expand Down
1 change: 1 addition & 0 deletions roomdb/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

type RoomConfig interface {
GetPrivacyMode(context.Context) (PrivacyMode, error)
SetPrivacyMode(context.Context, PrivacyMode) error
}

// AuthFallbackService allows password authentication which might be helpful for scenarios
Expand Down
42 changes: 40 additions & 2 deletions roomdb/sqlite/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import (

"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb/sqlite/models"
"github.com/volatiletech/sqlboiler/v4/boil"
)

// 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)
var _ roomdb.RoomConfig = (*Config)(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
}
Expand All @@ -43,3 +44,40 @@ func (c Config) GetPrivacyMode(ctx context.Context) (roomdb.PrivacyMode, error)

return pm, nil
}

func (c Config) SetPrivacyMode(ctx context.Context, pm roomdb.PrivacyMode) error {
fmt.Println("setting privacy mode!!")
// make sure the privacy mode is an ok value
err := pm.IsValid()
if err != nil {
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)
if err != nil {
return err
}

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

return nil
})

if err != nil {
return err
}

return nil // alles gut!!
}
2 changes: 1 addition & 1 deletion web/handlers/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (h aliasHandler) resolve(rw http.ResponseWriter, req *http.Request) {
ar = newAliasHTMLResponder(h.r, rw, req)
}

ar.UpdateRoomInfo(a.roomEndpoint)
ar.UpdateRoomInfo(h.roomEndpoint)

pm, err := h.config.GetPrivacyMode(req.Context())
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions web/handlers/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,9 @@ func New(
m.Get(router.CompleteAliasResolve).HandlerFunc(ah.resolve)

var ih = inviteHandler{
<<<<<<< HEAD
render: r,

=======
config: dbs.Config,
>>>>>>> adb0981 (get started with privacy modes, parse -mode flag)
invites: dbs.Invites,

networkInfo: netInfo,
Expand Down

0 comments on commit bd7a4e5

Please sign in to comment.