2021-03-25 19:19:40 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
2021-04-21 21:41:14 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
blocking,
|
|
|
|
get_local_user_view_from_jwt_opt,
|
2021-04-24 22:26:50 +00:00
|
|
|
mark_post_as_read,
|
2021-04-21 21:41:14 +00:00
|
|
|
post::*,
|
|
|
|
user_show_bot_accounts,
|
|
|
|
user_show_nsfw,
|
2021-04-24 22:26:50 +00:00
|
|
|
user_show_read_posts,
|
2021-04-21 21:41:14 +00:00
|
|
|
};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_db_queries::{ListingType, SortType};
|
|
|
|
use lemmy_db_views::{
|
|
|
|
comment_view::CommentQueryBuilder,
|
|
|
|
post_view::{PostQueryBuilder, PostView},
|
|
|
|
};
|
|
|
|
use lemmy_db_views_actor::{
|
|
|
|
community_moderator_view::CommunityModeratorView,
|
|
|
|
community_view::CommunityView,
|
|
|
|
};
|
|
|
|
use lemmy_utils::{ApiError, ConnectionId, LemmyError};
|
|
|
|
use lemmy_websocket::{messages::GetPostUsersOnline, LemmyContext};
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for GetPost {
|
|
|
|
type Response = GetPostResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<GetPostResponse, LemmyError> {
|
|
|
|
let data: &GetPost = &self;
|
|
|
|
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
|
2021-04-21 21:41:14 +00:00
|
|
|
|
|
|
|
let show_bot_accounts = user_show_bot_accounts(&local_user_view);
|
|
|
|
|
2021-03-25 19:19:40 +00:00
|
|
|
let person_id = local_user_view.map(|u| u.person.id);
|
|
|
|
|
|
|
|
let id = data.id;
|
2021-04-16 13:10:43 +00:00
|
|
|
let post_view = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
PostView::read(conn, id, person_id)
|
|
|
|
})
|
|
|
|
.await?
|
2021-04-16 13:10:43 +00:00
|
|
|
.map_err(|_| ApiError::err("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;
|
|
|
|
let comments = blocking(context.pool(), move |conn| {
|
|
|
|
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??;
|
|
|
|
|
|
|
|
let community_id = post_view.community.id;
|
|
|
|
let moderators = blocking(context.pool(), move |conn| {
|
|
|
|
CommunityModeratorView::for_community(conn, community_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Necessary for the sidebar
|
2021-04-16 13:10:43 +00:00
|
|
|
let community_view = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
CommunityView::read(conn, community_id, person_id)
|
|
|
|
})
|
|
|
|
.await?
|
2021-04-16 13:10:43 +00:00
|
|
|
.map_err(|_| ApiError::err("couldnt_find_community"))?;
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for GetPosts {
|
|
|
|
type Response = GetPostsResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<GetPostsResponse, LemmyError> {
|
|
|
|
let data: &GetPosts = &self;
|
|
|
|
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
|
|
|
|
|
2021-03-25 19:30:15 +00:00
|
|
|
let person_id = local_user_view.to_owned().map(|l| l.person.id);
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-04-21 21:41:14 +00:00
|
|
|
let show_nsfw = user_show_nsfw(&local_user_view);
|
|
|
|
let show_bot_accounts = user_show_bot_accounts(&local_user_view);
|
2021-04-24 22:26:50 +00:00
|
|
|
let show_read_posts = user_show_read_posts(&local_user_view);
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
let type_ = ListingType::from_str(&data.type_)?;
|
|
|
|
let sort = SortType::from_str(&data.sort)?;
|
|
|
|
|
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
|
|
|
let community_id = data.community_id;
|
|
|
|
let community_name = data.community_name.to_owned();
|
|
|
|
let saved_only = data.saved_only;
|
|
|
|
|
2021-04-16 13:10:43 +00:00
|
|
|
let posts = blocking(context.pool(), move |conn| {
|
2021-03-25 19:19:40 +00:00
|
|
|
PostQueryBuilder::create(conn)
|
|
|
|
.listing_type(&type_)
|
|
|
|
.sort(&sort)
|
|
|
|
.show_nsfw(show_nsfw)
|
2021-04-21 21:41:14 +00:00
|
|
|
.show_bot_accounts(show_bot_accounts)
|
2021-04-24 22:26:50 +00:00
|
|
|
.show_read_posts(show_read_posts)
|
2021-03-25 19:19:40 +00:00
|
|
|
.community_id(community_id)
|
|
|
|
.community_name(community_name)
|
|
|
|
.saved_only(saved_only)
|
|
|
|
.my_person_id(person_id)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await?
|
2021-04-16 13:10:43 +00:00
|
|
|
.map_err(|_| ApiError::err("couldnt_get_posts"))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
Ok(GetPostsResponse { posts })
|
|
|
|
}
|
|
|
|
}
|