1 use crate::{captcha_as_wav_base64, Perform};
2 use actix_web::web::Data;
5 use captcha::{gen, Difficulty};
7 use lemmy_api_common::{
9 collect_moderated_communities,
10 community::{GetFollowedCommunities, GetFollowedCommunitiesResponse},
11 get_local_user_view_from_jwt,
13 password_length_check,
16 use lemmy_db_queries::{
17 diesel_option_overwrite,
18 diesel_option_overwrite_to_url,
21 community::Community_,
22 local_user::LocalUser_,
23 password_reset_request::PasswordResetRequest_,
25 person_mention::PersonMention_,
27 private_message::PrivateMessage_,
32 use lemmy_db_schema::{
37 local_user::{LocalUser, LocalUserForm},
39 password_reset_request::*,
43 private_message::PrivateMessage,
48 comment_report_view::CommentReportView,
49 comment_view::CommentQueryBuilder,
50 local_user_view::LocalUserView,
51 post_report_view::PostReportView,
53 use lemmy_db_views_actor::{
54 community_follower_view::CommunityFollowerView,
55 person_mention_view::{PersonMentionQueryBuilder, PersonMentionView},
56 person_view::PersonViewSafe,
62 settings::structs::Settings,
63 utils::{generate_random_string, is_valid_display_name, naive_from_unix},
68 use lemmy_websocket::{
69 messages::{CaptchaItem, SendAllMessage, SendUserRoomMessage},
73 use std::str::FromStr;
75 #[async_trait::async_trait(?Send)]
76 impl Perform for Login {
77 type Response = LoginResponse;
81 context: &Data<LemmyContext>,
82 _websocket_id: Option<ConnectionId>,
83 ) -> Result<LoginResponse, LemmyError> {
84 let data: &Login = &self;
86 // Fetch that username / email
87 let username_or_email = data.username_or_email.clone();
88 let local_user_view = match blocking(context.pool(), move |conn| {
89 LocalUserView::find_by_email_or_name(conn, &username_or_email)
94 Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
97 // Verify the password
98 let valid: bool = verify(
100 &local_user_view.local_user.password_encrypted,
104 return Err(ApiError::err("password_incorrect").into());
109 jwt: Claims::jwt(local_user_view.local_user.id.0)?,
114 #[async_trait::async_trait(?Send)]
115 impl Perform for GetCaptcha {
116 type Response = GetCaptchaResponse;
120 context: &Data<LemmyContext>,
121 _websocket_id: Option<ConnectionId>,
122 ) -> Result<Self::Response, LemmyError> {
123 let captcha_settings = Settings::get().captcha();
125 if !captcha_settings.enabled {
126 return Ok(GetCaptchaResponse { ok: None });
129 let captcha = match captcha_settings.difficulty.as_str() {
130 "easy" => gen(Difficulty::Easy),
131 "medium" => gen(Difficulty::Medium),
132 "hard" => gen(Difficulty::Hard),
133 _ => gen(Difficulty::Medium),
136 let answer = captcha.chars_as_string();
138 let png = captcha.as_base64().expect("failed to generate captcha");
140 let uuid = uuid::Uuid::new_v4().to_string();
142 let wav = captcha_as_wav_base64(&captcha);
144 let captcha_item = CaptchaItem {
146 uuid: uuid.to_owned(),
147 expires: naive_now() + Duration::minutes(10), // expires in 10 minutes
150 // Stores the captcha item on the queue
151 context.chat_server().do_send(captcha_item);
153 Ok(GetCaptchaResponse {
154 ok: Some(CaptchaResponse { png, wav, uuid }),
159 #[async_trait::async_trait(?Send)]
160 impl Perform for SaveUserSettings {
161 type Response = LoginResponse;
165 context: &Data<LemmyContext>,
166 _websocket_id: Option<ConnectionId>,
167 ) -> Result<LoginResponse, LemmyError> {
168 let data: &SaveUserSettings = &self;
169 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
171 let avatar = diesel_option_overwrite_to_url(&data.avatar)?;
172 let banner = diesel_option_overwrite_to_url(&data.banner)?;
173 let email = diesel_option_overwrite(&data.email);
174 let bio = diesel_option_overwrite(&data.bio);
175 let display_name = diesel_option_overwrite(&data.display_name);
176 let matrix_user_id = diesel_option_overwrite(&data.matrix_user_id);
178 if let Some(Some(bio)) = &bio {
179 if bio.chars().count() > 300 {
180 return Err(ApiError::err("bio_length_overflow").into());
184 if let Some(Some(display_name)) = &display_name {
185 if !is_valid_display_name(display_name.trim()) {
186 return Err(ApiError::err("invalid_username").into());
190 let local_user_id = local_user_view.local_user.id;
191 let person_id = local_user_view.person.id;
192 let default_listing_type = data.default_listing_type;
193 let default_sort_type = data.default_sort_type;
194 let password_encrypted = local_user_view.local_user.password_encrypted;
196 let person_form = PersonForm {
197 name: local_user_view.person.name,
203 updated: Some(naive_now()),
212 last_refreshed_at: None,
213 shared_inbox_url: None,
217 let person_res = blocking(context.pool(), move |conn| {
218 Person::update(conn, person_id, &person_form)
221 let _updated_person: Person = match person_res {
224 return Err(ApiError::err("user_already_exists").into());
228 let local_user_form = LocalUserForm {
232 show_nsfw: data.show_nsfw,
233 show_scores: data.show_scores,
234 theme: data.theme.to_owned(),
236 default_listing_type,
237 lang: data.lang.to_owned(),
238 show_avatars: data.show_avatars,
239 send_notifications_to_email: data.send_notifications_to_email,
242 let local_user_res = blocking(context.pool(), move |conn| {
243 LocalUser::update(conn, local_user_id, &local_user_form)
246 let updated_local_user = match local_user_res {
249 let err_type = if e.to_string()
250 == "duplicate key value violates unique constraint \"local_user_email_key\""
252 "email_already_exists"
254 "user_already_exists"
257 return Err(ApiError::err(err_type).into());
263 jwt: Claims::jwt(updated_local_user.id.0)?,
268 #[async_trait::async_trait(?Send)]
269 impl Perform for ChangePassword {
270 type Response = LoginResponse;
274 context: &Data<LemmyContext>,
275 _websocket_id: Option<ConnectionId>,
276 ) -> Result<LoginResponse, LemmyError> {
277 let data: &ChangePassword = &self;
278 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
280 password_length_check(&data.new_password)?;
282 // Make sure passwords match
283 if data.new_password != data.new_password_verify {
284 return Err(ApiError::err("passwords_dont_match").into());
287 // Check the old password
288 let valid: bool = verify(
290 &local_user_view.local_user.password_encrypted,
294 return Err(ApiError::err("password_incorrect").into());
297 let local_user_id = local_user_view.local_user.id;
298 let new_password = data.new_password.to_owned();
299 let updated_local_user = blocking(context.pool(), move |conn| {
300 LocalUser::update_password(conn, local_user_id, &new_password)
306 jwt: Claims::jwt(updated_local_user.id.0)?,
311 #[async_trait::async_trait(?Send)]
312 impl Perform for AddAdmin {
313 type Response = AddAdminResponse;
317 context: &Data<LemmyContext>,
318 websocket_id: Option<ConnectionId>,
319 ) -> Result<AddAdminResponse, LemmyError> {
320 let data: &AddAdmin = &self;
321 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
323 // Make sure user is an admin
324 is_admin(&local_user_view)?;
326 let added = data.added;
327 let added_person_id = data.person_id;
328 let added_admin = match blocking(context.pool(), move |conn| {
329 Person::add_admin(conn, added_person_id, added)
335 return Err(ApiError::err("couldnt_update_user").into());
340 let form = ModAddForm {
341 mod_person_id: local_user_view.person.id,
342 other_person_id: added_admin.id,
343 removed: Some(!data.added),
346 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
348 let site_creator_id = blocking(context.pool(), move |conn| {
349 Site::read(conn, 1).map(|s| s.creator_id)
353 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
354 let creator_index = admins
356 .position(|r| r.person.id == site_creator_id)
357 .context(location_info!())?;
358 let creator_person = admins.remove(creator_index);
359 admins.insert(0, creator_person);
361 let res = AddAdminResponse { admins };
363 context.chat_server().do_send(SendAllMessage {
364 op: UserOperation::AddAdmin,
365 response: res.clone(),
373 #[async_trait::async_trait(?Send)]
374 impl Perform for BanPerson {
375 type Response = BanPersonResponse;
379 context: &Data<LemmyContext>,
380 websocket_id: Option<ConnectionId>,
381 ) -> Result<BanPersonResponse, LemmyError> {
382 let data: &BanPerson = &self;
383 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
385 // Make sure user is an admin
386 is_admin(&local_user_view)?;
389 let banned_person_id = data.person_id;
390 let ban_person = move |conn: &'_ _| Person::ban_person(conn, banned_person_id, ban);
391 if blocking(context.pool(), ban_person).await?.is_err() {
392 return Err(ApiError::err("couldnt_update_user").into());
395 // Remove their data if that's desired
396 if data.remove_data {
398 blocking(context.pool(), move |conn: &'_ _| {
399 Post::update_removed_for_creator(conn, banned_person_id, None, true)
404 blocking(context.pool(), move |conn: &'_ _| {
405 Community::update_removed_for_creator(conn, banned_person_id, true)
410 blocking(context.pool(), move |conn: &'_ _| {
411 Comment::update_removed_for_creator(conn, banned_person_id, true)
417 let expires = data.expires.map(naive_from_unix);
419 let form = ModBanForm {
420 mod_person_id: local_user_view.person.id,
421 other_person_id: data.person_id,
422 reason: data.reason.to_owned(),
423 banned: Some(data.ban),
427 blocking(context.pool(), move |conn| ModBan::create(conn, &form)).await??;
429 let person_id = data.person_id;
430 let person_view = blocking(context.pool(), move |conn| {
431 PersonViewSafe::read(conn, person_id)
435 let res = BanPersonResponse {
440 context.chat_server().do_send(SendAllMessage {
441 op: UserOperation::BanPerson,
442 response: res.clone(),
450 #[async_trait::async_trait(?Send)]
451 impl Perform for GetReplies {
452 type Response = GetRepliesResponse;
456 context: &Data<LemmyContext>,
457 _websocket_id: Option<ConnectionId>,
458 ) -> Result<GetRepliesResponse, LemmyError> {
459 let data: &GetReplies = &self;
460 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
462 let sort = SortType::from_str(&data.sort)?;
464 let page = data.page;
465 let limit = data.limit;
466 let unread_only = data.unread_only;
467 let person_id = local_user_view.person.id;
468 let replies = blocking(context.pool(), move |conn| {
469 CommentQueryBuilder::create(conn)
471 .unread_only(unread_only)
472 .recipient_id(person_id)
473 .my_person_id(person_id)
480 Ok(GetRepliesResponse { replies })
484 #[async_trait::async_trait(?Send)]
485 impl Perform for GetPersonMentions {
486 type Response = GetPersonMentionsResponse;
490 context: &Data<LemmyContext>,
491 _websocket_id: Option<ConnectionId>,
492 ) -> Result<GetPersonMentionsResponse, LemmyError> {
493 let data: &GetPersonMentions = &self;
494 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
496 let sort = SortType::from_str(&data.sort)?;
498 let page = data.page;
499 let limit = data.limit;
500 let unread_only = data.unread_only;
501 let person_id = local_user_view.person.id;
502 let mentions = blocking(context.pool(), move |conn| {
503 PersonMentionQueryBuilder::create(conn)
504 .recipient_id(person_id)
505 .my_person_id(person_id)
507 .unread_only(unread_only)
514 Ok(GetPersonMentionsResponse { mentions })
518 #[async_trait::async_trait(?Send)]
519 impl Perform for MarkPersonMentionAsRead {
520 type Response = PersonMentionResponse;
524 context: &Data<LemmyContext>,
525 _websocket_id: Option<ConnectionId>,
526 ) -> Result<PersonMentionResponse, LemmyError> {
527 let data: &MarkPersonMentionAsRead = &self;
528 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
530 let person_mention_id = data.person_mention_id;
531 let read_person_mention = blocking(context.pool(), move |conn| {
532 PersonMention::read(conn, person_mention_id)
536 if local_user_view.person.id != read_person_mention.recipient_id {
537 return Err(ApiError::err("couldnt_update_comment").into());
540 let person_mention_id = read_person_mention.id;
541 let read = data.read;
543 move |conn: &'_ _| PersonMention::update_read(conn, person_mention_id, read);
544 if blocking(context.pool(), update_mention).await?.is_err() {
545 return Err(ApiError::err("couldnt_update_comment").into());
548 let person_mention_id = read_person_mention.id;
549 let person_id = local_user_view.person.id;
550 let person_mention_view = blocking(context.pool(), move |conn| {
551 PersonMentionView::read(conn, person_mention_id, Some(person_id))
555 Ok(PersonMentionResponse {
561 #[async_trait::async_trait(?Send)]
562 impl Perform for MarkAllAsRead {
563 type Response = GetRepliesResponse;
567 context: &Data<LemmyContext>,
568 _websocket_id: Option<ConnectionId>,
569 ) -> Result<GetRepliesResponse, LemmyError> {
570 let data: &MarkAllAsRead = &self;
571 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
573 let person_id = local_user_view.person.id;
574 let replies = blocking(context.pool(), move |conn| {
575 CommentQueryBuilder::create(conn)
576 .my_person_id(person_id)
577 .recipient_id(person_id)
585 // TODO: this should probably be a bulk operation
586 // Not easy to do as a bulk operation,
587 // because recipient_id isn't in the comment table
588 for comment_view in &replies {
589 let reply_id = comment_view.comment.id;
590 let mark_as_read = move |conn: &'_ _| Comment::update_read(conn, reply_id, true);
591 if blocking(context.pool(), mark_as_read).await?.is_err() {
592 return Err(ApiError::err("couldnt_update_comment").into());
596 // Mark all user mentions as read
597 let update_person_mentions =
598 move |conn: &'_ _| PersonMention::mark_all_as_read(conn, person_id);
599 if blocking(context.pool(), update_person_mentions)
603 return Err(ApiError::err("couldnt_update_comment").into());
606 // Mark all private_messages as read
607 let update_pm = move |conn: &'_ _| PrivateMessage::mark_all_as_read(conn, person_id);
608 if blocking(context.pool(), update_pm).await?.is_err() {
609 return Err(ApiError::err("couldnt_update_private_message").into());
612 Ok(GetRepliesResponse { replies: vec![] })
616 #[async_trait::async_trait(?Send)]
617 impl Perform for PasswordReset {
618 type Response = PasswordResetResponse;
622 context: &Data<LemmyContext>,
623 _websocket_id: Option<ConnectionId>,
624 ) -> Result<PasswordResetResponse, LemmyError> {
625 let data: &PasswordReset = &self;
628 let email = data.email.clone();
629 let local_user_view = match blocking(context.pool(), move |conn| {
630 LocalUserView::find_by_email(conn, &email)
635 Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
638 // Generate a random token
639 let token = generate_random_string();
642 let token2 = token.clone();
643 let local_user_id = local_user_view.local_user.id;
644 blocking(context.pool(), move |conn| {
645 PasswordResetRequest::create_token(conn, local_user_id, &token2)
649 // Email the pure token to the user.
650 // TODO no i18n support here.
651 let email = &local_user_view.local_user.email.expect("email");
652 let subject = &format!("Password reset for {}", local_user_view.person.name);
653 let hostname = &Settings::get().get_protocol_and_hostname();
654 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);
655 match send_email(subject, email, &local_user_view.person.name, html) {
657 Err(_e) => return Err(ApiError::err(&_e).into()),
660 Ok(PasswordResetResponse {})
664 #[async_trait::async_trait(?Send)]
665 impl Perform for PasswordChange {
666 type Response = LoginResponse;
670 context: &Data<LemmyContext>,
671 _websocket_id: Option<ConnectionId>,
672 ) -> Result<LoginResponse, LemmyError> {
673 let data: &PasswordChange = &self;
675 // Fetch the user_id from the token
676 let token = data.token.clone();
677 let local_user_id = blocking(context.pool(), move |conn| {
678 PasswordResetRequest::read_from_token(conn, &token).map(|p| p.local_user_id)
682 password_length_check(&data.password)?;
684 // Make sure passwords match
685 if data.password != data.password_verify {
686 return Err(ApiError::err("passwords_dont_match").into());
689 // Update the user with the new password
690 let password = data.password.clone();
691 let updated_local_user = match blocking(context.pool(), move |conn| {
692 LocalUser::update_password(conn, local_user_id, &password)
697 Err(_e) => return Err(ApiError::err("couldnt_update_user").into()),
702 jwt: Claims::jwt(updated_local_user.id.0)?,
707 #[async_trait::async_trait(?Send)]
708 impl Perform for GetReportCount {
709 type Response = GetReportCountResponse;
713 context: &Data<LemmyContext>,
714 websocket_id: Option<ConnectionId>,
715 ) -> Result<GetReportCountResponse, LemmyError> {
716 let data: &GetReportCount = &self;
717 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
719 let person_id = local_user_view.person.id;
720 let community_id = data.community;
722 collect_moderated_communities(person_id, community_id, context.pool()).await?;
725 if community_ids.is_empty() {
726 GetReportCountResponse {
732 let ids = community_ids.clone();
733 let comment_reports = blocking(context.pool(), move |conn| {
734 CommentReportView::get_report_count(conn, &ids)
738 let ids = community_ids.clone();
739 let post_reports = blocking(context.pool(), move |conn| {
740 PostReportView::get_report_count(conn, &ids)
744 GetReportCountResponse {
745 community: data.community,
752 context.chat_server().do_send(SendUserRoomMessage {
753 op: UserOperation::GetReportCount,
754 response: res.clone(),
755 local_recipient_id: local_user_view.local_user.id,
763 #[async_trait::async_trait(?Send)]
764 impl Perform for GetFollowedCommunities {
765 type Response = GetFollowedCommunitiesResponse;
769 context: &Data<LemmyContext>,
770 _websocket_id: Option<ConnectionId>,
771 ) -> Result<GetFollowedCommunitiesResponse, LemmyError> {
772 let data: &GetFollowedCommunities = &self;
773 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
775 let person_id = local_user_view.person.id;
776 let communities = match blocking(context.pool(), move |conn| {
777 CommunityFollowerView::for_person(conn, person_id)
781 Ok(communities) => communities,
782 _ => return Err(ApiError::err("system_err_login").into()),
786 Ok(GetFollowedCommunitiesResponse { communities })