Refactor: Extract discord attachment creation into its own function

This commit is contained in:
Charlotte Som 2022-05-22 11:30:30 +01:00
parent 4fc8c15b07
commit 602a042bd5

View file

@ -1,6 +1,6 @@
use phoebe::{
attachments::attachment_to_url,
mid_chat::{ChatMessage, ChatMessageEdit, ChatMessageReference, ChatReference},
mid_chat::{ChatAttachment, ChatMessage, ChatMessageEdit, ChatMessageReference, ChatReference},
prelude::*,
};
@ -72,6 +72,34 @@ async fn create_webhook_reply_embeds(
vec![]
}
async fn create_discord_attachments(source: &'_ ChatMessage) -> Vec<AttachmentType<'_>> {
source
.attachments
.iter()
.map(|a| match a {
ChatAttachment::Online {
url,
media_type: Some(media_type),
} => {
if media_type.starts_with("image/") {
AttachmentType::Image(url)
} else {
todo!("Handle non-image online attachment")
}
}
ChatAttachment::Online { .. } => {
todo!("Handle online attachment with no media_type")
}
ChatAttachment::InMemory {
file_name, data, ..
} => AttachmentType::Bytes {
filename: file_name.clone(),
data: data.into(),
},
})
.collect()
}
pub async fn send_discord_message(
discord: &mut DiscordService,
source: &ChatMessage,
@ -95,19 +123,7 @@ pub async fn send_discord_message(
None
};
let files = source
.attachments
.iter()
.map(|a| match a {
phoebe::mid_chat::ChatAttachment::Online { url, .. } => AttachmentType::Image(url),
phoebe::mid_chat::ChatAttachment::InMemory {
file_name, data, ..
} => AttachmentType::Bytes {
filename: file_name.clone(),
data: data.into(),
},
})
.collect::<Vec<_>>();
let files = create_discord_attachments(source).await;
if let Some(webhook) = get_or_create_webhook_for_channel(&mut *discord, &channel_id).await {
let reply_embeds = if let Some((channel, message)) = discord_reply {