2023-03-20 21:32:31 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
|
|
|
context::LemmyContext,
|
|
|
|
custom_emoji::{CustomEmojiResponse, EditCustomEmoji},
|
2023-07-26 18:01:15 +00:00
|
|
|
utils::{is_admin, local_user_view_from_jwt, sanitize_html},
|
2023-03-20 21:32:31 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::source::{
|
|
|
|
custom_emoji::{CustomEmoji, CustomEmojiUpdateForm},
|
|
|
|
custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm},
|
|
|
|
local_site::LocalSite,
|
|
|
|
};
|
|
|
|
use lemmy_db_views::structs::CustomEmojiView;
|
2023-06-06 16:27:22 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
2023-03-20 21:32:31 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for EditCustomEmoji {
|
|
|
|
type Response = CustomEmojiResponse;
|
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
#[tracing::instrument(skip(self, context))]
|
|
|
|
async fn perform(&self, context: &Data<LemmyContext>) -> Result<CustomEmojiResponse, LemmyError> {
|
2023-03-20 21:32:31 +00:00
|
|
|
let data: &EditCustomEmoji = self;
|
2023-05-25 14:50:07 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
|
2023-03-20 21:32:31 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
let local_site = LocalSite::read(&mut context.pool()).await?;
|
2023-03-20 21:32:31 +00:00
|
|
|
// Make sure user is an admin
|
|
|
|
is_admin(&local_user_view)?;
|
|
|
|
|
2023-07-26 18:01:15 +00:00
|
|
|
let alt_text = sanitize_html(&data.alt_text);
|
|
|
|
let category = sanitize_html(&data.category);
|
|
|
|
|
2023-03-20 21:32:31 +00:00
|
|
|
let emoji_form = CustomEmojiUpdateForm::builder()
|
|
|
|
.local_site_id(local_site.id)
|
2023-07-26 18:01:15 +00:00
|
|
|
.alt_text(alt_text)
|
|
|
|
.category(category)
|
2023-03-20 21:32:31 +00:00
|
|
|
.image_url(data.clone().image_url.into())
|
|
|
|
.build();
|
2023-07-11 13:09:59 +00:00
|
|
|
let emoji = CustomEmoji::update(&mut context.pool(), data.id, &emoji_form).await?;
|
|
|
|
CustomEmojiKeyword::delete(&mut context.pool(), data.id).await?;
|
2023-03-20 21:32:31 +00:00
|
|
|
let mut keywords = vec![];
|
|
|
|
for keyword in &data.keywords {
|
|
|
|
let keyword_form = CustomEmojiKeywordInsertForm::builder()
|
|
|
|
.custom_emoji_id(emoji.id)
|
|
|
|
.keyword(keyword.to_lowercase().trim().to_string())
|
|
|
|
.build();
|
|
|
|
keywords.push(keyword_form);
|
|
|
|
}
|
2023-07-11 13:09:59 +00:00
|
|
|
CustomEmojiKeyword::create(&mut context.pool(), keywords).await?;
|
|
|
|
let view = CustomEmojiView::get(&mut context.pool(), emoji.id).await?;
|
2023-03-20 21:32:31 +00:00
|
|
|
Ok(CustomEmojiResponse { custom_emoji: view })
|
|
|
|
}
|
|
|
|
}
|