浏览代码

Ensure user invited when updating portal info. Probably fixes #62

Tulir Asokan 2 年之前
父节点
当前提交
f6c4f49bb0
共有 4 个文件被更改,包括 21 次插入10 次删除
  1. 2 2
      commands.go
  2. 2 1
      guildportal.go
  3. 9 5
      portal.go
  4. 8 2
      user.go

+ 2 - 2
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")

+ 2 - 1
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
 }
 

+ 9 - 5
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

+ 8 - 2
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{