threads
This commit is contained in:
58
bot/bot.go
58
bot/bot.go
@@ -90,8 +90,8 @@ func (b *Bot) Start() error {
|
||||
}
|
||||
|
||||
// Send email to matrix room
|
||||
func (b *Bot) Send(ctx context.Context, from, to, subject, plaintext, html string, files []*utils.File) error {
|
||||
roomID, ok := b.GetMapping(ctx, utils.Mailbox(to))
|
||||
func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
|
||||
roomID, ok := b.GetMapping(ctx, utils.Mailbox(email.To))
|
||||
if !ok {
|
||||
return errors.New("room not found")
|
||||
}
|
||||
@@ -104,26 +104,56 @@ func (b *Bot) Send(ctx context.Context, from, to, subject, plaintext, html strin
|
||||
var text strings.Builder
|
||||
if !settings.NoSender() {
|
||||
text.WriteString("From: ")
|
||||
text.WriteString(from)
|
||||
text.WriteString(email.From)
|
||||
text.WriteString("\n\n")
|
||||
}
|
||||
if !settings.NoSubject() {
|
||||
text.WriteString("# ")
|
||||
text.WriteString(subject)
|
||||
text.WriteString(email.Subject)
|
||||
text.WriteString("\n\n")
|
||||
}
|
||||
if html != "" {
|
||||
text.WriteString(format.HTMLToMarkdown(html))
|
||||
if email.HTML != "" {
|
||||
text.WriteString(format.HTMLToMarkdown(email.HTML))
|
||||
} else {
|
||||
text.WriteString(plaintext)
|
||||
text.WriteString(email.Text)
|
||||
}
|
||||
|
||||
content := format.RenderMarkdown(text.String(), true, true)
|
||||
_, err = b.lp.Send(roomID, content)
|
||||
if err != nil {
|
||||
return err
|
||||
contentParsed := format.RenderMarkdown(text.String(), true, true)
|
||||
|
||||
var threadID id.EventID
|
||||
if email.InReplyTo != "" {
|
||||
threadID = b.getThreadID(ctx, roomID, email.InReplyTo)
|
||||
if threadID != "" {
|
||||
contentParsed.SetRelatesTo(&event.RelatesTo{
|
||||
Type: event.RelThread,
|
||||
EventID: threadID,
|
||||
})
|
||||
b.setThreadID(ctx, roomID, email.MessageID, threadID)
|
||||
}
|
||||
}
|
||||
|
||||
content := &event.Content{
|
||||
Raw: map[string]interface{}{
|
||||
eventMessageIDkey: email.MessageID,
|
||||
eventInReplyToKey: email.InReplyTo,
|
||||
},
|
||||
Parsed: contentParsed,
|
||||
}
|
||||
eventID, serr := b.lp.Send(roomID, content)
|
||||
if serr != nil {
|
||||
return serr
|
||||
}
|
||||
|
||||
if threadID == "" {
|
||||
b.setThreadID(ctx, roomID, email.MessageID, eventID)
|
||||
threadID = eventID
|
||||
}
|
||||
|
||||
b.sendFiles(ctx, roomID, email.Files, threadID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bot) sendFiles(ctx context.Context, roomID id.RoomID, files []*utils.File, threadID id.EventID) {
|
||||
for _, file := range files {
|
||||
req := file.Convert()
|
||||
resp, err := b.lp.GetClient().UploadMedia(req)
|
||||
@@ -135,13 +165,15 @@ func (b *Bot) Send(ctx context.Context, from, to, subject, plaintext, html strin
|
||||
MsgType: event.MsgFile,
|
||||
Body: req.FileName,
|
||||
URL: resp.ContentURI.CUString(),
|
||||
RelatesTo: &event.RelatesTo{
|
||||
Type: event.RelThread,
|
||||
EventID: threadID,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
b.Error(ctx, roomID, "cannot send uploaded file %s: %v", req.FileName, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetMappings returns mapping of mailbox = room
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
116
bot/settings.go
Normal file
116
bot/settings.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
||||
"gitlab.com/etke.cc/postmoogle/utils"
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// 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"))
|
||||
defer span.Finish()
|
||||
|
||||
config := settings{}
|
||||
err := b.lp.GetClient().GetRoomAccountData(roomID, settingskey, &config)
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
return config, err
|
||||
}
|
||||
|
||||
func (b *Bot) setSettings(ctx context.Context, roomID id.RoomID, cfg settings) error {
|
||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("setSettings"))
|
||||
defer span.Finish()
|
||||
|
||||
return b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg)
|
||||
}
|
||||
Reference in New Issue
Block a user