Forward image attachments from Discord to Matrix

Also, there is work-in-progress work on forwarding images from encrypted
Matrix rooms
legacy
Charlotte Som 2021-11-29 16:58:00 +00:00
parent e0731c2df3
commit 0649ea0503
2 changed files with 26 additions and 4 deletions

View File

@ -61,6 +61,7 @@ impl EventHandler for DiscordHandler {
} }
async fn message(&self, ctx: Context, message: Message) { async fn message(&self, ctx: Context, message: Message) {
// TODO: Replace with proper management system for linking channels together
if let Some(target) = message.content.strip_prefix("phoebe!link ") { if let Some(target) = message.content.strip_prefix("phoebe!link ") {
if message if message
.member(&ctx) .member(&ctx)
@ -100,6 +101,16 @@ impl EventHandler for DiscordHandler {
author: get_message_author(&ctx, &message).await, author: get_message_author(&ctx, &message).await,
replies_to, replies_to,
}))); })));
for attachment in message.attachments.iter() {
let _ = self
.event_tx
.send(MessageEvent::SendImage(Box::new(SentImageMessage {
source: MessageReference::from(&message),
author: get_message_author(&ctx, &message).await,
image_url: attachment.proxy_url.clone(),
})));
}
} }
async fn message_update( async fn message_update(

View File

@ -149,6 +149,7 @@ async fn get_room_message_event(
} }
async fn on_room_message_event( async fn on_room_message_event(
client: Client,
ctx: Arc<MatrixHandler>, ctx: Arc<MatrixHandler>,
event: SyncMessageEvent<MessageEventContent>, event: SyncMessageEvent<MessageEventContent>,
room: Room, room: Room,
@ -157,12 +158,13 @@ async fn on_room_message_event(
if let Some(Relation::Replacement(replacement)) = &event.content.relates_to { if let Some(Relation::Replacement(replacement)) = &event.content.relates_to {
on_message_edited(ctx, &event, room, replacement).await; on_message_edited(ctx, &event, room, replacement).await;
} else { } else {
on_message_sent(ctx, &event, room).await; on_message_sent(client, ctx, &event, room).await;
} }
} }
} }
async fn on_message_sent( async fn on_message_sent(
client: Client,
ctx: Arc<MatrixHandler>, ctx: Arc<MatrixHandler>,
event: &SyncMessageEvent<MessageEventContent>, event: &SyncMessageEvent<MessageEventContent>,
room: Joined, room: Joined,
@ -226,8 +228,15 @@ async fn on_message_sent(
} }
} }
// TODO: Handle encrypted image uploads (we will have to decrypt it and upload it somewhere) matrix_sdk::media::MediaType::Encrypted(encrypted_file) => {
matrix_sdk::media::MediaType::Encrypted(_encrypted_file) => None, if let Some(file_data) =
client.get_file(content.clone(), true).await.ok().flatten()
{
todo!("Reupload encrypted file to publicly accessible URL")
} else {
None
}
}
} }
} else { } else {
None None
@ -461,7 +470,9 @@ pub async fn create_matrix_client(
let on_msg_ctx = event_handler.clone(); let on_msg_ctx = event_handler.clone();
client client
.register_event_handler(move |ev, room| on_room_message_event(on_msg_ctx.clone(), ev, room)) .register_event_handler(move |ev, room| {
on_room_message_event(client.clone(), on_msg_ctx.clone(), ev, room)
})
.await; .await;
let on_redact_ctx = event_handler.clone(); let on_redact_ctx = event_handler.clone();