handlematrix: bridge outgoing message attachments

This commit is contained in:
Skip R
2026-01-05 22:12:39 -08:00
parent cbfbe65619
commit e71075cd0d
6 changed files with 179 additions and 22 deletions

View File

@@ -54,6 +54,13 @@ func capID() string {
return base
}
// TODO: This limit is increased depending on user subscription status (Discord Nitro).
const MaxTextLength = 2000
// TODO: This limit is increased depending on user subscription status (Discord Nitro).
// TODO: Verify this figure (10 MiB).
const MaxFileSize = 10485760
var discordCaps = &event.RoomFeatures{
ID: capID(),
Reply: event.CapLevelFullySupported,
@@ -78,9 +85,57 @@ var discordCaps = &event.RoomFeatures{
event.FmtListJumpValue: event.CapLevelUnsupported,
event.FmtCustomEmoji: event.CapLevelUnsupported, // TODO: Support.
},
File: event.FileFeatureMap{
event.MsgImage: {
MimeTypes: map[string]event.CapabilitySupportLevel{
"image/jpeg": event.CapLevelFullySupported,
"image/png": event.CapLevelFullySupported,
"image/gif": event.CapLevelFullySupported,
"image/webp": event.CapLevelFullySupported,
},
Caption: event.CapLevelFullySupported,
MaxCaptionLength: MaxTextLength,
MaxSize: MaxFileSize,
},
event.MsgVideo: {
MimeTypes: map[string]event.CapabilitySupportLevel{
"video/mp4": event.CapLevelFullySupported,
"video/webm": event.CapLevelFullySupported,
},
Caption: event.CapLevelFullySupported,
MaxCaptionLength: MaxTextLength,
MaxSize: MaxFileSize,
},
event.MsgAudio: {
MimeTypes: map[string]event.CapabilitySupportLevel{
"audio/mpeg": event.CapLevelFullySupported,
"audio/webm": event.CapLevelFullySupported,
"audio/wav": event.CapLevelFullySupported,
},
Caption: event.CapLevelFullySupported,
MaxCaptionLength: MaxTextLength,
MaxSize: MaxFileSize,
},
event.MsgFile: {
MimeTypes: map[string]event.CapabilitySupportLevel{
"*/*": event.CapLevelFullySupported,
},
Caption: event.CapLevelFullySupported,
MaxCaptionLength: MaxTextLength,
MaxSize: MaxFileSize,
},
event.CapMsgGIF: {
MimeTypes: map[string]event.CapabilitySupportLevel{
"image/gif": event.CapLevelFullySupported,
},
Caption: event.CapLevelFullySupported,
MaxCaptionLength: MaxTextLength,
MaxSize: MaxFileSize,
},
// TODO: Support voice messages.
},
LocationMessage: event.CapLevelUnsupported,
// TODO: This limit is increased depending on Discord subscription (Nitro).
MaxTextLength: 2000,
MaxTextLength: MaxTextLength,
// TODO: Support reactions.
// TODO: Support threads.
// TODO: Support editing.

View File

@@ -33,7 +33,6 @@ import (
"maunium.net/go/mautrix/bridgev2/status"
"go.mau.fi/mautrix-discord/pkg/discordid"
"go.mau.fi/mautrix-discord/pkg/msgconv"
)
type DiscordClient struct {
@@ -42,7 +41,6 @@ type DiscordClient struct {
UserLogin *bridgev2.UserLogin
Session *discordgo.Session
hasBegunSyncing bool
MsgConv msgconv.MessageConverter
}
func (d *DiscordConnector) LoadUserLogin(ctx context.Context, login *bridgev2.UserLogin) error {
@@ -59,10 +57,6 @@ func (d *DiscordConnector) LoadUserLogin(ctx context.Context, login *bridgev2.Us
connector: d,
UserLogin: login,
Session: session,
MsgConv: msgconv.MessageConverter{
Bridge: d.Bridge,
ReuploadMedia: d.ReuploadMedia,
},
}
cl.SetUp(ctx, meta)

View File

@@ -33,10 +33,7 @@ var _ bridgev2.NetworkConnector = (*DiscordConnector)(nil)
func (d *DiscordConnector) Init(bridge *bridgev2.Bridge) {
d.Bridge = bridge
d.MsgConv = &msgconv.MessageConverter{
Bridge: bridge,
ReuploadMedia: d.ReuploadMedia,
}
d.MsgConv = msgconv.NewMessageConverter(bridge, d.ReuploadMedia)
}
func (d *DiscordConnector) Start(ctx context.Context) error {

View File

@@ -20,6 +20,7 @@ import (
"context"
"github.com/bwmarrin/discordgo"
"github.com/rs/zerolog"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/networkid"
@@ -41,7 +42,7 @@ func (d *DiscordClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.M
portal := msg.Portal
channelID := string(portal.ID)
sendReq, err := d.connector.MsgConv.ToDiscord(ctx, msg)
sendReq, err := d.connector.MsgConv.ToDiscord(ctx, d.Session, msg)
if err != nil {
return nil, err
}
@@ -51,7 +52,7 @@ func (d *DiscordClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.M
// TODO: Pass the guild ID when send messages in guild channels.
options = append(options, discordgo.WithChannelReferer("", channelID))
sentMsg, err := d.Session.ChannelMessageSendComplex(string(msg.Portal.ID), &sendReq, options...)
sentMsg, err := d.Session.ChannelMessageSendComplex(string(msg.Portal.ID), sendReq, options...)
if err != nil {
return nil, err
}