2 build_federated_instances,
3 get_local_user_view_from_jwt,
4 get_local_user_view_from_jwt_opt,
5 get_local_user_settings_view_from_jwt_opt,
9 use actix_web::web::Data;
11 use lemmy_api_structs::{blocking, site::*, person::Register};
12 use lemmy_apub::fetcher::search::search_by_apub_id;
13 use lemmy_db_queries::{
14 diesel_option_overwrite_to_url,
20 use lemmy_db_schema::{
28 comment_view::CommentQueryBuilder,
29 post_view::PostQueryBuilder,
32 use lemmy_db_views_actor::{
33 community_view::CommunityQueryBuilder,
34 person_view::{PersonQueryBuilder, PersonViewSafe},
36 use lemmy_db_views_moderator::{
37 mod_add_community_view::ModAddCommunityView,
38 mod_add_view::ModAddView,
39 mod_ban_from_community_view::ModBanFromCommunityView,
40 mod_ban_view::ModBanView,
41 mod_lock_post_view::ModLockPostView,
42 mod_remove_comment_view::ModRemoveCommentView,
43 mod_remove_community_view::ModRemoveCommunityView,
44 mod_remove_post_view::ModRemovePostView,
45 mod_sticky_post_view::ModStickyPostView,
49 settings::structs::Settings,
50 utils::{check_slurs, check_slurs_opt},
56 use lemmy_websocket::{
57 messages::{GetUsersOnline, SendAllMessage},
61 use log::{debug, info};
62 use std::str::FromStr;
64 #[async_trait::async_trait(?Send)]
65 impl Perform for GetModlog {
66 type Response = GetModlogResponse;
70 context: &Data<LemmyContext>,
71 _websocket_id: Option<ConnectionId>,
72 ) -> Result<GetModlogResponse, LemmyError> {
73 let data: &GetModlog = &self;
75 let community_id = data.community_id;
76 let mod_person_id = data.mod_person_id;
78 let limit = data.limit;
79 let removed_posts = blocking(context.pool(), move |conn| {
80 ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
84 let locked_posts = blocking(context.pool(), move |conn| {
85 ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
89 let stickied_posts = blocking(context.pool(), move |conn| {
90 ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
94 let removed_comments = blocking(context.pool(), move |conn| {
95 ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
99 let banned_from_community = blocking(context.pool(), move |conn| {
100 ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
104 let added_to_community = blocking(context.pool(), move |conn| {
105 ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
109 // These arrays are only for the full modlog, when a community isn't given
110 let (removed_communities, banned, added) = if data.community_id.is_none() {
111 blocking(context.pool(), move |conn| {
113 ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
114 ModBanView::list(conn, mod_person_id, page, limit)?,
115 ModAddView::list(conn, mod_person_id, page, limit)?,
116 )) as Result<_, LemmyError>
120 (Vec::new(), Vec::new(), Vec::new())
124 Ok(GetModlogResponse {
130 banned_from_community,
138 #[async_trait::async_trait(?Send)]
139 impl Perform for CreateSite {
140 type Response = SiteResponse;
144 context: &Data<LemmyContext>,
145 _websocket_id: Option<ConnectionId>,
146 ) -> Result<SiteResponse, LemmyError> {
147 let data: &CreateSite = &self;
149 let read_site = move |conn: &'_ _| Site::read_simple(conn);
150 if blocking(context.pool(), read_site).await?.is_ok() {
151 return Err(ApiError::err("site_already_exists").into());
154 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
156 check_slurs(&data.name)?;
157 check_slurs_opt(&data.description)?;
159 // Make sure user is an admin
160 is_admin(&local_user_view)?;
162 let site_form = SiteForm {
163 name: data.name.to_owned(),
164 description: data.description.to_owned(),
165 icon: Some(data.icon.to_owned().map(|url| url.into())),
166 banner: Some(data.banner.to_owned().map(|url| url.into())),
167 creator_id: local_user_view.person.id,
168 enable_downvotes: data.enable_downvotes,
169 open_registration: data.open_registration,
170 enable_nsfw: data.enable_nsfw,
174 let create_site = move |conn: &'_ _| Site::create(conn, &site_form);
175 if blocking(context.pool(), create_site).await?.is_err() {
176 return Err(ApiError::err("site_already_exists").into());
179 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
181 Ok(SiteResponse { site_view })
185 #[async_trait::async_trait(?Send)]
186 impl Perform for EditSite {
187 type Response = SiteResponse;
190 context: &Data<LemmyContext>,
191 websocket_id: Option<ConnectionId>,
192 ) -> Result<SiteResponse, LemmyError> {
193 let data: &EditSite = &self;
194 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
196 check_slurs(&data.name)?;
197 check_slurs_opt(&data.description)?;
199 // Make sure user is an admin
200 is_admin(&local_user_view)?;
202 let found_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
204 let icon = diesel_option_overwrite_to_url(&data.icon)?;
205 let banner = diesel_option_overwrite_to_url(&data.banner)?;
207 let site_form = SiteForm {
208 name: data.name.to_owned(),
209 description: data.description.to_owned(),
212 creator_id: found_site.creator_id,
213 updated: Some(naive_now()),
214 enable_downvotes: data.enable_downvotes,
215 open_registration: data.open_registration,
216 enable_nsfw: data.enable_nsfw,
219 let update_site = move |conn: &'_ _| Site::update(conn, 1, &site_form);
220 if blocking(context.pool(), update_site).await?.is_err() {
221 return Err(ApiError::err("couldnt_update_site").into());
224 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
226 let res = SiteResponse { site_view };
228 context.chat_server().do_send(SendAllMessage {
229 op: UserOperation::EditSite,
230 response: res.clone(),
238 #[async_trait::async_trait(?Send)]
239 impl Perform for GetSite {
240 type Response = GetSiteResponse;
244 context: &Data<LemmyContext>,
245 websocket_id: Option<ConnectionId>,
246 ) -> Result<GetSiteResponse, LemmyError> {
247 let data: &GetSite = &self;
249 let site_view = match blocking(context.pool(), move |conn| SiteView::read(conn)).await? {
250 Ok(site_view) => Some(site_view),
251 // If the site isn't created yet, check the setup
253 if let Some(setup) = Settings::get().setup().as_ref() {
254 let register = Register {
255 username: setup.admin_username.to_owned(),
256 email: setup.admin_email.to_owned(),
257 password: setup.admin_password.to_owned(),
258 password_verify: setup.admin_password.to_owned(),
261 captcha_answer: None,
263 let login_response = register.perform(context, websocket_id).await?;
264 info!("Admin {} created", setup.admin_username);
266 let create_site = CreateSite {
267 name: setup.site_name.to_owned(),
271 enable_downvotes: true,
272 open_registration: true,
274 auth: login_response.jwt,
276 create_site.perform(context, websocket_id).await?;
277 info!("Site {} created", setup.site_name);
278 Some(blocking(context.pool(), move |conn| SiteView::read(conn)).await??)
285 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
287 // Make sure the site creator is the top admin
288 if let Some(site_view) = site_view.to_owned() {
289 let site_creator_id = site_view.creator.id;
290 // TODO investigate why this is sometimes coming back null
291 // Maybe user_.admin isn't being set to true?
292 if let Some(creator_index) = admins.iter().position(|r| r.person.id == site_creator_id) {
293 let creator_person = admins.remove(creator_index);
294 admins.insert(0, creator_person);
298 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
302 .send(GetUsersOnline)
306 let my_user = get_local_user_settings_view_from_jwt_opt(&data.auth, context.pool()).await?;
307 let federated_instances = build_federated_instances(context.pool()).await?;
314 version: version::VERSION.to_string(),
321 #[async_trait::async_trait(?Send)]
322 impl Perform for Search {
323 type Response = SearchResponse;
327 context: &Data<LemmyContext>,
328 _websocket_id: Option<ConnectionId>,
329 ) -> Result<SearchResponse, LemmyError> {
330 let data: &Search = &self;
332 match search_by_apub_id(&data.q, context).await {
333 Ok(r) => return Ok(r),
334 Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
337 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
338 let person_id = local_user_view.map(|u| u.person.id);
340 let type_ = SearchType::from_str(&data.type_)?;
342 let mut posts = Vec::new();
343 let mut comments = Vec::new();
344 let mut communities = Vec::new();
345 let mut users = Vec::new();
347 // TODO no clean / non-nsfw searching rn
349 let q = data.q.to_owned();
350 let page = data.page;
351 let limit = data.limit;
352 let sort = SortType::from_str(&data.sort)?;
353 let community_id = data.community_id;
354 let community_name = data.community_name.to_owned();
356 SearchType::Posts => {
357 posts = blocking(context.pool(), move |conn| {
358 PostQueryBuilder::create(conn)
361 .community_id(community_id)
362 .community_name(community_name)
363 .my_person_id(person_id)
371 SearchType::Comments => {
372 comments = blocking(context.pool(), move |conn| {
373 CommentQueryBuilder::create(&conn)
376 .my_person_id(person_id)
383 SearchType::Communities => {
384 communities = blocking(context.pool(), move |conn| {
385 CommunityQueryBuilder::create(conn)
388 .my_person_id(person_id)
395 SearchType::Users => {
396 users = blocking(context.pool(), move |conn| {
397 PersonQueryBuilder::create(conn)
407 posts = blocking(context.pool(), move |conn| {
408 PostQueryBuilder::create(conn)
411 .community_id(community_id)
412 .community_name(community_name)
413 .my_person_id(person_id)
421 let q = data.q.to_owned();
422 let sort = SortType::from_str(&data.sort)?;
424 comments = blocking(context.pool(), move |conn| {
425 CommentQueryBuilder::create(conn)
428 .my_person_id(person_id)
435 let q = data.q.to_owned();
436 let sort = SortType::from_str(&data.sort)?;
438 communities = blocking(context.pool(), move |conn| {
439 CommunityQueryBuilder::create(conn)
442 .my_person_id(person_id)
449 let q = data.q.to_owned();
450 let sort = SortType::from_str(&data.sort)?;
452 users = blocking(context.pool(), move |conn| {
453 PersonQueryBuilder::create(conn)
463 posts = blocking(context.pool(), move |conn| {
464 PostQueryBuilder::create(conn)
467 .my_person_id(person_id)
468 .community_id(community_id)
469 .community_name(community_name)
481 type_: data.type_.to_owned(),
490 #[async_trait::async_trait(?Send)]
491 impl Perform for TransferSite {
492 type Response = GetSiteResponse;
496 context: &Data<LemmyContext>,
497 _websocket_id: Option<ConnectionId>,
498 ) -> Result<GetSiteResponse, LemmyError> {
499 let data: &TransferSite = &self;
500 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
502 is_admin(&local_user_view)?;
504 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
506 // Make sure user is the creator
507 if read_site.creator_id != local_user_view.person.id {
508 return Err(ApiError::err("not_an_admin").into());
511 let new_creator_id = data.person_id;
512 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
513 if blocking(context.pool(), transfer_site).await?.is_err() {
514 return Err(ApiError::err("couldnt_update_site").into());
518 let form = ModAddForm {
519 mod_person_id: local_user_view.person.id,
520 other_person_id: data.person_id,
521 removed: Some(false),
524 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
526 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
528 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
529 let creator_index = admins
531 .position(|r| r.person.id == site_view.creator.id)
532 .context(location_info!())?;
533 let creator_person = admins.remove(creator_index);
534 admins.insert(0, creator_person);
536 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
537 let federated_instances = build_federated_instances(context.pool()).await?;
540 site_view: Some(site_view),
544 version: version::VERSION.to_string(),
545 my_user: Some(local_user_view),
551 #[async_trait::async_trait(?Send)]
552 impl Perform for GetSiteConfig {
553 type Response = GetSiteConfigResponse;
557 context: &Data<LemmyContext>,
558 _websocket_id: Option<ConnectionId>,
559 ) -> Result<GetSiteConfigResponse, LemmyError> {
560 let data: &GetSiteConfig = &self;
561 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
563 // Only let admins read this
564 is_admin(&local_user_view)?;
566 let config_hjson = Settings::read_config_file()?;
568 Ok(GetSiteConfigResponse { config_hjson })
572 #[async_trait::async_trait(?Send)]
573 impl Perform for SaveSiteConfig {
574 type Response = GetSiteConfigResponse;
578 context: &Data<LemmyContext>,
579 _websocket_id: Option<ConnectionId>,
580 ) -> Result<GetSiteConfigResponse, LemmyError> {
581 let data: &SaveSiteConfig = &self;
582 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
584 // Only let admins read this
585 let person_id = local_user_view.person.id;
586 is_admin(&local_user_view)?;
588 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
589 let config_hjson = match Settings::save_config_file(&data.config_hjson) {
590 Ok(config_hjson) => config_hjson,
591 Err(_e) => return Err(ApiError::err("couldnt_update_site").into()),
594 Ok(GetSiteConfigResponse { config_hjson })