Forward images to Matrix

legacy
Charlotte Som 2021-11-22 22:48:43 +00:00
parent 4f9e2f5b5c
commit e0731c2df3
4 changed files with 41 additions and 7 deletions

8
Cargo.lock generated
View File

@ -1694,6 +1694,8 @@ dependencies = [
"kuchiki", "kuchiki",
"log", "log",
"matrix-sdk", "matrix-sdk",
"mime_guess",
"reqwest",
"serde", "serde",
"serde_json", "serde_json",
"serenity", "serenity",
@ -2019,9 +2021,9 @@ dependencies = [
[[package]] [[package]]
name = "reqwest" name = "reqwest"
version = "0.11.4" version = "0.11.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "246e9f61b9bb77df069a947682be06e31ac43ea37862e244a69f177694ea6d22" checksum = "66d2927ca2f685faf0fc620ac4834690d29e7abb153add10f5812eef20b5e280"
dependencies = [ dependencies = [
"base64 0.13.0", "base64 0.13.0",
"bytes 1.1.0", "bytes 1.1.0",
@ -3204,8 +3206,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e68338db6becec24d3c7977b5bf8a48be992c934b5d07177e3931f5dc9b076c" checksum = "5e68338db6becec24d3c7977b5bf8a48be992c934b5d07177e3931f5dc9b076c"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"serde",
"serde_json",
"wasm-bindgen-macro", "wasm-bindgen-macro",
] ]

View File

@ -17,6 +17,8 @@ html-escape = "0.2.9"
html5ever = "0.25.1" html5ever = "0.25.1"
kuchiki = "0.8.1" kuchiki = "0.8.1"
serde_json = "1.0.68" serde_json = "1.0.68"
mime_guess = "2.0.3"
reqwest = { version = "0.11.6", features = ["blocking"] }
[dependencies.serenity] [dependencies.serenity]
version = "0.10.9" version = "0.10.9"

View File

@ -7,7 +7,7 @@ use crate::{
discord::{ discord::{
self, delete_on_discord, edit_on_discord, forward_image_to_discord, forward_to_discord, self, delete_on_discord, edit_on_discord, forward_image_to_discord, forward_to_discord,
}, },
matrix::{self, delete_on_matrix, edit_on_matrix, forward_to_matrix}, matrix::{self, delete_on_matrix, edit_on_matrix, forward_image_to_matrix, forward_to_matrix},
messages::{DeletedMessage, EditedMessage, MessageReference, SentImageMessage, SentMessage}, messages::{DeletedMessage, EditedMessage, MessageReference, SentImageMessage, SentMessage},
}; };
@ -296,8 +296,12 @@ impl Bridgers {
} }
} }
if let Some(_matrix) = self.matrix.lock().unwrap().borrow().as_ref() { if let Some(matrix) = self.matrix.lock().unwrap().borrow().as_ref() {
if let Some(_room_id) = self.get_linked_matrix_room(&message.source) {} if let Some(room_id) = self.get_linked_matrix_room(&message.source) {
if let Some(m) = forward_image_to_matrix(matrix, room_id, &message).await {
related_messages.push(m);
}
}
} }
self.store_related_messages(&related_messages); self.store_related_messages(&related_messages);

View File

@ -343,6 +343,34 @@ pub async fn forward_to_matrix(
None None
} }
pub async fn forward_image_to_matrix(
client: &Client,
room_id: RoomId,
message: &SentImageMessage,
) -> Option<MessageReference> {
if let Some(room) = client.get_joined_room(&room_id) {
let image_url = message.image_url.clone();
let mut response = tokio::task::spawn_blocking(|| reqwest::blocking::get(image_url).ok())
.await
.ok()
.flatten()?;
let event = room
.send_attachment(
&message.image_url,
&mime_guess::from_path(&message.image_url).first_or_octet_stream(),
&mut response,
None,
)
.await
.ok()?;
return Some(MessageReference::from((&room_id, &event.event_id)));
}
None
}
pub async fn edit_on_matrix( pub async fn edit_on_matrix(
client: &Client, client: &Client,
room_id: RoomId, room_id: RoomId,