Fix double membership=join event handling

This fixes the problem mentioned in 8e1aa5b11c.

Turns out that it's a long-standing Synapse bug:
https://github.com/matrix-org/synapse/issues/9768
This commit is contained in:
Slavi Pantaleev
2022-08-25 20:10:43 +03:00
parent 8e1aa5b11c
commit 236a128129
2 changed files with 17 additions and 12 deletions

View File

@@ -19,14 +19,15 @@ import (
// Bot represents matrix bot
type Bot struct {
noowner bool
federation bool
prefix string
domain string
rooms map[string]id.RoomID
roomsmu *sync.Mutex
log *logger.Logger
lp *linkpearl.Linkpearl
noowner bool
federation bool
prefix string
domain string
rooms map[string]id.RoomID
roomsmu *sync.Mutex
log *logger.Logger
lp *linkpearl.Linkpearl
handledEvents sync.Map
}
// New creates a new matrix bot

View File

@@ -12,10 +12,6 @@ func (b *Bot) initSync() {
b.lp.OnEventType(
event.StateMember,
func(_ mautrix.EventSource, evt *event.Event) {
// Trying to debug the membership=join event being handled twice here.
eventJSON, _ := evt.MarshalJSON()
b.log.Debug(string(eventJSON))
go b.onMembership(evt)
},
)
@@ -108,6 +104,14 @@ func (b *Bot) onEncryptedMessage(evt *event.Event) {
// onBotJoin handles the "bot joined the room" event
func (b *Bot) onBotJoin(evt *event.Event, hub *sentry.Hub) {
// 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
_, exists := b.handledEvents.LoadOrStore(evt.ID, true)
if exists {
b.log.Info("Suppressing already handled event %s", evt.ID)
return
}
ctx := sentry.SetHubOnContext(context.Background(), hub)
span := sentry.StartSpan(ctx, "http.server", sentry.TransactionName("onBotJoin"))
defer span.Finish()