lemmy/crates/api/src/comment/save.rs

50 lines
1.5 KiB
Rust
Raw Normal View History

2022-04-13 18:12:25 +00:00
use crate::Perform;
use actix_web::web::Data;
use lemmy_api_common::{
comment::{CommentResponse, SaveComment},
context::LemmyContext,
utils::local_user_view_from_jwt,
2022-04-13 18:12:25 +00:00
};
use lemmy_db_schema::{
source::comment::{CommentSaved, CommentSavedForm},
traits::Saveable,
};
use lemmy_db_views::structs::CommentView;
use lemmy_utils::error::LemmyError;
2022-04-13 18:12:25 +00:00
#[async_trait::async_trait(?Send)]
impl Perform for SaveComment {
type Response = CommentResponse;
#[tracing::instrument(skip(context))]
async fn perform(&self, context: &Data<LemmyContext>) -> Result<CommentResponse, LemmyError> {
2022-04-13 18:12:25 +00:00
let data: &SaveComment = self;
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
2022-04-13 18:12:25 +00:00
let comment_saved_form = CommentSavedForm {
comment_id: data.comment_id,
person_id: local_user_view.person.id,
};
if data.save {
2022-11-09 10:05:00 +00:00
CommentSaved::save(context.pool(), &comment_saved_form)
.await
2022-04-13 18:12:25 +00:00
.map_err(|e| LemmyError::from_error_message(e, "couldnt_save_comment"))?;
} else {
2022-11-09 10:05:00 +00:00
CommentSaved::unsave(context.pool(), &comment_saved_form)
.await
2022-04-13 18:12:25 +00:00
.map_err(|e| LemmyError::from_error_message(e, "couldnt_save_comment"))?;
}
let comment_id = data.comment_id;
let person_id = local_user_view.person.id;
2022-11-09 10:05:00 +00:00
let comment_view = CommentView::read(context.pool(), comment_id, Some(person_id)).await?;
2022-04-13 18:12:25 +00:00
Ok(CommentResponse {
comment_view,
recipient_ids: Vec::new(),
form_id: None,
})
}
}