Add a 'ChatAttachment' enum that can store attachments by accessible URL or in-memory
This commit is contained in:
		
							parent
							
								
									316835c2f6
								
							
						
					
					
						commit
						77fd8b1ecb
					
				
					 5 changed files with 44 additions and 7 deletions
				
			
		|  | @ -1,12 +1,18 @@ | ||||||
| pub mod reference; | pub mod reference; | ||||||
| pub use reference::*; | pub use reference::*; | ||||||
| 
 | 
 | ||||||
|  | #[derive(Debug, Clone)] | ||||||
|  | pub enum ChatAttachment { | ||||||
|  |     URL(String), | ||||||
|  |     InMemory { file_name: String, data: Vec<u8> }, | ||||||
|  | } | ||||||
|  | 
 | ||||||
| #[derive(Debug, Clone)] | #[derive(Debug, Clone)] | ||||||
| pub struct ChatAuthor { | pub struct ChatAuthor { | ||||||
|     pub reference: ChatReference, |     pub reference: ChatReference, | ||||||
|     pub display_name: String, |     pub display_name: String, | ||||||
|     pub display_color: Option<[u8; 3]>, |     pub display_color: Option<[u8; 3]>, | ||||||
|     pub avatar_url: String, |     pub avatar: ChatAttachment, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| mod content; | mod content; | ||||||
|  | @ -17,7 +23,7 @@ pub struct ChatMessage { | ||||||
|     pub origin: ChatMessageReference, |     pub origin: ChatMessageReference, | ||||||
|     pub author: ChatAuthor, |     pub author: ChatAuthor, | ||||||
|     pub content: ChatMessageContent, |     pub content: ChatMessageContent, | ||||||
|     pub attachments: Vec<()>, |     pub attachments: Vec<ChatAttachment>, | ||||||
|     pub replying: Option<ChatMessageReference>, |     pub replying: Option<ChatMessageReference>, | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
							
								
								
									
										25
									
								
								phoebe/src/attachments.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										25
									
								
								phoebe/src/attachments.rs
									
									
									
									
									
										Normal 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") | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -6,6 +6,7 @@ use futures::StreamExt; | ||||||
| use sqlx::{Row, SqliteConnection, SqlitePool}; | use sqlx::{Row, SqliteConnection, SqlitePool}; | ||||||
| use tokio::sync::broadcast::*; | use tokio::sync::broadcast::*; | ||||||
| 
 | 
 | ||||||
|  | pub mod attachments; | ||||||
| pub mod db; | pub mod db; | ||||||
| pub mod prelude; | pub mod prelude; | ||||||
| pub mod service; | pub mod service; | ||||||
|  |  | ||||||
|  | @ -32,10 +32,12 @@ impl DiscordHandler { | ||||||
|             reference: discord_reference(message.author.id), |             reference: discord_reference(message.author.id), | ||||||
|             display_name, |             display_name, | ||||||
|             display_color, |             display_color, | ||||||
|             avatar_url: message |             avatar: ChatAttachment::URL( | ||||||
|                 .author |                 message | ||||||
|                 .static_avatar_url() |                     .author | ||||||
|                 .unwrap_or_else(|| message.author.default_avatar_url()), |                     .static_avatar_url() | ||||||
|  |                     .unwrap_or_else(|| message.author.default_avatar_url()), | ||||||
|  |             ), | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,4 +1,5 @@ | ||||||
| use phoebe::{ | use phoebe::{ | ||||||
|  |     attachments::attachment_to_url, | ||||||
|     mid_chat::{ChatMessage, ChatMessageReference, ChatReference}, |     mid_chat::{ChatMessage, ChatMessageReference, ChatReference}, | ||||||
|     prelude::*, |     prelude::*, | ||||||
| }; | }; | ||||||
|  | @ -101,6 +102,8 @@ pub async fn send_discord_message( | ||||||
|             vec![] |             vec![] | ||||||
|         }; |         }; | ||||||
| 
 | 
 | ||||||
|  |         let avatar_url = attachment_to_url(&source.author.avatar).await; | ||||||
|  | 
 | ||||||
|         if let Some(sent_message) = webhook |         if let Some(sent_message) = webhook | ||||||
|             .execute(&discord.ctx, true, |w| { |             .execute(&discord.ctx, true, |w| { | ||||||
|                 w.content(chat_conv::format(&source.content)) |                 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 |                         &source.author.display_name, &source.author.reference.service | ||||||
|                     )) |                     )) | ||||||
|                     .avatar_url(&source.author.avatar_url) |                     .avatar_url(&avatar_url) | ||||||
|                     .embeds(reply_embeds) |                     .embeds(reply_embeds) | ||||||
|             }) |             }) | ||||||
|             .await? |             .await? | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue