2023-07-03 09:01:41 +00:00
|
|
|
use crate::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::config::Data;
|
2023-07-03 09:01:41 +00:00
|
|
|
use actix_web::web::{Json, Query};
|
2022-04-13 18:12:25 +00:00
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-04-13 18:12:25 +00:00
|
|
|
site::{Search, SearchResponse},
|
2024-09-23 15:27:06 +00:00
|
|
|
utils::{check_conflicting_like_filters, check_private_instance, is_admin},
|
2022-04-13 18:12:25 +00:00
|
|
|
};
|
2024-02-16 12:24:35 +00:00
|
|
|
use lemmy_db_schema::{source::community::Community, utils::post_to_comment_sort_type, SearchType};
|
|
|
|
use lemmy_db_views::{
|
|
|
|
comment_view::CommentQuery,
|
|
|
|
post_view::PostQuery,
|
|
|
|
structs::{LocalUserView, SiteView},
|
2022-07-30 03:55:59 +00:00
|
|
|
};
|
2022-08-04 19:30:17 +00:00
|
|
|
use lemmy_db_views_actor::{community_view::CommunityQuery, person_view::PersonQuery};
|
2024-09-16 15:18:16 +00:00
|
|
|
use lemmy_utils::error::LemmyResult;
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn search(
|
|
|
|
data: Query<Search>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: Option<LocalUserView>,
|
2024-04-10 14:14:11 +00:00
|
|
|
) -> LemmyResult<Json<SearchResponse>> {
|
2024-09-16 15:18:16 +00:00
|
|
|
let local_site = SiteView::read_local(&mut context.pool()).await?;
|
2023-07-03 09:01:41 +00:00
|
|
|
|
2024-02-16 12:24:35 +00:00
|
|
|
check_private_instance(&local_user_view, &local_site.local_site)?;
|
2023-07-03 09:01:41 +00:00
|
|
|
|
2023-08-11 09:13:14 +00:00
|
|
|
let is_admin = local_user_view
|
|
|
|
.as_ref()
|
|
|
|
.map(|luv| is_admin(luv).is_ok())
|
|
|
|
.unwrap_or_default();
|
2023-07-03 09:01:41 +00:00
|
|
|
|
|
|
|
let mut posts = Vec::new();
|
|
|
|
let mut comments = Vec::new();
|
|
|
|
let mut communities = Vec::new();
|
|
|
|
let mut users = Vec::new();
|
|
|
|
|
|
|
|
// TODO no clean / non-nsfw searching rn
|
|
|
|
|
2024-09-23 15:27:06 +00:00
|
|
|
let Query(Search {
|
|
|
|
q,
|
|
|
|
community_id,
|
|
|
|
community_name,
|
|
|
|
creator_id,
|
|
|
|
type_,
|
|
|
|
sort,
|
|
|
|
listing_type,
|
|
|
|
page,
|
|
|
|
limit,
|
|
|
|
post_title_only,
|
|
|
|
post_url_only,
|
|
|
|
saved_only,
|
|
|
|
liked_only,
|
|
|
|
disliked_only,
|
|
|
|
}) = data;
|
|
|
|
|
|
|
|
let q = q.clone();
|
|
|
|
let search_type = type_.unwrap_or(SearchType::All);
|
|
|
|
let community_id = if let Some(name) = &community_name {
|
2023-07-04 11:04:38 +00:00
|
|
|
Some(
|
|
|
|
resolve_actor_identifier::<ApubCommunity, Community>(name, &context, &local_user_view, false)
|
|
|
|
.await?,
|
|
|
|
)
|
|
|
|
.map(|c| c.id)
|
2023-07-03 09:01:41 +00:00
|
|
|
} else {
|
2024-09-23 15:27:06 +00:00
|
|
|
community_id
|
2023-07-03 09:01:41 +00:00
|
|
|
};
|
2024-06-15 01:51:24 +00:00
|
|
|
let local_user = local_user_view.as_ref().map(|l| &l.local_user);
|
2024-09-23 15:27:06 +00:00
|
|
|
|
|
|
|
check_conflicting_like_filters(liked_only, disliked_only)?;
|
2024-09-16 07:59:09 +00:00
|
|
|
|
|
|
|
let posts_query = PostQuery {
|
2024-09-23 15:27:06 +00:00
|
|
|
sort,
|
|
|
|
listing_type,
|
|
|
|
community_id,
|
|
|
|
creator_id,
|
2024-09-16 07:59:09 +00:00
|
|
|
local_user,
|
2024-09-23 15:27:06 +00:00
|
|
|
search_term: Some(q.clone()),
|
|
|
|
page,
|
|
|
|
limit,
|
|
|
|
title_only: post_title_only,
|
|
|
|
url_only: post_url_only,
|
|
|
|
liked_only,
|
|
|
|
disliked_only,
|
|
|
|
saved_only,
|
2024-09-16 07:59:09 +00:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let comment_query = CommentQuery {
|
2024-09-23 15:27:06 +00:00
|
|
|
sort: sort.map(post_to_comment_sort_type),
|
|
|
|
listing_type,
|
|
|
|
search_term: Some(q.clone()),
|
|
|
|
community_id,
|
|
|
|
creator_id,
|
2024-09-16 07:59:09 +00:00
|
|
|
local_user,
|
2024-09-23 15:27:06 +00:00
|
|
|
page,
|
|
|
|
limit,
|
|
|
|
liked_only,
|
|
|
|
disliked_only,
|
|
|
|
saved_only,
|
2024-09-16 07:59:09 +00:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let community_query = CommunityQuery {
|
2024-09-23 15:27:06 +00:00
|
|
|
sort,
|
|
|
|
listing_type,
|
|
|
|
search_term: Some(q.clone()),
|
2024-09-16 07:59:09 +00:00
|
|
|
local_user,
|
2024-09-23 15:27:06 +00:00
|
|
|
is_mod_or_admin: is_admin,
|
|
|
|
page,
|
|
|
|
limit,
|
2024-09-16 07:59:09 +00:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let person_query = PersonQuery {
|
|
|
|
sort,
|
2024-09-23 15:27:06 +00:00
|
|
|
search_term: Some(q.clone()),
|
|
|
|
listing_type,
|
|
|
|
page,
|
|
|
|
limit,
|
2024-09-16 07:59:09 +00:00
|
|
|
};
|
2024-01-24 09:34:09 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
match search_type {
|
|
|
|
SearchType::Posts => {
|
2024-09-16 07:59:09 +00:00
|
|
|
posts = posts_query
|
|
|
|
.list(&local_site.site, &mut context.pool())
|
|
|
|
.await?;
|
2023-07-03 09:01:41 +00:00
|
|
|
}
|
|
|
|
SearchType::Comments => {
|
2024-09-16 07:59:09 +00:00
|
|
|
comments = comment_query.list(&mut context.pool()).await?;
|
2023-07-03 09:01:41 +00:00
|
|
|
}
|
|
|
|
SearchType::Communities => {
|
2024-09-16 07:59:09 +00:00
|
|
|
communities = community_query
|
|
|
|
.list(&local_site.site, &mut context.pool())
|
|
|
|
.await?;
|
2023-07-03 09:01:41 +00:00
|
|
|
}
|
|
|
|
SearchType::Users => {
|
2024-09-16 07:59:09 +00:00
|
|
|
users = person_query.list(&mut context.pool()).await?;
|
2023-07-03 09:01:41 +00:00
|
|
|
}
|
|
|
|
SearchType::All => {
|
|
|
|
// If the community or creator is included, dont search communities or users
|
|
|
|
let community_or_creator_included =
|
2024-09-23 15:27:06 +00:00
|
|
|
community_id.is_some() || community_name.is_some() || creator_id.is_some();
|
2023-07-03 09:01:41 +00:00
|
|
|
|
2024-09-16 07:59:09 +00:00
|
|
|
posts = posts_query
|
|
|
|
.list(&local_site.site, &mut context.pool())
|
|
|
|
.await?;
|
2023-07-03 09:01:41 +00:00
|
|
|
|
2024-09-16 07:59:09 +00:00
|
|
|
comments = comment_query.list(&mut context.pool()).await?;
|
2023-07-03 09:01:41 +00:00
|
|
|
|
|
|
|
communities = if community_or_creator_included {
|
|
|
|
vec![]
|
|
|
|
} else {
|
2024-09-16 07:59:09 +00:00
|
|
|
community_query
|
|
|
|
.list(&local_site.site, &mut context.pool())
|
|
|
|
.await?
|
2023-07-03 09:01:41 +00:00
|
|
|
};
|
2022-04-13 18:12:25 +00:00
|
|
|
|
2023-07-03 09:01:41 +00:00
|
|
|
users = if community_or_creator_included {
|
|
|
|
vec![]
|
|
|
|
} else {
|
2024-09-16 07:59:09 +00:00
|
|
|
person_query.list(&mut context.pool()).await?
|
2023-07-03 09:01:41 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Return the jwt
|
|
|
|
Ok(Json(SearchResponse {
|
|
|
|
type_: search_type,
|
|
|
|
comments,
|
|
|
|
posts,
|
|
|
|
communities,
|
|
|
|
users,
|
|
|
|
}))
|
2022-04-13 18:12:25 +00:00
|
|
|
}
|