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,
discord::{self, delete_on_discord, edit_on_discord, forward_to_discord},
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 {
@ -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()
.map(|m| MessageReference::from(m.as_ref()));
let _ = self.event_tx.send(MessageEvent::Send(Box::new(SentMessage {
source: message_ref,
content,
author: get_message_author(&ctx, &message).await,
replies_to,
})));
let _ = self
.event_tx
.send(MessageEvent::SendText(Box::new(SentMessage {
source: message_ref,
content,
author: get_message_author(&ctx, &message).await,
replies_to,
})));
}
async fn message_update(
@ -120,7 +122,7 @@ impl EventHandler for DiscordHandler {
let _ = self
.event_tx
.send(MessageEvent::Edit(Box::new(EditedMessage {
.send(MessageEvent::EditText(Box::new(EditedMessage {
replacing: message_ref,
content,
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 {
match event {
MessageEvent::Send(sent_message) => {
MessageEvent::SendText(sent_message) => {
bridgers.send_message(*sent_message).await;
}
MessageEvent::Edit(edited_message) => {
MessageEvent::EditText(edited_message) => {
bridgers.edit_message(*edited_message).await;
}
MessageEvent::Delete(deleted_message) => {
bridgers.delete_message(*deleted_message).await;
}
MessageEvent::SendMedia(sent_media) => {
bridgers.send_media(*sent_media).await;
}
MessageEvent::AdminLinkChannels(channels) => {
bridgers.link_channels(&channels);

View File

@ -2,6 +2,7 @@ use std::sync::Arc;
use matrix_sdk::{
config::ClientConfig,
media::MediaEventContent,
room::{Joined, Room},
ruma::{
api,
@ -36,7 +37,8 @@ use crate::{
MessageContent,
},
messages::{
DeletedMessage, EditedMessage, MessageAuthor, MessageEvent, MessageReference, SentMessage,
DeletedMessage, EditedMessage, MessageAuthor, MessageEvent, MessageReference,
SentMediaMessage, SentMessage,
},
};
@ -182,7 +184,7 @@ async fn on_message_sent(
None
};
Some(MessageEvent::Send(Box::new(SentMessage {
Some(MessageEvent::SendText(Box::new(SentMessage {
source: message_ref,
content,
author,
@ -194,7 +196,7 @@ async fn on_message_sent(
let mut content = ctx.get_content(&emote.body, &emote.formatted);
content.insert(0, MessageComponent::Plain("* ".to_string()));
Some(MessageEvent::Send(Box::new(SentMessage {
Some(MessageEvent::SendText(Box::new(SentMessage {
source: message_ref,
content,
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,
};
@ -228,7 +259,7 @@ async fn on_message_edited(
if let Some(author) = ctx.get_message_author(&room, &event.sender).await {
let _ = ctx
.message_tx
.send(MessageEvent::Edit(Box::new(EditedMessage {
.send(MessageEvent::EditText(Box::new(EditedMessage {
replacing: message_ref,
content,
author,

View File

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