Refresh emoji caches when emoji is deleted

This commit is contained in:
silverpill 2023-01-21 00:23:15 +00:00
parent 452de34780
commit 522fd5bafa
2 changed files with 34 additions and 1 deletions

View file

@ -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],

View file

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