Create custom type for emoji images

This commit is contained in:
silverpill 2023-01-20 01:11:29 +00:00
parent 6c6eb731f9
commit 99d45ee048
7 changed files with 57 additions and 47 deletions

View file

@ -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) => {

View file

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

View file

@ -54,23 +54,21 @@ pub struct LinkTag {
pub name: Option<String>,
}
#[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<String>,
}
#[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<Utc>,

View file

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

View file

@ -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<Utc>,
) -> Result<DbEmoji, DatabaseError> {
@ -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<Utc>,
) -> Result<DbEmoji, DatabaseError> {
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();

View file

@ -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<String>,
pub image: ProfileImage,
pub image: EmojiImage,
pub object_id: Option<String>,
pub updated_at: DateTime<Utc>,
}

View file

@ -37,6 +37,18 @@ pub struct ProfileImage {
pub media_type: Option<String>,
}
impl ProfileImage {
pub fn new(
file_name: String,
media_type: Option<String>,
) -> Self {
Self {
file_name,
media_type,
}
}
}
json_from_sql!(ProfileImage);
json_to_sql!(ProfileImage);