threads
This commit is contained in:
130
bot/data.go
130
bot/data.go
@@ -2,17 +2,25 @@ package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
||||
"gitlab.com/etke.cc/postmoogle/utils"
|
||||
)
|
||||
|
||||
const settingskey = "cc.etke.postmoogle.settings"
|
||||
// account data keys
|
||||
const (
|
||||
messagekey = "cc.etke.postmoogle.message"
|
||||
settingskey = "cc.etke.postmoogle.settings"
|
||||
)
|
||||
|
||||
// event keys
|
||||
const (
|
||||
eventMessageIDkey = "cc.etke.postmoogle.messageID"
|
||||
eventInReplyToKey = "cc.etke.postmoogle.inReplyTo"
|
||||
)
|
||||
|
||||
// option keys
|
||||
const (
|
||||
optionOwner = "owner"
|
||||
optionMailbox = "mailbox"
|
||||
@@ -22,62 +30,6 @@ const (
|
||||
|
||||
var migrations = []string{}
|
||||
|
||||
// settings of a room
|
||||
type settings map[string]string
|
||||
|
||||
// settingsOld of a room
|
||||
type settingsOld struct {
|
||||
Mailbox string
|
||||
Owner id.UserID
|
||||
NoSender bool
|
||||
}
|
||||
|
||||
// Allowed checks if change is allowed
|
||||
func (s settings) Allowed(noowner bool, userID id.UserID) bool {
|
||||
if noowner {
|
||||
return true
|
||||
}
|
||||
|
||||
owner := s.Owner()
|
||||
if owner == "" {
|
||||
return true
|
||||
}
|
||||
|
||||
return owner == userID.String()
|
||||
}
|
||||
|
||||
// Get option
|
||||
func (s settings) Get(key string) string {
|
||||
value := s[strings.ToLower(strings.TrimSpace(key))]
|
||||
|
||||
sanitizer, exists := sanitizers[key]
|
||||
if exists {
|
||||
return sanitizer(value)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (s settings) Mailbox() string {
|
||||
return s.Get(optionMailbox)
|
||||
}
|
||||
|
||||
func (s settings) Owner() string {
|
||||
return s.Get(optionOwner)
|
||||
}
|
||||
|
||||
func (s settings) NoSender() bool {
|
||||
return utils.Bool(s.Get(optionNoSender))
|
||||
}
|
||||
|
||||
func (s settings) NoSubject() bool {
|
||||
return utils.Bool(s.Get(optionNoSubject))
|
||||
}
|
||||
|
||||
// Set option
|
||||
func (s settings) Set(key, value string) {
|
||||
s[strings.ToLower(strings.TrimSpace(key))] = value
|
||||
}
|
||||
|
||||
func (b *Bot) migrate() error {
|
||||
b.log.Debug("migrating database...")
|
||||
tx, beginErr := b.lp.GetDB().Begin()
|
||||
@@ -134,50 +86,36 @@ func (b *Bot) syncRooms(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TODO: remove after migration
|
||||
func (b *Bot) migrateSettings(ctx context.Context, roomID id.RoomID) {
|
||||
var config settingsOld
|
||||
err := b.lp.GetClient().GetRoomAccountData(roomID, settingskey, &config)
|
||||
if err != nil {
|
||||
// any error = no need to migrate
|
||||
return
|
||||
}
|
||||
|
||||
if config.Mailbox == "" {
|
||||
return
|
||||
}
|
||||
cfg := settings{}
|
||||
cfg.Set(optionMailbox, config.Mailbox)
|
||||
cfg.Set(optionOwner, config.Owner.String())
|
||||
cfg.Set(optionNoSender, strconv.FormatBool(config.NoSender))
|
||||
|
||||
err = b.setSettings(ctx, roomID, cfg)
|
||||
if err != nil {
|
||||
b.log.Error("cannot migrate settings: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) getSettings(ctx context.Context, roomID id.RoomID) (settings, error) {
|
||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("getSettings"))
|
||||
func (b *Bot) getThreadID(ctx context.Context, roomID id.RoomID, messageID string) id.EventID {
|
||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("getThreadID"))
|
||||
defer span.Finish()
|
||||
|
||||
config := settings{}
|
||||
err := b.lp.GetClient().GetRoomAccountData(roomID, settingskey, &config)
|
||||
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") {
|
||||
// Suppress `M_NOT_FOUND (HTTP 404): Room account data not found` errors.
|
||||
// Until some settings are explicitly set, we don't store any.
|
||||
// In such cases, just return a default (empty) settings object.
|
||||
err = nil
|
||||
if !strings.Contains(err.Error(), "M_NOT_FOUND") {
|
||||
b.log.Error("cannot retrieve account data %s: %v", key, err)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
return config, err
|
||||
return data["eventID"]
|
||||
}
|
||||
|
||||
func (b *Bot) setSettings(ctx context.Context, roomID id.RoomID, cfg settings) error {
|
||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("setSettings"))
|
||||
func (b *Bot) setThreadID(ctx context.Context, roomID id.RoomID, messageID string, eventID id.EventID) {
|
||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("setThreadID"))
|
||||
defer span.Finish()
|
||||
|
||||
return b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg)
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user