diff bot and room settings

This commit is contained in:
Aine
2022-08-30 14:37:19 +03:00
parent f97ebb604a
commit 5ed3a53223
9 changed files with 127 additions and 102 deletions

View File

@@ -4,15 +4,11 @@ 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"
botconfigkey = "cc.etke.postmoogle.config"
messagekey = "cc.etke.postmoogle.message"
)
// event keys
@@ -21,19 +17,6 @@ const (
eventInReplyToKey = "cc.etke.postmoogle.inReplyTo"
)
// option keys
const (
optionOwner = "owner"
optionMailbox = "mailbox"
optionNoSender = "nosender"
optionNoSubject = "nosubject"
optionNoHTML = "nohtml"
optionNoThreads = "nothreads"
optionNoFiles = "nofiles"
botOptionUsers = "users"
)
var migrations = []string{}
func (b *Bot) migrate() error {
@@ -72,7 +55,7 @@ func (b *Bot) syncRooms() error {
}
for _, roomID := range resp.JoinedRooms {
b.migrateSettings(roomID)
cfg, serr := b.getSettings(roomID)
cfg, serr := b.getRoomSettings(roomID)
if serr != nil {
b.log.Warn("cannot get %s settings: %v", roomID, err)
continue
@@ -113,44 +96,3 @@ 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))
}