2 use actix_web::web::Data;
4 use lemmy_api_common::{
6 build_federated_instances,
7 get_local_user_settings_view_from_jwt,
8 get_local_user_view_from_jwt,
9 get_local_user_view_from_jwt_opt,
12 user_show_bot_accounts,
15 use lemmy_apub::fetcher::search::search_by_apub_id;
16 use lemmy_db_queries::{source::site::Site_, Crud, ListingType, SearchType, SortType};
17 use lemmy_db_schema::source::{moderator::*, site::Site};
19 comment_view::CommentQueryBuilder,
20 post_view::PostQueryBuilder,
23 use lemmy_db_views_actor::{
24 community_view::CommunityQueryBuilder,
25 person_view::{PersonQueryBuilder, PersonViewSafe},
27 use lemmy_db_views_moderator::{
28 mod_add_community_view::ModAddCommunityView,
29 mod_add_view::ModAddView,
30 mod_ban_from_community_view::ModBanFromCommunityView,
31 mod_ban_view::ModBanView,
32 mod_lock_post_view::ModLockPostView,
33 mod_remove_comment_view::ModRemoveCommentView,
34 mod_remove_community_view::ModRemoveCommunityView,
35 mod_remove_post_view::ModRemovePostView,
36 mod_sticky_post_view::ModStickyPostView,
40 settings::structs::Settings,
46 use lemmy_websocket::LemmyContext;
48 use std::str::FromStr;
50 #[async_trait::async_trait(?Send)]
51 impl Perform for GetModlog {
52 type Response = GetModlogResponse;
56 context: &Data<LemmyContext>,
57 _websocket_id: Option<ConnectionId>,
58 ) -> Result<GetModlogResponse, LemmyError> {
59 let data: &GetModlog = &self;
61 let community_id = data.community_id;
62 let mod_person_id = data.mod_person_id;
64 let limit = data.limit;
65 let removed_posts = blocking(context.pool(), move |conn| {
66 ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
70 let locked_posts = blocking(context.pool(), move |conn| {
71 ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
75 let stickied_posts = blocking(context.pool(), move |conn| {
76 ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
80 let removed_comments = blocking(context.pool(), move |conn| {
81 ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
85 let banned_from_community = blocking(context.pool(), move |conn| {
86 ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
90 let added_to_community = blocking(context.pool(), move |conn| {
91 ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
95 // These arrays are only for the full modlog, when a community isn't given
96 let (removed_communities, banned, added) = if data.community_id.is_none() {
97 blocking(context.pool(), move |conn| {
99 ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
100 ModBanView::list(conn, mod_person_id, page, limit)?,
101 ModAddView::list(conn, mod_person_id, page, limit)?,
102 )) as Result<_, LemmyError>
106 (Vec::new(), Vec::new(), Vec::new())
110 Ok(GetModlogResponse {
116 banned_from_community,
124 #[async_trait::async_trait(?Send)]
125 impl Perform for Search {
126 type Response = SearchResponse;
130 context: &Data<LemmyContext>,
131 _websocket_id: Option<ConnectionId>,
132 ) -> Result<SearchResponse, LemmyError> {
133 let data: &Search = &self;
135 match search_by_apub_id(&data.q, context).await {
136 Ok(r) => return Ok(r),
137 Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
140 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
142 let show_nsfw = user_show_nsfw(&local_user_view);
143 let show_bot_accounts = user_show_bot_accounts(&local_user_view);
145 let person_id = local_user_view.map(|u| u.person.id);
147 let mut posts = Vec::new();
148 let mut comments = Vec::new();
149 let mut communities = Vec::new();
150 let mut users = Vec::new();
152 // TODO no clean / non-nsfw searching rn
154 let q = data.q.to_owned();
155 let page = data.page;
156 let limit = data.limit;
157 let sort = SortType::from_str(&data.sort)?;
158 let type_ = SearchType::from_str(&data.type_)?;
159 let listing_type = ListingType::from_str(&data.listing_type)?;
160 let community_id = data.community_id;
161 let community_name = data.community_name.to_owned();
162 let creator_id = data.creator_id;
164 SearchType::Posts => {
165 posts = blocking(context.pool(), move |conn| {
166 PostQueryBuilder::create(conn)
168 .show_nsfw(show_nsfw)
169 .show_bot_accounts(show_bot_accounts)
170 .listing_type(&listing_type)
172 .community_id(community_id)
173 .community_name(community_name)
174 .creator_id(creator_id)
175 .my_person_id(person_id)
183 SearchType::Comments => {
184 comments = blocking(context.pool(), move |conn| {
185 CommentQueryBuilder::create(&conn)
187 .listing_type(&listing_type)
189 .show_bot_accounts(show_bot_accounts)
190 .community_id(community_id)
191 .community_name(community_name)
192 .creator_id(creator_id)
193 .my_person_id(person_id)
200 SearchType::Communities => {
201 communities = blocking(context.pool(), move |conn| {
202 CommunityQueryBuilder::create(conn)
204 .listing_type(&listing_type)
206 .my_person_id(person_id)
213 SearchType::Users => {
214 users = blocking(context.pool(), move |conn| {
215 PersonQueryBuilder::create(conn)
225 posts = blocking(context.pool(), move |conn| {
226 PostQueryBuilder::create(conn)
228 .show_nsfw(show_nsfw)
229 .show_bot_accounts(show_bot_accounts)
230 .listing_type(&listing_type)
232 .community_id(community_id)
233 .community_name(community_name)
234 .creator_id(creator_id)
235 .my_person_id(person_id)
243 let q = data.q.to_owned();
244 let sort = SortType::from_str(&data.sort)?;
245 let listing_type = ListingType::from_str(&data.listing_type)?;
246 let community_name = data.community_name.to_owned();
248 comments = blocking(context.pool(), move |conn| {
249 CommentQueryBuilder::create(conn)
251 .listing_type(&listing_type)
253 .show_bot_accounts(show_bot_accounts)
254 .community_id(community_id)
255 .community_name(community_name)
256 .creator_id(creator_id)
257 .my_person_id(person_id)
264 let q = data.q.to_owned();
265 let sort = SortType::from_str(&data.sort)?;
266 let listing_type = ListingType::from_str(&data.listing_type)?;
268 communities = blocking(context.pool(), move |conn| {
269 CommunityQueryBuilder::create(conn)
271 .listing_type(&listing_type)
273 .my_person_id(person_id)
280 let q = data.q.to_owned();
281 let sort = SortType::from_str(&data.sort)?;
283 users = blocking(context.pool(), move |conn| {
284 PersonQueryBuilder::create(conn)
294 posts = blocking(context.pool(), move |conn| {
295 PostQueryBuilder::create(conn)
297 .show_nsfw(show_nsfw)
298 .show_bot_accounts(show_bot_accounts)
299 .listing_type(&listing_type)
301 .my_person_id(person_id)
302 .community_id(community_id)
303 .community_name(community_name)
304 .creator_id(creator_id)
316 type_: data.type_.to_owned(),
325 #[async_trait::async_trait(?Send)]
326 impl Perform for TransferSite {
327 type Response = GetSiteResponse;
331 context: &Data<LemmyContext>,
332 _websocket_id: Option<ConnectionId>,
333 ) -> Result<GetSiteResponse, LemmyError> {
334 let data: &TransferSite = &self;
335 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
337 is_admin(&local_user_view)?;
339 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
341 // Make sure user is the creator
342 if read_site.creator_id != local_user_view.person.id {
343 return Err(ApiError::err("not_an_admin").into());
346 let new_creator_id = data.person_id;
347 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
348 if blocking(context.pool(), transfer_site).await?.is_err() {
349 return Err(ApiError::err("couldnt_update_site").into());
353 let form = ModAddForm {
354 mod_person_id: local_user_view.person.id,
355 other_person_id: data.person_id,
356 removed: Some(false),
359 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
361 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
363 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
364 let creator_index = admins
366 .position(|r| r.person.id == site_view.creator.id)
367 .context(location_info!())?;
368 let creator_person = admins.remove(creator_index);
369 admins.insert(0, creator_person);
371 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
372 let federated_instances = build_federated_instances(context.pool()).await?;
374 let my_user = Some(get_local_user_settings_view_from_jwt(&data.auth, context.pool()).await?);
377 site_view: Some(site_view),
381 version: version::VERSION.to_string(),
388 #[async_trait::async_trait(?Send)]
389 impl Perform for GetSiteConfig {
390 type Response = GetSiteConfigResponse;
394 context: &Data<LemmyContext>,
395 _websocket_id: Option<ConnectionId>,
396 ) -> Result<GetSiteConfigResponse, LemmyError> {
397 let data: &GetSiteConfig = &self;
398 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
400 // Only let admins read this
401 is_admin(&local_user_view)?;
403 let config_hjson = Settings::read_config_file()?;
405 Ok(GetSiteConfigResponse { config_hjson })
409 #[async_trait::async_trait(?Send)]
410 impl Perform for SaveSiteConfig {
411 type Response = GetSiteConfigResponse;
415 context: &Data<LemmyContext>,
416 _websocket_id: Option<ConnectionId>,
417 ) -> Result<GetSiteConfigResponse, LemmyError> {
418 let data: &SaveSiteConfig = &self;
419 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
421 // Only let admins read this
422 is_admin(&local_user_view)?;
424 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
425 let config_hjson = Settings::save_config_file(&data.config_hjson)
426 .map_err(|_| ApiError::err("couldnt_update_site"))?;
428 Ok(GetSiteConfigResponse { config_hjson })