rename account data keys, rearrange code

This commit is contained in:
Aine
2022-08-30 14:42:07 +03:00
parent 5ed3a53223
commit 627e090afd
4 changed files with 47 additions and 55 deletions

View File

@@ -1,22 +1,5 @@
package bot package bot
import (
"strings"
"maunium.net/go/mautrix/id"
)
// account data keys
const (
messagekey = "cc.etke.postmoogle.message"
)
// event keys
const (
eventMessageIDkey = "cc.etke.postmoogle.messageID"
eventInReplyToKey = "cc.etke.postmoogle.inReplyTo"
)
var migrations = []string{} var migrations = []string{}
func (b *Bot) migrate() error { func (b *Bot) migrate() error {
@@ -68,31 +51,3 @@ func (b *Bot) syncRooms() error {
return nil return nil
} }
func (b *Bot) getThreadID(roomID id.RoomID, messageID string) id.EventID {
key := messagekey + "." + messageID
data := map[string]id.EventID{}
err := b.lp.GetClient().GetRoomAccountData(roomID, key, &data)
if err != nil {
if !strings.Contains(err.Error(), "M_NOT_FOUND") {
b.log.Error("cannot retrieve account data %s: %v", key, err)
return ""
}
}
return data["eventID"]
}
func (b *Bot) setThreadID(roomID id.RoomID, messageID string, eventID id.EventID) {
key := messagekey + "." + messageID
data := map[string]id.EventID{
"eventID": eventID,
}
err := b.lp.GetClient().SetRoomAccountData(roomID, key, data)
if err != nil {
if !strings.Contains(err.Error(), "M_NOT_FOUND") {
b.log.Error("cannot save account data %s: %v", key, err)
}
}
}

View File

@@ -12,6 +12,15 @@ import (
"gitlab.com/etke.cc/postmoogle/utils" "gitlab.com/etke.cc/postmoogle/utils"
) )
// account data key
const acMessagePrefix = "cc.etke.postmoogle.message"
// event keys
const (
eventMessageIDkey = "cc.etke.postmoogle.messageID"
eventInReplyToKey = "cc.etke.postmoogle.inReplyTo"
)
func email2content(email *utils.Email, cfg roomsettings, threadID id.EventID) *event.Content { func email2content(email *utils.Email, cfg roomsettings, threadID id.EventID) *event.Content {
var text strings.Builder var text strings.Builder
if !cfg.NoSender() { if !cfg.NoSender() {
@@ -115,3 +124,31 @@ func (b *Bot) sendFiles(ctx context.Context, roomID id.RoomID, files []*utils.Fi
} }
} }
} }
func (b *Bot) getThreadID(roomID id.RoomID, messageID string) id.EventID {
key := acMessagePrefix + "." + messageID
data := map[string]id.EventID{}
err := b.lp.GetClient().GetRoomAccountData(roomID, key, &data)
if err != nil {
if !strings.Contains(err.Error(), "M_NOT_FOUND") {
b.log.Error("cannot retrieve account data %s: %v", key, err)
return ""
}
}
return data["eventID"]
}
func (b *Bot) setThreadID(roomID id.RoomID, messageID string, eventID id.EventID) {
key := acMessagePrefix + "." + messageID
data := map[string]id.EventID{
"eventID": eventID,
}
err := b.lp.GetClient().SetRoomAccountData(roomID, key, data)
if err != nil {
if !strings.Contains(err.Error(), "M_NOT_FOUND") {
b.log.Error("cannot save account data %s: %v", key, err)
}
}
}

View File

@@ -7,7 +7,7 @@ import (
) )
// account data key // account data key
const botsettingskey = "cc.etke.postmoogle.config" const acBotSettingsKey = "cc.etke.postmoogle.config"
// bot options keys // bot options keys
const ( const (
@@ -48,26 +48,26 @@ func (b *Bot) migrateBotSettings(users []string) error {
} }
func (b *Bot) getBotSettings() botsettings { func (b *Bot) getBotSettings() botsettings {
cfg := b.botcfg.Get(botsettingskey) cfg := b.botcfg.Get(acBotSettingsKey)
if cfg != nil { if cfg != nil {
return cfg return cfg
} }
config := botsettings{} config := botsettings{}
err := b.lp.GetClient().GetAccountData(botsettingskey, &config) err := b.lp.GetClient().GetAccountData(acBotSettingsKey, &config)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "M_NOT_FOUND") { if strings.Contains(err.Error(), "M_NOT_FOUND") {
err = nil err = nil
} }
b.log.Error("cannot get bot settings: %v", utils.UnwrapError(err)) b.log.Error("cannot get bot settings: %v", utils.UnwrapError(err))
} else { } else {
b.botcfg.Set(botsettingskey, config) b.botcfg.Set(acBotSettingsKey, config)
} }
return config return config
} }
func (b *Bot) setBotSettings(cfg botsettings) error { func (b *Bot) setBotSettings(cfg botsettings) error {
b.botcfg.Set(botsettingskey, cfg) b.botcfg.Set(acBotSettingsKey, cfg)
return utils.UnwrapError(b.lp.GetClient().SetAccountData(botsettingskey, cfg)) return utils.UnwrapError(b.lp.GetClient().SetAccountData(acBotSettingsKey, cfg))
} }

View File

@@ -10,7 +10,7 @@ import (
) )
// account data key // account data key
const roomsettingskey = "cc.etke.postmoogle.settings" const acRoomSettingsKey = "cc.etke.postmoogle.settings"
// option keys // option keys
const ( const (
@@ -73,7 +73,7 @@ func (s roomsettings) NoFiles() bool {
// TODO: remove after migration // TODO: remove after migration
func (b *Bot) migrateSettings(roomID id.RoomID) { func (b *Bot) migrateSettings(roomID id.RoomID) {
var config settingsOld var config settingsOld
err := b.lp.GetClient().GetRoomAccountData(roomID, roomsettingskey, &config) err := b.lp.GetClient().GetRoomAccountData(roomID, acRoomSettingsKey, &config)
if err != nil { if err != nil {
// any error = no need to migrate // any error = no need to migrate
return return
@@ -100,7 +100,7 @@ func (b *Bot) getRoomSettings(roomID id.RoomID) (roomsettings, error) {
} }
config := roomsettings{} config := roomsettings{}
err := b.lp.GetClient().GetRoomAccountData(roomID, roomsettingskey, &config) err := b.lp.GetClient().GetRoomAccountData(roomID, acRoomSettingsKey, &config)
if err != nil { if err != nil {
if strings.Contains(err.Error(), "M_NOT_FOUND") { if strings.Contains(err.Error(), "M_NOT_FOUND") {
// Suppress `M_NOT_FOUND (HTTP 404): Room account data not found` errors. // Suppress `M_NOT_FOUND (HTTP 404): Room account data not found` errors.
@@ -117,5 +117,5 @@ func (b *Bot) getRoomSettings(roomID id.RoomID) (roomsettings, error) {
func (b *Bot) setRoomSettings(roomID id.RoomID, cfg roomsettings) error { func (b *Bot) setRoomSettings(roomID id.RoomID, cfg roomsettings) error {
b.cfg.Set(roomID.String(), cfg) b.cfg.Set(roomID.String(), cfg)
return utils.UnwrapError(b.lp.GetClient().SetRoomAccountData(roomID, roomsettingskey, cfg)) return utils.UnwrapError(b.lp.GetClient().SetRoomAccountData(roomID, acRoomSettingsKey, cfg))
} }