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,
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())
35 check_private_instance(&local_user_view, context.pool()).await?;
37 let show_bot_accounts = local_user_view
39 .map(|t| t.local_user.show_bot_accounts);
41 let person_id = local_user_view.as_ref().map(|u| u.person.id);
43 let mut posts = Vec::new();
44 let mut comments = Vec::new();
45 let mut communities = Vec::new();
46 let mut users = Vec::new();
48 // TODO no clean / non-nsfw searching rn
50 let q = data.q.to_owned();
52 let limit = data.limit;
54 let listing_type = data.listing_type;
55 let search_type = data.type_.unwrap_or(SearchType::All);
56 let community_id = data.community_id;
57 let community_actor_id = if let Some(name) = &data.community_name {
58 resolve_actor_identifier::<ApubCommunity, Community>(name, context)
65 let creator_id = data.creator_id;
67 SearchType::Posts => {
68 posts = blocking(context.pool(), move |conn| {
72 .listing_type(listing_type)
73 .community_id(community_id)
74 .community_actor_id(community_actor_id)
75 .creator_id(creator_id)
76 .my_person_id(person_id)
85 SearchType::Comments => {
86 comments = blocking(context.pool(), move |conn| {
87 CommentQuery::builder()
89 .sort(sort.map(post_to_comment_sort_type))
90 .listing_type(listing_type)
92 .show_bot_accounts(show_bot_accounts)
93 .community_id(community_id)
94 .community_actor_id(community_actor_id)
95 .creator_id(creator_id)
96 .my_person_id(person_id)
104 SearchType::Communities => {
105 communities = blocking(context.pool(), move |conn| {
106 CommunityQuery::builder()
109 .listing_type(listing_type)
110 .search_term(Some(q))
111 .my_person_id(person_id)
119 SearchType::Users => {
120 users = blocking(context.pool(), move |conn| {
121 PersonQuery::builder()
124 .search_term(Some(q))
133 // If the community or creator is included, dont search communities or users
134 let community_or_creator_included =
135 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
136 let community_actor_id_2 = community_actor_id.to_owned();
138 posts = blocking(context.pool(), move |conn| {
142 .listing_type(listing_type)
143 .community_id(community_id)
144 .community_actor_id(community_actor_id_2)
145 .creator_id(creator_id)
146 .my_person_id(person_id)
147 .search_term(Some(q))
155 let q = data.q.to_owned();
156 let community_actor_id = community_actor_id.to_owned();
158 comments = blocking(context.pool(), move |conn| {
159 CommentQuery::builder()
161 .sort(sort.map(post_to_comment_sort_type))
162 .listing_type(listing_type)
163 .search_term(Some(q))
164 .show_bot_accounts(show_bot_accounts)
165 .community_id(community_id)
166 .community_actor_id(community_actor_id)
167 .creator_id(creator_id)
168 .my_person_id(person_id)
176 let q = data.q.to_owned();
178 communities = if community_or_creator_included {
181 blocking(context.pool(), move |conn| {
182 CommunityQuery::builder()
185 .listing_type(listing_type)
186 .search_term(Some(q))
187 .my_person_id(person_id)
196 let q = data.q.to_owned();
198 users = if community_or_creator_included {
201 blocking(context.pool(), move |conn| {
202 PersonQuery::builder()
205 .search_term(Some(q))
215 posts = blocking(context.pool(), move |conn| {
219 .listing_type(listing_type)
220 .community_id(community_id)
221 .community_actor_id(community_actor_id)
222 .creator_id(creator_id)
233 // Blank out deleted or removed info for non logged in users
234 if person_id.is_none() {
235 for cv in communities
237 .filter(|cv| cv.community.deleted || cv.community.removed)
239 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
244 .filter(|p| p.post.deleted || p.post.removed)
246 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
251 .filter(|cv| cv.comment.deleted || cv.comment.removed)
253 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
259 type_: search_type.to_string(),