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_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 = 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 preferred_username = diesel_option_overwrite(&data.preferred_username);
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(preferred_username)) = &preferred_username {
185 if !is_valid_preferred_username(preferred_username.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 password_encrypted = match &data.new_password {
193 Some(new_password) => {
194 match &data.new_password_verify {
195 Some(new_password_verify) => {
196 password_length_check(&new_password)?;
198 // Make sure passwords match
199 if new_password != new_password_verify {
200 return Err(ApiError::err("passwords_dont_match").into());
203 // Check the old password
204 match &data.old_password {
205 Some(old_password) => {
207 verify(old_password, &local_user_view.local_user.password_encrypted)
210 return Err(ApiError::err("password_incorrect").into());
212 let new_password = new_password.to_owned();
213 let user = blocking(context.pool(), move |conn| {
214 LocalUser::update_password(conn, local_user_id, &new_password)
217 user.password_encrypted
219 None => return Err(ApiError::err("password_incorrect").into()),
222 None => return Err(ApiError::err("passwords_dont_match").into()),
225 None => local_user_view.local_user.password_encrypted,
228 let default_listing_type = data.default_listing_type;
229 let default_sort_type = data.default_sort_type;
231 let person_form = PersonForm {
232 name: local_user_view.person.name,
238 updated: Some(naive_now()),
247 last_refreshed_at: None,
248 shared_inbox_url: None,
252 let person_res = blocking(context.pool(), move |conn| {
253 Person::update(conn, person_id, &person_form)
256 let _updated_person: Person = match person_res {
259 return Err(ApiError::err("user_already_exists").into());
263 let local_user_form = LocalUserForm {
267 show_nsfw: data.show_nsfw,
268 theme: data.theme.to_owned(),
270 default_listing_type,
271 lang: data.lang.to_owned(),
272 show_avatars: data.show_avatars,
273 send_notifications_to_email: data.send_notifications_to_email,
276 let local_user_res = blocking(context.pool(), move |conn| {
277 LocalUser::update(conn, local_user_id, &local_user_form)
280 let updated_local_user = match local_user_res {
283 let err_type = if e.to_string()
284 == "duplicate key value violates unique constraint \"local_user_email_key\""
286 "email_already_exists"
288 "user_already_exists"
291 return Err(ApiError::err(err_type).into());
297 jwt: Claims::jwt(updated_local_user.id.0)?,
302 #[async_trait::async_trait(?Send)]
303 impl Perform for AddAdmin {
304 type Response = AddAdminResponse;
308 context: &Data<LemmyContext>,
309 websocket_id: Option<ConnectionId>,
310 ) -> Result<AddAdminResponse, LemmyError> {
311 let data: &AddAdmin = &self;
312 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
314 // Make sure user is an admin
315 is_admin(&local_user_view)?;
317 let added = data.added;
318 let added_person_id = data.person_id;
319 let added_admin = match blocking(context.pool(), move |conn| {
320 Person::add_admin(conn, added_person_id, added)
326 return Err(ApiError::err("couldnt_update_user").into());
331 let form = ModAddForm {
332 mod_person_id: local_user_view.person.id,
333 other_person_id: added_admin.id,
334 removed: Some(!data.added),
337 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
339 let site_creator_id = blocking(context.pool(), move |conn| {
340 Site::read(conn, 1).map(|s| s.creator_id)
344 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
345 let creator_index = admins
347 .position(|r| r.person.id == site_creator_id)
348 .context(location_info!())?;
349 let creator_person = admins.remove(creator_index);
350 admins.insert(0, creator_person);
352 let res = AddAdminResponse { admins };
354 context.chat_server().do_send(SendAllMessage {
355 op: UserOperation::AddAdmin,
356 response: res.clone(),
364 #[async_trait::async_trait(?Send)]
365 impl Perform for BanPerson {
366 type Response = BanPersonResponse;
370 context: &Data<LemmyContext>,
371 websocket_id: Option<ConnectionId>,
372 ) -> Result<BanPersonResponse, LemmyError> {
373 let data: &BanPerson = &self;
374 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
376 // Make sure user is an admin
377 is_admin(&local_user_view)?;
380 let banned_person_id = data.person_id;
381 let ban_person = move |conn: &'_ _| Person::ban_person(conn, banned_person_id, ban);
382 if blocking(context.pool(), ban_person).await?.is_err() {
383 return Err(ApiError::err("couldnt_update_user").into());
386 // Remove their data if that's desired
387 if data.remove_data {
389 blocking(context.pool(), move |conn: &'_ _| {
390 Post::update_removed_for_creator(conn, banned_person_id, None, true)
395 blocking(context.pool(), move |conn: &'_ _| {
396 Community::update_removed_for_creator(conn, banned_person_id, true)
401 blocking(context.pool(), move |conn: &'_ _| {
402 Comment::update_removed_for_creator(conn, banned_person_id, true)
408 let expires = data.expires.map(naive_from_unix);
410 let form = ModBanForm {
411 mod_person_id: local_user_view.person.id,
412 other_person_id: data.person_id,
413 reason: data.reason.to_owned(),
414 banned: Some(data.ban),
418 blocking(context.pool(), move |conn| ModBan::create(conn, &form)).await??;
420 let person_id = data.person_id;
421 let person_view = blocking(context.pool(), move |conn| {
422 PersonViewSafe::read(conn, person_id)
426 let res = BanPersonResponse {
431 context.chat_server().do_send(SendAllMessage {
432 op: UserOperation::BanPerson,
433 response: res.clone(),
441 #[async_trait::async_trait(?Send)]
442 impl Perform for GetReplies {
443 type Response = GetRepliesResponse;
447 context: &Data<LemmyContext>,
448 _websocket_id: Option<ConnectionId>,
449 ) -> Result<GetRepliesResponse, LemmyError> {
450 let data: &GetReplies = &self;
451 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
453 let sort = SortType::from_str(&data.sort)?;
455 let page = data.page;
456 let limit = data.limit;
457 let unread_only = data.unread_only;
458 let person_id = local_user_view.person.id;
459 let replies = blocking(context.pool(), move |conn| {
460 CommentQueryBuilder::create(conn)
462 .unread_only(unread_only)
463 .recipient_id(person_id)
464 .my_person_id(person_id)
471 Ok(GetRepliesResponse { replies })
475 #[async_trait::async_trait(?Send)]
476 impl Perform for GetPersonMentions {
477 type Response = GetPersonMentionsResponse;
481 context: &Data<LemmyContext>,
482 _websocket_id: Option<ConnectionId>,
483 ) -> Result<GetPersonMentionsResponse, LemmyError> {
484 let data: &GetPersonMentions = &self;
485 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
487 let sort = SortType::from_str(&data.sort)?;
489 let page = data.page;
490 let limit = data.limit;
491 let unread_only = data.unread_only;
492 let person_id = local_user_view.person.id;
493 let mentions = blocking(context.pool(), move |conn| {
494 PersonMentionQueryBuilder::create(conn)
495 .recipient_id(person_id)
496 .my_person_id(person_id)
498 .unread_only(unread_only)
505 Ok(GetPersonMentionsResponse { mentions })
509 #[async_trait::async_trait(?Send)]
510 impl Perform for MarkPersonMentionAsRead {
511 type Response = PersonMentionResponse;
515 context: &Data<LemmyContext>,
516 _websocket_id: Option<ConnectionId>,
517 ) -> Result<PersonMentionResponse, LemmyError> {
518 let data: &MarkPersonMentionAsRead = &self;
519 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
521 let person_mention_id = data.person_mention_id;
522 let read_person_mention = blocking(context.pool(), move |conn| {
523 PersonMention::read(conn, person_mention_id)
527 if local_user_view.person.id != read_person_mention.recipient_id {
528 return Err(ApiError::err("couldnt_update_comment").into());
531 let person_mention_id = read_person_mention.id;
532 let read = data.read;
534 move |conn: &'_ _| PersonMention::update_read(conn, person_mention_id, read);
535 if blocking(context.pool(), update_mention).await?.is_err() {
536 return Err(ApiError::err("couldnt_update_comment").into());
539 let person_mention_id = read_person_mention.id;
540 let person_id = local_user_view.person.id;
541 let person_mention_view = blocking(context.pool(), move |conn| {
542 PersonMentionView::read(conn, person_mention_id, Some(person_id))
546 Ok(PersonMentionResponse {
552 #[async_trait::async_trait(?Send)]
553 impl Perform for MarkAllAsRead {
554 type Response = GetRepliesResponse;
558 context: &Data<LemmyContext>,
559 _websocket_id: Option<ConnectionId>,
560 ) -> Result<GetRepliesResponse, LemmyError> {
561 let data: &MarkAllAsRead = &self;
562 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
564 let person_id = local_user_view.person.id;
565 let replies = blocking(context.pool(), move |conn| {
566 CommentQueryBuilder::create(conn)
567 .my_person_id(person_id)
568 .recipient_id(person_id)
576 // TODO: this should probably be a bulk operation
577 // Not easy to do as a bulk operation,
578 // because recipient_id isn't in the comment table
579 for comment_view in &replies {
580 let reply_id = comment_view.comment.id;
581 let mark_as_read = move |conn: &'_ _| Comment::update_read(conn, reply_id, true);
582 if blocking(context.pool(), mark_as_read).await?.is_err() {
583 return Err(ApiError::err("couldnt_update_comment").into());
587 // Mark all user mentions as read
588 let update_person_mentions =
589 move |conn: &'_ _| PersonMention::mark_all_as_read(conn, person_id);
590 if blocking(context.pool(), update_person_mentions)
594 return Err(ApiError::err("couldnt_update_comment").into());
597 // Mark all private_messages as read
598 let update_pm = move |conn: &'_ _| PrivateMessage::mark_all_as_read(conn, person_id);
599 if blocking(context.pool(), update_pm).await?.is_err() {
600 return Err(ApiError::err("couldnt_update_private_message").into());
603 Ok(GetRepliesResponse { replies: vec![] })
607 #[async_trait::async_trait(?Send)]
608 impl Perform for PasswordReset {
609 type Response = PasswordResetResponse;
613 context: &Data<LemmyContext>,
614 _websocket_id: Option<ConnectionId>,
615 ) -> Result<PasswordResetResponse, LemmyError> {
616 let data: &PasswordReset = &self;
619 let email = data.email.clone();
620 let local_user_view = match blocking(context.pool(), move |conn| {
621 LocalUserView::find_by_email(conn, &email)
626 Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
629 // Generate a random token
630 let token = generate_random_string();
633 let token2 = token.clone();
634 let local_user_id = local_user_view.local_user.id;
635 blocking(context.pool(), move |conn| {
636 PasswordResetRequest::create_token(conn, local_user_id, &token2)
640 // Email the pure token to the user.
641 // TODO no i18n support here.
642 let email = &local_user_view.local_user.email.expect("email");
643 let subject = &format!("Password reset for {}", local_user_view.person.name);
644 let hostname = &Settings::get().get_protocol_and_hostname();
645 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);
646 match send_email(subject, email, &local_user_view.person.name, html) {
648 Err(_e) => return Err(ApiError::err(&_e).into()),
651 Ok(PasswordResetResponse {})
655 #[async_trait::async_trait(?Send)]
656 impl Perform for PasswordChange {
657 type Response = LoginResponse;
661 context: &Data<LemmyContext>,
662 _websocket_id: Option<ConnectionId>,
663 ) -> Result<LoginResponse, LemmyError> {
664 let data: &PasswordChange = &self;
666 // Fetch the user_id from the token
667 let token = data.token.clone();
668 let local_user_id = blocking(context.pool(), move |conn| {
669 PasswordResetRequest::read_from_token(conn, &token).map(|p| p.local_user_id)
673 password_length_check(&data.password)?;
675 // Make sure passwords match
676 if data.password != data.password_verify {
677 return Err(ApiError::err("passwords_dont_match").into());
680 // Update the user with the new password
681 let password = data.password.clone();
682 let updated_local_user = match blocking(context.pool(), move |conn| {
683 LocalUser::update_password(conn, local_user_id, &password)
688 Err(_e) => return Err(ApiError::err("couldnt_update_user").into()),
693 jwt: Claims::jwt(updated_local_user.id.0)?,
698 #[async_trait::async_trait(?Send)]
699 impl Perform for GetReportCount {
700 type Response = GetReportCountResponse;
704 context: &Data<LemmyContext>,
705 websocket_id: Option<ConnectionId>,
706 ) -> Result<GetReportCountResponse, LemmyError> {
707 let data: &GetReportCount = &self;
708 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
710 let person_id = local_user_view.person.id;
711 let community_id = data.community;
713 collect_moderated_communities(person_id, community_id, context.pool()).await?;
716 if community_ids.is_empty() {
717 GetReportCountResponse {
723 let ids = community_ids.clone();
724 let comment_reports = blocking(context.pool(), move |conn| {
725 CommentReportView::get_report_count(conn, &ids)
729 let ids = community_ids.clone();
730 let post_reports = blocking(context.pool(), move |conn| {
731 PostReportView::get_report_count(conn, &ids)
735 GetReportCountResponse {
736 community: data.community,
743 context.chat_server().do_send(SendUserRoomMessage {
744 op: UserOperation::GetReportCount,
745 response: res.clone(),
746 local_recipient_id: local_user_view.local_user.id,
754 #[async_trait::async_trait(?Send)]
755 impl Perform for GetFollowedCommunities {
756 type Response = GetFollowedCommunitiesResponse;
760 context: &Data<LemmyContext>,
761 _websocket_id: Option<ConnectionId>,
762 ) -> Result<GetFollowedCommunitiesResponse, LemmyError> {
763 let data: &GetFollowedCommunities = &self;
764 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
766 let person_id = local_user_view.person.id;
767 let communities = match blocking(context.pool(), move |conn| {
768 CommunityFollowerView::for_person(conn, person_id)
772 Ok(communities) => communities,
773 _ => return Err(ApiError::err("system_err_login").into()),
777 Ok(GetFollowedCommunitiesResponse { communities })