diff --git a/src/models/emojis/queries.rs b/src/models/emojis/queries.rs index 7183659..1d1b177 100644 --- a/src/models/emojis/queries.rs +++ b/src/models/emojis/queries.rs @@ -11,6 +11,7 @@ use crate::database::{ use crate::models::{ cleanup::{find_orphaned_files, DeletionQueue}, instances::queries::create_instance, + profiles::queries::update_emoji_caches, }; use super::types::{DbEmoji, EmojiImage}; @@ -73,7 +74,8 @@ pub async fn update_emoji( &emoji_id, ], ).await?; - let emoji = row.try_get("emoji")?; + let emoji: DbEmoji = row.try_get("emoji")?; + update_emoji_caches(db_client, &emoji.id).await?; Ok(emoji) } @@ -175,6 +177,7 @@ pub async fn delete_emoji( ).await?; let row = maybe_row.ok_or(DatabaseError::NotFound("emoji"))?; let emoji: DbEmoji = row.try_get("emoji")?; + update_emoji_caches(db_client, &emoji.id).await?; let orphaned_files = find_orphaned_files( db_client, vec![emoji.image.file_name], diff --git a/src/models/profiles/queries.rs b/src/models/profiles/queries.rs index a6790b1..d6dbe8d 100644 --- a/src/models/profiles/queries.rs +++ b/src/models/profiles/queries.rs @@ -89,6 +89,36 @@ async fn update_emoji_cache( Ok(profile) } +pub async fn update_emoji_caches( + db_client: &impl DatabaseClient, + emoji_id: &Uuid, +) -> Result<(), DatabaseError> { + db_client.execute( + " + WITH profile_emojis AS ( + SELECT + actor_profile.id AS profile_id, + COALESCE( + jsonb_agg(emoji) FILTER (WHERE emoji.id IS NOT NULL), + '[]' + ) AS emojis + FROM actor_profile + CROSS JOIN jsonb_array_elements(actor_profile.emojis) AS cached_emoji + LEFT JOIN profile_emoji ON (profile_emoji.profile_id = actor_profile.id) + LEFT JOIN emoji ON (emoji.id = profile_emoji.emoji_id) + WHERE CAST(cached_emoji ->> 'id' AS UUID) = $1 + GROUP BY actor_profile.id + ) + UPDATE actor_profile + SET emojis = profile_emojis.emojis + FROM profile_emojis + WHERE actor_profile.id = profile_emojis.profile_id + ", + &[&emoji_id], + ).await?; + Ok(()) +} + /// Create new profile using given Client or Transaction. pub async fn create_profile( db_client: &mut impl DatabaseClient,