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};
8 use lemmy_db_schema::{source::community::Community, traits::DeleteableOrRemoveable, SearchType};
9 use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder};
10 use lemmy_db_views_actor::{
11 community_view::CommunityQueryBuilder,
12 person_view::PersonQueryBuilder,
14 use lemmy_utils::{ConnectionId, LemmyError};
15 use lemmy_websocket::LemmyContext;
17 #[async_trait::async_trait(?Send)]
18 impl Perform for Search {
19 type Response = SearchResponse;
21 #[tracing::instrument(skip(context, _websocket_id))]
24 context: &Data<LemmyContext>,
25 _websocket_id: Option<ConnectionId>,
26 ) -> Result<SearchResponse, LemmyError> {
27 let data: &Search = self;
30 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
33 check_private_instance(&local_user_view, context.pool()).await?;
35 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
36 let show_bot_accounts = local_user_view
38 .map(|t| t.local_user.show_bot_accounts);
39 let show_read_posts = local_user_view
41 .map(|t| t.local_user.show_read_posts);
43 let person_id = local_user_view.map(|u| u.person.id);
45 let mut posts = Vec::new();
46 let mut comments = Vec::new();
47 let mut communities = Vec::new();
48 let mut users = Vec::new();
50 // TODO no clean / non-nsfw searching rn
52 let q = data.q.to_owned();
54 let limit = data.limit;
56 let listing_type = data.listing_type;
57 let search_type = data.type_.unwrap_or(SearchType::All);
58 let community_id = data.community_id;
59 let community_actor_id = if let Some(name) = &data.community_name {
60 resolve_actor_identifier::<ApubCommunity, Community>(name, context)
67 let creator_id = data.creator_id;
69 SearchType::Posts => {
70 posts = blocking(context.pool(), move |conn| {
71 PostQueryBuilder::create(conn)
74 .show_bot_accounts(show_bot_accounts)
75 .show_read_posts(show_read_posts)
76 .listing_type(listing_type)
77 .community_id(community_id)
78 .community_actor_id(community_actor_id)
79 .creator_id(creator_id)
80 .my_person_id(person_id)
88 SearchType::Comments => {
89 comments = blocking(context.pool(), move |conn| {
90 CommentQueryBuilder::create(conn)
92 .listing_type(listing_type)
94 .show_bot_accounts(show_bot_accounts)
95 .community_id(community_id)
96 .community_actor_id(community_actor_id)
97 .creator_id(creator_id)
98 .my_person_id(person_id)
105 SearchType::Communities => {
106 communities = blocking(context.pool(), move |conn| {
107 CommunityQueryBuilder::create(conn)
109 .listing_type(listing_type)
111 .my_person_id(person_id)
118 SearchType::Users => {
119 users = blocking(context.pool(), move |conn| {
120 PersonQueryBuilder::create(conn)
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 posts = blocking(context.pool(), move |conn| {
136 PostQueryBuilder::create(conn)
138 .show_nsfw(show_nsfw)
139 .show_bot_accounts(show_bot_accounts)
140 .show_read_posts(show_read_posts)
141 .listing_type(listing_type)
142 .community_id(community_id)
143 .community_actor_id(community_actor_id_2)
144 .creator_id(creator_id)
145 .my_person_id(person_id)
153 let q = data.q.to_owned();
154 let community_actor_id = community_actor_id.to_owned();
156 comments = blocking(context.pool(), move |conn| {
157 CommentQueryBuilder::create(conn)
159 .listing_type(listing_type)
161 .show_bot_accounts(show_bot_accounts)
162 .community_id(community_id)
163 .community_actor_id(community_actor_id)
164 .creator_id(creator_id)
165 .my_person_id(person_id)
172 let q = data.q.to_owned();
174 communities = if community_or_creator_included {
177 blocking(context.pool(), move |conn| {
178 CommunityQueryBuilder::create(conn)
180 .listing_type(listing_type)
182 .my_person_id(person_id)
190 let q = data.q.to_owned();
192 users = if community_or_creator_included {
195 blocking(context.pool(), move |conn| {
196 PersonQueryBuilder::create(conn)
207 posts = blocking(context.pool(), move |conn| {
208 PostQueryBuilder::create(conn)
210 .show_nsfw(show_nsfw)
211 .show_bot_accounts(show_bot_accounts)
212 .show_read_posts(show_read_posts)
213 .listing_type(listing_type)
214 .my_person_id(person_id)
215 .community_id(community_id)
216 .community_actor_id(community_actor_id)
217 .creator_id(creator_id)
227 // Blank out deleted or removed info for non logged in users
228 if person_id.is_none() {
229 for cv in communities
231 .filter(|cv| cv.community.deleted || cv.community.removed)
233 cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
238 .filter(|p| p.post.deleted || p.post.removed)
240 pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
245 .filter(|cv| cv.comment.deleted || cv.comment.removed)
247 cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
253 type_: search_type.to_string(),