2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 site::{Search, SearchResponse},
5 utils::{blocking, 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 = blocking(context.pool(), LocalSite::read).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.to_owned();
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 = blocking(context.pool(), move |conn| {
70 .listing_type(listing_type)
71 .community_id(community_id)
72 .community_actor_id(community_actor_id)
73 .creator_id(creator_id)
74 .local_user(local_user.as_ref())
83 SearchType::Comments => {
84 comments = blocking(context.pool(), move |conn| {
85 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())
101 SearchType::Communities => {
102 communities = blocking(context.pool(), move |conn| {
103 CommunityQuery::builder()
106 .listing_type(listing_type)
107 .search_term(Some(q))
108 .local_user(local_user.as_ref())
116 SearchType::Users => {
117 users = blocking(context.pool(), move |conn| {
118 PersonQuery::builder()
121 .search_term(Some(q))
130 // If the community or creator is included, dont search communities or users
131 let community_or_creator_included =
132 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
133 let community_actor_id_2 = community_actor_id.to_owned();
135 let local_user_ = local_user.clone();
136 posts = blocking(context.pool(), move |conn| {
140 .listing_type(listing_type)
141 .community_id(community_id)
142 .community_actor_id(community_actor_id_2)
143 .creator_id(creator_id)
144 .local_user(local_user_.as_ref())
145 .search_term(Some(q))
153 let q = data.q.to_owned();
154 let community_actor_id = community_actor_id.to_owned();
156 let local_user_ = local_user.clone();
157 comments = blocking(context.pool(), move |conn| {
158 CommentQuery::builder()
160 .sort(sort.map(post_to_comment_sort_type))
161 .listing_type(listing_type)
162 .search_term(Some(q))
163 .community_id(community_id)
164 .community_actor_id(community_actor_id)
165 .creator_id(creator_id)
166 .local_user(local_user_.as_ref())
174 let q = data.q.to_owned();
176 communities = if community_or_creator_included {
179 blocking(context.pool(), move |conn| {
180 CommunityQuery::builder()
183 .listing_type(listing_type)
184 .search_term(Some(q))
185 .local_user(local_user.as_ref())
194 let q = data.q.to_owned();
196 users = if community_or_creator_included {
199 blocking(context.pool(), move |conn| {
200 PersonQuery::builder()
203 .search_term(Some(q))
213 posts = blocking(context.pool(), move |conn| {
217 .listing_type(listing_type)
218 .community_id(community_id)
219 .community_actor_id(community_actor_id)
220 .creator_id(creator_id)
231 // Blank out deleted or removed info for non logged in users
232 if person_id.is_none() {
233 for cv in communities
235 .filter(|cv| cv.community.deleted || cv.community.removed)
237 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
242 .filter(|p| p.post.deleted || p.post.removed)
244 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
249 .filter(|cv| cv.comment.deleted || cv.comment.removed)
251 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
257 type_: search_type.to_string(),