Preliminary support for media uploads

legacy
Charlotte Som 2021-10-03 13:08:43 +01:00
parent 8e699a310a
commit ee5e9611cf
5 changed files with 78 additions and 17 deletions

View File

@ -6,7 +6,7 @@ use crate::{
channels::ChannelReference, channels::ChannelReference,
discord::{self, delete_on_discord, edit_on_discord, forward_to_discord}, discord::{self, delete_on_discord, edit_on_discord, forward_to_discord},
matrix::{self, delete_on_matrix, edit_on_matrix, forward_to_matrix}, matrix::{self, delete_on_matrix, edit_on_matrix, forward_to_matrix},
messages::{DeletedMessage, EditedMessage, MessageReference, SentMessage}, messages::{DeletedMessage, EditedMessage, MessageReference, SentMediaMessage, SentMessage},
}; };
pub struct Bridgers { pub struct Bridgers {
@ -277,4 +277,22 @@ impl Bridgers {
} }
} }
} }
pub async fn send_media(&self, message: SentMediaMessage) {
if self.get_related_messages(&message.source).is_some() {
return;
}
if let Some(_discord) = self.discord.lock().unwrap().borrow().as_ref() {
if let Some(_discord_channel) = self.get_linked_discord_channel(&message.source) {
// TODO: Send the media
}
}
if let Some(_matrix) = self.matrix.lock().unwrap().borrow().as_ref() {
if let Some(_room_id) = self.get_linked_matrix_room(&message.source) {
// TODO: Send the media
}
}
}
} }

View File

@ -91,12 +91,14 @@ impl EventHandler for DiscordHandler {
.as_ref() .as_ref()
.map(|m| MessageReference::from(m.as_ref())); .map(|m| MessageReference::from(m.as_ref()));
let _ = self.event_tx.send(MessageEvent::Send(Box::new(SentMessage { let _ = self
source: message_ref, .event_tx
content, .send(MessageEvent::SendText(Box::new(SentMessage {
author: get_message_author(&ctx, &message).await, source: message_ref,
replies_to, content,
}))); author: get_message_author(&ctx, &message).await,
replies_to,
})));
} }
async fn message_update( async fn message_update(
@ -120,7 +122,7 @@ impl EventHandler for DiscordHandler {
let _ = self let _ = self
.event_tx .event_tx
.send(MessageEvent::Edit(Box::new(EditedMessage { .send(MessageEvent::EditText(Box::new(EditedMessage {
replacing: message_ref, replacing: message_ref,
content, content,
author: get_message_author(&ctx, &new_message).await, author: get_message_author(&ctx, &new_message).await,

View File

@ -87,15 +87,18 @@ async fn main() {
while let Some(event) = event_rx.recv().await { while let Some(event) = event_rx.recv().await {
match event { match event {
MessageEvent::Send(sent_message) => { MessageEvent::SendText(sent_message) => {
bridgers.send_message(*sent_message).await; bridgers.send_message(*sent_message).await;
} }
MessageEvent::Edit(edited_message) => { MessageEvent::EditText(edited_message) => {
bridgers.edit_message(*edited_message).await; bridgers.edit_message(*edited_message).await;
} }
MessageEvent::Delete(deleted_message) => { MessageEvent::Delete(deleted_message) => {
bridgers.delete_message(*deleted_message).await; bridgers.delete_message(*deleted_message).await;
} }
MessageEvent::SendMedia(sent_media) => {
bridgers.send_media(*sent_media).await;
}
MessageEvent::AdminLinkChannels(channels) => { MessageEvent::AdminLinkChannels(channels) => {
bridgers.link_channels(&channels); bridgers.link_channels(&channels);

View File

@ -2,6 +2,7 @@ use std::sync::Arc;
use matrix_sdk::{ use matrix_sdk::{
config::ClientConfig, config::ClientConfig,
media::MediaEventContent,
room::{Joined, Room}, room::{Joined, Room},
ruma::{ ruma::{
api, api,
@ -36,7 +37,8 @@ use crate::{
MessageContent, MessageContent,
}, },
messages::{ messages::{
DeletedMessage, EditedMessage, MessageAuthor, MessageEvent, MessageReference, SentMessage, DeletedMessage, EditedMessage, MessageAuthor, MessageEvent, MessageReference,
SentMediaMessage, SentMessage,
}, },
}; };
@ -182,7 +184,7 @@ async fn on_message_sent(
None None
}; };
Some(MessageEvent::Send(Box::new(SentMessage { Some(MessageEvent::SendText(Box::new(SentMessage {
source: message_ref, source: message_ref,
content, content,
author, author,
@ -194,7 +196,7 @@ async fn on_message_sent(
let mut content = ctx.get_content(&emote.body, &emote.formatted); let mut content = ctx.get_content(&emote.body, &emote.formatted);
content.insert(0, MessageComponent::Plain("* ".to_string())); content.insert(0, MessageComponent::Plain("* ".to_string()));
Some(MessageEvent::Send(Box::new(SentMessage { Some(MessageEvent::SendText(Box::new(SentMessage {
source: message_ref, source: message_ref,
content, content,
author, author,
@ -202,7 +204,36 @@ async fn on_message_sent(
}))) })))
} }
// TODO: Handle reactions, uploads (audio, video, image, file), and any other types of event MessageType::Image(content) => {
if let Some(file) = content.file() {
match file {
matrix_sdk::media::MediaType::Uri(uri) => {
if let Some((server_name, hash)) = uri.parts() {
let server_name = server_name.as_str();
let media_url = format!(
"https://{}/_matrix/media/r0/download/{}/{}",
server_name, server_name, hash
);
Some(MessageEvent::SendMedia(Box::new(SentMediaMessage {
source: message_ref,
author,
media_url,
})))
} else {
None
}
}
matrix_sdk::media::MediaType::Encrypted(_encrypted_file) => None,
}
} else {
None
}
}
// TODO: Handle reactions, more uploads (audio, video, file), and any other types of event
_ => None, _ => None,
}; };
@ -228,7 +259,7 @@ async fn on_message_edited(
if let Some(author) = ctx.get_message_author(&room, &event.sender).await { if let Some(author) = ctx.get_message_author(&room, &event.sender).await {
let _ = ctx let _ = ctx
.message_tx .message_tx
.send(MessageEvent::Edit(Box::new(EditedMessage { .send(MessageEvent::EditText(Box::new(EditedMessage {
replacing: message_ref, replacing: message_ref,
content, content,
author, author,

View File

@ -28,13 +28,20 @@ pub struct EditedMessage {
pub author: MessageAuthor, pub author: MessageAuthor,
} }
pub struct SentMediaMessage {
pub source: MessageReference,
pub author: MessageAuthor,
pub media_url: String,
}
pub struct DeletedMessage { pub struct DeletedMessage {
pub reference: MessageReference, pub reference: MessageReference,
} }
pub enum MessageEvent { pub enum MessageEvent {
AdminLinkChannels(Vec<ChannelReference>), AdminLinkChannels(Vec<ChannelReference>),
Send(Box<SentMessage>), SendText(Box<SentMessage>),
Edit(Box<EditedMessage>), EditText(Box<EditedMessage>),
SendMedia(Box<SentMediaMessage>),
Delete(Box<DeletedMessage>), Delete(Box<DeletedMessage>),
} }