1 use crate::PerformCrud;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 custom_emoji::{CreateCustomEmoji, CustomEmojiResponse},
6 utils::{is_admin, local_user_view_from_jwt},
8 use lemmy_db_schema::source::{
9 custom_emoji::{CustomEmoji, CustomEmojiInsertForm},
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 CreateCustomEmoji {
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: &CreateCustomEmoji = self;
27 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
29 let local_site = LocalSite::read(context.pool()).await?;
30 // Make sure user is an admin
31 is_admin(&local_user_view)?;
33 let emoji_form = CustomEmojiInsertForm::builder()
34 .local_site_id(local_site.id)
35 .shortcode(data.shortcode.to_lowercase().trim().to_string())
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::create(context.pool(), &emoji_form).await?;
41 let mut keywords = vec![];
42 for keyword in &data.keywords {
43 let keyword_form = CustomEmojiKeywordInsertForm::builder()
44 .custom_emoji_id(emoji.id)
45 .keyword(keyword.to_lowercase().trim().to_string())
47 keywords.push(keyword_form);
49 CustomEmojiKeyword::create(context.pool(), keywords).await?;
50 let view = CustomEmojiView::get(context.pool(), emoji.id).await?;
51 Ok(CustomEmojiResponse { custom_emoji: view })