Add a 'ChatAttachment' enum that can store attachments by accessible URL or in-memory

This commit is contained in:
Charlotte Som 2022-04-17 11:53:52 +01:00
parent 316835c2f6
commit 77fd8b1ecb
5 changed files with 44 additions and 7 deletions

View file

@ -1,12 +1,18 @@
pub mod reference;
pub use reference::*;
#[derive(Debug, Clone)]
pub enum ChatAttachment {
URL(String),
InMemory { file_name: String, data: Vec<u8> },
}
#[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<ChatAttachment>,
pub replying: Option<ChatMessageReference>,
}

25
phoebe/src/attachments.rs Normal file
View file

@ -0,0 +1,25 @@
use mid_chat::ChatAttachment;
/*
use tokio::sync::OnceCell;
static PHOEBE_MEDIA_BASE_URL: OnceCell<String> = 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")
}
}
}

View file

@ -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;

View file

@ -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()),
),
}
}
}

View file

@ -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?