diff --git a/src/activitypub/actors/helpers.rs b/src/activitypub/actors/helpers.rs index 6b2fc99..d9fe00f 100644 --- a/src/activitypub/actors/helpers.rs +++ b/src/activitypub/actors/helpers.rs @@ -37,10 +37,10 @@ async fn fetch_actor_images( media_dir, ).await { Ok((file_name, maybe_media_type)) => { - let image = ProfileImage { + let image = ProfileImage::new( file_name, - media_type: maybe_media_type, - }; + maybe_media_type, + ); Some(image) }, Err(error) => { @@ -60,10 +60,10 @@ async fn fetch_actor_images( media_dir, ).await { Ok((file_name, maybe_media_type)) => { - let image = ProfileImage { + let image = ProfileImage::new( file_name, - media_type: maybe_media_type, - }; + maybe_media_type, + ); Some(image) }, Err(error) => { diff --git a/src/activitypub/handlers/create.rs b/src/activitypub/handlers/create.rs index 7d3b995..9679eeb 100644 --- a/src/activitypub/handlers/create.rs +++ b/src/activitypub/handlers/create.rs @@ -28,6 +28,7 @@ use crate::models::{ get_emoji_by_remote_object_id, update_emoji, }, + emojis::types::EmojiImage, posts::{ hashtags::normalize_hashtag, helpers::get_post_by_object_id, @@ -416,8 +417,8 @@ pub async fn handle_note( continue; }, }; - let media_type = match maybe_media_type.as_deref() { - Some(media_type) if EMOJI_MEDIA_TYPES.contains(&media_type) => { + let media_type = match maybe_media_type { + Some(media_type) if EMOJI_MEDIA_TYPES.contains(&media_type.as_str()) => { media_type }, _ => { @@ -429,12 +430,12 @@ pub async fn handle_note( }, }; log::info!("downloaded emoji {}", tag.icon.url); + let image = EmojiImage { file_name, media_type }; let emoji = if let Some(emoji_id) = maybe_emoji_id { update_emoji( db_client, &emoji_id, - &file_name, - media_type, + image, &tag.updated, ).await? } else { @@ -444,8 +445,7 @@ pub async fn handle_note( db_client, tag_name, Some(&hostname), - &file_name, - media_type, + image, Some(&tag.id), &tag.updated, ).await? diff --git a/src/activitypub/types.rs b/src/activitypub/types.rs index 63d3d42..22c68e6 100644 --- a/src/activitypub/types.rs +++ b/src/activitypub/types.rs @@ -54,23 +54,21 @@ pub struct LinkTag { pub name: Option, } -#[allow(dead_code)] #[derive(Deserialize)] #[serde(rename_all = "camelCase")] -pub struct EmojiImage { +pub struct EmojiTagImage { #[serde(rename = "type")] - object_type: String, + pub object_type: String, pub url: String, pub media_type: Option, } -#[allow(dead_code)] #[derive(Deserialize)] #[serde(rename_all = "camelCase")] pub struct EmojiTag { #[serde(rename = "type")] - tag_type: String, - pub icon: EmojiImage, + pub tag_type: String, + pub icon: EmojiTagImage, pub id: String, pub name: String, pub updated: DateTime, diff --git a/src/mastodon_api/accounts/types.rs b/src/mastodon_api/accounts/types.rs index ddfac79..7912ffd 100644 --- a/src/mastodon_api/accounts/types.rs +++ b/src/mastodon_api/accounts/types.rs @@ -243,10 +243,10 @@ fn process_b64_image_field_value( output_dir, Some("image/"), )?; - let image = ProfileImage { + let image = ProfileImage::new( file_name, - media_type: Some(media_type), - }; + Some(media_type), + ); Some(image) } }, @@ -487,10 +487,7 @@ mod tests { #[test] fn test_create_account_from_profile() { let profile = DbActorProfile { - avatar: Some(ProfileImage { - file_name: "test".to_string(), - media_type: None, - }), + avatar: Some(ProfileImage::new("test".to_string(), None)), ..Default::default() }; let account = Account::from_profile(profile, INSTANCE_URL); diff --git a/src/models/emojis/queries.rs b/src/models/emojis/queries.rs index 3c9e608..fa6db63 100644 --- a/src/models/emojis/queries.rs +++ b/src/models/emojis/queries.rs @@ -9,17 +9,15 @@ use crate::database::{ use crate::models::{ cleanup::{find_orphaned_files, DeletionQueue}, instances::queries::create_instance, - profiles::types::ProfileImage, }; use crate::utils::id::new_uuid; -use super::types::DbEmoji; +use super::types::{DbEmoji, EmojiImage}; pub async fn create_emoji( db_client: &impl DatabaseClient, emoji_name: &str, hostname: Option<&str>, - file_name: &str, - media_type: &str, + image: EmojiImage, object_id: Option<&str>, updated_at: &DateTime, ) -> Result { @@ -27,10 +25,6 @@ pub async fn create_emoji( if let Some(hostname) = hostname { create_instance(db_client, hostname).await?; }; - let image = ProfileImage { - file_name: file_name.to_string(), - media_type: Some(media_type.to_string()), - }; let row = db_client.query_one( " INSERT INTO emoji ( @@ -60,14 +54,9 @@ pub async fn create_emoji( pub async fn update_emoji( db_client: &impl DatabaseClient, emoji_id: &Uuid, - file_name: &str, - media_type: &str, + image: EmojiImage, updated_at: &DateTime, ) -> Result { - let image = ProfileImage { - file_name: file_name.to_string(), - media_type: Some(media_type.to_string()), - }; let row = db_client.query_one( " UPDATE emoji @@ -138,16 +127,17 @@ mod tests { let db_client = &create_test_database().await; let emoji_name = "test"; let hostname = "example.org"; - let file_name = "test.png"; - let media_type = "image/png"; + let image = EmojiImage { + file_name: "test.png".to_string(), + media_type: "image/png".to_string(), + }; let object_id = "https://example.org/emojis/test"; let updated_at = Utc::now(); let DbEmoji { id: emoji_id, .. } = create_emoji( db_client, emoji_name, Some(hostname), - file_name, - media_type, + image, Some(object_id), &updated_at, ).await.unwrap(); @@ -164,12 +154,15 @@ mod tests { #[serial] async fn test_delete_emoji() { let db_client = &create_test_database().await; + let image = EmojiImage { + file_name: "test.png".to_string(), + media_type: "image/png".to_string(), + }; let emoji = create_emoji( db_client, "test", None, - "test.png", - "image/png", + image, None, &Utc::now(), ).await.unwrap(); diff --git a/src/models/emojis/types.rs b/src/models/emojis/types.rs index b73b131..1a68489 100644 --- a/src/models/emojis/types.rs +++ b/src/models/emojis/types.rs @@ -1,8 +1,18 @@ use chrono::{DateTime, Utc}; use postgres_types::FromSql; +use serde::{Deserialize, Serialize}; use uuid::Uuid; -use crate::models::profiles::types::ProfileImage; +use crate::database::json_macro::{json_from_sql, json_to_sql}; + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct EmojiImage { + pub file_name: String, + pub media_type: String, +} + +json_from_sql!(EmojiImage); +json_to_sql!(EmojiImage); #[derive(Clone, FromSql)] #[postgres(name = "emoji")] @@ -10,7 +20,7 @@ pub struct DbEmoji { pub id: Uuid, pub emoji_name: String, pub hostname: Option, - pub image: ProfileImage, + pub image: EmojiImage, pub object_id: Option, pub updated_at: DateTime, } diff --git a/src/models/profiles/types.rs b/src/models/profiles/types.rs index 52b54a5..185cee1 100644 --- a/src/models/profiles/types.rs +++ b/src/models/profiles/types.rs @@ -37,6 +37,18 @@ pub struct ProfileImage { pub media_type: Option, } +impl ProfileImage { + pub fn new( + file_name: String, + media_type: Option, + ) -> Self { + Self { + file_name, + media_type, + } + } +} + json_from_sql!(ProfileImage); json_to_sql!(ProfileImage);