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_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
38 let show_bot_accounts = local_user_view
40 .map(|t| t.local_user.show_bot_accounts);
41 let show_read_posts = local_user_view
43 .map(|t| t.local_user.show_read_posts);
45 let person_id = local_user_view.map(|u| u.person.id);
47 let mut posts = Vec::new();
48 let mut comments = Vec::new();
49 let mut communities = Vec::new();
50 let mut users = Vec::new();
52 // TODO no clean / non-nsfw searching rn
54 let q = data.q.to_owned();
56 let limit = data.limit;
58 let listing_type = data.listing_type;
59 let search_type = data.type_.unwrap_or(SearchType::All);
60 let community_id = data.community_id;
61 let community_actor_id = if let Some(name) = &data.community_name {
62 resolve_actor_identifier::<ApubCommunity, Community>(name, context)
69 let creator_id = data.creator_id;
71 SearchType::Posts => {
72 posts = blocking(context.pool(), move |conn| {
77 .show_bot_accounts(show_bot_accounts)
78 .show_read_posts(show_read_posts)
79 .listing_type(listing_type)
80 .community_id(community_id)
81 .community_actor_id(community_actor_id)
82 .creator_id(creator_id)
83 .my_person_id(person_id)
92 SearchType::Comments => {
93 comments = blocking(context.pool(), move |conn| {
94 CommentQuery::builder()
96 .sort(sort.map(post_to_comment_sort_type))
97 .listing_type(listing_type)
99 .show_bot_accounts(show_bot_accounts)
100 .community_id(community_id)
101 .community_actor_id(community_actor_id)
102 .creator_id(creator_id)
103 .my_person_id(person_id)
111 SearchType::Communities => {
112 communities = blocking(context.pool(), move |conn| {
113 CommunityQuery::builder()
116 .listing_type(listing_type)
117 .search_term(Some(q))
118 .my_person_id(person_id)
126 SearchType::Users => {
127 users = blocking(context.pool(), move |conn| {
128 PersonQuery::builder()
131 .search_term(Some(q))
140 // If the community or creator is included, dont search communities or users
141 let community_or_creator_included =
142 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
143 let community_actor_id_2 = community_actor_id.to_owned();
145 posts = blocking(context.pool(), move |conn| {
149 .show_nsfw(show_nsfw)
150 .show_bot_accounts(show_bot_accounts)
151 .show_read_posts(show_read_posts)
152 .listing_type(listing_type)
153 .community_id(community_id)
154 .community_actor_id(community_actor_id_2)
155 .creator_id(creator_id)
156 .my_person_id(person_id)
157 .search_term(Some(q))
165 let q = data.q.to_owned();
166 let community_actor_id = community_actor_id.to_owned();
168 comments = blocking(context.pool(), move |conn| {
169 CommentQuery::builder()
171 .sort(sort.map(post_to_comment_sort_type))
172 .listing_type(listing_type)
173 .search_term(Some(q))
174 .show_bot_accounts(show_bot_accounts)
175 .community_id(community_id)
176 .community_actor_id(community_actor_id)
177 .creator_id(creator_id)
178 .my_person_id(person_id)
186 let q = data.q.to_owned();
188 communities = if community_or_creator_included {
191 blocking(context.pool(), move |conn| {
192 CommunityQuery::builder()
195 .listing_type(listing_type)
196 .search_term(Some(q))
197 .my_person_id(person_id)
206 let q = data.q.to_owned();
208 users = if community_or_creator_included {
211 blocking(context.pool(), move |conn| {
212 PersonQuery::builder()
215 .search_term(Some(q))
225 posts = blocking(context.pool(), move |conn| {
229 .show_nsfw(show_nsfw)
230 .show_bot_accounts(show_bot_accounts)
231 .show_read_posts(show_read_posts)
232 .listing_type(listing_type)
233 .my_person_id(person_id)
234 .community_id(community_id)
235 .community_actor_id(community_actor_id)
236 .creator_id(creator_id)
247 // Blank out deleted or removed info for non logged in users
248 if person_id.is_none() {
249 for cv in communities
251 .filter(|cv| cv.community.deleted || cv.community.removed)
253 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
258 .filter(|p| p.post.deleted || p.post.removed)
260 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
265 .filter(|cv| cv.comment.deleted || cv.comment.removed)
267 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
273 type_: search_type.to_string(),