From f6c4f49bb0044b344e388347e75ca0422ace141d Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Fri, 28 Apr 2023 14:58:24 +0300 Subject: [PATCH] Ensure user invited when updating portal info. Probably fixes #62 --- commands.go | 4 ++-- guildportal.go | 3 ++- portal.go | 14 +++++++++----- user.go | 10 ++++++++-- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/commands.go b/commands.go index 12b4070..faaa662 100644 --- a/commands.go +++ b/commands.go @@ -371,10 +371,10 @@ func fnRejoinSpace(ce *WrappedCommandEvent) { } user := ce.User if ce.Args[0] == "main" { - user.ensureInvited(nil, user.GetSpaceRoom(), false) + user.ensureInvited(nil, user.GetSpaceRoom(), false, true) ce.Reply("Invited you to your main space ([link](%s))", user.GetSpaceRoom().URI(ce.Bridge.AS.HomeserverDomain).MatrixToURL()) } else if ce.Args[0] == "dms" { - user.ensureInvited(nil, user.GetDMSpaceRoom(), false) + user.ensureInvited(nil, user.GetDMSpaceRoom(), false, true) ce.Reply("Invited you to your DM space ([link](%s))", user.GetDMSpaceRoom().URI(ce.Bridge.AS.HomeserverDomain).MatrixToURL()) } else if _, err := strconv.Atoi(ce.Args[0]); err == nil { ce.Reply("Rejoining guild spaces is not yet implemented") diff --git a/guildportal.go b/guildportal.go index 847b973..fce4396 100644 --- a/guildportal.go +++ b/guildportal.go @@ -219,7 +219,7 @@ func (guild *Guild) CreateMatrixRoom(user *User, meta *discordgo.Guild) error { guild.bridge.guildsLock.Unlock() guild.log.Infoln("Matrix room created:", guild.MXID) - user.ensureInvited(nil, guild.MXID, false) + user.ensureInvited(nil, guild.MXID, false, true) return nil } @@ -236,6 +236,7 @@ func (guild *Guild) UpdateInfo(source *User, meta *discordgo.Guild) *discordgo.G guild.UpdateBridgeInfo() guild.Update() } + source.ensureInvited(nil, guild.MXID, false, false) return meta } diff --git a/portal.go b/portal.go index acab01e..c02396c 100644 --- a/portal.go +++ b/portal.go @@ -383,6 +383,7 @@ func (portal *Portal) CreateMatrixRoom(user *User, channel *discordgo.Channel) e portal.roomCreateLock.Lock() defer portal.roomCreateLock.Unlock() if portal.MXID != "" { + portal.ensureUserInvited(user, false) return nil } portal.log.Infoln("Creating Matrix room for channel") @@ -520,7 +521,7 @@ func (portal *Portal) CreateMatrixRoom(user *User, channel *discordgo.Channel) e } else { portal.updateSpace() } - portal.ensureUserInvited(user) + portal.ensureUserInvited(user, true) user.syncChatDoublePuppetDetails(portal, true) portal.syncParticipants(user, channel.Recipients) @@ -579,8 +580,8 @@ func (portal *Portal) handleDiscordMessages(msg portalDiscordMessage) { } } -func (portal *Portal) ensureUserInvited(user *User) bool { - return user.ensureInvited(portal.MainIntent(), portal.MXID, portal.IsPrivateChat()) +func (portal *Portal) ensureUserInvited(user *User, ignoreCache bool) bool { + return user.ensureInvited(portal.MainIntent(), portal.MXID, portal.IsPrivateChat(), ignoreCache) } func (portal *Portal) markMessageHandled(discordID string, editIndex int, authorID string, timestamp time.Time, threadID string, parts []database.MessagePart) { @@ -909,7 +910,7 @@ func (portal *Portal) syncParticipant(source *User, participant *discordgo.User, user := portal.bridge.GetUserByID(participant.ID) if user != nil { log.Debug().Msg("Ensuring Matrix user is invited or joined to room") - portal.ensureUserInvited(user) + portal.ensureUserInvited(user, false) } if remove { @@ -931,7 +932,7 @@ func (portal *Portal) syncParticipants(source *User, participants []*discordgo.U user := portal.bridge.GetUserByID(participant.ID) if user != nil { - portal.ensureUserInvited(user) + portal.ensureUserInvited(user, false) } if user == nil || !puppet.IntentFor(portal).IsCustomPuppet { @@ -2134,6 +2135,9 @@ func (portal *Portal) UpdateInfo(source *User, meta *discordgo.Channel) *discord fallthrough default: changed = portal.UpdateName(meta) || changed + if portal.MXID != "" { + portal.ensureUserInvited(source, false) + } } changed = portal.UpdateTopic(meta.Topic) || changed changed = portal.UpdateParent(meta.ParentID) || changed diff --git a/user.go b/user.go index 992797f..6e54fdf 100644 --- a/user.go +++ b/user.go @@ -333,7 +333,7 @@ func (user *User) getSpaceRoom(ptr *id.RoomID, name, topic string, parent id.Roo } else { *ptr = resp.RoomID user.Update() - user.ensureInvited(nil, *ptr, false) + user.ensureInvited(nil, *ptr, false, true) if parent != "" { _, err = user.bridge.Bot.SendStateEvent(parent, event.StateSpaceChild, resp.RoomID.String(), &event.SpaceChildEventContent{ @@ -1246,10 +1246,16 @@ func (user *User) interactionSuccessHandler(s *discordgo.InteractionSuccess) { } } -func (user *User) ensureInvited(intent *appservice.IntentAPI, roomID id.RoomID, isDirect bool) bool { +func (user *User) ensureInvited(intent *appservice.IntentAPI, roomID id.RoomID, isDirect, ignoreCache bool) bool { + if roomID == "" { + return false + } if intent == nil { intent = user.bridge.Bot } + if !ignoreCache && intent.StateStore.IsInvited(roomID, user.MXID) { + return true + } ret := false inviteContent := event.Content{