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::{from_opt_str_to_opt_enum, ListingType, SearchType, SortType},
13 use lemmy_db_views::{comment_view::CommentQueryBuilder, post_view::PostQueryBuilder};
14 use lemmy_db_views_actor::{
15 community_view::CommunityQueryBuilder,
16 person_view::PersonQueryBuilder,
18 use lemmy_utils::{ConnectionId, LemmyError};
19 use lemmy_websocket::LemmyContext;
21 #[async_trait::async_trait(?Send)]
22 impl Perform for Search {
23 type Response = SearchResponse;
25 #[tracing::instrument(skip(context, _websocket_id))]
28 context: &Data<LemmyContext>,
29 _websocket_id: Option<ConnectionId>,
30 ) -> Result<SearchResponse, LemmyError> {
31 let data: &Search = self;
34 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
37 check_private_instance(&local_user_view, context.pool()).await?;
39 let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
40 let show_bot_accounts = local_user_view
42 .map(|t| t.local_user.show_bot_accounts);
43 let show_read_posts = local_user_view
45 .map(|t| t.local_user.show_read_posts);
47 let person_id = local_user_view.map(|u| u.person.id);
49 let mut posts = Vec::new();
50 let mut comments = Vec::new();
51 let mut communities = Vec::new();
52 let mut users = Vec::new();
54 // TODO no clean / non-nsfw searching rn
56 let q = data.q.to_owned();
58 let limit = data.limit;
59 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
60 let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.listing_type);
61 let search_type: SearchType = from_opt_str_to_opt_enum(&data.type_).unwrap_or(SearchType::All);
62 let community_id = data.community_id;
63 let community_actor_id = if let Some(name) = &data.community_name {
64 resolve_actor_identifier::<ApubCommunity, Community>(name, context)
71 let creator_id = data.creator_id;
73 SearchType::Posts => {
74 posts = blocking(context.pool(), move |conn| {
75 PostQueryBuilder::create(conn)
78 .show_bot_accounts(show_bot_accounts)
79 .show_read_posts(show_read_posts)
80 .listing_type(listing_type)
81 .community_id(community_id)
82 .community_actor_id(community_actor_id)
83 .creator_id(creator_id)
84 .my_person_id(person_id)
92 SearchType::Comments => {
93 comments = blocking(context.pool(), move |conn| {
94 CommentQueryBuilder::create(conn)
96 .listing_type(listing_type)
98 .show_bot_accounts(show_bot_accounts)
99 .community_id(community_id)
100 .community_actor_id(community_actor_id)
101 .creator_id(creator_id)
102 .my_person_id(person_id)
109 SearchType::Communities => {
110 communities = blocking(context.pool(), move |conn| {
111 CommunityQueryBuilder::create(conn)
113 .listing_type(listing_type)
115 .my_person_id(person_id)
122 SearchType::Users => {
123 users = blocking(context.pool(), move |conn| {
124 PersonQueryBuilder::create(conn)
134 // If the community or creator is included, dont search communities or users
135 let community_or_creator_included =
136 data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
137 let community_actor_id_2 = community_actor_id.to_owned();
139 posts = blocking(context.pool(), move |conn| {
140 PostQueryBuilder::create(conn)
142 .show_nsfw(show_nsfw)
143 .show_bot_accounts(show_bot_accounts)
144 .show_read_posts(show_read_posts)
145 .listing_type(listing_type)
146 .community_id(community_id)
147 .community_actor_id(community_actor_id_2)
148 .creator_id(creator_id)
149 .my_person_id(person_id)
157 let q = data.q.to_owned();
158 let community_actor_id = community_actor_id.to_owned();
160 comments = blocking(context.pool(), move |conn| {
161 CommentQueryBuilder::create(conn)
163 .listing_type(listing_type)
165 .show_bot_accounts(show_bot_accounts)
166 .community_id(community_id)
167 .community_actor_id(community_actor_id)
168 .creator_id(creator_id)
169 .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 CommunityQueryBuilder::create(conn)
184 .listing_type(listing_type)
186 .my_person_id(person_id)
194 let q = data.q.to_owned();
196 users = if community_or_creator_included {
199 blocking(context.pool(), move |conn| {
200 PersonQueryBuilder::create(conn)
211 posts = blocking(context.pool(), move |conn| {
212 PostQueryBuilder::create(conn)
214 .show_nsfw(show_nsfw)
215 .show_bot_accounts(show_bot_accounts)
216 .show_read_posts(show_read_posts)
217 .listing_type(listing_type)
218 .my_person_id(person_id)
219 .community_id(community_id)
220 .community_actor_id(community_actor_id)
221 .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(),