2 captcha_espeak_wav_base64,
4 collect_moderated_communities,
10 use actix_web::web::Data;
13 use captcha::{gen, Difficulty};
16 generate_apub_endpoint,
17 generate_followers_url,
19 generate_shared_inbox_url,
23 use lemmy_db_queries::{
24 diesel_option_overwrite,
27 community::Community_,
28 password_reset_request::PasswordResetRequest_,
30 private_message::PrivateMessage_,
33 user_mention::UserMention_,
41 use lemmy_db_schema::{
47 password_reset_request::*,
56 comment_report_view::CommentReportView,
57 comment_view::CommentQueryBuilder,
58 post_report_view::PostReportView,
59 post_view::PostQueryBuilder,
60 private_message_view::{PrivateMessageQueryBuilder, PrivateMessageView},
62 use lemmy_db_views_actor::{
63 community_follower_view::CommunityFollowerView,
64 community_moderator_view::CommunityModeratorView,
65 user_mention_view::{UserMentionQueryBuilder, UserMentionView},
66 user_view::UserViewSafe,
68 use lemmy_structs::{blocking, send_email_to_user, user::*};
70 apub::generate_actor_keypair,
77 generate_random_string,
78 is_valid_preferred_username,
87 use lemmy_websocket::{
88 messages::{CaptchaItem, CheckCaptcha, SendAllMessage, SendUserRoomMessage},
92 use std::str::FromStr;
94 #[async_trait::async_trait(?Send)]
95 impl Perform for Login {
96 type Response = LoginResponse;
100 context: &Data<LemmyContext>,
101 _websocket_id: Option<ConnectionId>,
102 ) -> Result<LoginResponse, LemmyError> {
103 let data: &Login = &self;
105 // Fetch that username / email
106 let username_or_email = data.username_or_email.clone();
107 let user = match blocking(context.pool(), move |conn| {
108 User_::find_by_email_or_username(conn, &username_or_email)
113 Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
116 // Verify the password
117 let valid: bool = verify(&data.password, &user.password_encrypted).unwrap_or(false);
119 return Err(ApiError::err("password_incorrect").into());
124 jwt: Claims::jwt(user.id, Settings::get().hostname)?,
129 #[async_trait::async_trait(?Send)]
130 impl Perform for Register {
131 type Response = LoginResponse;
135 context: &Data<LemmyContext>,
136 _websocket_id: Option<ConnectionId>,
137 ) -> Result<LoginResponse, LemmyError> {
138 let data: &Register = &self;
140 // Make sure site has open registration
141 if let Ok(site) = blocking(context.pool(), move |conn| Site::read_simple(conn)).await? {
142 if !site.open_registration {
143 return Err(ApiError::err("registration_closed").into());
147 // Password length check
148 if data.password.len() > 60 {
149 return Err(ApiError::err("invalid_password").into());
152 // Make sure passwords match
153 if data.password != data.password_verify {
154 return Err(ApiError::err("passwords_dont_match").into());
157 // Check if there are admins. False if admins exist
158 let no_admins = blocking(context.pool(), move |conn| {
159 UserViewSafe::admins(conn).map(|a| a.is_empty())
163 // If its not the admin, check the captcha
164 if !no_admins && Settings::get().captcha.enabled {
171 .unwrap_or_else(|| "".to_string()),
175 .unwrap_or_else(|| "".to_string()),
179 return Err(ApiError::err("captcha_incorrect").into());
183 check_slurs(&data.username)?;
185 let user_keypair = generate_actor_keypair()?;
186 if !is_valid_username(&data.username) {
187 return Err(ApiError::err("invalid_username").into());
189 let user_actor_id = generate_apub_endpoint(EndpointType::User, &data.username)?;
191 // Register the new user
192 let user_form = UserForm {
193 name: data.username.to_owned(),
194 email: Some(data.email.to_owned()),
195 matrix_user_id: None,
198 password_encrypted: data.password.to_owned(),
199 preferred_username: None,
204 show_nsfw: data.show_nsfw,
205 theme: "browser".into(),
206 default_sort_type: SortType::Active as i16,
207 default_listing_type: ListingType::Subscribed as i16,
208 lang: "browser".into(),
210 send_notifications_to_email: false,
211 actor_id: Some(user_actor_id.clone()),
214 private_key: Some(user_keypair.private_key),
215 public_key: Some(user_keypair.public_key),
216 last_refreshed_at: None,
217 inbox_url: Some(generate_inbox_url(&user_actor_id)?),
218 shared_inbox_url: Some(Some(generate_shared_inbox_url(&user_actor_id)?)),
222 let inserted_user = match blocking(context.pool(), move |conn| {
223 User_::register(conn, &user_form)
229 let err_type = if e.to_string()
230 == "duplicate key value violates unique constraint \"user__email_key\""
232 "email_already_exists"
234 "user_already_exists"
237 return Err(ApiError::err(err_type).into());
241 let main_community_keypair = generate_actor_keypair()?;
243 // Create the main community if it doesn't exist
245 match blocking(context.pool(), move |conn| Community::read(conn, 2)).await? {
248 let default_community_name = "main";
249 let actor_id = generate_apub_endpoint(EndpointType::Community, default_community_name)?;
250 let community_form = CommunityForm {
251 name: default_community_name.to_string(),
252 title: "The Default Community".to_string(),
253 description: Some("The Default Community".to_string()),
255 creator_id: inserted_user.id,
259 actor_id: Some(actor_id.to_owned()),
261 private_key: Some(main_community_keypair.private_key),
262 public_key: Some(main_community_keypair.public_key),
263 last_refreshed_at: None,
267 followers_url: Some(generate_followers_url(&actor_id)?),
268 inbox_url: Some(generate_inbox_url(&actor_id)?),
269 shared_inbox_url: Some(Some(generate_shared_inbox_url(&actor_id)?)),
271 blocking(context.pool(), move |conn| {
272 Community::create(conn, &community_form)
278 // Sign them up for main community no matter what
279 let community_follower_form = CommunityFollowerForm {
280 community_id: main_community.id,
281 person_id: inserted_user.id,
285 let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
286 if blocking(context.pool(), follow).await?.is_err() {
287 return Err(ApiError::err("community_follower_already_exists").into());
290 // If its an admin, add them as a mod and follower to main
292 let community_moderator_form = CommunityModeratorForm {
293 community_id: main_community.id,
294 person_id: inserted_user.id,
297 let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
298 if blocking(context.pool(), join).await?.is_err() {
299 return Err(ApiError::err("community_moderator_already_exists").into());
305 jwt: Claims::jwt(inserted_user.id, Settings::get().hostname)?,
310 #[async_trait::async_trait(?Send)]
311 impl Perform for GetCaptcha {
312 type Response = GetCaptchaResponse;
316 context: &Data<LemmyContext>,
317 _websocket_id: Option<ConnectionId>,
318 ) -> Result<Self::Response, LemmyError> {
319 let captcha_settings = Settings::get().captcha;
321 if !captcha_settings.enabled {
322 return Ok(GetCaptchaResponse { ok: None });
325 let captcha = match captcha_settings.difficulty.as_str() {
326 "easy" => gen(Difficulty::Easy),
327 "medium" => gen(Difficulty::Medium),
328 "hard" => gen(Difficulty::Hard),
329 _ => gen(Difficulty::Medium),
332 let answer = captcha.chars_as_string();
334 let png_byte_array = captcha.as_png().expect("failed to generate captcha");
336 let png = base64::encode(png_byte_array);
338 let uuid = uuid::Uuid::new_v4().to_string();
340 let wav = captcha_espeak_wav_base64(&answer).ok();
342 let captcha_item = CaptchaItem {
344 uuid: uuid.to_owned(),
345 expires: naive_now() + Duration::minutes(10), // expires in 10 minutes
348 // Stores the captcha item on the queue
349 context.chat_server().do_send(captcha_item);
351 Ok(GetCaptchaResponse {
352 ok: Some(CaptchaResponse { png, uuid, wav }),
357 #[async_trait::async_trait(?Send)]
358 impl Perform for SaveUserSettings {
359 type Response = LoginResponse;
363 context: &Data<LemmyContext>,
364 _websocket_id: Option<ConnectionId>,
365 ) -> Result<LoginResponse, LemmyError> {
366 let data: &SaveUserSettings = &self;
367 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
369 let avatar = diesel_option_overwrite(&data.avatar);
370 let banner = diesel_option_overwrite(&data.banner);
371 let email = diesel_option_overwrite(&data.email);
372 let bio = diesel_option_overwrite(&data.bio);
373 let preferred_username = diesel_option_overwrite(&data.preferred_username);
374 let matrix_user_id = diesel_option_overwrite(&data.matrix_user_id);
376 // Check to make sure the avatar and banners are urls
377 check_optional_url(&avatar)?;
378 check_optional_url(&banner)?;
380 if let Some(Some(bio)) = &bio {
381 if bio.chars().count() > 300 {
382 return Err(ApiError::err("bio_length_overflow").into());
386 if let Some(Some(preferred_username)) = &preferred_username {
387 if !is_valid_preferred_username(preferred_username.trim()) {
388 return Err(ApiError::err("invalid_username").into());
392 let user_id = user.id;
393 let password_encrypted = match &data.new_password {
394 Some(new_password) => {
395 match &data.new_password_verify {
396 Some(new_password_verify) => {
397 // Make sure passwords match
398 if new_password != new_password_verify {
399 return Err(ApiError::err("passwords_dont_match").into());
402 // Check the old password
403 match &data.old_password {
404 Some(old_password) => {
405 let valid: bool = verify(old_password, &user.password_encrypted).unwrap_or(false);
407 return Err(ApiError::err("password_incorrect").into());
409 let new_password = new_password.to_owned();
410 let user = blocking(context.pool(), move |conn| {
411 User_::update_password(conn, user_id, &new_password)
414 user.password_encrypted
416 None => return Err(ApiError::err("password_incorrect").into()),
419 None => return Err(ApiError::err("passwords_dont_match").into()),
422 None => user.password_encrypted,
425 let default_listing_type = data.default_listing_type;
426 let default_sort_type = data.default_sort_type;
428 let user_form = UserForm {
437 published: Some(user.published),
438 updated: Some(naive_now()),
440 banned: Some(user.banned),
441 show_nsfw: data.show_nsfw,
442 theme: data.theme.to_owned(),
444 default_listing_type,
445 lang: data.lang.to_owned(),
446 show_avatars: data.show_avatars,
447 send_notifications_to_email: data.send_notifications_to_email,
448 actor_id: Some(user.actor_id),
451 private_key: user.private_key,
452 public_key: user.public_key,
453 last_refreshed_at: None,
454 shared_inbox_url: None,
457 let res = blocking(context.pool(), move |conn| {
458 User_::update(conn, user_id, &user_form)
461 let updated_user: User_ = match res {
464 let err_type = if e.to_string()
465 == "duplicate key value violates unique constraint \"user__email_key\""
467 "email_already_exists"
469 "user_already_exists"
472 return Err(ApiError::err(err_type).into());
478 jwt: Claims::jwt(updated_user.id, Settings::get().hostname)?,
483 #[async_trait::async_trait(?Send)]
484 impl Perform for GetUserDetails {
485 type Response = GetUserDetailsResponse;
489 context: &Data<LemmyContext>,
490 _websocket_id: Option<ConnectionId>,
491 ) -> Result<GetUserDetailsResponse, LemmyError> {
492 let data: &GetUserDetails = &self;
493 let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
495 let show_nsfw = match &user {
496 Some(user) => user.show_nsfw,
500 let sort = SortType::from_str(&data.sort)?;
505 .unwrap_or_else(|| "admin".to_string());
506 let user_details_id = match data.user_id {
509 let user = blocking(context.pool(), move |conn| {
510 User_::read_from_name(conn, &username)
515 Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
520 let user_id = user.map(|u| u.id);
522 // You don't need to return settings for the user, since this comes back with GetSite
524 let user_view = blocking(context.pool(), move |conn| {
525 UserViewSafe::read(conn, user_details_id)
529 let page = data.page;
530 let limit = data.limit;
531 let saved_only = data.saved_only;
532 let community_id = data.community_id;
534 let (posts, comments) = blocking(context.pool(), move |conn| {
535 let mut posts_query = PostQueryBuilder::create(conn)
537 .show_nsfw(show_nsfw)
538 .saved_only(saved_only)
539 .community_id(community_id)
544 let mut comments_query = CommentQueryBuilder::create(conn)
547 .saved_only(saved_only)
551 // If its saved only, you don't care what creator it was
552 // Or, if its not saved, then you only want it for that specific creator
554 posts_query = posts_query.creator_id(user_details_id);
555 comments_query = comments_query.creator_id(user_details_id);
558 let posts = posts_query.list()?;
559 let comments = comments_query.list()?;
561 Ok((posts, comments)) as Result<_, LemmyError>
565 let mut follows = vec![];
566 if let Some(uid) = user_id {
567 if uid == user_details_id {
568 follows = blocking(context.pool(), move |conn| {
569 CommunityFollowerView::for_user(conn, user_details_id)
574 let moderates = blocking(context.pool(), move |conn| {
575 CommunityModeratorView::for_user(conn, user_details_id)
580 Ok(GetUserDetailsResponse {
590 #[async_trait::async_trait(?Send)]
591 impl Perform for AddAdmin {
592 type Response = AddAdminResponse;
596 context: &Data<LemmyContext>,
597 websocket_id: Option<ConnectionId>,
598 ) -> Result<AddAdminResponse, LemmyError> {
599 let data: &AddAdmin = &self;
600 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
602 // Make sure user is an admin
603 is_admin(context.pool(), user.id).await?;
605 let added = data.added;
606 let added_user_id = data.user_id;
607 let add_admin = move |conn: &'_ _| User_::add_admin(conn, added_user_id, added);
608 if blocking(context.pool(), add_admin).await?.is_err() {
609 return Err(ApiError::err("couldnt_update_user").into());
613 let form = ModAddForm {
614 mod_person_id: user.id,
615 other_person_id: data.user_id,
616 removed: Some(!data.added),
619 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
621 let site_creator_id = blocking(context.pool(), move |conn| {
622 Site::read(conn, 1).map(|s| s.creator_id)
626 let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
627 let creator_index = admins
629 .position(|r| r.user.id == site_creator_id)
630 .context(location_info!())?;
631 let creator_user = admins.remove(creator_index);
632 admins.insert(0, creator_user);
634 let res = AddAdminResponse { admins };
636 context.chat_server().do_send(SendAllMessage {
637 op: UserOperation::AddAdmin,
638 response: res.clone(),
646 #[async_trait::async_trait(?Send)]
647 impl Perform for BanUser {
648 type Response = BanUserResponse;
652 context: &Data<LemmyContext>,
653 websocket_id: Option<ConnectionId>,
654 ) -> Result<BanUserResponse, LemmyError> {
655 let data: &BanUser = &self;
656 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
658 // Make sure user is an admin
659 is_admin(context.pool(), user.id).await?;
662 let banned_user_id = data.user_id;
663 let ban_user = move |conn: &'_ _| User_::ban_user(conn, banned_user_id, ban);
664 if blocking(context.pool(), ban_user).await?.is_err() {
665 return Err(ApiError::err("couldnt_update_user").into());
668 // Remove their data if that's desired
669 if data.remove_data {
671 blocking(context.pool(), move |conn: &'_ _| {
672 Post::update_removed_for_creator(conn, banned_user_id, None, true)
677 blocking(context.pool(), move |conn: &'_ _| {
678 Community::update_removed_for_creator(conn, banned_user_id, true)
683 blocking(context.pool(), move |conn: &'_ _| {
684 Comment::update_removed_for_creator(conn, banned_user_id, true)
690 let expires = match data.expires {
691 Some(time) => Some(naive_from_unix(time)),
695 let form = ModBanForm {
696 mod_person_id: user.id,
697 other_person_id: data.user_id,
698 reason: data.reason.to_owned(),
699 banned: Some(data.ban),
703 blocking(context.pool(), move |conn| ModBan::create(conn, &form)).await??;
705 let user_id = data.user_id;
706 let user_view = blocking(context.pool(), move |conn| {
707 UserViewSafe::read(conn, user_id)
711 let res = BanUserResponse {
716 context.chat_server().do_send(SendAllMessage {
717 op: UserOperation::BanUser,
718 response: res.clone(),
726 #[async_trait::async_trait(?Send)]
727 impl Perform for GetReplies {
728 type Response = GetRepliesResponse;
732 context: &Data<LemmyContext>,
733 _websocket_id: Option<ConnectionId>,
734 ) -> Result<GetRepliesResponse, LemmyError> {
735 let data: &GetReplies = &self;
736 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
738 let sort = SortType::from_str(&data.sort)?;
740 let page = data.page;
741 let limit = data.limit;
742 let unread_only = data.unread_only;
743 let user_id = user.id;
744 let replies = blocking(context.pool(), move |conn| {
745 CommentQueryBuilder::create(conn)
747 .unread_only(unread_only)
748 .recipient_id(user_id)
756 Ok(GetRepliesResponse { replies })
760 #[async_trait::async_trait(?Send)]
761 impl Perform for GetUserMentions {
762 type Response = GetUserMentionsResponse;
766 context: &Data<LemmyContext>,
767 _websocket_id: Option<ConnectionId>,
768 ) -> Result<GetUserMentionsResponse, LemmyError> {
769 let data: &GetUserMentions = &self;
770 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
772 let sort = SortType::from_str(&data.sort)?;
774 let page = data.page;
775 let limit = data.limit;
776 let unread_only = data.unread_only;
777 let user_id = user.id;
778 let mentions = blocking(context.pool(), move |conn| {
779 UserMentionQueryBuilder::create(conn)
780 .recipient_id(user_id)
783 .unread_only(unread_only)
790 Ok(GetUserMentionsResponse { mentions })
794 #[async_trait::async_trait(?Send)]
795 impl Perform for MarkUserMentionAsRead {
796 type Response = UserMentionResponse;
800 context: &Data<LemmyContext>,
801 _websocket_id: Option<ConnectionId>,
802 ) -> Result<UserMentionResponse, LemmyError> {
803 let data: &MarkUserMentionAsRead = &self;
804 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
806 let user_mention_id = data.user_mention_id;
807 let read_user_mention = blocking(context.pool(), move |conn| {
808 UserMention::read(conn, user_mention_id)
812 if user.id != read_user_mention.recipient_id {
813 return Err(ApiError::err("couldnt_update_comment").into());
816 let user_mention_id = read_user_mention.id;
817 let read = data.read;
818 let update_mention = move |conn: &'_ _| UserMention::update_read(conn, user_mention_id, read);
819 if blocking(context.pool(), update_mention).await?.is_err() {
820 return Err(ApiError::err("couldnt_update_comment").into());
823 let user_mention_id = read_user_mention.id;
824 let user_id = user.id;
825 let user_mention_view = blocking(context.pool(), move |conn| {
826 UserMentionView::read(conn, user_mention_id, Some(user_id))
830 Ok(UserMentionResponse { user_mention_view })
834 #[async_trait::async_trait(?Send)]
835 impl Perform for MarkAllAsRead {
836 type Response = GetRepliesResponse;
840 context: &Data<LemmyContext>,
841 _websocket_id: Option<ConnectionId>,
842 ) -> Result<GetRepliesResponse, LemmyError> {
843 let data: &MarkAllAsRead = &self;
844 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
846 let user_id = user.id;
847 let replies = blocking(context.pool(), move |conn| {
848 CommentQueryBuilder::create(conn)
850 .recipient_id(user_id)
858 // TODO: this should probably be a bulk operation
859 // Not easy to do as a bulk operation,
860 // because recipient_id isn't in the comment table
861 for comment_view in &replies {
862 let reply_id = comment_view.comment.id;
863 let mark_as_read = move |conn: &'_ _| Comment::update_read(conn, reply_id, true);
864 if blocking(context.pool(), mark_as_read).await?.is_err() {
865 return Err(ApiError::err("couldnt_update_comment").into());
869 // Mark all user mentions as read
870 let update_user_mentions = move |conn: &'_ _| UserMention::mark_all_as_read(conn, user_id);
871 if blocking(context.pool(), update_user_mentions)
875 return Err(ApiError::err("couldnt_update_comment").into());
878 // Mark all private_messages as read
879 let update_pm = move |conn: &'_ _| PrivateMessage::mark_all_as_read(conn, user_id);
880 if blocking(context.pool(), update_pm).await?.is_err() {
881 return Err(ApiError::err("couldnt_update_private_message").into());
884 Ok(GetRepliesResponse { replies: vec![] })
888 #[async_trait::async_trait(?Send)]
889 impl Perform for DeleteAccount {
890 type Response = LoginResponse;
894 context: &Data<LemmyContext>,
895 _websocket_id: Option<ConnectionId>,
896 ) -> Result<LoginResponse, LemmyError> {
897 let data: &DeleteAccount = &self;
898 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
900 // Verify the password
901 let valid: bool = verify(&data.password, &user.password_encrypted).unwrap_or(false);
903 return Err(ApiError::err("password_incorrect").into());
907 let user_id = user.id;
908 let permadelete = move |conn: &'_ _| Comment::permadelete_for_creator(conn, user_id);
909 if blocking(context.pool(), permadelete).await?.is_err() {
910 return Err(ApiError::err("couldnt_update_comment").into());
914 let permadelete = move |conn: &'_ _| Post::permadelete_for_creator(conn, user_id);
915 if blocking(context.pool(), permadelete).await?.is_err() {
916 return Err(ApiError::err("couldnt_update_post").into());
919 blocking(context.pool(), move |conn| {
920 User_::delete_account(conn, user_id)
925 jwt: data.auth.to_owned(),
930 #[async_trait::async_trait(?Send)]
931 impl Perform for PasswordReset {
932 type Response = PasswordResetResponse;
936 context: &Data<LemmyContext>,
937 _websocket_id: Option<ConnectionId>,
938 ) -> Result<PasswordResetResponse, LemmyError> {
939 let data: &PasswordReset = &self;
942 let email = data.email.clone();
943 let user = match blocking(context.pool(), move |conn| {
944 User_::find_by_email(conn, &email)
949 Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
952 // Generate a random token
953 let token = generate_random_string();
956 let token2 = token.clone();
957 let user_id = user.id;
958 blocking(context.pool(), move |conn| {
959 PasswordResetRequest::create_token(conn, user_id, &token2)
963 // Email the pure token to the user.
964 // TODO no i18n support here.
965 let user_email = &user.email.expect("email");
966 let subject = &format!("Password reset for {}", user.name);
967 let hostname = &Settings::get().get_protocol_and_hostname();
968 let html = &format!("<h1>Password Reset Request for {}</h1><br><a href={}/password_change/{}>Click here to reset your password</a>", user.name, hostname, &token);
969 match send_email(subject, user_email, &user.name, html) {
971 Err(_e) => return Err(ApiError::err(&_e).into()),
974 Ok(PasswordResetResponse {})
978 #[async_trait::async_trait(?Send)]
979 impl Perform for PasswordChange {
980 type Response = LoginResponse;
984 context: &Data<LemmyContext>,
985 _websocket_id: Option<ConnectionId>,
986 ) -> Result<LoginResponse, LemmyError> {
987 let data: &PasswordChange = &self;
989 // Fetch the user_id from the token
990 let token = data.token.clone();
991 let user_id = blocking(context.pool(), move |conn| {
992 PasswordResetRequest::read_from_token(conn, &token).map(|p| p.local_user_id)
996 // Make sure passwords match
997 if data.password != data.password_verify {
998 return Err(ApiError::err("passwords_dont_match").into());
1001 // Update the user with the new password
1002 let password = data.password.clone();
1003 let updated_user = match blocking(context.pool(), move |conn| {
1004 User_::update_password(conn, user_id, &password)
1009 Err(_e) => return Err(ApiError::err("couldnt_update_user").into()),
1014 jwt: Claims::jwt(updated_user.id, Settings::get().hostname)?,
1019 #[async_trait::async_trait(?Send)]
1020 impl Perform for CreatePrivateMessage {
1021 type Response = PrivateMessageResponse;
1025 context: &Data<LemmyContext>,
1026 websocket_id: Option<ConnectionId>,
1027 ) -> Result<PrivateMessageResponse, LemmyError> {
1028 let data: &CreatePrivateMessage = &self;
1029 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
1031 let content_slurs_removed = remove_slurs(&data.content.to_owned());
1033 let private_message_form = PrivateMessageForm {
1034 content: content_slurs_removed.to_owned(),
1035 creator_id: user.id,
1036 recipient_id: data.recipient_id,
1045 let inserted_private_message = match blocking(context.pool(), move |conn| {
1046 PrivateMessage::create(conn, &private_message_form)
1050 Ok(private_message) => private_message,
1052 return Err(ApiError::err("couldnt_create_private_message").into());
1056 let inserted_private_message_id = inserted_private_message.id;
1057 let updated_private_message = match blocking(
1059 move |conn| -> Result<PrivateMessage, LemmyError> {
1060 let apub_id = generate_apub_endpoint(
1061 EndpointType::PrivateMessage,
1062 &inserted_private_message_id.to_string(),
1064 Ok(PrivateMessage::update_ap_id(
1066 inserted_private_message_id,
1073 Ok(private_message) => private_message,
1074 Err(_e) => return Err(ApiError::err("couldnt_create_private_message").into()),
1077 updated_private_message.send_create(&user, context).await?;
1079 // Send notifications to the recipient
1080 let recipient_id = data.recipient_id;
1081 let recipient_user =
1082 blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
1083 if recipient_user.send_notifications_to_email {
1086 "Private Message from",
1088 &content_slurs_removed,
1092 let message = blocking(context.pool(), move |conn| {
1093 PrivateMessageView::read(conn, inserted_private_message.id)
1097 let res = PrivateMessageResponse {
1098 private_message_view: message,
1101 context.chat_server().do_send(SendUserRoomMessage {
1102 op: UserOperation::CreatePrivateMessage,
1103 response: res.clone(),
1112 #[async_trait::async_trait(?Send)]
1113 impl Perform for EditPrivateMessage {
1114 type Response = PrivateMessageResponse;
1118 context: &Data<LemmyContext>,
1119 websocket_id: Option<ConnectionId>,
1120 ) -> Result<PrivateMessageResponse, LemmyError> {
1121 let data: &EditPrivateMessage = &self;
1122 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
1124 // Checking permissions
1125 let private_message_id = data.private_message_id;
1126 let orig_private_message = blocking(context.pool(), move |conn| {
1127 PrivateMessage::read(conn, private_message_id)
1130 if user.id != orig_private_message.creator_id {
1131 return Err(ApiError::err("no_private_message_edit_allowed").into());
1135 let content_slurs_removed = remove_slurs(&data.content);
1136 let private_message_id = data.private_message_id;
1137 let updated_private_message = match blocking(context.pool(), move |conn| {
1138 PrivateMessage::update_content(conn, private_message_id, &content_slurs_removed)
1142 Ok(private_message) => private_message,
1143 Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
1146 // Send the apub update
1147 updated_private_message.send_update(&user, context).await?;
1149 let private_message_id = data.private_message_id;
1150 let message = blocking(context.pool(), move |conn| {
1151 PrivateMessageView::read(conn, private_message_id)
1154 let recipient_id = message.recipient.id;
1156 let res = PrivateMessageResponse {
1157 private_message_view: message,
1160 context.chat_server().do_send(SendUserRoomMessage {
1161 op: UserOperation::EditPrivateMessage,
1162 response: res.clone(),
1171 #[async_trait::async_trait(?Send)]
1172 impl Perform for DeletePrivateMessage {
1173 type Response = PrivateMessageResponse;
1177 context: &Data<LemmyContext>,
1178 websocket_id: Option<ConnectionId>,
1179 ) -> Result<PrivateMessageResponse, LemmyError> {
1180 let data: &DeletePrivateMessage = &self;
1181 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
1183 // Checking permissions
1184 let private_message_id = data.private_message_id;
1185 let orig_private_message = blocking(context.pool(), move |conn| {
1186 PrivateMessage::read(conn, private_message_id)
1189 if user.id != orig_private_message.creator_id {
1190 return Err(ApiError::err("no_private_message_edit_allowed").into());
1194 let private_message_id = data.private_message_id;
1195 let deleted = data.deleted;
1196 let updated_private_message = match blocking(context.pool(), move |conn| {
1197 PrivateMessage::update_deleted(conn, private_message_id, deleted)
1201 Ok(private_message) => private_message,
1202 Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
1205 // Send the apub update
1207 updated_private_message.send_delete(&user, context).await?;
1209 updated_private_message
1210 .send_undo_delete(&user, context)
1214 let private_message_id = data.private_message_id;
1215 let message = blocking(context.pool(), move |conn| {
1216 PrivateMessageView::read(conn, private_message_id)
1219 let recipient_id = message.recipient.id;
1221 let res = PrivateMessageResponse {
1222 private_message_view: message,
1225 context.chat_server().do_send(SendUserRoomMessage {
1226 op: UserOperation::DeletePrivateMessage,
1227 response: res.clone(),
1236 #[async_trait::async_trait(?Send)]
1237 impl Perform for MarkPrivateMessageAsRead {
1238 type Response = PrivateMessageResponse;
1242 context: &Data<LemmyContext>,
1243 websocket_id: Option<ConnectionId>,
1244 ) -> Result<PrivateMessageResponse, LemmyError> {
1245 let data: &MarkPrivateMessageAsRead = &self;
1246 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
1248 // Checking permissions
1249 let private_message_id = data.private_message_id;
1250 let orig_private_message = blocking(context.pool(), move |conn| {
1251 PrivateMessage::read(conn, private_message_id)
1254 if user.id != orig_private_message.recipient_id {
1255 return Err(ApiError::err("couldnt_update_private_message").into());
1259 let private_message_id = data.private_message_id;
1260 let read = data.read;
1261 match blocking(context.pool(), move |conn| {
1262 PrivateMessage::update_read(conn, private_message_id, read)
1266 Ok(private_message) => private_message,
1267 Err(_e) => return Err(ApiError::err("couldnt_update_private_message").into()),
1270 // No need to send an apub update
1272 let private_message_id = data.private_message_id;
1273 let message = blocking(context.pool(), move |conn| {
1274 PrivateMessageView::read(conn, private_message_id)
1277 let recipient_id = message.recipient.id;
1279 let res = PrivateMessageResponse {
1280 private_message_view: message,
1283 context.chat_server().do_send(SendUserRoomMessage {
1284 op: UserOperation::MarkPrivateMessageAsRead,
1285 response: res.clone(),
1294 #[async_trait::async_trait(?Send)]
1295 impl Perform for GetPrivateMessages {
1296 type Response = PrivateMessagesResponse;
1300 context: &Data<LemmyContext>,
1301 _websocket_id: Option<ConnectionId>,
1302 ) -> Result<PrivateMessagesResponse, LemmyError> {
1303 let data: &GetPrivateMessages = &self;
1304 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
1305 let user_id = user.id;
1307 let page = data.page;
1308 let limit = data.limit;
1309 let unread_only = data.unread_only;
1310 let messages = blocking(context.pool(), move |conn| {
1311 PrivateMessageQueryBuilder::create(&conn, user_id)
1314 .unread_only(unread_only)
1319 Ok(PrivateMessagesResponse {
1320 private_messages: messages,
1325 #[async_trait::async_trait(?Send)]
1326 impl Perform for GetReportCount {
1327 type Response = GetReportCountResponse;
1331 context: &Data<LemmyContext>,
1332 websocket_id: Option<ConnectionId>,
1333 ) -> Result<GetReportCountResponse, LemmyError> {
1334 let data: &GetReportCount = &self;
1335 let user = get_user_from_jwt(&data.auth, context.pool()).await?;
1337 let user_id = user.id;
1338 let community_id = data.community;
1340 collect_moderated_communities(user_id, community_id, context.pool()).await?;
1343 if community_ids.is_empty() {
1344 GetReportCountResponse {
1350 let ids = community_ids.clone();
1351 let comment_reports = blocking(context.pool(), move |conn| {
1352 CommentReportView::get_report_count(conn, &ids)
1356 let ids = community_ids.clone();
1357 let post_reports = blocking(context.pool(), move |conn| {
1358 PostReportView::get_report_count(conn, &ids)
1362 GetReportCountResponse {
1363 community: data.community,
1370 context.chat_server().do_send(SendUserRoomMessage {
1371 op: UserOperation::GetReportCount,
1372 response: res.clone(),
1373 recipient_id: user.id,