2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 site::{Search, SearchResponse},
5 utils::{check_private_instance, get_local_user_view_from_jwt_opt},
7 use lemmy_apub::{fetcher::resolve_actor_identifier, objects::community::ApubCommunity};
9 source::{community::Community, local_site::LocalSite},
10 traits::DeleteableOrRemoveable,
11 utils::post_to_comment_sort_type,
14 use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery};
15 use lemmy_db_views_actor::{community_view::CommunityQuery, person_view::PersonQuery};
16 use lemmy_utils::{error::LemmyError, ConnectionId};
17 use lemmy_websocket::LemmyContext;
19 #[async_trait::async_trait(?Send)]
20 impl Perform for Search {
21 type Response = SearchResponse;
23 #[tracing::instrument(skip(context, _websocket_id))]
26 context: &Data<LemmyContext>,
27 _websocket_id: Option<ConnectionId>,
28 ) -> Result<SearchResponse, LemmyError> {
29 let data: &Search = self;
32 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
34 let local_site = LocalSite::read(context.pool()).await?;
36 check_private_instance(&local_user_view, &local_site)?;
38 let person_id = local_user_view.as_ref().map(|u| u.person.id);
39 let local_user = local_user_view.map(|l| l.local_user);
41 let mut posts = Vec::new();
42 let mut comments = Vec::new();
43 let mut communities = Vec::new();
44 let mut users = Vec::new();
46 // TODO no clean / non-nsfw searching rn
48 let q = data.q.clone();
50 let limit = data.limit;
52 let listing_type = data.listing_type;
53 let search_type = data.type_.unwrap_or(SearchType::All);
54 let community_id = data.community_id;
55 let community_actor_id = if let Some(name) = &data.community_name {
56 resolve_actor_identifier::<ApubCommunity, Community>(name, context, false)
63 let creator_id = data.creator_id;
65 SearchType::Posts => {
66 posts = PostQuery::builder()
69 .listing_type(listing_type)
70 .community_id(community_id)
71 .community_actor_id(community_actor_id)
72 .creator_id(creator_id)
73 .local_user(local_user.as_ref())
81 SearchType::Comments => {
82 comments = CommentQuery::builder()
84 .sort(sort.map(post_to_comment_sort_type))
85 .listing_type(listing_type)
87 .community_id(community_id)
88 .community_actor_id(community_actor_id)
89 .creator_id(creator_id)
90 .local_user(local_user.as_ref())
97 SearchType::Communities => {
98 communities = CommunityQuery::builder()
101 .listing_type(listing_type)
102 .search_term(Some(q))
103 .local_user(local_user.as_ref())
110 SearchType::Users => {
111 users = PersonQuery::builder()
112 .pool(context.pool())
114 .search_term(Some(q))
122 // If the community or creator is included, dont search communities or users
123 let community_or_creator_included =
124 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
125 let community_actor_id_2 = community_actor_id.clone();
127 let local_user_ = local_user.clone();
128 posts = PostQuery::builder()
129 .pool(context.pool())
131 .listing_type(listing_type)
132 .community_id(community_id)
133 .community_actor_id(community_actor_id_2)
134 .creator_id(creator_id)
135 .local_user(local_user_.as_ref())
136 .search_term(Some(q))
143 let q = data.q.clone();
144 let community_actor_id = community_actor_id.clone();
146 let local_user_ = local_user.clone();
147 comments = CommentQuery::builder()
148 .pool(context.pool())
149 .sort(sort.map(post_to_comment_sort_type))
150 .listing_type(listing_type)
151 .search_term(Some(q))
152 .community_id(community_id)
153 .community_actor_id(community_actor_id)
154 .creator_id(creator_id)
155 .local_user(local_user_.as_ref())
162 let q = data.q.clone();
164 communities = if community_or_creator_included {
167 CommunityQuery::builder()
168 .pool(context.pool())
170 .listing_type(listing_type)
171 .search_term(Some(q))
172 .local_user(local_user.as_ref())
180 let q = data.q.clone();
182 users = if community_or_creator_included {
185 PersonQuery::builder()
186 .pool(context.pool())
188 .search_term(Some(q))
197 posts = PostQuery::builder()
198 .pool(context.pool())
200 .listing_type(listing_type)
201 .community_id(community_id)
202 .community_actor_id(community_actor_id)
203 .creator_id(creator_id)
213 // Blank out deleted or removed info for non logged in users
214 if person_id.is_none() {
215 for cv in communities
217 .filter(|cv| cv.community.deleted || cv.community.removed)
219 cv.community = cv.clone().community.blank_out_deleted_or_removed_info();
224 .filter(|p| p.post.deleted || p.post.removed)
226 pv.post = pv.clone().post.blank_out_deleted_or_removed_info();
231 .filter(|cv| cv.comment.deleted || cv.comment.removed)
233 cv.comment = cv.clone().comment.blank_out_deleted_or_removed_info();
239 type_: search_type.to_string(),