Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

various improvements to attachment bridging #2172

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions bridge/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,26 +380,51 @@ func (b *Bdiscord) handleEventBotUser(msg *config.Message, channelID string) (st
// handleUploadFile handles native upload of files
func (b *Bdiscord) handleUploadFile(msg *config.Message, channelID string) (string, error) {
for _, f := range msg.Extra["file"] {
var err error
fi := f.(config.FileInfo)
file := discordgo.File{
Name: fi.Name,
ContentType: "",
Reader: bytes.NewReader(*fi.Data),
}
m := discordgo.MessageSend{
Content: msg.Username + fi.Comment,
Files: []*discordgo.File{&file},
AllowedMentions: b.getAllowedMentions(),

if fi.URL != "" {
err = b.handleUploadFileFromURL(msg, channelID, &fi)
} else if fi.Data != nil {
err = b.handleUploadFileFromData(msg, channelID, &fi)
} else {
b.Log.Errorf("Attachment %#v for message %#v had neither Data nor URL", fi, msg)
}
res, err := b.c.ChannelMessageSendComplex(channelID, &m)

if err != nil {
return "", fmt.Errorf("file upload failed: %s", err)
b.Log.Errorf("Could not send attachment %#v for message %#v: %s", fi, msg, err)
return "", err
}

// link file_upload_nativeID (file ID from the original bridge) to our upload id
// so that we can remove this later when it eg needs to be deleted
b.cache.Add(cFileUpload+fi.NativeID, res.ID)
}

return "", nil
}

func (b *Bdiscord) handleUploadFileFromData(msg *config.Message, channelID string, fi *config.FileInfo) error {
file := discordgo.File{
Name: fi.Name,
ContentType: "",
Reader: bytes.NewReader(*fi.Data),
}
m := discordgo.MessageSend{
Content: msg.Username + fi.Comment,
Files: []*discordgo.File{&file},
AllowedMentions: b.getAllowedMentions(),
}
res, err := b.c.ChannelMessageSendComplex(channelID, &m)
if err != nil {
return fmt.Errorf("file upload failed: %s", err)
}
// link file_upload_nativeID (file ID from the original bridge) to our upload id
// so that we can remove this later when it eg needs to be deleted
b.cache.Add(cFileUpload+fi.NativeID, res.ID)
return nil
}

func (b *Bdiscord) handleUploadFileFromURL(msg *config.Message, channelID string, fi *config.FileInfo) error {
_, err := b.c.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{
Content: fi.URL,
AllowedMentions: b.getAllowedMentions(),
})
return err
}
12 changes: 8 additions & 4 deletions bridge/discord/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,19 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
return
}


