3 fetcher::resolve_actor_identifier,
4 objects::community::ApubCommunity,
6 use actix_web::web::Data;
7 use lemmy_api_common::{
9 site::{Search, SearchResponse},
10 utils::{check_private_instance, get_local_user_view_from_jwt_opt},
12 use lemmy_db_schema::{
13 source::{community::Community, local_site::LocalSite},
14 traits::DeleteableOrRemoveable,
15 utils::post_to_comment_sort_type,
18 use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery};
19 use lemmy_db_views_actor::{community_view::CommunityQuery, person_view::PersonQuery};
20 use lemmy_utils::{error::LemmyError, ConnectionId};
22 #[async_trait::async_trait(?Send)]
23 impl PerformApub for Search {
24 type Response = SearchResponse;
26 #[tracing::instrument(skip(context, _websocket_id))]
29 context: &Data<LemmyContext>,
30 _websocket_id: Option<ConnectionId>,
31 ) -> Result<SearchResponse, LemmyError> {
32 let data: &Search = self;
35 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
37 let local_site = LocalSite::read(context.pool()).await?;
39 check_private_instance(&local_user_view, &local_site)?;
41 let person_id = local_user_view.as_ref().map(|u| u.person.id);
42 let local_user = local_user_view.map(|l| l.local_user);
44 let mut posts = Vec::new();
45 let mut comments = Vec::new();
46 let mut communities = Vec::new();
47 let mut users = Vec::new();
49 // TODO no clean / non-nsfw searching rn
51 let q = data.q.clone();
53 let limit = data.limit;
55 let listing_type = data.listing_type;
56 let search_type = data.type_.unwrap_or(SearchType::All);
57 let community_id = data.community_id;
58 let community_actor_id = if let Some(name) = &data.community_name {
59 resolve_actor_identifier::<ApubCommunity, Community>(name, context, false)
66 let creator_id = data.creator_id;
68 SearchType::Posts => {
69 posts = PostQuery::builder()
72 .listing_type(listing_type)
73 .community_id(community_id)
74 .community_actor_id(community_actor_id)
75 .creator_id(creator_id)
76 .local_user(local_user.as_ref())
84 SearchType::Comments => {
85 comments = CommentQuery::builder()
87 .sort(sort.map(post_to_comment_sort_type))
88 .listing_type(listing_type)
90 .community_id(community_id)
91 .community_actor_id(community_actor_id)
92 .creator_id(creator_id)
93 .local_user(local_user.as_ref())
100 SearchType::Communities => {
101 communities = CommunityQuery::builder()
102 .pool(context.pool())
104 .listing_type(listing_type)
105 .search_term(Some(q))
106 .local_user(local_user.as_ref())
113 SearchType::Users => {
114 users = PersonQuery::builder()
115 .pool(context.pool())
117 .search_term(Some(q))
125 // If the community or creator is included, dont search communities or users
126 let community_or_creator_included =
127 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
128 let community_actor_id_2 = community_actor_id.clone();
130 let local_user_ = local_user.clone();
131 posts = PostQuery::builder()
132 .pool(context.pool())
134 .listing_type(listing_type)
135 .community_id(community_id)
136 .community_actor_id(community_actor_id_2)
137 .creator_id(creator_id)
138 .local_user(local_user_.as_ref())
139 .search_term(Some(q))
146 let q = data.q.clone();
147 let community_actor_id = community_actor_id.clone();
149 let local_user_ = local_user.clone();
150 comments = CommentQuery::builder()
151 .pool(context.pool())
152 .sort(sort.map(post_to_comment_sort_type))
153 .listing_type(listing_type)
154 .search_term(Some(q))
155 .community_id(community_id)
156 .community_actor_id(community_actor_id)
157 .creator_id(creator_id)
158 .local_user(local_user_.as_ref())
165 let q = data.q.clone();
167 communities = if community_or_creator_included {
170 CommunityQuery::builder()
171 .pool(context.pool())
173 .listing_type(listing_type)
174 .search_term(Some(q))
175 .local_user(local_user.as_ref())
183 let q = data.q.clone();
185 users = if community_or_creator_included {
188 PersonQuery::builder()
189 .pool(context.pool())
191 .search_term(Some(q))
200 posts = PostQuery::builder()
201 .pool(context.pool())
203 .listing_type(listing_type)
204 .community_id(community_id)
205 .community_actor_id(community_actor_id)
206 .creator_id(creator_id)
216 // Blank out deleted or removed info for non logged in users
217 if person_id.is_none() {
218 for cv in communities
220 .filter(|cv| cv.community.deleted || cv.community.removed)
222 cv.community = cv.clone().community.blank_out_deleted_or_removed_info();
227 .filter(|p| p.post.deleted || p.post.removed)
229 pv.post = pv.clone().post.blank_out_deleted_or_removed_info();
234 .filter(|cv| cv.comment.deleted || cv.comment.removed)
236 cv.comment = cv.clone().comment.blank_out_deleted_or_removed_info();
242 type_: search_type.to_string(),