From 77fd8b1ecbd29e3b560531a51bb2fa321a797666 Mon Sep 17 00:00:00 2001 From: videogame hacker Date: Sun, 17 Apr 2022 11:53:52 +0100 Subject: [PATCH] Add a 'ChatAttachment' enum that can store attachments by accessible URL or in-memory --- mid-chat/src/lib.rs | 10 ++++++++-- phoebe/src/attachments.rs | 25 +++++++++++++++++++++++++ phoebe/src/lib.rs | 1 + services/phoebe-discord/src/handler.rs | 10 ++++++---- services/phoebe-discord/src/sender.rs | 5 ++++- 5 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 phoebe/src/attachments.rs diff --git a/mid-chat/src/lib.rs b/mid-chat/src/lib.rs index f74f77a..860c833 100644 --- a/mid-chat/src/lib.rs +++ b/mid-chat/src/lib.rs @@ -1,12 +1,18 @@ pub mod reference; pub use reference::*; +#[derive(Debug, Clone)] +pub enum ChatAttachment { + URL(String), + InMemory { file_name: String, data: Vec }, +} + #[derive(Debug, Clone)] pub struct ChatAuthor { pub reference: ChatReference, pub display_name: String, pub display_color: Option<[u8; 3]>, - pub avatar_url: String, + pub avatar: ChatAttachment, } mod content; @@ -17,7 +23,7 @@ pub struct ChatMessage { pub origin: ChatMessageReference, pub author: ChatAuthor, pub content: ChatMessageContent, - pub attachments: Vec<()>, + pub attachments: Vec, pub replying: Option, } diff --git a/phoebe/src/attachments.rs b/phoebe/src/attachments.rs new file mode 100644 index 0000000..c70349d --- /dev/null +++ b/phoebe/src/attachments.rs @@ -0,0 +1,25 @@ +use mid_chat::ChatAttachment; + +/* +use tokio::sync::OnceCell; + +static PHOEBE_MEDIA_BASE_URL: OnceCell = OnceCell::const_new(); + +async fn get_base_url() -> &'static str { + PHOEBE_MEDIA_BASE_URL + .get_or_init(|| async { + std::env::var("PHOEBE_MEDIA_BASE_URL") + .expect("PHOEBE_MEDIA_BASE_URL environment variable was not set!") + }) + .await +} +*/ + +pub async fn attachment_to_url(attachment: &ChatAttachment) -> String { + match attachment { + ChatAttachment::URL(s) => s.clone(), + ChatAttachment::InMemory { .. } => { + todo!("Put in-memory attachment into webroot") + } + } +} diff --git a/phoebe/src/lib.rs b/phoebe/src/lib.rs index 77f0c5c..f48db9a 100644 --- a/phoebe/src/lib.rs +++ b/phoebe/src/lib.rs @@ -6,6 +6,7 @@ use futures::StreamExt; use sqlx::{Row, SqliteConnection, SqlitePool}; use tokio::sync::broadcast::*; +pub mod attachments; pub mod db; pub mod prelude; pub mod service; diff --git a/services/phoebe-discord/src/handler.rs b/services/phoebe-discord/src/handler.rs index a352927..6a3e352 100644 --- a/services/phoebe-discord/src/handler.rs +++ b/services/phoebe-discord/src/handler.rs @@ -32,10 +32,12 @@ impl DiscordHandler { reference: discord_reference(message.author.id), display_name, display_color, - avatar_url: message - .author - .static_avatar_url() - .unwrap_or_else(|| message.author.default_avatar_url()), + avatar: ChatAttachment::URL( + message + .author + .static_avatar_url() + .unwrap_or_else(|| message.author.default_avatar_url()), + ), } } } diff --git a/services/phoebe-discord/src/sender.rs b/services/phoebe-discord/src/sender.rs index beeec41..e4903ad 100644 --- a/services/phoebe-discord/src/sender.rs +++ b/services/phoebe-discord/src/sender.rs @@ -1,4 +1,5 @@ use phoebe::{ + attachments::attachment_to_url, mid_chat::{ChatMessage, ChatMessageReference, ChatReference}, prelude::*, }; @@ -101,6 +102,8 @@ pub async fn send_discord_message( vec![] }; + let avatar_url = attachment_to_url(&source.author.avatar).await; + if let Some(sent_message) = webhook .execute(&discord.ctx, true, |w| { w.content(chat_conv::format(&source.content)) @@ -108,7 +111,7 @@ pub async fn send_discord_message( "{} ({})", &source.author.display_name, &source.author.reference.service )) - .avatar_url(&source.author.avatar_url) + .avatar_url(&avatar_url) .embeds(reply_embeds) }) .await?