manage users in runtime, closes #16
This commit is contained in:
50
bot/data.go
50
bot/data.go
@@ -4,12 +4,15 @@ import (
|
||||
"strings"
|
||||
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
||||
"gitlab.com/etke.cc/postmoogle/utils"
|
||||
)
|
||||
|
||||
// account data keys
|
||||
const (
|
||||
messagekey = "cc.etke.postmoogle.message"
|
||||
settingskey = "cc.etke.postmoogle.settings"
|
||||
messagekey = "cc.etke.postmoogle.message"
|
||||
settingskey = "cc.etke.postmoogle.settings"
|
||||
botconfigkey = "cc.etke.postmoogle.config"
|
||||
)
|
||||
|
||||
// event keys
|
||||
@@ -27,6 +30,8 @@ const (
|
||||
optionNoHTML = "nohtml"
|
||||
optionNoThreads = "nothreads"
|
||||
optionNoFiles = "nofiles"
|
||||
|
||||
botOptionUsers = "users"
|
||||
)
|
||||
|
||||
var migrations = []string{}
|
||||
@@ -108,3 +113,44 @@ func (b *Bot) setThreadID(roomID id.RoomID, messageID string, eventID id.EventID
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: remove after migration
|
||||
func (b *Bot) migrateBotSettings(users []string) error {
|
||||
config := b.getBotSettings()
|
||||
cfgUsers := config.Users()
|
||||
if len(users) > 0 && len(cfgUsers) == 0 {
|
||||
_, err := parseMXIDpatterns(users, "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
config.Set(botOptionUsers, strings.Join(users, " "))
|
||||
return b.setBotSettings(config)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bot) getBotSettings() settings {
|
||||
cfg := b.cfg.Get(botconfigkey)
|
||||
if cfg != nil {
|
||||
return cfg
|
||||
}
|
||||
|
||||
config := settings{}
|
||||
err := b.lp.GetClient().GetAccountData(botconfigkey, &config)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "M_NOT_FOUND") {
|
||||
err = nil
|
||||
}
|
||||
b.log.Error("cannot get bot settings: %v", utils.UnwrapError(err))
|
||||
} else {
|
||||
b.cfg.Set(botconfigkey, config)
|
||||
}
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func (b *Bot) setBotSettings(cfg settings) error {
|
||||
b.cfg.Set(botconfigkey, cfg)
|
||||
return utils.UnwrapError(b.lp.GetClient().SetAccountData(botconfigkey, cfg))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user