1 use crate::{captcha_espeak_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_preferred_username, 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_byte_array = captcha.as_png().expect("failed to generate captcha");
140 let png = base64::encode(png_byte_array);
142 let uuid = uuid::Uuid::new_v4().to_string();
144 let wav = captcha_espeak_wav_base64(&answer).ok();
146 let captcha_item = CaptchaItem {
148 uuid: uuid.to_owned(),
149 expires: naive_now() + Duration::minutes(10), // expires in 10 minutes
152 // Stores the captcha item on the queue
153 context.chat_server().do_send(captcha_item);
155 Ok(GetCaptchaResponse {
156 ok: Some(CaptchaResponse { png, wav, uuid }),
161 #[async_trait::async_trait(?Send)]
162 impl Perform for SaveUserSettings {
163 type Response = LoginResponse;
167 context: &Data<LemmyContext>,
168 _websocket_id: Option<ConnectionId>,
169 ) -> Result<LoginResponse, LemmyError> {
170 let data: &SaveUserSettings = &self;
171 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
173 let avatar = diesel_option_overwrite_to_url(&data.avatar)?;
174 let banner = diesel_option_overwrite_to_url(&data.banner)?;
175 let email = diesel_option_overwrite(&data.email);
176 let bio = diesel_option_overwrite(&data.bio);
177 let preferred_username = diesel_option_overwrite(&data.preferred_username);
178 let matrix_user_id = diesel_option_overwrite(&data.matrix_user_id);
180 if let Some(Some(bio)) = &bio {
181 if bio.chars().count() > 300 {
182 return Err(ApiError::err("bio_length_overflow").into());
186 if let Some(Some(preferred_username)) = &preferred_username {
187 if !is_valid_preferred_username(preferred_username.trim()) {
188 return Err(ApiError::err("invalid_username").into());
192 let local_user_id = local_user_view.local_user.id;
193 let person_id = local_user_view.person.id;
194 let password_encrypted = match &data.new_password {
195 Some(new_password) => {
196 match &data.new_password_verify {
197 Some(new_password_verify) => {
198 password_length_check(&new_password)?;
200 // Make sure passwords match
201 if new_password != new_password_verify {
202 return Err(ApiError::err("passwords_dont_match").into());
205 // Check the old password
206 match &data.old_password {
207 Some(old_password) => {
209 verify(old_password, &local_user_view.local_user.password_encrypted)
212 return Err(ApiError::err("password_incorrect").into());
214 let new_password = new_password.to_owned();
215 let user = blocking(context.pool(), move |conn| {
216 LocalUser::update_password(conn, local_user_id, &new_password)
219 user.password_encrypted
221 None => return Err(ApiError::err("password_incorrect").into()),
224 None => return Err(ApiError::err("passwords_dont_match").into()),
227 None => local_user_view.local_user.password_encrypted,
230 let default_listing_type = data.default_listing_type;
231 let default_sort_type = data.default_sort_type;
233 let person_form = PersonForm {
234 name: local_user_view.person.name,
240 updated: Some(naive_now()),
249 last_refreshed_at: None,
250 shared_inbox_url: None,
254 let person_res = blocking(context.pool(), move |conn| {
255 Person::update(conn, person_id, &person_form)
258 let _updated_person: Person = match person_res {
261 return Err(ApiError::err("user_already_exists").into());
265 let local_user_form = LocalUserForm {
269 show_nsfw: data.show_nsfw,
270 show_scores: data.show_scores,
271 theme: data.theme.to_owned(),
273 default_listing_type,
274 lang: data.lang.to_owned(),
275 show_avatars: data.show_avatars,
276 send_notifications_to_email: data.send_notifications_to_email,
279 let local_user_res = blocking(context.pool(), move |conn| {
280 LocalUser::update(conn, local_user_id, &local_user_form)
283 let updated_local_user = match local_user_res {
286 let err_type = if e.to_string()
287 == "duplicate key value violates unique constraint \"local_user_email_key\""
289 "email_already_exists"
291 "user_already_exists"
294 return Err(ApiError::err(err_type).into());
300 jwt: Claims::jwt(updated_local_user.id.0)?,
305 #[async_trait::async_trait(?Send)]
306 impl Perform for AddAdmin {
307 type Response = AddAdminResponse;
311 context: &Data<LemmyContext>,
312 websocket_id: Option<ConnectionId>,
313 ) -> Result<AddAdminResponse, LemmyError> {
314 let data: &AddAdmin = &self;
315 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
317 // Make sure user is an admin
318 is_admin(&local_user_view)?;
320 let added = data.added;
321 let added_person_id = data.person_id;
322 let added_admin = match blocking(context.pool(), move |conn| {
323 Person::add_admin(conn, added_person_id, added)
329 return Err(ApiError::err("couldnt_update_user").into());
334 let form = ModAddForm {
335 mod_person_id: local_user_view.person.id,
336 other_person_id: added_admin.id,
337 removed: Some(!data.added),
340 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
342 let site_creator_id = blocking(context.pool(), move |conn| {
343 Site::read(conn, 1).map(|s| s.creator_id)
347 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
348 let creator_index = admins
350 .position(|r| r.person.id == site_creator_id)
351 .context(location_info!())?;
352 let creator_person = admins.remove(creator_index);
353 admins.insert(0, creator_person);
355 let res = AddAdminResponse { admins };
357 context.chat_server().do_send(SendAllMessage {
358 op: UserOperation::AddAdmin,
359 response: res.clone(),
367 #[async_trait::async_trait(?Send)]
368 impl Perform for BanPerson {
369 type Response = BanPersonResponse;
373 context: &Data<LemmyContext>,
374 websocket_id: Option<ConnectionId>,
375 ) -> Result<BanPersonResponse, LemmyError> {
376 let data: &BanPerson = &self;
377 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
379 // Make sure user is an admin
380 is_admin(&local_user_view)?;
383 let banned_person_id = data.person_id;
384 let ban_person = move |conn: &'_ _| Person::ban_person(conn, banned_person_id, ban);
385 if blocking(context.pool(), ban_person).await?.is_err() {
386 return Err(ApiError::err("couldnt_update_user").into());
389 // Remove their data if that's desired
390 if data.remove_data {
392 blocking(context.pool(), move |conn: &'_ _| {
393 Post::update_removed_for_creator(conn, banned_person_id, None, true)
398 blocking(context.pool(), move |conn: &'_ _| {
399 Community::update_removed_for_creator(conn, banned_person_id, true)
404 blocking(context.pool(), move |conn: &'_ _| {
405 Comment::update_removed_for_creator(conn, banned_person_id, true)
411 let expires = data.expires.map(naive_from_unix);
413 let form = ModBanForm {
414 mod_person_id: local_user_view.person.id,
415 other_person_id: data.person_id,
416 reason: data.reason.to_owned(),
417 banned: Some(data.ban),
421 blocking(context.pool(), move |conn| ModBan::create(conn, &form)).await??;
423 let person_id = data.person_id;
424 let person_view = blocking(context.pool(), move |conn| {
425 PersonViewSafe::read(conn, person_id)
429 let res = BanPersonResponse {
434 context.chat_server().do_send(SendAllMessage {
435 op: UserOperation::BanPerson,
436 response: res.clone(),
444 #[async_trait::async_trait(?Send)]
445 impl Perform for GetReplies {
446 type Response = GetRepliesResponse;
450 context: &Data<LemmyContext>,
451 _websocket_id: Option<ConnectionId>,
452 ) -> Result<GetRepliesResponse, LemmyError> {
453 let data: &GetReplies = &self;
454 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
456 let sort = SortType::from_str(&data.sort)?;
458 let page = data.page;
459 let limit = data.limit;
460 let unread_only = data.unread_only;
461 let person_id = local_user_view.person.id;
462 let replies = blocking(context.pool(), move |conn| {
463 CommentQueryBuilder::create(conn)
465 .unread_only(unread_only)
466 .recipient_id(person_id)
467 .my_person_id(person_id)
474 Ok(GetRepliesResponse { replies })
478 #[async_trait::async_trait(?Send)]
479 impl Perform for GetPersonMentions {
480 type Response = GetPersonMentionsResponse;
484 context: &Data<LemmyContext>,
485 _websocket_id: Option<ConnectionId>,
486 ) -> Result<GetPersonMentionsResponse, LemmyError> {
487 let data: &GetPersonMentions = &self;
488 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
490 let sort = SortType::from_str(&data.sort)?;
492 let page = data.page;
493 let limit = data.limit;
494 let unread_only = data.unread_only;
495 let person_id = local_user_view.person.id;
496 let mentions = blocking(context.pool(), move |conn| {
497 PersonMentionQueryBuilder::create(conn)
498 .recipient_id(person_id)
499 .my_person_id(person_id)
501 .unread_only(unread_only)
508 Ok(GetPersonMentionsResponse { mentions })
512 #[async_trait::async_trait(?Send)]
513 impl Perform for MarkPersonMentionAsRead {
514 type Response = PersonMentionResponse;
518 context: &Data<LemmyContext>,
519 _websocket_id: Option<ConnectionId>,
520 ) -> Result<PersonMentionResponse, LemmyError> {
521 let data: &MarkPersonMentionAsRead = &self;
522 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
524 let person_mention_id = data.person_mention_id;
525 let read_person_mention = blocking(context.pool(), move |conn| {
526 PersonMention::read(conn, person_mention_id)
530 if local_user_view.person.id != read_person_mention.recipient_id {
531 return Err(ApiError::err("couldnt_update_comment").into());
534 let person_mention_id = read_person_mention.id;
535 let read = data.read;
537 move |conn: &'_ _| PersonMention::update_read(conn, person_mention_id, read);
538 if blocking(context.pool(), update_mention).await?.is_err() {
539 return Err(ApiError::err("couldnt_update_comment").into());
542 let person_mention_id = read_person_mention.id;
543 let person_id = local_user_view.person.id;
544 let person_mention_view = blocking(context.pool(), move |conn| {
545 PersonMentionView::read(conn, person_mention_id, Some(person_id))
549 Ok(PersonMentionResponse {
555 #[async_trait::async_trait(?Send)]
556 impl Perform for MarkAllAsRead {
557 type Response = GetRepliesResponse;
561 context: &Data<LemmyContext>,
562 _websocket_id: Option<ConnectionId>,
563 ) -> Result<GetRepliesResponse, LemmyError> {
564 let data: &MarkAllAsRead = &self;
565 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
567 let person_id = local_user_view.person.id;
568 let replies = blocking(context.pool(), move |conn| {
569 CommentQueryBuilder::create(conn)
570 .my_person_id(person_id)
571 .recipient_id(person_id)
579 // TODO: this should probably be a bulk operation
580 // Not easy to do as a bulk operation,
581 // because recipient_id isn't in the comment table
582 for comment_view in &replies {
583 let reply_id = comment_view.comment.id;
584 let mark_as_read = move |conn: &'_ _| Comment::update_read(conn, reply_id, true);
585 if blocking(context.pool(), mark_as_read).await?.is_err() {
586 return Err(ApiError::err("couldnt_update_comment").into());
590 // Mark all user mentions as read
591 let update_person_mentions =
592 move |conn: &'_ _| PersonMention::mark_all_as_read(conn, person_id);
593 if blocking(context.pool(), update_person_mentions)
597 return Err(ApiError::err("couldnt_update_comment").into());
600 // Mark all private_messages as read
601 let update_pm = move |conn: &'_ _| PrivateMessage::mark_all_as_read(conn, person_id);
602 if blocking(context.pool(), update_pm).await?.is_err() {
603 return Err(ApiError::err("couldnt_update_private_message").into());
606 Ok(GetRepliesResponse { replies: vec![] })
610 #[async_trait::async_trait(?Send)]
611 impl Perform for PasswordReset {
612 type Response = PasswordResetResponse;
616 context: &Data<LemmyContext>,
617 _websocket_id: Option<ConnectionId>,
618 ) -> Result<PasswordResetResponse, LemmyError> {
619 let data: &PasswordReset = &self;
622 let email = data.email.clone();
623 let local_user_view = match blocking(context.pool(), move |conn| {
624 LocalUserView::find_by_email(conn, &email)
629 Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
632 // Generate a random token
633 let token = generate_random_string();
636 let token2 = token.clone();
637 let local_user_id = local_user_view.local_user.id;
638 blocking(context.pool(), move |conn| {
639 PasswordResetRequest::create_token(conn, local_user_id, &token2)
643 // Email the pure token to the user.
644 // TODO no i18n support here.
645 let email = &local_user_view.local_user.email.expect("email");
646 let subject = &format!("Password reset for {}", local_user_view.person.name);
647 let hostname = &Settings::get().get_protocol_and_hostname();
648 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);
649 match send_email(subject, email, &local_user_view.person.name, html) {
651 Err(_e) => return Err(ApiError::err(&_e).into()),
654 Ok(PasswordResetResponse {})
658 #[async_trait::async_trait(?Send)]
659 impl Perform for PasswordChange {
660 type Response = LoginResponse;
664 context: &Data<LemmyContext>,
665 _websocket_id: Option<ConnectionId>,
666 ) -> Result<LoginResponse, LemmyError> {
667 let data: &PasswordChange = &self;
669 // Fetch the user_id from the token
670 let token = data.token.clone();
671 let local_user_id = blocking(context.pool(), move |conn| {
672 PasswordResetRequest::read_from_token(conn, &token).map(|p| p.local_user_id)
676 password_length_check(&data.password)?;
678 // Make sure passwords match
679 if data.password != data.password_verify {
680 return Err(ApiError::err("passwords_dont_match").into());
683 // Update the user with the new password
684 let password = data.password.clone();
685 let updated_local_user = match blocking(context.pool(), move |conn| {
686 LocalUser::update_password(conn, local_user_id, &password)
691 Err(_e) => return Err(ApiError::err("couldnt_update_user").into()),
696 jwt: Claims::jwt(updated_local_user.id.0)?,
701 #[async_trait::async_trait(?Send)]
702 impl Perform for GetReportCount {
703 type Response = GetReportCountResponse;
707 context: &Data<LemmyContext>,
708 websocket_id: Option<ConnectionId>,
709 ) -> Result<GetReportCountResponse, LemmyError> {
710 let data: &GetReportCount = &self;
711 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
713 let person_id = local_user_view.person.id;
714 let community_id = data.community;
716 collect_moderated_communities(person_id, community_id, context.pool()).await?;
719 if community_ids.is_empty() {
720 GetReportCountResponse {
726 let ids = community_ids.clone();
727 let comment_reports = blocking(context.pool(), move |conn| {
728 CommentReportView::get_report_count(conn, &ids)
732 let ids = community_ids.clone();
733 let post_reports = blocking(context.pool(), move |conn| {
734 PostReportView::get_report_count(conn, &ids)
738 GetReportCountResponse {
739 community: data.community,
746 context.chat_server().do_send(SendUserRoomMessage {
747 op: UserOperation::GetReportCount,
748 response: res.clone(),
749 local_recipient_id: local_user_view.local_user.id,
757 #[async_trait::async_trait(?Send)]
758 impl Perform for GetFollowedCommunities {
759 type Response = GetFollowedCommunitiesResponse;
763 context: &Data<LemmyContext>,
764 _websocket_id: Option<ConnectionId>,
765 ) -> Result<GetFollowedCommunitiesResponse, LemmyError> {
766 let data: &GetFollowedCommunities = &self;
767 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
769 let person_id = local_user_view.person.id;
770 let communities = match blocking(context.pool(), move |conn| {
771 CommunityFollowerView::for_person(conn, person_id)
775 Ok(communities) => communities,
776 _ => return Err(ApiError::err("system_err_login").into()),
780 Ok(GetFollowedCommunitiesResponse { communities })