]> Untitled Git - lemmy.git/blob - crates/api/src/comment/save.rs
add enable_federated_downvotes site option
[lemmy.git] / crates / api / src / comment / save.rs
1 use actix_web::web::{Data, Json};
2 use lemmy_api_common::{
3   comment::{CommentResponse, SaveComment},
4   context::LemmyContext,
5   utils::local_user_view_from_jwt,
6 };
7 use lemmy_db_schema::{
8   source::comment::{CommentSaved, CommentSavedForm},
9   traits::Saveable,
10 };
11 use lemmy_db_views::structs::CommentView;
12 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
13
14 #[tracing::instrument(skip(context))]
15 pub async fn save_comment(
16   data: Json<SaveComment>,
17   context: Data<LemmyContext>,
18 ) -> Result<Json<CommentResponse>, LemmyError> {
19   let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
20
21   let comment_saved_form = CommentSavedForm {
22     comment_id: data.comment_id,
23     person_id: local_user_view.person.id,
24   };
25
26   if data.save {
27     CommentSaved::save(&mut context.pool(), &comment_saved_form)
28       .await
29       .with_lemmy_type(LemmyErrorType::CouldntSaveComment)?;
30   } else {
31     CommentSaved::unsave(&mut context.pool(), &comment_saved_form)
32       .await
33       .with_lemmy_type(LemmyErrorType::CouldntSaveComment)?;
34   }
35
36   let comment_id = data.comment_id;
37   let person_id = local_user_view.person.id;
38   let comment_view = CommentView::read(&mut context.pool(), comment_id, Some(person_id)).await?;
39
40   Ok(Json(CommentResponse {
41     comment_view,
42     recipient_ids: Vec::new(),
43   }))
44 }