1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 custom_emoji::{CustomEmojiResponse, EditCustomEmoji},
6 utils::{get_local_user_view_from_jwt, is_admin},
8 use lemmy_db_schema::source::{
9 custom_emoji::{CustomEmoji, CustomEmojiUpdateForm},
10 custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm},
11 local_site::LocalSite,
13 use lemmy_db_views::structs::CustomEmojiView;
14 use lemmy_utils::{error::LemmyError, ConnectionId};
16 #[async_trait::async_trait(?Send)]
17 impl PerformCrud for EditCustomEmoji {
18 type Response = CustomEmojiResponse;
20 #[tracing::instrument(skip(self, context, _websocket_id))]
23 context: &Data<LemmyContext>,
24 _websocket_id: Option<ConnectionId>,
25 ) -> Result<CustomEmojiResponse, LemmyError> {
26 let data: &EditCustomEmoji = self;
28 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
30 let local_site = LocalSite::read(context.pool()).await?;
31 // Make sure user is an admin
32 is_admin(&local_user_view)?;
34 let emoji_form = CustomEmojiUpdateForm::builder()
35 .local_site_id(local_site.id)
36 .alt_text(data.alt_text.to_string())
37 .category(data.category.to_string())
38 .image_url(data.clone().image_url.into())
40 let emoji = CustomEmoji::update(context.pool(), data.id, &emoji_form).await?;
41 CustomEmojiKeyword::delete(context.pool(), data.id).await?;
42 let mut keywords = vec![];
43 for keyword in &data.keywords {
44 let keyword_form = CustomEmojiKeywordInsertForm::builder()
45 .custom_emoji_id(emoji.id)
46 .keyword(keyword.to_lowercase().trim().to_string())
48 keywords.push(keyword_form);
50 CustomEmojiKeyword::create(context.pool(), keywords).await?;
51 let view = CustomEmojiView::get(context.pool(), emoji.id).await?;
52 Ok(CustomEmojiResponse { custom_emoji: view })