adjust initBotUsers()

This commit is contained in:
Aine
2022-08-30 18:29:16 +03:00
parent 0c74ae02fb
commit 84b1900dbb
2 changed files with 24 additions and 24 deletions

View File

@@ -37,7 +37,7 @@ func New(
log *logger.Logger, log *logger.Logger,
prefix string, prefix string,
domain string, domain string,
users []string, envUsers []string,
admins []string, admins []string,
) (*Bot, error) { ) (*Bot, error) {
b := &Bot{ b := &Bot{
@@ -50,15 +50,22 @@ func New(
lp: lp, lp: lp,
mu: map[id.RoomID]*sync.Mutex{}, mu: map[id.RoomID]*sync.Mutex{},
} }
err := b.initBotUsers(users) users, err := b.initBotUsers(envUsers)
if err != nil { if err != nil {
return nil, err return nil, err
} }
allowedUsers, uerr := parseMXIDpatterns(users, "")
if uerr != nil {
return nil, uerr
}
b.allowedUsers = allowedUsers
allowedAdmins, aerr := parseMXIDpatterns(admins, "") allowedAdmins, aerr := parseMXIDpatterns(admins, "")
if aerr != nil { if aerr != nil {
return nil, aerr return nil, aerr
} }
b.allowedAdmins = allowedAdmins b.allowedAdmins = allowedAdmins
b.commands = b.buildCommandList() b.commands = b.buildCommandList()
return b, nil return b, nil

View File

@@ -35,33 +35,26 @@ func (s botSettings) Users() []string {
return []string{value} return []string{value}
} }
func (b *Bot) initBotUsers(users []string) error { func (b *Bot) initBotUsers(envUsers []string) ([]string, error) {
config := b.getBotSettings()
cfgUsers := config.Users()
if len(cfgUsers) > 0 {
// already migrated
return cfgUsers, nil
}
if len(envUsers) == 0 {
_, homeserver, err := b.lp.GetClient().UserID.Parse() _, homeserver, err := b.lp.GetClient().UserID.Parse()
if err != nil { if err != nil {
return err return nil, err
} }
config := b.getBotSettings() config.Set(botOptionUsers, "@*:"+homeserver)
oldUsers := config.Get(botOptionUsers) } else {
// TODO: remove after migration // Initialize from environment variable
if len(users) > 0 && oldUsers == "" { // TODO: remove this migration later and always initialize to `"@*:"+homeserver`
_, err := parseMXIDpatterns(users, "@*:"+homeserver) config.Set(botOptionUsers, strings.Join(envUsers, " "))
if err != nil {
return err
}
config.Set(botOptionUsers, strings.Join(users, " "))
} }
allowedUsers, uerr := parseMXIDpatterns(config.Users(), "@*:"+homeserver) return config.Users(), b.setBotSettings(config)
if uerr != nil {
return uerr
}
b.allowedUsers = allowedUsers
if oldUsers != config.Get(botOptionUsers) {
return b.setBotSettings(config)
}
return nil
} }
func (b *Bot) getBotSettings() botSettings { func (b *Bot) getBotSettings() botSettings {