2021-03-25 19:19:40 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
2021-12-15 19:49:59 +00:00
|
|
|
use lemmy_api_common::{
|
2022-04-19 19:05:08 +00:00
|
|
|
post::{GetPost, GetPostResponse},
|
2022-05-03 17:44:13 +00:00
|
|
|
utils::{blocking, check_private_instance, get_local_user_view_from_jwt_opt, mark_post_as_read},
|
2021-11-16 17:03:09 +00:00
|
|
|
};
|
2022-04-13 18:12:25 +00:00
|
|
|
use lemmy_db_schema::traits::DeleteableOrRemoveable;
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_db_views::{comment_view::CommentQueryBuilder, structs::PostView};
|
|
|
|
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView};
|
2021-12-06 14:54:47 +00:00
|
|
|
use lemmy_utils::{ConnectionId, LemmyError};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext};
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for GetPost {
|
|
|
|
type Response = GetPostResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2021-03-25 19:19:40 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<GetPostResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &GetPost = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
2021-12-06 14:54:47 +00:00
|
|
|
get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
|
|
|
|
.await?;
|
2021-04-21 21:41:14 +00:00
|
|
|
|
2021-12-15 19:49:59 +00:00
|
|
|
check_private_instance(&local_user_view, context.pool()).await?;
|
|
|
|
|
2021-04-26 14:44:19 +00:00
|
|
|
let show_bot_accounts = local_user_view
|
|
|
|
.as_ref()
|
|
|
|
.map(|t| t.local_user.show_bot_accounts);
|
2021-03-25 19:19:40 +00:00
|
|
|
let person_id = local_user_view.map(|u| u.person.id);
|
|
|
|
|
|
|
|
let id = data.id;
|
2021-07-30 18:44:15 +00:00
|
|
|
let mut post_view = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
PostView::read(conn, id, person_id)
|
|
|
|
})
|
|
|
|
.await?
|
2022-03-16 20:11:49 +00:00
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_find_post"))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-04-24 22:26:50 +00:00
|
|
|
// Mark the post as read
|
|
|
|
if let Some(person_id) = person_id {
|
|
|
|
mark_post_as_read(person_id, id, context.pool()).await?;
|
|
|
|
}
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
let id = data.id;
|
2021-07-30 18:44:15 +00:00
|
|
|
let mut comments = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
CommentQueryBuilder::create(conn)
|
|
|
|
.my_person_id(person_id)
|
2021-04-21 21:41:14 +00:00
|
|
|
.show_bot_accounts(show_bot_accounts)
|
2021-03-25 19:19:40 +00:00
|
|
|
.post_id(id)
|
|
|
|
.limit(9999)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Necessary for the sidebar
|
2021-10-27 13:34:18 +00:00
|
|
|
let community_id = post_view.community.id;
|
2021-07-30 18:44:15 +00:00
|
|
|
let mut community_view = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
CommunityView::read(conn, community_id, person_id)
|
|
|
|
})
|
|
|
|
.await?
|
2022-03-16 20:11:49 +00:00
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_find_community"))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-10-27 13:34:18 +00:00
|
|
|
// Blank out deleted or removed info for non-logged in users
|
|
|
|
if person_id.is_none() {
|
|
|
|
if post_view.post.deleted || post_view.post.removed {
|
|
|
|
post_view.post = post_view.post.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
|
|
|
|
|
|
|
for cv in comments
|
|
|
|
.iter_mut()
|
|
|
|
.filter(|cv| cv.comment.deleted || cv.comment.removed)
|
|
|
|
{
|
|
|
|
cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
|
|
|
if community_view.community.deleted || community_view.community.removed {
|
|
|
|
community_view.community = community_view.community.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
2021-07-30 18:44:15 +00:00
|
|
|
}
|
|
|
|
|
2021-10-27 13:34:18 +00:00
|
|
|
let moderators = blocking(context.pool(), move |conn| {
|
|
|
|
CommunityModeratorView::for_community(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
let online = context
|
|
|
|
.chat_server()
|
|
|
|
.send(GetPostUsersOnline { post_id: data.id })
|
|
|
|
.await
|
|
|
|
.unwrap_or(1);
|
|
|
|
|
|
|
|
// Return the jwt
|
|
|
|
Ok(GetPostResponse {
|
|
|
|
post_view,
|
|
|
|
community_view,
|
|
|
|
comments,
|
|
|
|
moderators,
|
|
|
|
online,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|