]> Untitled Git - lemmy.git/blob - crates/api/src/comment/save.rs
Add SendActivity trait so that api crates compile in parallel with lemmy_apub
[lemmy.git] / crates / api / src / comment / save.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   comment::{CommentResponse, SaveComment},
5   context::LemmyContext,
6   utils::get_local_user_view_from_jwt,
7 };
8 use lemmy_db_schema::{
9   source::comment::{CommentSaved, CommentSavedForm},
10   traits::Saveable,
11 };
12 use lemmy_db_views::structs::CommentView;
13 use lemmy_utils::{error::LemmyError, ConnectionId};
14
15 #[async_trait::async_trait(?Send)]
16 impl Perform for SaveComment {
17   type Response = CommentResponse;
18
19   #[tracing::instrument(skip(context, _websocket_id))]
20   async fn perform(
21     &self,
22     context: &Data<LemmyContext>,
23     _websocket_id: Option<ConnectionId>,
24   ) -> Result<CommentResponse, LemmyError> {
25     let data: &SaveComment = self;
26     let local_user_view =
27       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
28
29     let comment_saved_form = CommentSavedForm {
30       comment_id: data.comment_id,
31       person_id: local_user_view.person.id,
32     };
33
34     if data.save {
35       CommentSaved::save(context.pool(), &comment_saved_form)
36         .await
37         .map_err(|e| LemmyError::from_error_message(e, "couldnt_save_comment"))?;
38     } else {
39       CommentSaved::unsave(context.pool(), &comment_saved_form)
40         .await
41         .map_err(|e| LemmyError::from_error_message(e, "couldnt_save_comment"))?;
42     }
43
44     let comment_id = data.comment_id;
45     let person_id = local_user_view.person.id;
46     let comment_view = CommentView::read(context.pool(), comment_id, Some(person_id)).await?;
47
48     Ok(CommentResponse {
49       comment_view,
50       recipient_ids: Vec::new(),
51       form_id: None,
52     })
53   }
54 }