implement SetPrivacyMode, rework -mode flag to use

This commit is contained in:
cblgh 2021-03-31 15:58:42 +02:00
parent b0761783f9
commit 75c60dbb16
4 changed files with 50 additions and 27 deletions

View File

@ -54,7 +54,7 @@ var (
logToFile string
repoDir string
config roomdb.RoomConfig
privacyMode = roomdb.ModeUnknown
// helper
log kitlog.Logger
@ -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)")
@ -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
})
@ -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(
@ -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,

View File

@ -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

View File

@ -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
}
@ -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!!
}

View File

@ -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 {