Fetch missing channel info on message to support DMs for bots

This commit is contained in:
Tulir Asokan
2023-02-27 11:41:53 +02:00
parent 4eba894573
commit ccd29752c7
2 changed files with 42 additions and 9 deletions

View File

@@ -1650,6 +1650,8 @@ func (portal *Portal) UpdateNameDirect(name string) bool {
func (portal *Portal) UpdateAvatarFromPuppet(puppet *Puppet) bool {
if portal.Avatar == puppet.Avatar && portal.AvatarURL == puppet.AvatarURL && (portal.AvatarSet || portal.MXID == "") {
return false
} else if !portal.Encrypted && !portal.bridge.Config.Bridge.PrivateChatPortalMeta && portal.IsPrivateChat() {
return false
}
portal.log.Debugfln("Updating avatar from puppet %q -> %q", portal.Avatar, puppet.Avatar)
portal.Avatar = puppet.Avatar

45
user.go
View File

@@ -383,7 +383,7 @@ func (user *User) tryAutomaticDoublePuppeting() {
}
func (user *User) ViewingChannel(portal *Portal) bool {
if portal.GuildID != "" {
if portal.GuildID != "" || !user.Session.IsUser {
return false
}
user.markedOpenedLock.Lock()
@@ -910,22 +910,53 @@ func (user *User) channelUpdateHandler(_ *discordgo.Session, c *discordgo.Channe
}
}
func (user *User) findPortal(channelID string) (*Portal, *Thread) {
portal := user.GetExistingPortalByID(channelID)
if portal != nil {
return portal, nil
}
thread := user.bridge.GetThreadByID(channelID, nil)
if thread != nil && thread.Parent != nil {
return thread.Parent, thread
}
if !user.Session.IsUser {
channel, _ := user.Session.State.Channel(channelID)
if channel == nil {
user.log.Debugfln("Fetching info of unknown channel %s to handle message", channelID)
var err error
channel, err = user.Session.Channel(channelID)
if err != nil {
user.log.Warnfln("Failed to get info of unknown channel %s: %v", channelID, err)
} else {
user.log.Debugfln("Got info for channel %s to handle message", channelID)
_ = user.Session.State.ChannelAdd(channel)
}
}
if channel != nil && user.channelIsBridgeable(channel) {
user.log.Debugfln("Creating portal and updating info for %s to handle message", channelID)
portal = user.GetPortalByMeta(channel)
if channel.GuildID == "" {
user.handlePrivateChannel(portal, channel, time.Now(), false, false)
} else {
user.log.Warnfln("Unexpected unknown guild channel %s/%s", channel.GuildID, channel.ID)
}
return portal, nil
}
}
return nil, nil
}
func (user *User) pushPortalMessage(msg interface{}, typeName, channelID, guildID string) {
if user.getGuildBridgingMode(guildID) <= database.GuildBridgeNothing {
// If guild bridging mode is nothing, don't even check if the portal exists
return
}
portal := user.GetExistingPortalByID(channelID)
var thread *Thread
portal, thread := user.findPortal(channelID)
if portal == nil {
thread = user.bridge.GetThreadByID(channelID, nil)
if thread == nil || thread.Parent == nil {
user.log.Debugfln("Dropping %s in unknown channel %s/%s", typeName, guildID, channelID)
return
}
portal = thread.Parent
}
if mode := user.getGuildBridgingMode(portal.GuildID); mode <= database.GuildBridgeNothing || (portal.MXID == "" && mode <= database.GuildBridgeIfPortalExists) {
return
}