Skip to content

Commit

Permalink
Fix finding client to fetch messages through
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Feb 18, 2024
1 parent 2a7a2c3 commit 8d01c30
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions directmedia.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"maunium.net/go/mautrix/id"

"go.mau.fi/mautrix-discord/config"
"go.mau.fi/mautrix-discord/database"
)

type DirectMediaAPI struct {
Expand Down Expand Up @@ -306,10 +307,23 @@ var ErrAttachmentNotFound = errors.New("attachment not found")
func (dma *DirectMediaAPI) fetchNewAttachmentURL(ctx context.Context, meta *AttachmentMediaData) (string, time.Time, error) {
var client *discordgo.Session
channelIDStr := strconv.FormatUint(meta.ChannelID, 10)
users := dma.bridge.DB.GetUsersInPortal(channelIDStr)
portal := dma.bridge.GetExistingPortalByID(database.PortalKey{ChannelID: channelIDStr})
var users []id.UserID
if portal != nil && portal.GuildID != "" {
users = dma.bridge.DB.GetUsersInPortal(portal.GuildID)
} else {
users = dma.bridge.DB.GetUsersInPortal(channelIDStr)
}
for _, userID := range users {
user := dma.bridge.GetCachedUserByMXID(userID)
if user != nil && user.Session != nil {
if user == nil || user.Session == nil {
continue
}
perms, err := user.Session.State.UserChannelPermissions(user.DiscordID, channelIDStr)
if err == nil && perms&discordgo.PermissionViewChannel == 0 {
continue
}
if client == nil || err == nil {
client = user.Session
if !client.IsUser {
break
Expand Down

0 comments on commit 8d01c30

Please sign in to comment.