rmsg := config.Message{Account: b.Account, Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg", UserID: m.Author.ID, ID: m.ID, Extra: make(map[string][]interface{}, 0)}

// add the url of the attachments to content
if len(m.Attachments) > 0 {
for _, attach := range m.Attachments {
m.Content = m.Content + "\n" + attach.URL
rmsg.Extra["file"] = append(rmsg.Extra["file"], config.FileInfo {
URL: attach.URL,
})
// m.Content = m.Content + "\n" + attach.URL
}
}

rmsg := config.Message{Account: b.Account, Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg", UserID: m.Author.ID, ID: m.ID}

b.Log.Debugf("== Receiving event %#v", m.Message)

if m.Content != "" {
Expand Down Expand Up @@ -139,7 +143,7 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
}

// no empty messages
if rmsg.Text == "" {
if rmsg.Text == "" && len(rmsg.Extra["file"]) == 0 {
return
}

Expand Down
66 changes: 48 additions & 18 deletions bridge/discord/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,33 +69,63 @@ func (b *Bdiscord) webhookSendTextOnly(msg *config.Message, channelID string) (s
func (b *Bdiscord) webhookSendFilesOnly(msg *config.Message, channelID string) error {
for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo) //nolint:forcetypeassert
file := discordgo.File{
Name: fi.Name,
ContentType: "",
Reader: bytes.NewReader(*fi.Data),
var err error

if fi.URL != "" {
err = b.webhookSendFileFromURL(msg, channelID, &fi)
} else if fi.Data != nil {
err = b.webhookSendFileFromData(msg, channelID, &fi)
} else {
b.Log.Errorf("Attachment %#v for message %#v had neither Data nor URL", fi, msg)
}
content := fi.Comment

// Cannot use the resulting ID for any edits anyway, so throw it away.
// This has to be re-enabled when we implement message deletion.
_, err := b.transmitter.Send(
channelID,
&discordgo.WebhookParams{
Username: msg.Username,
AvatarURL: msg.Avatar,
Files: []*discordgo.File{&file},
Content: content,
AllowedMentions: b.getAllowedMentions(),
},
)
if err != nil {
b.Log.Errorf("Could not send file %#v for message %#v: %s", file, msg, err)
b.Log.Errorf("Could not send attachment %#v for message %#v: %s", fi, msg, err)
return err
}
}

return nil
}

func (b *Bdiscord) webhookSendFileFromData(msg *config.Message, channelID string, fi *config.FileInfo) error {
file := discordgo.File{
Name: fi.Name,
ContentType: "",
Reader: bytes.NewReader(*fi.Data),
}
content := fi.Comment

// Cannot use the resulting ID for any edits anyway, so throw it away.
// This has to be re-enabled when we implement message deletion.
_, err := b.transmitter.Send(
channelID,
&discordgo.WebhookParams{
Username: msg.Username,
AvatarURL: msg.Avatar,
Files: []*discordgo.File{&file},
Content: content,
AllowedMentions: b.getAllowedMentions(),
},
)
return err
}

func (b *Bdiscord) webhookSendFileFromURL(msg *config.Message, channelID string, fi *config.FileInfo) error {
// discord client will display any file url as an inline embed,
// without us having to do anything special.
_, err := b.transmitter.Send(
channelID,
&discordgo.WebhookParams{
Content: fi.URL,
Username: msg.Username,
AvatarURL: msg.Avatar,
AllowedMentions: b.getAllowedMentions(),
},
)
return err
}

// webhookSend send one or more message via webhook, taking care of file
// uploads (from slack, telegram or mattermost).
// Returns messageID and error.
Expand Down
20 changes: 14 additions & 6 deletions bridge/irc/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,26 @@ func (b *Birc) handleFiles(msg *config.Message) bool {
if len(msg.Extra["file"]) == 0 {
return false
}

if msg.Text != "" {
b.Local <- config.Message{
Text: msg.Text,
Username: msg.Username,
Channel: msg.Channel,
Event: msg.Event,
}
}

for _, f := range msg.Extra["file"] {
fi := f.(config.FileInfo)
var text = ""
if fi.Comment != "" {
msg.Text += fi.Comment + " : "
text += fi.Comment + " : "
}
if fi.URL != "" {
msg.Text = fi.URL
if fi.Comment != "" {
msg.Text = fi.Comment + " : " + fi.URL
}
text += fi.URL
}
b.Local <- config.Message{Text: msg.Text, Username: msg.Username, Channel: msg.Channel, Event: msg.Event}
b.Local <- config.Message{Text: text, Username: msg.Username, Channel: msg.Channel, Event: msg.Event}
}
return true
}
Expand Down
19 changes: 7 additions & 12 deletions bridge/xmpp/xmpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,20 +362,14 @@ func (b *Bxmpp) replaceAction(text string) (string, bool) {

// handleUploadFile handles native upload of files
func (b *Bxmpp) handleUploadFile(msg *config.Message) error {
var urlDesc string

for _, file := range msg.Extra["file"] {
fileInfo := file.(config.FileInfo)
if fileInfo.Comment != "" {
msg.Text += fileInfo.Comment + ": "
}
if fileInfo.URL != "" {
msg.Text = fileInfo.URL
if fileInfo.Comment != "" {
msg.Text = fileInfo.Comment + ": " + fileInfo.URL
urlDesc = fileInfo.Comment
}
msg.Text += fileInfo.Comment
}

// this is sent even if Text is empty,
// so that you can tell who sent the message
if _, err := b.xc.Send(xmpp.Chat{
Type: "groupchat",
Remote: msg.Channel + "@" + b.GetString("Muc"),
Expand All @@ -385,13 +379,14 @@ func (b *Bxmpp) handleUploadFile(msg *config.Message) error {
}

if fileInfo.URL != "" {
if _, err := b.xc.SendOOB(xmpp.Chat{
if _, err := b.xc.Send(xmpp.Chat{
Type: "groupchat",
Remote: msg.Channel + "@" + b.GetString("Muc"),
Text: fileInfo.URL,
Ooburl: fileInfo.URL,
Oobdesc: urlDesc,
}); err != nil {
b.Log.WithError(err).Warn("Failed to send share URL.")
return err
}
}
}
Expand Down