2022-04-13 18:12:25 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
2022-04-19 19:05:08 +00:00
|
|
|
post::{GetPosts, GetPostsResponse},
|
2022-05-03 17:44:13 +00:00
|
|
|
utils::{blocking, check_private_instance, get_local_user_view_from_jwt_opt},
|
2022-04-13 18:12:25 +00:00
|
|
|
};
|
|
|
|
use lemmy_apub::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
|
|
|
|
use lemmy_db_schema::{
|
2022-04-19 19:05:08 +00:00
|
|
|
source::{community::Community, site::Site},
|
2022-04-13 18:12:25 +00:00
|
|
|
traits::DeleteableOrRemoveable,
|
2022-05-03 17:44:13 +00:00
|
|
|
utils::{from_opt_str_to_opt_enum, ListingType, SortType},
|
2022-04-13 18:12:25 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_views::post_view::PostQueryBuilder;
|
|
|
|
use lemmy_utils::{ConnectionId, LemmyError};
|
|
|
|
use lemmy_websocket::LemmyContext;
|
2022-04-19 19:05:08 +00:00
|
|
|
use std::str::FromStr;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for GetPosts {
|
|
|
|
type Response = GetPostsResponse;
|
|
|
|
|
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
|
|
|
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.as_ref(), context.pool(), context.secret())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
check_private_instance(&local_user_view, context.pool()).await?;
|
|
|
|
|
|
|
|
let person_id = local_user_view.to_owned().map(|l| l.person.id);
|
|
|
|
|
|
|
|
let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
|
|
|
|
let show_bot_accounts = local_user_view
|
|
|
|
.as_ref()
|
|
|
|
.map(|t| t.local_user.show_bot_accounts);
|
|
|
|
let show_read_posts = local_user_view
|
|
|
|
.as_ref()
|
|
|
|
.map(|t| t.local_user.show_read_posts);
|
|
|
|
|
|
|
|
let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
|
2022-04-19 19:05:08 +00:00
|
|
|
let listing_type: ListingType = match from_opt_str_to_opt_enum(&data.type_) {
|
|
|
|
Some(l) => l,
|
|
|
|
None => {
|
|
|
|
let site = blocking(context.pool(), Site::read_local_site).await??;
|
|
|
|
ListingType::from_str(&site.default_post_listing_type)?
|
|
|
|
}
|
|
|
|
};
|
2022-04-13 18:12:25 +00:00
|
|
|
|
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
|
|
|
let community_id = data.community_id;
|
|
|
|
let community_actor_id = if let Some(name) = &data.community_name {
|
|
|
|
resolve_actor_identifier::<ApubCommunity, Community>(name, context)
|
|
|
|
.await
|
|
|
|
.ok()
|
|
|
|
.map(|c| c.actor_id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
let saved_only = data.saved_only;
|
|
|
|
|
|
|
|
let mut posts = blocking(context.pool(), move |conn| {
|
|
|
|
PostQueryBuilder::create(conn)
|
|
|
|
.listing_type(listing_type)
|
|
|
|
.sort(sort)
|
|
|
|
.show_nsfw(show_nsfw)
|
|
|
|
.show_bot_accounts(show_bot_accounts)
|
|
|
|
.show_read_posts(show_read_posts)
|
|
|
|
.community_id(community_id)
|
|
|
|
.community_actor_id(community_actor_id)
|
|
|
|
.saved_only(saved_only)
|
|
|
|
.my_person_id(person_id)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await?
|
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_get_posts"))?;
|
|
|
|
|
|
|
|
// Blank out deleted or removed info for non-logged in users
|
|
|
|
if person_id.is_none() {
|
|
|
|
for pv in posts
|
|
|
|
.iter_mut()
|
|
|
|
.filter(|p| p.post.deleted || p.post.removed)
|
|
|
|
{
|
|
|
|
pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
|
|
|
|
|
|
|
for pv in posts
|
|
|
|
.iter_mut()
|
|
|
|
.filter(|p| p.community.deleted || p.community.removed)
|
|
|
|
{
|
|
|
|
pv.community = pv.to_owned().community.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(GetPostsResponse { posts })
|
|
|
|
}
|
|
|
|
}
|