refactor to context, remove sentry spans
This commit is contained in:
@@ -39,7 +39,6 @@ env vars
|
||||
* **POSTMOOGLE_FEDERATION** - allow usage of Postmoogle by users from others homeservers
|
||||
* **POSTMOOGLE_NOENCRYPTION** - disable encryption support
|
||||
* **POSTMOOGLE_SENTRY_DSN** - sentry DSN
|
||||
* **POSTMOOGLE_SENTRY_RATE** - sentry sample rate, from 0 to 100 (default: 20)
|
||||
* **POSTMOOGLE_LOGLEVEL** - log level
|
||||
* **POSTMOOGLE_DB_DSN** - database connection string
|
||||
* **POSTMOOGLE_DB_DIALECT** - database dialect (postgres, sqlite3)
|
||||
|
||||
29
bot/bot.go
29
bot/bot.go
@@ -47,11 +47,7 @@ func New(lp *linkpearl.Linkpearl, log *logger.Logger, prefix, domain string, noo
|
||||
func (b *Bot) Error(ctx context.Context, roomID id.RoomID, message string, args ...interface{}) {
|
||||
b.log.Error(message, args...)
|
||||
|
||||
if sentry.HasHubOnContext(ctx) {
|
||||
sentry.GetHubFromContext(ctx).CaptureException(fmt.Errorf(message, args...))
|
||||
} else {
|
||||
sentry.CaptureException(fmt.Errorf(message, args...))
|
||||
}
|
||||
sentry.GetHubFromContext(ctx).CaptureException(fmt.Errorf(message, args...))
|
||||
if roomID != "" {
|
||||
// nolint // if something goes wrong here nobody can help...
|
||||
b.lp.Send(roomID, &event.MessageEventContent{
|
||||
@@ -67,11 +63,7 @@ func (b *Bot) Notice(ctx context.Context, roomID id.RoomID, message string) {
|
||||
content.MsgType = event.MsgNotice
|
||||
_, err := b.lp.Send(roomID, &content)
|
||||
if err != nil {
|
||||
if sentry.HasHubOnContext(ctx) {
|
||||
sentry.GetHubFromContext(ctx).CaptureException(err)
|
||||
} else {
|
||||
sentry.CaptureException(err)
|
||||
}
|
||||
sentry.GetHubFromContext(ctx).CaptureException(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,8 +72,7 @@ func (b *Bot) Start() error {
|
||||
if err := b.migrate(); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx := sentry.SetHubOnContext(context.Background(), sentry.CurrentHub().Clone())
|
||||
if err := b.syncRooms(ctx); err != nil {
|
||||
if err := b.syncRooms(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -92,12 +83,12 @@ func (b *Bot) Start() error {
|
||||
|
||||
// Send email to matrix room
|
||||
func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
|
||||
roomID, ok := b.GetMapping(ctx, utils.Mailbox(email.To))
|
||||
roomID, ok := b.GetMapping(utils.Mailbox(email.To))
|
||||
if !ok {
|
||||
return errors.New("room not found")
|
||||
}
|
||||
|
||||
settings, err := b.getSettings(ctx, roomID)
|
||||
settings, err := b.getSettings(roomID)
|
||||
if err != nil {
|
||||
b.Error(ctx, roomID, "cannot get settings: %v", err)
|
||||
}
|
||||
@@ -123,13 +114,13 @@ func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
|
||||
|
||||
var threadID id.EventID
|
||||
if email.InReplyTo != "" {
|
||||
threadID = b.getThreadID(ctx, roomID, email.InReplyTo)
|
||||
threadID = b.getThreadID(roomID, email.InReplyTo)
|
||||
if threadID != "" {
|
||||
contentParsed.SetRelatesTo(&event.RelatesTo{
|
||||
Type: event.RelThread,
|
||||
EventID: threadID,
|
||||
})
|
||||
b.setThreadID(ctx, roomID, email.MessageID, threadID)
|
||||
b.setThreadID(roomID, email.MessageID, threadID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,7 +137,7 @@ func (b *Bot) Send(ctx context.Context, email *utils.Email) error {
|
||||
}
|
||||
|
||||
if threadID == "" {
|
||||
b.setThreadID(ctx, roomID, email.MessageID, eventID)
|
||||
b.setThreadID(roomID, email.MessageID, eventID)
|
||||
threadID = eventID
|
||||
}
|
||||
|
||||
@@ -178,9 +169,9 @@ func (b *Bot) sendFiles(ctx context.Context, roomID id.RoomID, files []*utils.Fi
|
||||
}
|
||||
|
||||
// GetMappings returns mapping of mailbox = room
|
||||
func (b *Bot) GetMapping(ctx context.Context, mailbox string) (id.RoomID, bool) {
|
||||
func (b *Bot) GetMapping(mailbox string) (id.RoomID, bool) {
|
||||
if len(b.rooms) == 0 {
|
||||
err := b.syncRooms(ctx)
|
||||
err := b.syncRooms()
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
||||
@@ -89,9 +88,9 @@ func (b *Bot) handleCommand(ctx context.Context, evt *event.Event, command []str
|
||||
case "help":
|
||||
b.sendHelp(ctx, evt.RoomID)
|
||||
case "stop":
|
||||
b.runStop(ctx, evt)
|
||||
b.runStop(ctx)
|
||||
default:
|
||||
b.handleOption(ctx, evt, command)
|
||||
b.handleOption(ctx, command)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,9 +109,6 @@ func (b *Bot) parseCommand(message string) []string {
|
||||
}
|
||||
|
||||
func (b *Bot) sendIntroduction(ctx context.Context, roomID id.RoomID) {
|
||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("sendIntroduction"))
|
||||
defer span.Finish()
|
||||
|
||||
var msg strings.Builder
|
||||
msg.WriteString("Hello!\n\n")
|
||||
msg.WriteString("This is Postmoogle - a bot that bridges Email to Matrix.\n\n")
|
||||
@@ -130,9 +126,6 @@ func (b *Bot) sendIntroduction(ctx context.Context, roomID id.RoomID) {
|
||||
}
|
||||
|
||||
func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
|
||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("sendHelp"))
|
||||
defer span.Finish()
|
||||
|
||||
var msg strings.Builder
|
||||
msg.WriteString("The following commands are supported:\n\n")
|
||||
for _, command := range commands {
|
||||
@@ -142,24 +135,22 @@ func (b *Bot) sendHelp(ctx context.Context, roomID id.RoomID) {
|
||||
b.Notice(ctx, roomID, msg.String())
|
||||
}
|
||||
|
||||
func (b *Bot) runStop(ctx context.Context, evt *event.Event) {
|
||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("runStop"))
|
||||
defer span.Finish()
|
||||
|
||||
cfg, err := b.getSettings(span.Context(), evt.RoomID)
|
||||
func (b *Bot) runStop(ctx context.Context) {
|
||||
evt := eventFromContext(ctx)
|
||||
cfg, err := b.getSettings(evt.RoomID)
|
||||
if err != nil {
|
||||
b.Error(span.Context(), evt.RoomID, "failed to retrieve settings: %v", err)
|
||||
b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if !cfg.Allowed(b.noowner, evt.Sender) {
|
||||
b.Notice(span.Context(), evt.RoomID, "you don't have permission to do that")
|
||||
b.Notice(ctx, evt.RoomID, "you don't have permission to do that")
|
||||
return
|
||||
}
|
||||
|
||||
mailbox := cfg.Get(optionMailbox)
|
||||
if mailbox == "" {
|
||||
b.Notice(span.Context(), evt.RoomID, "that room is not configured yet")
|
||||
b.Notice(ctx, evt.RoomID, "that room is not configured yet")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -167,37 +158,36 @@ func (b *Bot) runStop(ctx context.Context, evt *event.Event) {
|
||||
delete(b.rooms, mailbox)
|
||||
b.roomsmu.Unlock()
|
||||
|
||||
err = b.setSettings(span.Context(), evt.RoomID, settings{})
|
||||
err = b.setSettings(evt.RoomID, settings{})
|
||||
if err != nil {
|
||||
b.Error(span.Context(), evt.RoomID, "cannot update settings: %v", err)
|
||||
b.Error(ctx, evt.RoomID, "cannot update settings: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
b.Notice(span.Context(), evt.RoomID, "mailbox has been disabled")
|
||||
b.Notice(ctx, evt.RoomID, "mailbox has been disabled")
|
||||
}
|
||||
|
||||
func (b *Bot) handleOption(ctx context.Context, evt *event.Event, command []string) {
|
||||
func (b *Bot) handleOption(ctx context.Context, command []string) {
|
||||
if len(command) == 1 {
|
||||
b.getOption(ctx, evt, command[0])
|
||||
b.getOption(ctx, command[0])
|
||||
return
|
||||
}
|
||||
b.setOption(ctx, evt, command[0], command[1])
|
||||
b.setOption(ctx, command[0], command[1])
|
||||
}
|
||||
|
||||
func (b *Bot) getOption(ctx context.Context, evt *event.Event, name string) {
|
||||
func (b *Bot) getOption(ctx context.Context, name string) {
|
||||
msg := "`%s` of this room is %s"
|
||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("getOption"))
|
||||
defer span.Finish()
|
||||
|
||||
cfg, err := b.getSettings(span.Context(), evt.RoomID)
|
||||
evt := eventFromContext(ctx)
|
||||
cfg, err := b.getSettings(evt.RoomID)
|
||||
if err != nil {
|
||||
b.Error(span.Context(), evt.RoomID, "failed to retrieve settings: %v", err)
|
||||
b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
value := cfg.Get(name)
|
||||
if value == "" {
|
||||
b.Notice(span.Context(), evt.RoomID, fmt.Sprintf("`%s` is not set", name))
|
||||
b.Notice(ctx, evt.RoomID, fmt.Sprintf("`%s` is not set", name))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -205,12 +195,10 @@ func (b *Bot) getOption(ctx context.Context, evt *event.Event, name string) {
|
||||
msg = msg + "@" + b.domain
|
||||
}
|
||||
|
||||
b.Notice(span.Context(), evt.RoomID, fmt.Sprintf(msg, name, value))
|
||||
b.Notice(ctx, evt.RoomID, fmt.Sprintf(msg, name, value))
|
||||
}
|
||||
|
||||
func (b *Bot) setOption(ctx context.Context, evt *event.Event, name, value string) {
|
||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("setOption"))
|
||||
defer span.Finish()
|
||||
func (b *Bot) setOption(ctx context.Context, name, value string) {
|
||||
msg := "`%s` of this room set to %s"
|
||||
|
||||
sanitizer, ok := sanitizers[name]
|
||||
@@ -218,22 +206,23 @@ func (b *Bot) setOption(ctx context.Context, evt *event.Event, name, value strin
|
||||
value = sanitizer(value)
|
||||
}
|
||||
|
||||
evt := eventFromContext(ctx)
|
||||
if name == optionMailbox {
|
||||
existingID, ok := b.GetMapping(ctx, value)
|
||||
existingID, ok := b.GetMapping(value)
|
||||
if ok && existingID != "" && existingID != evt.RoomID {
|
||||
b.Notice(span.Context(), evt.RoomID, fmt.Sprintf("Mailbox `%s@%s` already taken", value, b.domain))
|
||||
b.Notice(ctx, evt.RoomID, fmt.Sprintf("Mailbox `%s@%s` already taken", value, b.domain))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
cfg, err := b.getSettings(span.Context(), evt.RoomID)
|
||||
cfg, err := b.getSettings(evt.RoomID)
|
||||
if err != nil {
|
||||
b.Error(span.Context(), evt.RoomID, "failed to retrieve settings: %v", err)
|
||||
b.Error(ctx, evt.RoomID, "failed to retrieve settings: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
if !cfg.Allowed(b.noowner, evt.Sender) {
|
||||
b.Notice(span.Context(), evt.RoomID, "you don't have permission to do that")
|
||||
b.Notice(ctx, evt.RoomID, "you don't have permission to do that")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -246,11 +235,11 @@ func (b *Bot) setOption(ctx context.Context, evt *event.Event, name, value strin
|
||||
b.roomsmu.Unlock()
|
||||
}
|
||||
|
||||
err = b.setSettings(span.Context(), evt.RoomID, cfg)
|
||||
err = b.setSettings(evt.RoomID, cfg)
|
||||
if err != nil {
|
||||
b.Error(span.Context(), evt.RoomID, "cannot update settings: %v", err)
|
||||
b.Error(ctx, evt.RoomID, "cannot update settings: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
b.Notice(span.Context(), evt.RoomID, fmt.Sprintf(msg, name, value))
|
||||
b.Notice(ctx, evt.RoomID, fmt.Sprintf(msg, name, value))
|
||||
}
|
||||
|
||||
51
bot/context.go
Normal file
51
bot/context.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"maunium.net/go/mautrix/event"
|
||||
)
|
||||
|
||||
type ctxkey int
|
||||
|
||||
const (
|
||||
ctxEvent ctxkey = iota
|
||||
)
|
||||
|
||||
func newContext(evt *event.Event) context.Context {
|
||||
ctx := context.Background()
|
||||
hub := sentry.CurrentHub().Clone()
|
||||
ctx = sentry.SetHubOnContext(ctx, hub)
|
||||
ctx = eventToContext(ctx, evt)
|
||||
|
||||
return ctx
|
||||
}
|
||||
|
||||
func eventFromContext(ctx context.Context) *event.Event {
|
||||
v := ctx.Value(ctxEvent)
|
||||
if v == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
evt, ok := v.(*event.Event)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
return evt
|
||||
}
|
||||
|
||||
func eventToContext(ctx context.Context, evt *event.Event) context.Context {
|
||||
ctx = context.WithValue(ctx, ctxEvent, evt)
|
||||
sentry.GetHubFromContext(ctx).ConfigureScope(func(scope *sentry.Scope) {
|
||||
scope.SetUser(sentry.User{ID: evt.Sender.String()})
|
||||
scope.SetContext("event", map[string]string{
|
||||
"id": evt.ID.String(),
|
||||
"room": evt.RoomID.String(),
|
||||
"sender": evt.Sender.String(),
|
||||
})
|
||||
})
|
||||
|
||||
return ctx
|
||||
}
|
||||
20
bot/data.go
20
bot/data.go
@@ -1,10 +1,8 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
@@ -59,11 +57,9 @@ func (b *Bot) migrate() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *Bot) syncRooms(ctx context.Context) error {
|
||||
func (b *Bot) syncRooms() error {
|
||||
b.roomsmu.Lock()
|
||||
defer b.roomsmu.Unlock()
|
||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("syncRooms"))
|
||||
defer span.Finish()
|
||||
|
||||
resp, err := b.lp.GetClient().JoinedRooms()
|
||||
if err != nil {
|
||||
@@ -71,8 +67,8 @@ func (b *Bot) syncRooms(ctx context.Context) error {
|
||||
}
|
||||
b.rooms = make(map[string]id.RoomID, len(resp.JoinedRooms))
|
||||
for _, roomID := range resp.JoinedRooms {
|
||||
b.migrateSettings(span.Context(), roomID)
|
||||
cfg, serr := b.getSettings(span.Context(), roomID)
|
||||
b.migrateSettings(roomID)
|
||||
cfg, serr := b.getSettings(roomID)
|
||||
if serr != nil {
|
||||
b.log.Warn("cannot get %s settings: %v", roomID, err)
|
||||
continue
|
||||
@@ -86,10 +82,7 @@ func (b *Bot) syncRooms(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
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)
|
||||
@@ -103,10 +96,7 @@ func (b *Bot) getThreadID(ctx context.Context, roomID id.RoomID, messageID strin
|
||||
return data["eventID"]
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
func (b *Bot) setThreadID(roomID id.RoomID, messageID string, eventID id.EventID) {
|
||||
key := messagekey + "." + messageID
|
||||
data := map[string]id.EventID{
|
||||
"eventID": eventID,
|
||||
|
||||
@@ -3,11 +3,10 @@ package bot
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"maunium.net/go/mautrix/event"
|
||||
)
|
||||
|
||||
func (b *Bot) handle(ctx context.Context, evt *event.Event) {
|
||||
func (b *Bot) handle(ctx context.Context) {
|
||||
evt := eventFromContext(ctx)
|
||||
content := evt.Content.AsMessage()
|
||||
if content == nil {
|
||||
b.Error(ctx, evt.RoomID, "cannot read message")
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"maunium.net/go/mautrix/id"
|
||||
|
||||
"gitlab.com/etke.cc/postmoogle/utils"
|
||||
@@ -68,7 +66,7 @@ func (s settings) Set(key, value string) {
|
||||
}
|
||||
|
||||
// TODO: remove after migration
|
||||
func (b *Bot) migrateSettings(ctx context.Context, roomID id.RoomID) {
|
||||
func (b *Bot) migrateSettings(roomID id.RoomID) {
|
||||
var config settingsOld
|
||||
err := b.lp.GetClient().GetRoomAccountData(roomID, settingskey, &config)
|
||||
if err != nil {
|
||||
@@ -84,16 +82,13 @@ func (b *Bot) migrateSettings(ctx context.Context, roomID id.RoomID) {
|
||||
cfg.Set(optionOwner, config.Owner.String())
|
||||
cfg.Set(optionNoSender, strconv.FormatBool(config.NoSender))
|
||||
|
||||
err = b.setSettings(ctx, roomID, cfg)
|
||||
err = b.setSettings(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()
|
||||
|
||||
func (b *Bot) getSettings(roomID id.RoomID) (settings, error) {
|
||||
config := settings{}
|
||||
err := b.lp.GetClient().GetRoomAccountData(roomID, settingskey, &config)
|
||||
if err != nil {
|
||||
@@ -108,9 +103,6 @@ func (b *Bot) getSettings(ctx context.Context, roomID id.RoomID) (settings, erro
|
||||
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()
|
||||
|
||||
func (b *Bot) setSettings(roomID id.RoomID, cfg settings) error {
|
||||
return b.lp.GetClient().SetRoomAccountData(roomID, settingskey, cfg)
|
||||
}
|
||||
|
||||
46
bot/sync.go
46
bot/sync.go
@@ -3,7 +3,6 @@ package bot
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/getsentry/sentry-go"
|
||||
"maunium.net/go/mautrix"
|
||||
"maunium.net/go/mautrix/event"
|
||||
)
|
||||
@@ -28,10 +27,10 @@ func (b *Bot) initSync() {
|
||||
}
|
||||
|
||||
func (b *Bot) onMembership(evt *event.Event) {
|
||||
hub := sentry.CurrentHub().Clone()
|
||||
ctx := newContext(evt)
|
||||
|
||||
if evt.Content.AsMember().Membership == event.MembershipJoin && evt.Sender == b.lp.GetClient().UserID {
|
||||
b.onBotJoin(evt, hub)
|
||||
b.onBotJoin(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -43,21 +42,8 @@ func (b *Bot) onMessage(evt *event.Event) {
|
||||
if evt.Sender == b.lp.GetClient().UserID {
|
||||
return
|
||||
}
|
||||
|
||||
hub := sentry.CurrentHub().Clone()
|
||||
hub.ConfigureScope(func(scope *sentry.Scope) {
|
||||
scope.SetUser(sentry.User{ID: evt.Sender.String()})
|
||||
scope.SetContext("event", map[string]string{
|
||||
"id": evt.ID.String(),
|
||||
"room": evt.RoomID.String(),
|
||||
"sender": evt.Sender.String(),
|
||||
})
|
||||
})
|
||||
ctx := sentry.SetHubOnContext(context.Background(), hub)
|
||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("onMessage"))
|
||||
defer span.Finish()
|
||||
|
||||
b.handle(span.Context(), evt)
|
||||
ctx := newContext(evt)
|
||||
b.handle(ctx)
|
||||
}
|
||||
|
||||
func (b *Bot) onEncryptedMessage(evt *event.Event) {
|
||||
@@ -65,31 +51,21 @@ func (b *Bot) onEncryptedMessage(evt *event.Event) {
|
||||
if evt.Sender == b.lp.GetClient().UserID {
|
||||
return
|
||||
}
|
||||
|
||||
hub := sentry.CurrentHub().Clone()
|
||||
hub.ConfigureScope(func(scope *sentry.Scope) {
|
||||
scope.SetUser(sentry.User{ID: evt.Sender.String()})
|
||||
scope.SetContext("event", map[string]string{
|
||||
"id": evt.ID.String(),
|
||||
"room": evt.RoomID.String(),
|
||||
"sender": evt.Sender.String(),
|
||||
})
|
||||
})
|
||||
ctx := sentry.SetHubOnContext(context.Background(), hub)
|
||||
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("onMessage"))
|
||||
defer span.Finish()
|
||||
ctx := newContext(evt)
|
||||
|
||||
decrypted, err := b.lp.GetMachine().DecryptMegolmEvent(evt)
|
||||
if err != nil {
|
||||
b.Error(span.Context(), evt.RoomID, "cannot decrypt a message: %v", err)
|
||||
b.Error(ctx, evt.RoomID, "cannot decrypt a message: %v", err)
|
||||
return
|
||||
}
|
||||
ctx = eventToContext(ctx, decrypted)
|
||||
|
||||
b.handle(span.Context(), decrypted)
|
||||
b.handle(ctx)
|
||||
}
|
||||
|
||||
// onBotJoin handles the "bot joined the room" event
|
||||
func (b *Bot) onBotJoin(evt *event.Event, hub *sentry.Hub) {
|
||||
func (b *Bot) onBotJoin(ctx context.Context) {
|
||||
evt := eventFromContext(ctx)
|
||||
// Workaround for membership=join events which are delivered to us twice,
|
||||
// as described in this bug report: https://github.com/matrix-org/synapse/issues/9768
|
||||
_, ok := b.handledJoinEvents.LoadOrStore(evt.ID, true)
|
||||
@@ -98,8 +74,6 @@ func (b *Bot) onBotJoin(evt *event.Event, hub *sentry.Hub) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := sentry.SetHubOnContext(context.Background(), hub)
|
||||
|
||||
b.sendIntroduction(ctx, evt.RoomID)
|
||||
b.sendHelp(ctx, evt.RoomID)
|
||||
}
|
||||
|
||||
@@ -53,7 +53,6 @@ func initSentry(cfg *config.Config) {
|
||||
err := sentry.Init(sentry.ClientOptions{
|
||||
Dsn: cfg.Sentry.DSN,
|
||||
AttachStacktrace: true,
|
||||
TracesSampleRate: float64(cfg.Sentry.SampleRate) / 100,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatal("cannot initialize sentry: %v", err)
|
||||
|
||||
@@ -21,8 +21,7 @@ func New() *Config {
|
||||
Federation: env.Bool("federation"),
|
||||
MaxSize: env.Int("maxsize", defaultConfig.MaxSize),
|
||||
Sentry: Sentry{
|
||||
DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN),
|
||||
SampleRate: env.Int("sentry.rate", defaultConfig.Sentry.SampleRate),
|
||||
DSN: env.String("sentry.dsn", defaultConfig.Sentry.DSN),
|
||||
},
|
||||
LogLevel: env.String("loglevel", defaultConfig.LogLevel),
|
||||
DB: DB{
|
||||
|
||||
@@ -10,7 +10,4 @@ var defaultConfig = &Config{
|
||||
DSN: "local.db",
|
||||
Dialect: "sqlite3",
|
||||
},
|
||||
Sentry: Sentry{
|
||||
SampleRate: 20,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -42,6 +42,5 @@ type DB struct {
|
||||
|
||||
// Sentry config
|
||||
type Sentry struct {
|
||||
DSN string
|
||||
SampleRate int
|
||||
DSN string
|
||||
}
|
||||
|
||||
6
go.mod
6
go.mod
@@ -9,8 +9,9 @@ require (
|
||||
github.com/lib/pq v1.10.6
|
||||
github.com/mattn/go-sqlite3 v1.14.14
|
||||
gitlab.com/etke.cc/go/env v1.0.0
|
||||
gitlab.com/etke.cc/go/logger v1.0.0
|
||||
gitlab.com/etke.cc/go/logger v1.1.0
|
||||
gitlab.com/etke.cc/linkpearl v0.0.0-20220823124436-ff8541c79eee
|
||||
golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c
|
||||
maunium.net/go/mautrix v0.12.0
|
||||
)
|
||||
|
||||
@@ -36,8 +37,7 @@ require (
|
||||
github.com/tidwall/sjson v1.2.5 // indirect
|
||||
github.com/yuin/goldmark v1.4.12 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8 // indirect
|
||||
golang.org/x/net v0.0.0-20220822230855-b0a4917ee28c // indirect
|
||||
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 // indirect
|
||||
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 // indirect
|
||||
golang.org/x/text v0.3.7 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
maunium.net/go/maulogger/v2 v2.3.2 // indirect
|
||||
|
||||
8
go.sum
8
go.sum
@@ -70,8 +70,8 @@ github.com/yuin/goldmark v1.4.12 h1:6hffw6vALvEDqJ19dOJvJKOoAOKe4NDaTqvd2sktGN0=
|
||||
github.com/yuin/goldmark v1.4.12/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
gitlab.com/etke.cc/go/env v1.0.0 h1:J98BwzOuELnjsVPFvz5wa79L7IoRV9CmrS41xLYXtSw=
|
||||
gitlab.com/etke.cc/go/env v1.0.0/go.mod h1:e1l4RM5MA1sc0R1w/RBDAESWRwgo5cOG9gx8BKUn2C4=
|
||||
gitlab.com/etke.cc/go/logger v1.0.0 h1:vp8omRMhYn+/peIlHFf9AK7GlTnQQurDCIall3N88b0=
|
||||
gitlab.com/etke.cc/go/logger v1.0.0/go.mod h1:8Vw5HFXlZQ5XeqvUs5zan+GnhrQyYtm/xe+yj8H/0zk=
|
||||
gitlab.com/etke.cc/go/logger v1.1.0 h1:Yngp/DDLmJ0jJNLvLXrfan5Gi5QV+r7z6kCczTv8t4U=
|
||||
gitlab.com/etke.cc/go/logger v1.1.0/go.mod h1:8Vw5HFXlZQ5XeqvUs5zan+GnhrQyYtm/xe+yj8H/0zk=
|
||||
gitlab.com/etke.cc/linkpearl v0.0.0-20220823124436-ff8541c79eee h1:B880ePbakEEr02p2khEpdwUSzzO49ZnZhnQoxGfKj6U=
|
||||
gitlab.com/etke.cc/linkpearl v0.0.0-20220823124436-ff8541c79eee/go.mod h1:CqwzwxVogKG6gDWTPTen3NyWbTESg42jxoTfXXwDGKQ=
|
||||
golang.org/x/crypto v0.0.0-20220817201139-bc19a97f63c8 h1:GIAS/yBem/gq2MUqgNIzUHW7cJMmx3TGZOrnyYaNQ6c=
|
||||
@@ -84,8 +84,8 @@ golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6 h1:Sx/u41w+OwrInGdEckYmEuU5gHoGSL4QbDz3S9s6j4U=
|
||||
golang.org/x/sys v0.0.0-20220818161305-2296e01440c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24 h1:TyKJRhyo17yWxOMCTHKWrc5rddHORMlnZ/j57umaUd8=
|
||||
golang.org/x/sys v0.0.0-20220823224334-20c2bfdbfe24/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
|
||||
|
||||
@@ -37,7 +37,7 @@ func (s *session) Rcpt(to string) error {
|
||||
return smtp.ErrAuthRequired
|
||||
}
|
||||
|
||||
_, ok := s.client.GetMapping(s.ctx, utils.Mailbox(to))
|
||||
_, ok := s.client.GetMapping(utils.Mailbox(to))
|
||||
if !ok {
|
||||
s.log.Debug("mapping for %s not found", to)
|
||||
return smtp.ErrAuthRequired
|
||||
|
||||
@@ -10,6 +10,6 @@ import (
|
||||
|
||||
// Client interface to send emails
|
||||
type Client interface {
|
||||
GetMapping(context.Context, string) (id.RoomID, bool)
|
||||
GetMapping(string) (id.RoomID, bool)
|
||||
Send(ctx context.Context, email *utils.Email) error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user