2023-08-02 16:52:41 +00:00
|
|
|
use activitypub_federation::config::Data;
|
|
|
|
use actix_web::web::Json;
|
2023-03-20 21:32:31 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
context::LemmyContext,
|
|
|
|
custom_emoji::{CustomEmojiResponse, EditCustomEmoji},
|
2023-10-11 14:48:19 +00:00
|
|
|
utils::is_admin,
|
2023-03-20 21:32:31 +00:00
|
|
|
};
|
2024-09-19 09:15:04 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
custom_emoji::{CustomEmoji, CustomEmojiUpdateForm},
|
|
|
|
custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm},
|
|
|
|
},
|
|
|
|
traits::Crud,
|
2023-03-20 21:32:31 +00:00
|
|
|
};
|
2023-09-21 10:42:28 +00:00
|
|
|
use lemmy_db_views::structs::{CustomEmojiView, LocalUserView};
|
2024-04-10 14:14:11 +00:00
|
|
|
use lemmy_utils::error::LemmyResult;
|
2023-03-20 21:32:31 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn update_custom_emoji(
|
|
|
|
data: Json<EditCustomEmoji>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: LocalUserView,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<Json<CustomEmojiResponse>> {
|
2023-08-02 16:52:41 +00:00
|
|
|
// Make sure user is an admin
|
|
|
|
is_admin(&local_user_view)?;
|
2023-03-20 21:32:31 +00:00
|
|
|
|
2024-09-20 12:15:25 +00:00
|
|
|
let emoji_form = CustomEmojiUpdateForm::new(
|
|
|
|
data.clone().image_url.into(),
|
|
|
|
data.alt_text.to_string(),
|
|
|
|
data.category.to_string(),
|
|
|
|
);
|
2023-08-02 16:52:41 +00:00
|
|
|
let emoji = CustomEmoji::update(&mut context.pool(), data.id, &emoji_form).await?;
|
|
|
|
CustomEmojiKeyword::delete(&mut context.pool(), data.id).await?;
|
|
|
|
let mut keywords = vec![];
|
|
|
|
for keyword in &data.keywords {
|
2024-09-20 12:15:25 +00:00
|
|
|
let keyword_form =
|
|
|
|
CustomEmojiKeywordInsertForm::new(emoji.id, keyword.to_lowercase().trim().to_string());
|
2023-08-02 16:52:41 +00:00
|
|
|
keywords.push(keyword_form);
|
2023-03-20 21:32:31 +00:00
|
|
|
}
|
2023-08-02 16:52:41 +00:00
|
|
|
CustomEmojiKeyword::create(&mut context.pool(), keywords).await?;
|
|
|
|
let view = CustomEmojiView::get(&mut context.pool(), emoji.id).await?;
|
|
|
|
Ok(Json(CustomEmojiResponse { custom_emoji: view }))
|
2023-03-20 21:32:31 +00:00
|
|
|
}
|