2 captcha_espeak_wav_base64,
3 collect_moderated_communities,
4 get_local_user_view_from_jwt,
5 get_local_user_view_from_jwt_opt,
10 use actix_web::web::Data;
13 use captcha::{gen, Difficulty};
15 use lemmy_api_structs::{blocking, person::*, send_email_to_user};
17 generate_apub_endpoint,
18 generate_followers_url,
20 generate_shared_inbox_url,
24 use lemmy_db_queries::{
25 diesel_option_overwrite,
26 diesel_option_overwrite_to_url,
29 community::Community_,
30 local_user::LocalUser_,
31 password_reset_request::PasswordResetRequest_,
33 person_mention::PersonMention_,
35 private_message::PrivateMessage_,
44 use lemmy_db_schema::{
49 local_user::{LocalUser, LocalUserForm},
51 password_reset_request::*,
61 comment_report_view::CommentReportView,
62 comment_view::CommentQueryBuilder,
63 local_user_view::LocalUserView,
64 post_report_view::PostReportView,
65 post_view::PostQueryBuilder,
66 private_message_view::{PrivateMessageQueryBuilder, PrivateMessageView},
68 use lemmy_db_views_actor::{
69 community_follower_view::CommunityFollowerView,
70 community_moderator_view::CommunityModeratorView,
71 person_mention_view::{PersonMentionQueryBuilder, PersonMentionView},
72 person_view::PersonViewSafe,
75 apub::generate_actor_keypair,
79 settings::structs::Settings,
82 generate_random_string,
83 is_valid_preferred_username,
92 use lemmy_websocket::{
93 messages::{CaptchaItem, CheckCaptcha, SendAllMessage, SendUserRoomMessage},
97 use std::str::FromStr;
99 #[async_trait::async_trait(?Send)]
100 impl Perform for Login {
101 type Response = LoginResponse;
105 context: &Data<LemmyContext>,
106 _websocket_id: Option<ConnectionId>,
107 ) -> Result<LoginResponse, LemmyError> {
108 let data: &Login = &self;
110 // Fetch that username / email
111 let username_or_email = data.username_or_email.clone();
112 let local_user_view = match blocking(context.pool(), move |conn| {
113 LocalUserView::find_by_email_or_name(conn, &username_or_email)
118 Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
121 // Verify the password
122 let valid: bool = verify(
124 &local_user_view.local_user.password_encrypted,
128 return Err(ApiError::err("password_incorrect").into());
133 jwt: Claims::jwt(local_user_view.local_user.id.0, Settings::get().hostname())?,
138 #[async_trait::async_trait(?Send)]
139 impl Perform for Register {
140 type Response = LoginResponse;
144 context: &Data<LemmyContext>,
145 _websocket_id: Option<ConnectionId>,
146 ) -> Result<LoginResponse, LemmyError> {
147 let data: &Register = &self;
149 // Make sure site has open registration
150 if let Ok(site) = blocking(context.pool(), move |conn| Site::read_simple(conn)).await? {
151 if !site.open_registration {
152 return Err(ApiError::err("registration_closed").into());
156 password_length_check(&data.password)?;
158 // Make sure passwords match
159 if data.password != data.password_verify {
160 return Err(ApiError::err("passwords_dont_match").into());
163 // Check if there are admins. False if admins exist
164 let no_admins = blocking(context.pool(), move |conn| {
165 PersonViewSafe::admins(conn).map(|a| a.is_empty())
169 // If its not the admin, check the captcha
170 if !no_admins && Settings::get().captcha().enabled {
177 .unwrap_or_else(|| "".to_string()),
181 .unwrap_or_else(|| "".to_string()),
185 return Err(ApiError::err("captcha_incorrect").into());
189 check_slurs(&data.username)?;
191 let actor_keypair = generate_actor_keypair()?;
192 if !is_valid_username(&data.username) {
193 return Err(ApiError::err("invalid_username").into());
195 let actor_id = generate_apub_endpoint(EndpointType::Person, &data.username)?;
197 // We have to create both a person, and local_user
199 // Register the new person
200 let person_form = PersonForm {
201 name: data.username.to_owned(),
204 preferred_username: None,
209 actor_id: Some(actor_id.clone()),
212 private_key: Some(Some(actor_keypair.private_key)),
213 public_key: Some(Some(actor_keypair.public_key)),
214 last_refreshed_at: None,
215 inbox_url: Some(generate_inbox_url(&actor_id)?),
216 shared_inbox_url: Some(Some(generate_shared_inbox_url(&actor_id)?)),
220 let inserted_person = match blocking(context.pool(), move |conn| {
221 Person::create(conn, &person_form)
227 return Err(ApiError::err("user_already_exists").into());
231 // Create the local user
232 let local_user_form = LocalUserForm {
233 person_id: inserted_person.id,
234 email: Some(data.email.to_owned()),
235 matrix_user_id: None,
236 password_encrypted: data.password.to_owned(),
237 admin: Some(no_admins),
238 show_nsfw: Some(data.show_nsfw),
239 theme: Some("browser".into()),
240 default_sort_type: Some(SortType::Active as i16),
241 default_listing_type: Some(ListingType::Subscribed as i16),
242 lang: Some("browser".into()),
243 show_avatars: Some(true),
244 send_notifications_to_email: Some(false),
247 let inserted_local_user = match blocking(context.pool(), move |conn| {
248 LocalUser::register(conn, &local_user_form)
254 let err_type = if e.to_string()
255 == "duplicate key value violates unique constraint \"local_user_email_key\""
257 "email_already_exists"
259 "user_already_exists"
262 // If the local user creation errored, then delete that person
263 blocking(context.pool(), move |conn| {
264 Person::delete(&conn, inserted_person.id)
268 return Err(ApiError::err(err_type).into());
272 let main_community_keypair = generate_actor_keypair()?;
274 // Create the main community if it doesn't exist
275 let main_community = match blocking(context.pool(), move |conn| {
276 Community::read(conn, CommunityId(2))
282 let default_community_name = "main";
283 let actor_id = generate_apub_endpoint(EndpointType::Community, default_community_name)?;
284 let community_form = CommunityForm {
285 name: default_community_name.to_string(),
286 title: "The Default Community".to_string(),
287 description: Some("The Default Community".to_string()),
289 creator_id: inserted_person.id,
293 actor_id: Some(actor_id.to_owned()),
295 private_key: Some(main_community_keypair.private_key),
296 public_key: Some(main_community_keypair.public_key),
297 last_refreshed_at: None,
301 followers_url: Some(generate_followers_url(&actor_id)?),
302 inbox_url: Some(generate_inbox_url(&actor_id)?),
303 shared_inbox_url: Some(Some(generate_shared_inbox_url(&actor_id)?)),
305 blocking(context.pool(), move |conn| {
306 Community::create(conn, &community_form)
312 // Sign them up for main community no matter what
313 let community_follower_form = CommunityFollowerForm {
314 community_id: main_community.id,
315 person_id: inserted_person.id,
319 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
320 if blocking(context.pool(), follow).await?.is_err() {
321 return Err(ApiError::err("community_follower_already_exists").into());
324 // If its an admin, add them as a mod and follower to main
326 let community_moderator_form = CommunityModeratorForm {
327 community_id: main_community.id,
328 person_id: inserted_person.id,
331 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
332 if blocking(context.pool(), join).await?.is_err() {
333 return Err(ApiError::err("community_moderator_already_exists").into());
339 jwt: Claims::jwt(inserted_local_user.id.0, Settings::get().hostname())?,
344 #[async_trait::async_trait(?Send)]
345 impl Perform for GetCaptcha {
346 type Response = GetCaptchaResponse;
350 context: &Data<LemmyContext>,
351 _websocket_id: Option<ConnectionId>,
352 ) -> Result<Self::Response, LemmyError> {
353 let captcha_settings = Settings::get().captcha();
355 if !captcha_settings.enabled {
356 return Ok(GetCaptchaResponse { ok: None });
359 let captcha = match captcha_settings.difficulty.as_str() {
360 "easy" => gen(Difficulty::Easy),
361 "medium" => gen(Difficulty::Medium),
362 "hard" => gen(Difficulty::Hard),
363 _ => gen(Difficulty::Medium),
366 let answer = captcha.chars_as_string();
368 let png_byte_array = captcha.as_png().expect("failed to generate captcha");
370 let png = base64::encode(png_byte_array);
372 let uuid = uuid::Uuid::new_v4().to_string();
374 let wav = captcha_espeak_wav_base64(&answer).ok();
376 let captcha_item = CaptchaItem {
378 uuid: uuid.to_owned(),
379 expires: naive_now() + Duration::minutes(10), // expires in 10 minutes
382 // Stores the captcha item on the queue
383 context.chat_server().do_send(captcha_item);
385 Ok(GetCaptchaResponse {
386 ok: Some(CaptchaResponse { png, uuid, wav }),
391 #[async_trait::async_trait(?Send)]
392 impl Perform for SaveUserSettings {
393 type Response = LoginResponse;
397 context: &Data<LemmyContext>,
398 _websocket_id: Option<ConnectionId>,
399 ) -> Result<LoginResponse, LemmyError> {
400 let data: &SaveUserSettings = &self;
401 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
403 let avatar = diesel_option_overwrite_to_url(&data.avatar)?;
404 let banner = diesel_option_overwrite_to_url(&data.banner)?;
405 let email = diesel_option_overwrite(&data.email);
406 let bio = diesel_option_overwrite(&data.bio);
407 let preferred_username = diesel_option_overwrite(&data.preferred_username);
408 let matrix_user_id = diesel_option_overwrite(&data.matrix_user_id);
410 if let Some(Some(bio)) = &bio {
411 if bio.chars().count() > 300 {
412 return Err(ApiError::err("bio_length_overflow").into());
416 if let Some(Some(preferred_username)) = &preferred_username {
417 if !is_valid_preferred_username(preferred_username.trim()) {
418 return Err(ApiError::err("invalid_username").into());
422 let local_user_id = local_user_view.local_user.id;
423 let person_id = local_user_view.person.id;
424 let password_encrypted = match &data.new_password {
425 Some(new_password) => {
426 match &data.new_password_verify {
427 Some(new_password_verify) => {
428 password_length_check(&new_password)?;
430 // Make sure passwords match
431 if new_password != new_password_verify {
432 return Err(ApiError::err("passwords_dont_match").into());
435 // Check the old password
436 match &data.old_password {
437 Some(old_password) => {
439 verify(old_password, &local_user_view.local_user.password_encrypted)
442 return Err(ApiError::err("password_incorrect").into());
444 let new_password = new_password.to_owned();
445 let user = blocking(context.pool(), move |conn| {
446 LocalUser::update_password(conn, local_user_id, &new_password)
449 user.password_encrypted
451 None => return Err(ApiError::err("password_incorrect").into()),
454 None => return Err(ApiError::err("passwords_dont_match").into()),
457 None => local_user_view.local_user.password_encrypted,
460 let default_listing_type = data.default_listing_type;
461 let default_sort_type = data.default_sort_type;
463 let person_form = PersonForm {
464 name: local_user_view.person.name,
470 updated: Some(naive_now()),
478 last_refreshed_at: None,
479 shared_inbox_url: None,
482 let person_res = blocking(context.pool(), move |conn| {
483 Person::update(conn, person_id, &person_form)
486 let _updated_person: Person = match person_res {
489 return Err(ApiError::err("user_already_exists").into());
493 let local_user_form = LocalUserForm {
499 show_nsfw: data.show_nsfw,
500 theme: data.theme.to_owned(),
502 default_listing_type,
503 lang: data.lang.to_owned(),
504 show_avatars: data.show_avatars,
505 send_notifications_to_email: data.send_notifications_to_email,
508 let local_user_res = blocking(context.pool(), move |conn| {
509 LocalUser::update(conn, local_user_id, &local_user_form)
512 let updated_local_user = match local_user_res {
515 let err_type = if e.to_string()
516 == "duplicate key value violates unique constraint \"local_user_email_key\""
518 "email_already_exists"
520 "user_already_exists"
523 return Err(ApiError::err(err_type).into());
529 jwt: Claims::jwt(updated_local_user.id.0, Settings::get().hostname())?,
534 #[async_trait::async_trait(?Send)]
535 impl Perform for GetPersonDetails {
536 type Response = GetPersonDetailsResponse;
540 context: &Data<LemmyContext>,
541 _websocket_id: Option<ConnectionId>,
542 ) -> Result<GetPersonDetailsResponse, LemmyError> {
543 let data: &GetPersonDetails = &self;
544 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
546 let show_nsfw = match &local_user_view {
547 Some(uv) => uv.local_user.show_nsfw,
551 let sort = SortType::from_str(&data.sort)?;
556 .unwrap_or_else(|| "admin".to_string());
557 let person_details_id = match data.person_id {
560 let person = blocking(context.pool(), move |conn| {
561 Person::find_by_name(conn, &username)
566 Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
571 let person_id = local_user_view.map(|uv| uv.person.id);
573 // You don't need to return settings for the user, since this comes back with GetSite
575 let person_view = blocking(context.pool(), move |conn| {
576 PersonViewSafe::read(conn, person_details_id)
580 let page = data.page;
581 let limit = data.limit;
582 let saved_only = data.saved_only;
583 let community_id = data.community_id;
585 let (posts, comments) = blocking(context.pool(), move |conn| {
586 let mut posts_query = PostQueryBuilder::create(conn)
588 .show_nsfw(show_nsfw)
589 .saved_only(saved_only)
590 .community_id(community_id)
591 .my_person_id(person_id)
595 let mut comments_query = CommentQueryBuilder::create(conn)
596 .my_person_id(person_id)
598 .saved_only(saved_only)
602 // If its saved only, you don't care what creator it was
603 // Or, if its not saved, then you only want it for that specific creator
605 posts_query = posts_query.creator_id(person_details_id);
606 comments_query = comments_query.creator_id(person_details_id);
609 let posts = posts_query.list()?;
610 let comments = comments_query.list()?;
612 Ok((posts, comments)) as Result<_, LemmyError>
616 let mut follows = vec![];
617 if let Some(pid) = person_id {
618 if pid == person_details_id {
619 follows = blocking(context.pool(), move |conn| {
620 CommunityFollowerView::for_person(conn, person_details_id)
625 let moderates = blocking(context.pool(), move |conn| {
626 CommunityModeratorView::for_person(conn, person_details_id)
631 Ok(GetPersonDetailsResponse {
641 #[async_trait::async_trait(?Send)]
642 impl Perform for AddAdmin {
643 type Response = AddAdminResponse;
647 context: &Data<LemmyContext>,
648 websocket_id: Option<ConnectionId>,
649 ) -> Result<AddAdminResponse, LemmyError> {
650 let data: &AddAdmin = &self;
651 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
653 // Make sure user is an admin
654 is_admin(&local_user_view)?;
656 let added = data.added;
657 let added_person_id = data.person_id;
658 let added_admin = match blocking(context.pool(), move |conn| {
659 LocalUser::add_admin(conn, added_person_id, added)
665 return Err(ApiError::err("couldnt_update_user").into());
670 let form = ModAddForm {
671 mod_person_id: local_user_view.person.id,
672 other_person_id: added_admin.person_id,
673 removed: Some(!data.added),
676 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
678 let site_creator_id = blocking(context.pool(), move |conn| {
679 Site::read(conn, 1).map(|s| s.creator_id)
683 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
684 let creator_index = admins
686 .position(|r| r.person.id == site_creator_id)
687 .context(location_info!())?;
688 let creator_person = admins.remove(creator_index);
689 admins.insert(0, creator_person);
691 let res = AddAdminResponse { admins };
693 context.chat_server().do_send(SendAllMessage {
694 op: UserOperation::AddAdmin,
695 response: res.clone(),
703 #[async_trait::async_trait(?Send)]
704 impl Perform for BanPerson {
705 type Response = BanPersonResponse;
709 context: &Data<LemmyContext>,
710 websocket_id: Option<ConnectionId>,
711 ) -> Result<BanPersonResponse, LemmyError> {
712 let data: &BanPerson = &self;
713 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
715 // Make sure user is an admin
716 is_admin(&local_user_view)?;
719 let banned_person_id = data.person_id;
720 let ban_person = move |conn: &'_ _| Person::ban_person(conn, banned_person_id, ban);
721 if blocking(context.pool(), ban_person).await?.is_err() {
722 return Err(ApiError::err("couldnt_update_user").into());
725 // Remove their data if that's desired
726 if data.remove_data {
728 blocking(context.pool(), move |conn: &'_ _| {
729 Post::update_removed_for_creator(conn, banned_person_id, None, true)
734 blocking(context.pool(), move |conn: &'_ _| {
735 Community::update_removed_for_creator(conn, banned_person_id, true)
740 blocking(context.pool(), move |conn: &'_ _| {
741 Comment::update_removed_for_creator(conn, banned_person_id, true)
747 let expires = match data.expires {
748 Some(time) => Some(naive_from_unix(time)),
752 let form = ModBanForm {
753 mod_person_id: local_user_view.person.id,
754 other_person_id: data.person_id,
755 reason: data.reason.to_owned(),
756 banned: Some(data.ban),
760 blocking(context.pool(), move |conn| ModBan::create(conn, &form)).await??;
762 let person_id = data.person_id;
763 let person_view = blocking(context.pool(), move |conn| {
764 PersonViewSafe::read(conn, person_id)
768 let res = BanPersonResponse {
773 context.chat_server().do_send(SendAllMessage {
774 op: UserOperation::BanPerson,
775 response: res.clone(),
783 #[async_trait::async_trait(?Send)]
784 impl Perform for GetReplies {
785 type Response = GetRepliesResponse;
789 context: &Data<LemmyContext>,
790 _websocket_id: Option<ConnectionId>,
791 ) -> Result<GetRepliesResponse, LemmyError> {
792 let data: &GetReplies = &self;
793 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
795 let sort = SortType::from_str(&data.sort)?;
797 let page = data.page;
798 let limit = data.limit;
799 let unread_only = data.unread_only;
800 let person_id = local_user_view.person.id;
801 let replies = blocking(context.pool(), move |conn| {
802 CommentQueryBuilder::create(conn)
804 .unread_only(unread_only)
805 .recipient_id(person_id)
806 .my_person_id(person_id)
813 Ok(GetRepliesResponse { replies })
817 #[async_trait::async_trait(?Send)]
818 impl Perform for GetPersonMentions {
819 type Response = GetPersonMentionsResponse;
823 context: &Data<LemmyContext>,
824 _websocket_id: Option<ConnectionId>,
825 ) -> Result<GetPersonMentionsResponse, LemmyError> {
826 let data: &GetPersonMentions = &self;
827 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
829 let sort = SortType::from_str(&data.sort)?;
831 let page = data.page;
832 let limit = data.limit;
833 let unread_only = data.unread_only;
834 let person_id = local_user_view.person.id;
835 let mentions = blocking(context.pool(), move |conn| {
836 PersonMentionQueryBuilder::create(conn)
837 .recipient_id(person_id)
838 .my_person_id(person_id)
840 .unread_only(unread_only)
847 Ok(GetPersonMentionsResponse { mentions })
851 #[async_trait::async_trait(?Send)]
852 impl Perform for MarkPersonMentionAsRead {
853 type Response = PersonMentionResponse;
857 context: &Data<LemmyContext>,
858 _websocket_id: Option<ConnectionId>,
859 ) -> Result<PersonMentionResponse, LemmyError> {
860 let data: &MarkPersonMentionAsRead = &self;
861 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
863 let person_mention_id = data.person_mention_id;
864 let read_person_mention = blocking(context.pool(), move |conn| {
865 PersonMention::read(conn, person_mention_id)
869 if local_user_view.person.id != read_person_mention.recipient_id {
870 return Err(ApiError::err("couldnt_update_comment").into());
873 let person_mention_id = read_person_mention.id;
874 let read = data.read;
876 move |conn: &'_ _| PersonMention::update_read(conn, person_mention_id, read);
877 if blocking(context.pool(), update_mention).await?.is_err() {
878 return Err(ApiError::err("couldnt_update_comment").into());
881 let person_mention_id = read_person_mention.id;
882 let person_id = local_user_view.person.id;
883 let person_mention_view = blocking(context.pool(), move |conn| {
884 PersonMentionView::read(conn, person_mention_id, Some(person_id))
888 Ok(PersonMentionResponse {
894 #[async_trait::async_trait(?Send)]
895 impl Perform for MarkAllAsRead {
896 type Response = GetRepliesResponse;
900 context: &Data<LemmyContext>,
901 _websocket_id: Option<ConnectionId>,
902 ) -> Result<GetRepliesResponse, LemmyError> {
903 let data: &MarkAllAsRead = &self;
904 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
906 let person_id = local_user_view.person.id;
907 let replies = blocking(context.pool(), move |conn| {
908 CommentQueryBuilder::create(conn)
909 .my_person_id(person_id)
910 .recipient_id(person_id)
918 // TODO: this should probably be a bulk operation
919 // Not easy to do as a bulk operation,
920 // because recipient_id isn't in the comment table
921 for comment_view in &replies {
922 let reply_id = comment_view.comment.id;
923 let mark_as_read = move |conn: &'_ _| Comment::update_read(conn, reply_id, true);
924 if blocking(context.pool(), mark_as_read).await?.is_err() {
925 return Err(ApiError::err("couldnt_update_comment").into());
929 // Mark all user mentions as read
930 let update_person_mentions =
931 move |conn: &'_ _| PersonMention::mark_all_as_read(conn, person_id);
932 if blocking(context.pool(), update_person_mentions)
936 return Err(ApiError::err("couldnt_update_comment").into());
939 // Mark all private_messages as read
940 let update_pm = move |conn: &'_ _| PrivateMessage::mark_all_as_read(conn, person_id);
941 if blocking(context.pool(), update_pm).await?.is_err() {
942 return Err(ApiError::err("couldnt_update_private_message").into());
945 Ok(GetRepliesResponse { replies: vec![] })
949 #[async_trait::async_trait(?Send)]
950 impl Perform for DeleteAccount {
951 type Response = LoginResponse;
955 context: &Data<LemmyContext>,
956 _websocket_id: Option<ConnectionId>,
957 ) -> Result<LoginResponse, LemmyError> {
958 let data: &DeleteAccount = &self;
959 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
961 // Verify the password
962 let valid: bool = verify(
964 &local_user_view.local_user.password_encrypted,
968 return Err(ApiError::err("password_incorrect").into());
972 let person_id = local_user_view.person.id;
973 let permadelete = move |conn: &'_ _| Comment::permadelete_for_creator(conn, person_id);
974 if blocking(context.pool(), permadelete).await?.is_err() {
975 return Err(ApiError::err("couldnt_update_comment").into());
979 let permadelete = move |conn: &'_ _| Post::permadelete_for_creator(conn, person_id);
980 if blocking(context.pool(), permadelete).await?.is_err() {
981 return Err(ApiError::err("couldnt_update_post").into());
984 blocking(context.pool(), move |conn| {
985 Person::delete_account(conn, person_id)
990 jwt: data.auth.to_owned(),
995 #[async_trait::async_trait(?Send)]
996 impl Perform for PasswordReset {
997 type Response = PasswordResetResponse;
1001 context: &Data<LemmyContext>,
1002 _websocket_id: Option<ConnectionId>,
1003 ) -> Result<PasswordResetResponse, LemmyError> {
1004 let data: &PasswordReset = &self;
1007 let email = data.email.clone();
1008 let local_user_view = match blocking(context.pool(), move |conn| {
1009 LocalUserView::find_by_email(conn, &email)
1014 Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
1017 // Generate a random token
1018 let token = generate_random_string();
1021 let token2 = token.clone();
1022 let local_user_id = local_user_view.local_user.id;
1023 blocking(context.pool(), move |conn| {
1024 PasswordResetRequest::create_token(conn, local_user_id, &token2)
1028 // Email the pure token to the user.
1029 // TODO no i18n support here.
1030 let email = &local_user_view.local_user.email.expect("email");
1031 let subject = &format!("Password reset for {}", local_user_view.person.name);
1032 let hostname = &Settings::get().get_protocol_and_hostname();
1033 let html = &format!("<h1>Password Reset Request for {}</h1><br><a href={}/password_change/{}>Click here to reset your password</a>", local_user_view.person.name, hostname, &token);
1034 match send_email(subject, email, &local_user_view.person.name, html) {
1036 Err(_e) => return Err(ApiError::err(&_e).into()),
1039 Ok(PasswordResetResponse {})
1043 #[async_trait::async_trait(?Send)]
1044 impl Perform for PasswordChange {
1045 type Response = LoginResponse;
1049 context: &Data<LemmyContext>,
1050 _websocket_id: Option<ConnectionId>,
1051 ) -> Result<LoginResponse, LemmyError> {
1052 let data: &PasswordChange = &self;
1054 // Fetch the user_id from the token
1055 let token = data.token.clone();
1056 let local_user_id = blocking(context.pool(), move |conn| {
1057 PasswordResetRequest::read_from_token(conn, &token).map(|p| p.local_user_id)
1061 password_length_check(&data.password)?;
1063 // Make sure passwords match
1064 if data.password != data.password_verify {
1065 return Err(ApiError::err("passwords_dont_match").into());
1068 // Update the user with the new password
1069 let password = data.password.clone();
1070 let updated_local_user = match blocking(context.pool(), move |conn| {
1071 LocalUser::update_password(conn, local_user_id, &password)
1076 Err(_e) => return Err(ApiError::err("couldnt_update_user").into()),
1081 jwt: Claims::jwt(updated_local_user.id.0, Settings::get().hostname())?,
1086 #[async_trait::async_trait(?Send)]
1087 impl Perform for CreatePrivateMessage {
1088 type Response = PrivateMessageResponse;
1092 context: &Data<LemmyContext>,
1093 websocket_id: Option<ConnectionId>,
1094 ) -> Result<PrivateMessageResponse, LemmyError> {
1095 let data: &CreatePrivateMessage = &self;
1096 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
1098 let content_slurs_removed = remove_slurs(&data.content.to_owned());
1100 let private_message_form = PrivateMessageForm {
1101 content: content_slurs_removed.to_owned(),
1102 creator_id: local_user_view.person.id,
1103 recipient_id: data.recipient_id,
1112 let inserted_private_message = match blocking(context.pool(), move |conn| {
1113 PrivateMessage::create(conn, &private_message_form)
1117 Ok(private_message) => private_message,
1119 return Err(ApiError::err("couldnt_create_private_message").into());
1123 let inserted_private_message_id = inserted_private_message.id;
1124 let updated_private_message = match blocking(
1126 move |conn| -> Result<PrivateMessage, LemmyError> {
1127 let apub_id = generate_apub_endpoint(
1128 EndpointType::PrivateMessage,
1129 &inserted_private_message_id.to_string(),
1131 Ok(PrivateMessage::update_ap_id(
1133 inserted_private_message_id,
1140 Ok(private_message) => private_message,
1141 Err(_e) => return Err(ApiError::err("couldnt_create_private_message").into()),
1144 updated_private_message
1145 .send_create(&local_user_view.person, context)
1148 let private_message_view = blocking(context.pool(), move |conn| {
1149 PrivateMessageView::read(conn, inserted_private_message.id)
1153 let res = PrivateMessageResponse {
1154 private_message_view,
1157 // Send notifications to the local recipient, if one exists
1158 let recipient_id = data.recipient_id;
1159 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
1160 LocalUserView::read_person(conn, recipient_id)
1166 "Private Message from",
1168 &content_slurs_removed,
1171 let local_recipient_id = local_recipient.local_user.id;
1172 context.chat_server().do_send(SendUserRoomMessage {
1173 op: UserOperation::CreatePrivateMessage,
1174 response: res.clone(),
1184 #[async_trait::async_trait(?Send)]
1185 impl Perform for EditPrivateMessage {
1186 type Response = PrivateMessageResponse;
1190 context: &Data<LemmyContext>,
1191 websocket_id: Option<ConnectionId>,
1192 ) -> Result<PrivateMessageResponse, LemmyError> {
1193 let data: &EditPrivateMessage = &self;
1194 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
1196 // Checking permissions
1197 let private_message_id = data.private_message_id;
1198 let orig_private_message = blocking(context.pool(), move |conn| {
1199 PrivateMessage::read(conn, private_message_id)
1202 if local_user_view.person.id != orig_private_message.creator_id {
1203 return Err(ApiError::err("no_private_message_edit_allowed").into());
1207 let content_slurs_removed = remove_slurs(&data.content);
1208 let private_message_id = data.private_message_id;
1209 let updated_private_message = match blocking(context.pool(), move |conn| {
1210 PrivateMessage::update_content(conn, private_message_id, &content_slurs_removed)
1214 Ok(private_message) => private_message,
1215 Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
1218 // Send the apub update
1219 updated_private_message
1220 .send_update(&local_user_view.person, context)
1223 let private_message_id = data.private_message_id;
1224 let private_message_view = blocking(context.pool(), move |conn| {
1225 PrivateMessageView::read(conn, private_message_id)
1229 let res = PrivateMessageResponse {
1230 private_message_view,
1233 // Send notifications to the local recipient, if one exists
1234 let recipient_id = orig_private_message.recipient_id;
1235 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
1236 LocalUserView::read_person(conn, recipient_id)
1240 let local_recipient_id = local_recipient.local_user.id;
1241 context.chat_server().do_send(SendUserRoomMessage {
1242 op: UserOperation::EditPrivateMessage,
1243 response: res.clone(),
1253 #[async_trait::async_trait(?Send)]
1254 impl Perform for DeletePrivateMessage {
1255 type Response = PrivateMessageResponse;
1259 context: &Data<LemmyContext>,
1260 websocket_id: Option<ConnectionId>,
1261 ) -> Result<PrivateMessageResponse, LemmyError> {
1262 let data: &DeletePrivateMessage = &self;
1263 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
1265 // Checking permissions
1266 let private_message_id = data.private_message_id;
1267 let orig_private_message = blocking(context.pool(), move |conn| {
1268 PrivateMessage::read(conn, private_message_id)
1271 if local_user_view.person.id != orig_private_message.creator_id {
1272 return Err(ApiError::err("no_private_message_edit_allowed").into());
1276 let private_message_id = data.private_message_id;
1277 let deleted = data.deleted;
1278 let updated_private_message = match blocking(context.pool(), move |conn| {
1279 PrivateMessage::update_deleted(conn, private_message_id, deleted)
1283 Ok(private_message) => private_message,
1284 Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
1287 // Send the apub update
1289 updated_private_message
1290 .send_delete(&local_user_view.person, context)
1293 updated_private_message
1294 .send_undo_delete(&local_user_view.person, context)
1298 let private_message_id = data.private_message_id;
1299 let private_message_view = blocking(context.pool(), move |conn| {
1300 PrivateMessageView::read(conn, private_message_id)
1304 let res = PrivateMessageResponse {
1305 private_message_view,
1308 // Send notifications to the local recipient, if one exists
1309 let recipient_id = orig_private_message.recipient_id;
1310 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
1311 LocalUserView::read_person(conn, recipient_id)
1315 let local_recipient_id = local_recipient.local_user.id;
1316 context.chat_server().do_send(SendUserRoomMessage {
1317 op: UserOperation::DeletePrivateMessage,
1318 response: res.clone(),
1328 #[async_trait::async_trait(?Send)]
1329 impl Perform for MarkPrivateMessageAsRead {
1330 type Response = PrivateMessageResponse;
1334 context: &Data<LemmyContext>,
1335 websocket_id: Option<ConnectionId>,
1336 ) -> Result<PrivateMessageResponse, LemmyError> {
1337 let data: &MarkPrivateMessageAsRead = &self;
1338 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
1340 // Checking permissions
1341 let private_message_id = data.private_message_id;
1342 let orig_private_message = blocking(context.pool(), move |conn| {
1343 PrivateMessage::read(conn, private_message_id)
1346 if local_user_view.person.id != orig_private_message.recipient_id {
1347 return Err(ApiError::err("couldnt_update_private_message").into());
1351 let private_message_id = data.private_message_id;
1352 let read = data.read;
1353 match blocking(context.pool(), move |conn| {
1354 PrivateMessage::update_read(conn, private_message_id, read)
1358 Ok(private_message) => private_message,
1359 Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
1362 // No need to send an apub update
1363 let private_message_id = data.private_message_id;
1364 let private_message_view = blocking(context.pool(), move |conn| {
1365 PrivateMessageView::read(conn, private_message_id)
1369 let res = PrivateMessageResponse {
1370 private_message_view,
1373 // Send notifications to the local recipient, if one exists
1374 let recipient_id = orig_private_message.recipient_id;
1375 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
1376 LocalUserView::read_person(conn, recipient_id)
1380 let local_recipient_id = local_recipient.local_user.id;
1381 context.chat_server().do_send(SendUserRoomMessage {
1382 op: UserOperation::MarkPrivateMessageAsRead,
1383 response: res.clone(),
1393 #[async_trait::async_trait(?Send)]
1394 impl Perform for GetPrivateMessages {
1395 type Response = PrivateMessagesResponse;
1399 context: &Data<LemmyContext>,
1400 _websocket_id: Option<ConnectionId>,
1401 ) -> Result<PrivateMessagesResponse, LemmyError> {
1402 let data: &GetPrivateMessages = &self;
1403 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
1404 let person_id = local_user_view.person.id;
1406 let page = data.page;
1407 let limit = data.limit;
1408 let unread_only = data.unread_only;
1409 let messages = blocking(context.pool(), move |conn| {
1410 PrivateMessageQueryBuilder::create(&conn, person_id)
1413 .unread_only(unread_only)
1418 Ok(PrivateMessagesResponse {
1419 private_messages: messages,
1424 #[async_trait::async_trait(?Send)]
1425 impl Perform for GetReportCount {
1426 type Response = GetReportCountResponse;
1430 context: &Data<LemmyContext>,
1431 websocket_id: Option<ConnectionId>,
1432 ) -> Result<GetReportCountResponse, LemmyError> {
1433 let data: &GetReportCount = &self;
1434 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
1436 let person_id = local_user_view.person.id;
1437 let community_id = data.community;
1439 collect_moderated_communities(person_id, community_id, context.pool()).await?;
1442 if community_ids.is_empty() {
1443 GetReportCountResponse {
1449 let ids = community_ids.clone();
1450 let comment_reports = blocking(context.pool(), move |conn| {
1451 CommentReportView::get_report_count(conn, &ids)
1455 let ids = community_ids.clone();
1456 let post_reports = blocking(context.pool(), move |conn| {
1457 PostReportView::get_report_count(conn, &ids)
1461 GetReportCountResponse {
1462 community: data.community,
1469 context.chat_server().do_send(SendUserRoomMessage {
1470 op: UserOperation::GetReportCount,
1471 response: res.clone(),
1472 local_recipient_id: local_user_view.local_user.id,