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,
16 use lemmy_apub::fetcher::search::search_by_apub_id;
17 use lemmy_db_queries::{source::site::Site_, Crud, SearchType, SortType};
18 use lemmy_db_schema::source::{moderator::*, site::Site};
20 comment_view::CommentQueryBuilder,
21 post_view::PostQueryBuilder,
24 use lemmy_db_views_actor::{
25 community_view::CommunityQueryBuilder,
26 person_view::{PersonQueryBuilder, PersonViewSafe},
28 use lemmy_db_views_moderator::{
29 mod_add_community_view::ModAddCommunityView,
30 mod_add_view::ModAddView,
31 mod_ban_from_community_view::ModBanFromCommunityView,
32 mod_ban_view::ModBanView,
33 mod_lock_post_view::ModLockPostView,
34 mod_remove_comment_view::ModRemoveCommentView,
35 mod_remove_community_view::ModRemoveCommunityView,
36 mod_remove_post_view::ModRemovePostView,
37 mod_sticky_post_view::ModStickyPostView,
41 settings::structs::Settings,
47 use lemmy_websocket::LemmyContext;
49 use std::str::FromStr;
51 #[async_trait::async_trait(?Send)]
52 impl Perform for GetModlog {
53 type Response = GetModlogResponse;
57 context: &Data<LemmyContext>,
58 _websocket_id: Option<ConnectionId>,
59 ) -> Result<GetModlogResponse, LemmyError> {
60 let data: &GetModlog = &self;
62 let community_id = data.community_id;
63 let mod_person_id = data.mod_person_id;
65 let limit = data.limit;
66 let removed_posts = blocking(context.pool(), move |conn| {
67 ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
71 let locked_posts = blocking(context.pool(), move |conn| {
72 ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
76 let stickied_posts = blocking(context.pool(), move |conn| {
77 ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
81 let removed_comments = blocking(context.pool(), move |conn| {
82 ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
86 let banned_from_community = blocking(context.pool(), move |conn| {
87 ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
91 let added_to_community = blocking(context.pool(), move |conn| {
92 ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
96 // These arrays are only for the full modlog, when a community isn't given
97 let (removed_communities, banned, added) = if data.community_id.is_none() {
98 blocking(context.pool(), move |conn| {
100 ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
101 ModBanView::list(conn, mod_person_id, page, limit)?,
102 ModAddView::list(conn, mod_person_id, page, limit)?,
103 )) as Result<_, LemmyError>
107 (Vec::new(), Vec::new(), Vec::new())
111 Ok(GetModlogResponse {
117 banned_from_community,
125 #[async_trait::async_trait(?Send)]
126 impl Perform for Search {
127 type Response = SearchResponse;
131 context: &Data<LemmyContext>,
132 _websocket_id: Option<ConnectionId>,
133 ) -> Result<SearchResponse, LemmyError> {
134 let data: &Search = &self;
136 match search_by_apub_id(&data.q, context).await {
137 Ok(r) => return Ok(r),
138 Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
141 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
143 let show_nsfw = user_show_nsfw(&local_user_view);
144 let show_bot_accounts = user_show_bot_accounts(&local_user_view);
145 let show_read_posts = user_show_read_posts(&local_user_view);
147 let person_id = local_user_view.map(|u| u.person.id);
149 let type_ = SearchType::from_str(&data.type_)?;
151 let mut posts = Vec::new();
152 let mut comments = Vec::new();
153 let mut communities = Vec::new();
154 let mut users = Vec::new();
156 // TODO no clean / non-nsfw searching rn
158 let q = data.q.to_owned();
159 let page = data.page;
160 let limit = data.limit;
161 let sort = SortType::from_str(&data.sort)?;
162 let community_id = data.community_id;
163 let community_name = data.community_name.to_owned();
165 SearchType::Posts => {
166 posts = blocking(context.pool(), move |conn| {
167 PostQueryBuilder::create(conn)
169 .show_nsfw(show_nsfw)
170 .show_bot_accounts(show_bot_accounts)
171 .show_read_posts(show_read_posts)
172 .community_id(community_id)
173 .community_name(community_name)
174 .my_person_id(person_id)
182 SearchType::Comments => {
183 comments = blocking(context.pool(), move |conn| {
184 CommentQueryBuilder::create(&conn)
187 .show_bot_accounts(show_bot_accounts)
188 .my_person_id(person_id)
195 SearchType::Communities => {
196 communities = blocking(context.pool(), move |conn| {
197 CommunityQueryBuilder::create(conn)
200 .my_person_id(person_id)
207 SearchType::Users => {
208 users = blocking(context.pool(), move |conn| {
209 PersonQueryBuilder::create(conn)
219 posts = blocking(context.pool(), move |conn| {
220 PostQueryBuilder::create(conn)
222 .show_nsfw(show_nsfw)
223 .show_bot_accounts(show_bot_accounts)
224 .show_read_posts(show_read_posts)
225 .community_id(community_id)
226 .community_name(community_name)
227 .my_person_id(person_id)
235 let q = data.q.to_owned();
236 let sort = SortType::from_str(&data.sort)?;
238 comments = blocking(context.pool(), move |conn| {
239 CommentQueryBuilder::create(conn)
242 .show_bot_accounts(show_bot_accounts)
243 .my_person_id(person_id)
250 let q = data.q.to_owned();
251 let sort = SortType::from_str(&data.sort)?;
253 communities = blocking(context.pool(), move |conn| {
254 CommunityQueryBuilder::create(conn)
257 .my_person_id(person_id)
264 let q = data.q.to_owned();
265 let sort = SortType::from_str(&data.sort)?;
267 users = blocking(context.pool(), move |conn| {
268 PersonQueryBuilder::create(conn)
278 posts = blocking(context.pool(), move |conn| {
279 PostQueryBuilder::create(conn)
281 .show_nsfw(show_nsfw)
282 .show_bot_accounts(show_bot_accounts)
283 .show_read_posts(show_read_posts)
284 .my_person_id(person_id)
285 .community_id(community_id)
286 .community_name(community_name)
298 type_: data.type_.to_owned(),
307 #[async_trait::async_trait(?Send)]
308 impl Perform for TransferSite {
309 type Response = GetSiteResponse;
313 context: &Data<LemmyContext>,
314 _websocket_id: Option<ConnectionId>,
315 ) -> Result<GetSiteResponse, LemmyError> {
316 let data: &TransferSite = &self;
317 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
319 is_admin(&local_user_view)?;
321 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
323 // Make sure user is the creator
324 if read_site.creator_id != local_user_view.person.id {
325 return Err(ApiError::err("not_an_admin").into());
328 let new_creator_id = data.person_id;
329 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
330 if blocking(context.pool(), transfer_site).await?.is_err() {
331 return Err(ApiError::err("couldnt_update_site").into());
335 let form = ModAddForm {
336 mod_person_id: local_user_view.person.id,
337 other_person_id: data.person_id,
338 removed: Some(false),
341 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
343 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
345 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
346 let creator_index = admins
348 .position(|r| r.person.id == site_view.creator.id)
349 .context(location_info!())?;
350 let creator_person = admins.remove(creator_index);
351 admins.insert(0, creator_person);
353 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
354 let federated_instances = build_federated_instances(context.pool()).await?;
356 let my_user = Some(get_local_user_settings_view_from_jwt(&data.auth, context.pool()).await?);
359 site_view: Some(site_view),
363 version: version::VERSION.to_string(),
370 #[async_trait::async_trait(?Send)]
371 impl Perform for GetSiteConfig {
372 type Response = GetSiteConfigResponse;
376 context: &Data<LemmyContext>,
377 _websocket_id: Option<ConnectionId>,
378 ) -> Result<GetSiteConfigResponse, LemmyError> {
379 let data: &GetSiteConfig = &self;
380 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
382 // Only let admins read this
383 is_admin(&local_user_view)?;
385 let config_hjson = Settings::read_config_file()?;
387 Ok(GetSiteConfigResponse { config_hjson })
391 #[async_trait::async_trait(?Send)]
392 impl Perform for SaveSiteConfig {
393 type Response = GetSiteConfigResponse;
397 context: &Data<LemmyContext>,
398 _websocket_id: Option<ConnectionId>,
399 ) -> Result<GetSiteConfigResponse, LemmyError> {
400 let data: &SaveSiteConfig = &self;
401 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
403 // Only let admins read this
404 is_admin(&local_user_view)?;
406 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
407 let config_hjson = Settings::save_config_file(&data.config_hjson)
408 .map_err(|_| ApiError::err("couldnt_update_site"))?;
410 Ok(GetSiteConfigResponse { config_hjson })