diff --git a/bridgev2/matrixinvite.go b/bridgev2/matrixinvite.go index 0f1601d1..a57c91b8 100644 --- a/bridgev2/matrixinvite.go +++ b/bridgev2/matrixinvite.go @@ -187,6 +187,26 @@ func (br *Bridge) handleGhostDMInvite(ctx context.Context, evt *event.Event, sen message += "\n\nWarning: failed to promote bot" hasWarning = true } + if resp.DMRedirectedTo != "" && resp.DMRedirectedTo != invitedGhost.ID { + log.Debug(). + Str("dm_redirected_to_id", string(resp.DMRedirectedTo)). + Msg("Created DM was redirected to another user ID") + _, err = invitedGhost.Intent.SendState(ctx, portal.MXID, event.StateMember, invitedGhost.Intent.GetMXID().String(), &event.Content{ + Parsed: &event.MemberEventContent{ + Membership: event.MembershipLeave, + Reason: "Direct chat redirected to another internal user ID", + }, + }, time.Time{}) + if err != nil { + log.Err(err).Msg("Failed to make incorrect ghost leave new DM room") + } + otherUserGhost, err := br.GetGhostByID(ctx, resp.DMRedirectedTo) + if err != nil { + log.Err(err).Msg("Failed to get ghost of real portal other user ID") + } else { + invitedGhost = otherUserGhost + } + } if resp.PortalInfo != nil { portal.UpdateInfo(ctx, resp.PortalInfo, sourceLogin, nil, time.Time{}) } else { diff --git a/bridgev2/networkinterface.go b/bridgev2/networkinterface.go index 2b99e4e6..457a7bd4 100644 --- a/bridgev2/networkinterface.go +++ b/bridgev2/networkinterface.go @@ -696,6 +696,9 @@ type CreateChatResponse struct { // Portal and PortalInfo are not required, the caller will fetch them automatically based on PortalKey if necessary. Portal *Portal PortalInfo *ChatInfo + // If a start DM request (CreateChatWithGhost or ResolveIdentifier) returns the DM to a different user, + // this field should have the user ID of said different user. + DMRedirectedTo networkid.UserID } // IdentifierResolvingNetworkAPI is an optional interface that network connectors can implement to support starting new direct chats.