Skip to content

Commit

Permalink
Redo direct media access with URL refreshing (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Feb 18, 2024
1 parent a5813a9 commit d0e3d29
Show file tree
Hide file tree
Showing 14 changed files with 947 additions and 162 deletions.
12 changes: 5 additions & 7 deletions attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,19 +323,17 @@ func (br *DiscordBridge) copyAttachmentToMatrix(intent *appservice.IntentAPI, ur
}

func (portal *Portal) getEmojiMXCByDiscordID(emojiID, name string, animated bool) id.ContentURI {
var url, mimeType, ext string
mxc := portal.bridge.DMA.EmojiMXC(emojiID, name, animated)
if !mxc.IsEmpty() {
return mxc
}
var url, mimeType string
if animated {
url = discordgo.EndpointEmojiAnimated(emojiID)
mimeType = "image/gif"
ext = "gif"
} else {
url = discordgo.EndpointEmoji(emojiID)
mimeType = "image/png"
ext = "png"
}
mxc := portal.bridge.Config.Bridge.MediaPatterns.Emoji(emojiID, ext)
if !mxc.IsEmpty() {
return mxc
}
dbFile, err := portal.bridge.copyAttachmentToMatrix(portal.MainIntent(), url, false, AttachmentMeta{
AttachmentID: emojiID,
Expand Down
116 changes: 8 additions & 108 deletions config/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/bwmarrin/discordgo"

"maunium.net/go/mautrix/bridge/bridgeconfig"
"maunium.net/go/mautrix/id"
)

type BridgeConfig struct {
Expand Down Expand Up @@ -55,8 +54,8 @@ type BridgeConfig struct {
EnableWebhookAvatars bool `yaml:"enable_webhook_avatars"`
UseDiscordCDNUpload bool `yaml:"use_discord_cdn_upload"`

CacheMedia string `yaml:"cache_media"`
MediaPatterns MediaPatterns `yaml:"media_patterns"`
CacheMedia string `yaml:"cache_media"`
DirectMedia DirectMedia `yaml:"direct_media"`

AnimatedSticker struct {
Target string `yaml:"target"`
Expand Down Expand Up @@ -96,111 +95,12 @@ type BridgeConfig struct {
guildNameTemplate *template.Template `yaml:"-"`
}

type MediaPatterns struct {
Enabled bool `yaml:"enabled"`
TplAttachments string `yaml:"attachments"`
TplEmojis string `yaml:"emojis"`
TplStickers string `yaml:"stickers"`
TplAvatars string `yaml:"avatars"`

attachments *template.Template `yaml:"-"`
emojis *template.Template `yaml:"-"`
stickers *template.Template `yaml:"-"`
avatars *template.Template `yaml:"-"`
}

type umMediaPatterns MediaPatterns

func (mp *MediaPatterns) UnmarshalYAML(unmarshal func(interface{}) error) error {
err := unmarshal((*umMediaPatterns)(mp))
if err != nil {
return err
}
tpl := template.New("media_patterns")

pairs := []struct {
ptr **template.Template
name string
template string
}{
{&mp.attachments, "attachments", mp.TplAttachments},
{&mp.emojis, "emojis", mp.TplEmojis},
{&mp.stickers, "stickers", mp.TplStickers},
{&mp.avatars, "avatars", mp.TplAvatars},
}
for _, pair := range pairs {
if pair.template == "" {
continue
}
*pair.ptr, err = tpl.New(pair.name).Parse(pair.template)
if err != nil {
return err
}
}
return nil
}

type attachmentParams struct {
ChannelID string
AttachmentID string
FileName string
}

type emojiStickerParams struct {
ID string
Ext string
}

type avatarParams struct {
UserID string
AvatarID string
Ext string
}

func (mp *MediaPatterns) execute(tpl *template.Template, params any) id.ContentURI {
if tpl == nil || !mp.Enabled {
return id.ContentURI{}
}
var out strings.Builder
err := tpl.Execute(&out, params)
if err != nil {
panic(err)
}
uri, err := id.ParseContentURI(out.String())
if err != nil {
panic(err)
}
return uri
}

func (mp *MediaPatterns) Attachment(channelID, attachmentID, filename string) id.ContentURI {
return mp.execute(mp.attachments, attachmentParams{
ChannelID: channelID,
AttachmentID: attachmentID,
FileName: filename,
})
}

func (mp *MediaPatterns) Emoji(emojiID, ext string) id.ContentURI {
return mp.execute(mp.emojis, emojiStickerParams{
ID: emojiID,
Ext: ext,
})
}

func (mp *MediaPatterns) Sticker(stickerID, ext string) id.ContentURI {
return mp.execute(mp.stickers, emojiStickerParams{
ID: stickerID,
Ext: ext,
})
}

func (mp *MediaPatterns) Avatar(userID, avatarID, ext string) id.ContentURI {
return mp.execute(mp.avatars, avatarParams{
UserID: userID,
AvatarID: avatarID,
Ext: ext,
})
type DirectMedia struct {
Enabled bool `yaml:"enabled"`
ServerName string `yaml:"server_name"`
WellKnownResponse string `yaml:"well_known_response"`
AllowProxy bool `yaml:"allow_proxy"`
ServerKey string `yaml:"server_key"`
}

type BackfillLimitPart struct {
Expand Down
16 changes: 11 additions & 5 deletions config/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
up "go.mau.fi/util/configupgrade"
"go.mau.fi/util/random"
"maunium.net/go/mautrix/bridge/bridgeconfig"
"maunium.net/go/mautrix/federation"
)

func DoUpgrade(helper *up.Helper) {
Expand Down Expand Up @@ -58,12 +59,17 @@ func DoUpgrade(helper *up.Helper) {
helper.Copy(up.Bool, "bridge", "prefix_webhook_messages")
helper.Copy(up.Bool, "bridge", "enable_webhook_avatars")
helper.Copy(up.Bool, "bridge", "use_discord_cdn_upload")
helper.Copy(up.Bool, "bridge", "media_patterns", "enabled")
helper.Copy(up.Str, "bridge", "cache_media")
helper.Copy(up.Str|up.Null, "bridge", "media_patterns", "attachments")
helper.Copy(up.Str|up.Null, "bridge", "media_patterns", "emojis")
helper.Copy(up.Str|up.Null, "bridge", "media_patterns", "stickers")
helper.Copy(up.Str|up.Null, "bridge", "media_patterns", "avatars")
helper.Copy(up.Bool, "bridge", "direct_media", "enabled")
helper.Copy(up.Str, "bridge", "direct_media", "server_name")
helper.Copy(up.Str|up.Null, "bridge", "direct_media", "well_known_response")
helper.Copy(up.Bool, "bridge", "direct_media", "allow_proxy")
if serverKey, ok := helper.Get(up.Str, "bridge", "direct_media", "server_key"); !ok || serverKey == "generate" {
serverKey = federation.GenerateSigningKey().SynapseString()
helper.Set(up.Str, serverKey, "bridge", "direct_media", "server_key")
} else {
helper.Copy(up.Str, "bridge", "direct_media", "server_key")
}
helper.Copy(up.Str, "bridge", "animated_sticker", "target")
helper.Copy(up.Int, "bridge", "animated_sticker", "args", "width")
helper.Copy(up.Int, "bridge", "animated_sticker", "args", "height")
Expand Down
19 changes: 19 additions & 0 deletions database/userportal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"go.mau.fi/util/dbutil"
log "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix/id"
)

const (
Expand Down Expand Up @@ -44,6 +45,24 @@ func (u *User) scanUserPortals(rows dbutil.Rows) []UserPortal {
return ups
}

func (db *Database) GetUsersInPortal(channelID string) []id.UserID {
rows, err := db.Query("SELECT user_mxid FROM user_portal WHERE discord_id=$1", channelID)
if err != nil {
db.Portal.log.Errorln("Failed to get users in portal:", err)
}
var users []id.UserID
for rows.Next() {
var mxid id.UserID
err = rows.Scan(&mxid)
if err != nil {
db.Portal.log.Errorln("Failed to scan user in portal:", err)
} else {
users = append(users, mxid)
}
}
return users
}

func (u *User) GetPortals() []UserPortal {
rows, err := u.db.Query("SELECT discord_id, type, timestamp, in_space FROM user_portal WHERE user_mxid=$1", u.MXID)
if err != nil {
Expand Down
Loading

0 comments on commit d0e3d29

Please sign in to comment.