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 check_registration_application,
10 get_local_user_view_from_jwt,
12 password_length_check,
14 send_email_verification_success,
15 send_password_reset_email,
16 send_verification_email,
18 use lemmy_db_schema::{
19 diesel_option_overwrite,
20 diesel_option_overwrite_to_url,
21 from_opt_str_to_opt_enum,
26 email_verification::EmailVerification,
27 local_user::{LocalUser, LocalUserForm},
29 password_reset_request::*,
31 person_block::{PersonBlock, PersonBlockForm},
34 private_message::PrivateMessage,
37 traits::{Blockable, Crud},
41 comment_report_view::CommentReportView,
42 comment_view::{CommentQueryBuilder, CommentView},
43 local_user_view::LocalUserView,
44 post_report_view::PostReportView,
45 private_message_view::PrivateMessageView,
47 use lemmy_db_views_actor::{
48 community_moderator_view::CommunityModeratorView,
49 person_mention_view::{PersonMentionQueryBuilder, PersonMentionView},
50 person_view::PersonViewSafe,
55 utils::{is_valid_display_name, is_valid_matrix_id, naive_from_unix},
59 use lemmy_websocket::{
60 messages::{CaptchaItem, SendAllMessage},
65 #[async_trait::async_trait(?Send)]
66 impl Perform for Login {
67 type Response = LoginResponse;
69 #[tracing::instrument(skip(context, _websocket_id))]
72 context: &Data<LemmyContext>,
73 _websocket_id: Option<ConnectionId>,
74 ) -> Result<LoginResponse, LemmyError> {
75 let data: &Login = self;
77 // Fetch that username / email
78 let username_or_email = data.username_or_email.clone();
79 let local_user_view = blocking(context.pool(), move |conn| {
80 LocalUserView::find_by_email_or_name(conn, &username_or_email)
83 .map_err(LemmyError::from)
84 .map_err(|e| e.with_message("couldnt_find_that_username_or_email"))?;
86 // Verify the password
87 let valid: bool = verify(
89 &local_user_view.local_user.password_encrypted,
93 return Err(LemmyError::from_message("password_incorrect"));
96 let site = blocking(context.pool(), Site::read_simple).await??;
97 if site.require_email_verification && !local_user_view.local_user.email_verified {
98 return Err(LemmyError::from_message("email_not_verified"));
101 check_registration_application(&site, &local_user_view, context.pool()).await?;
107 local_user_view.local_user.id.0,
108 &context.secret().jwt_secret,
109 &context.settings().hostname,
113 verify_email_sent: false,
114 registration_created: false,
119 #[async_trait::async_trait(?Send)]
120 impl Perform for GetCaptcha {
121 type Response = GetCaptchaResponse;
123 #[tracing::instrument(skip(context, _websocket_id))]
126 context: &Data<LemmyContext>,
127 _websocket_id: Option<ConnectionId>,
128 ) -> Result<Self::Response, LemmyError> {
129 let captcha_settings = context.settings().captcha;
131 if !captcha_settings.enabled {
132 return Ok(GetCaptchaResponse { ok: None });
135 let captcha = match captcha_settings.difficulty.as_str() {
136 "easy" => gen(Difficulty::Easy),
137 "medium" => gen(Difficulty::Medium),
138 "hard" => gen(Difficulty::Hard),
139 _ => gen(Difficulty::Medium),
142 let answer = captcha.chars_as_string();
144 let png = captcha.as_base64().expect("failed to generate captcha");
146 let uuid = uuid::Uuid::new_v4().to_string();
148 let wav = captcha_as_wav_base64(&captcha);
150 let captcha_item = CaptchaItem {
152 uuid: uuid.to_owned(),
153 expires: naive_now() + Duration::minutes(10), // expires in 10 minutes
156 // Stores the captcha item on the queue
157 context.chat_server().do_send(captcha_item);
159 Ok(GetCaptchaResponse {
160 ok: Some(CaptchaResponse { png, wav, uuid }),
165 #[async_trait::async_trait(?Send)]
166 impl Perform for SaveUserSettings {
167 type Response = LoginResponse;
169 #[tracing::instrument(skip(context, _websocket_id))]
172 context: &Data<LemmyContext>,
173 _websocket_id: Option<ConnectionId>,
174 ) -> Result<LoginResponse, LemmyError> {
175 let data: &SaveUserSettings = self;
176 let local_user_view =
177 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
179 let avatar = diesel_option_overwrite_to_url(&data.avatar)?;
180 let banner = diesel_option_overwrite_to_url(&data.banner)?;
181 let bio = diesel_option_overwrite(&data.bio);
182 let display_name = diesel_option_overwrite(&data.display_name);
183 let matrix_user_id = diesel_option_overwrite(&data.matrix_user_id);
184 let bot_account = data.bot_account;
185 let email_deref = data.email.as_deref().map(|e| e.to_owned());
186 let email = diesel_option_overwrite(&email_deref);
188 if let Some(Some(email)) = &email {
189 let previous_email = local_user_view.local_user.email.unwrap_or_default();
190 // Only send the verification email if there was an email change
191 if previous_email.ne(email) {
192 send_verification_email(
193 local_user_view.local_user.id,
195 &local_user_view.person.name,
203 // When the site requires email, make sure email is not Some(None). IE, an overwrite to a None value
204 if let Some(email) = &email {
205 let site_fut = blocking(context.pool(), Site::read_simple);
206 if email.is_none() && site_fut.await??.require_email_verification {
207 return Err(LemmyError::from_message("email_required"));
211 if let Some(Some(bio)) = &bio {
212 if bio.chars().count() > 300 {
213 return Err(LemmyError::from_message("bio_length_overflow"));
217 if let Some(Some(display_name)) = &display_name {
218 if !is_valid_display_name(
220 context.settings().actor_name_max_length,
222 return Err(LemmyError::from_message("invalid_username"));
226 if let Some(Some(matrix_user_id)) = &matrix_user_id {
227 if !is_valid_matrix_id(matrix_user_id) {
228 return Err(LemmyError::from_message("invalid_matrix_id"));
232 let local_user_id = local_user_view.local_user.id;
233 let person_id = local_user_view.person.id;
234 let default_listing_type = data.default_listing_type;
235 let default_sort_type = data.default_sort_type;
236 let password_encrypted = local_user_view.local_user.password_encrypted;
237 let public_key = local_user_view.person.public_key;
239 let person_form = PersonForm {
240 name: local_user_view.person.name,
246 updated: Some(naive_now()),
255 last_refreshed_at: None,
256 shared_inbox_url: None,
261 blocking(context.pool(), move |conn| {
262 Person::update(conn, person_id, &person_form)
265 .map_err(LemmyError::from)
266 .map_err(|e| e.with_message("user_already_exists"))?;
268 let local_user_form = LocalUserForm {
269 person_id: Some(person_id),
271 password_encrypted: Some(password_encrypted),
272 show_nsfw: data.show_nsfw,
273 show_bot_accounts: data.show_bot_accounts,
274 show_scores: data.show_scores,
275 theme: data.theme.to_owned(),
277 default_listing_type,
278 lang: data.lang.to_owned(),
279 show_avatars: data.show_avatars,
280 show_read_posts: data.show_read_posts,
281 show_new_post_notifs: data.show_new_post_notifs,
282 send_notifications_to_email: data.send_notifications_to_email,
283 email_verified: None,
284 accepted_application: None,
287 let local_user_res = blocking(context.pool(), move |conn| {
288 LocalUser::update(conn, local_user_id, &local_user_form)
291 let updated_local_user = match local_user_res {
294 let err_type = if e.to_string()
295 == "duplicate key value violates unique constraint \"local_user_email_key\""
297 "email_already_exists"
299 "user_already_exists"
302 return Err(LemmyError::from(e).with_message(err_type));
310 updated_local_user.id.0,
311 &context.secret().jwt_secret,
312 &context.settings().hostname,
316 verify_email_sent: false,
317 registration_created: false,
322 #[async_trait::async_trait(?Send)]
323 impl Perform for ChangePassword {
324 type Response = LoginResponse;
326 #[tracing::instrument(skip(self, context, _websocket_id))]
329 context: &Data<LemmyContext>,
330 _websocket_id: Option<ConnectionId>,
331 ) -> Result<LoginResponse, LemmyError> {
332 let data: &ChangePassword = self;
333 let local_user_view =
334 get_local_user_view_from_jwt(data.auth.as_ref(), context.pool(), context.secret()).await?;
336 password_length_check(&data.new_password)?;
338 // Make sure passwords match
339 if data.new_password != data.new_password_verify {
340 return Err(LemmyError::from_message("passwords_dont_match"));
343 // Check the old password
344 let valid: bool = verify(
346 &local_user_view.local_user.password_encrypted,
350 return Err(LemmyError::from_message("password_incorrect"));
353 let local_user_id = local_user_view.local_user.id;
354 let new_password = data.new_password.to_owned();
355 let updated_local_user = blocking(context.pool(), move |conn| {
356 LocalUser::update_password(conn, local_user_id, &new_password)
364 updated_local_user.id.0,
365 &context.secret().jwt_secret,
366 &context.settings().hostname,
370 verify_email_sent: false,
371 registration_created: false,
376 #[async_trait::async_trait(?Send)]
377 impl Perform for AddAdmin {
378 type Response = AddAdminResponse;
380 #[tracing::instrument(skip(context, websocket_id))]
383 context: &Data<LemmyContext>,
384 websocket_id: Option<ConnectionId>,
385 ) -> Result<AddAdminResponse, LemmyError> {
386 let data: &AddAdmin = self;
387 let local_user_view =
388 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
390 // Make sure user is an admin
391 is_admin(&local_user_view)?;
393 let added = data.added;
394 let added_person_id = data.person_id;
395 let added_admin = blocking(context.pool(), move |conn| {
396 Person::add_admin(conn, added_person_id, added)
399 .map_err(LemmyError::from)
400 .map_err(|e| e.with_message("couldnt_update_user"))?;
403 let form = ModAddForm {
404 mod_person_id: local_user_view.person.id,
405 other_person_id: added_admin.id,
406 removed: Some(!data.added),
409 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
411 let site_creator_id = blocking(context.pool(), move |conn| {
412 Site::read(conn, 1).map(|s| s.creator_id)
416 let mut admins = blocking(context.pool(), PersonViewSafe::admins).await??;
417 let creator_index = admins
419 .position(|r| r.person.id == site_creator_id)
420 .context(location_info!())?;
421 let creator_person = admins.remove(creator_index);
422 admins.insert(0, creator_person);
424 let res = AddAdminResponse { admins };
426 context.chat_server().do_send(SendAllMessage {
427 op: UserOperation::AddAdmin,
428 response: res.clone(),
436 #[async_trait::async_trait(?Send)]
437 impl Perform for BanPerson {
438 type Response = BanPersonResponse;
440 #[tracing::instrument(skip(context, websocket_id))]
443 context: &Data<LemmyContext>,
444 websocket_id: Option<ConnectionId>,
445 ) -> Result<BanPersonResponse, LemmyError> {
446 let data: &BanPerson = self;
447 let local_user_view =
448 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
450 // Make sure user is an admin
451 is_admin(&local_user_view)?;
454 let banned_person_id = data.person_id;
455 let ban_person = move |conn: &'_ _| Person::ban_person(conn, banned_person_id, ban);
456 blocking(context.pool(), ban_person)
458 .map_err(LemmyError::from)
459 .map_err(|e| e.with_message("couldnt_update_user"))?;
461 // Remove their data if that's desired
462 if data.remove_data.unwrap_or(false) {
464 blocking(context.pool(), move |conn: &'_ _| {
465 Post::update_removed_for_creator(conn, banned_person_id, None, true)
470 // Remove all communities where they're the top mod
471 // for now, remove the communities manually
472 let first_mod_communities = blocking(context.pool(), move |conn: &'_ _| {
473 CommunityModeratorView::get_community_first_mods(conn)
477 // Filter to only this banned users top communities
478 let banned_user_first_communities: Vec<CommunityModeratorView> = first_mod_communities
480 .filter(|fmc| fmc.moderator.id == banned_person_id)
483 for first_mod_community in banned_user_first_communities {
484 blocking(context.pool(), move |conn: &'_ _| {
485 Community::update_removed(conn, first_mod_community.community.id, true)
491 blocking(context.pool(), move |conn: &'_ _| {
492 Comment::update_removed_for_creator(conn, banned_person_id, true)
498 let expires = data.expires.map(naive_from_unix);
500 let form = ModBanForm {
501 mod_person_id: local_user_view.person.id,
502 other_person_id: data.person_id,
503 reason: data.reason.to_owned(),
504 banned: Some(data.ban),
508 blocking(context.pool(), move |conn| ModBan::create(conn, &form)).await??;
510 let person_id = data.person_id;
511 let person_view = blocking(context.pool(), move |conn| {
512 PersonViewSafe::read(conn, person_id)
516 let res = BanPersonResponse {
521 context.chat_server().do_send(SendAllMessage {
522 op: UserOperation::BanPerson,
523 response: res.clone(),
531 #[async_trait::async_trait(?Send)]
532 impl Perform for GetBannedPersons {
533 type Response = BannedPersonsResponse;
537 context: &Data<LemmyContext>,
538 _websocket_id: Option<ConnectionId>,
539 ) -> Result<Self::Response, LemmyError> {
540 let data: &GetBannedPersons = self;
541 let local_user_view =
542 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
544 // Make sure user is an admin
545 is_admin(&local_user_view)?;
547 let banned = blocking(context.pool(), PersonViewSafe::banned).await??;
549 let res = Self::Response { banned };
555 #[async_trait::async_trait(?Send)]
556 impl Perform for BlockPerson {
557 type Response = BlockPersonResponse;
559 #[tracing::instrument(skip(context, _websocket_id))]
562 context: &Data<LemmyContext>,
563 _websocket_id: Option<ConnectionId>,
564 ) -> Result<BlockPersonResponse, LemmyError> {
565 let data: &BlockPerson = self;
566 let local_user_view =
567 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
569 let target_id = data.person_id;
570 let person_id = local_user_view.person.id;
572 // Don't let a person block themselves
573 if target_id == person_id {
574 return Err(LemmyError::from_message("cant_block_yourself"));
577 let person_block_form = PersonBlockForm {
583 let block = move |conn: &'_ _| PersonBlock::block(conn, &person_block_form);
584 blocking(context.pool(), block)
586 .map_err(LemmyError::from)
587 .map_err(|e| e.with_message("person_block_already_exists"))?;
589 let unblock = move |conn: &'_ _| PersonBlock::unblock(conn, &person_block_form);
590 blocking(context.pool(), unblock)
592 .map_err(LemmyError::from)
593 .map_err(|e| e.with_message("person_block_already_exists"))?;
596 // TODO does any federated stuff need to be done here?
598 let person_view = blocking(context.pool(), move |conn| {
599 PersonViewSafe::read(conn, target_id)
603 let res = BlockPersonResponse {
612 #[async_trait::async_trait(?Send)]
613 impl Perform for GetReplies {
614 type Response = GetRepliesResponse;
616 #[tracing::instrument(skip(context, _websocket_id))]
619 context: &Data<LemmyContext>,
620 _websocket_id: Option<ConnectionId>,
621 ) -> Result<GetRepliesResponse, LemmyError> {
622 let data: &GetReplies = self;
623 let local_user_view =
624 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
626 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
628 let page = data.page;
629 let limit = data.limit;
630 let unread_only = data.unread_only;
631 let person_id = local_user_view.person.id;
632 let show_bot_accounts = local_user_view.local_user.show_bot_accounts;
634 let replies = blocking(context.pool(), move |conn| {
635 CommentQueryBuilder::create(conn)
637 .unread_only(unread_only)
638 .recipient_id(person_id)
639 .show_bot_accounts(show_bot_accounts)
640 .my_person_id(person_id)
647 Ok(GetRepliesResponse { replies })
651 #[async_trait::async_trait(?Send)]
652 impl Perform for GetPersonMentions {
653 type Response = GetPersonMentionsResponse;
655 #[tracing::instrument(skip(context, _websocket_id))]
658 context: &Data<LemmyContext>,
659 _websocket_id: Option<ConnectionId>,
660 ) -> Result<GetPersonMentionsResponse, LemmyError> {
661 let data: &GetPersonMentions = self;
662 let local_user_view =
663 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
665 let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
667 let page = data.page;
668 let limit = data.limit;
669 let unread_only = data.unread_only;
670 let person_id = local_user_view.person.id;
671 let mentions = blocking(context.pool(), move |conn| {
672 PersonMentionQueryBuilder::create(conn)
673 .recipient_id(person_id)
674 .my_person_id(person_id)
676 .unread_only(unread_only)
683 Ok(GetPersonMentionsResponse { mentions })
687 #[async_trait::async_trait(?Send)]
688 impl Perform for MarkPersonMentionAsRead {
689 type Response = PersonMentionResponse;
691 #[tracing::instrument(skip(context, _websocket_id))]
694 context: &Data<LemmyContext>,
695 _websocket_id: Option<ConnectionId>,
696 ) -> Result<PersonMentionResponse, LemmyError> {
697 let data: &MarkPersonMentionAsRead = self;
698 let local_user_view =
699 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
701 let person_mention_id = data.person_mention_id;
702 let read_person_mention = blocking(context.pool(), move |conn| {
703 PersonMention::read(conn, person_mention_id)
707 if local_user_view.person.id != read_person_mention.recipient_id {
708 return Err(LemmyError::from_message("couldnt_update_comment"));
711 let person_mention_id = read_person_mention.id;
712 let read = data.read;
714 move |conn: &'_ _| PersonMention::update_read(conn, person_mention_id, read);
715 blocking(context.pool(), update_mention)
717 .map_err(LemmyError::from)
718 .map_err(|e| e.with_message("couldnt_update_comment"))?;
720 let person_mention_id = read_person_mention.id;
721 let person_id = local_user_view.person.id;
722 let person_mention_view = blocking(context.pool(), move |conn| {
723 PersonMentionView::read(conn, person_mention_id, Some(person_id))
727 Ok(PersonMentionResponse {
733 #[async_trait::async_trait(?Send)]
734 impl Perform for MarkAllAsRead {
735 type Response = GetRepliesResponse;
737 #[tracing::instrument(skip(context, _websocket_id))]
740 context: &Data<LemmyContext>,
741 _websocket_id: Option<ConnectionId>,
742 ) -> Result<GetRepliesResponse, LemmyError> {
743 let data: &MarkAllAsRead = self;
744 let local_user_view =
745 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
747 let person_id = local_user_view.person.id;
748 let replies = blocking(context.pool(), move |conn| {
749 CommentQueryBuilder::create(conn)
750 .my_person_id(person_id)
751 .recipient_id(person_id)
759 // TODO: this should probably be a bulk operation
760 // Not easy to do as a bulk operation,
761 // because recipient_id isn't in the comment table
762 for comment_view in &replies {
763 let reply_id = comment_view.comment.id;
764 let mark_as_read = move |conn: &'_ _| Comment::update_read(conn, reply_id, true);
765 blocking(context.pool(), mark_as_read)
767 .map_err(LemmyError::from)
768 .map_err(|e| e.with_message("couldnt_update_comment"))?;
771 // Mark all user mentions as read
772 let update_person_mentions =
773 move |conn: &'_ _| PersonMention::mark_all_as_read(conn, person_id);
774 blocking(context.pool(), update_person_mentions)
776 .map_err(LemmyError::from)
777 .map_err(|e| e.with_message("couldnt_update_comment"))?;
779 // Mark all private_messages as read
780 let update_pm = move |conn: &'_ _| PrivateMessage::mark_all_as_read(conn, person_id);
781 blocking(context.pool(), update_pm)
783 .map_err(LemmyError::from)
784 .map_err(|e| e.with_message("couldnt_update_private_message"))?;
786 Ok(GetRepliesResponse { replies: vec![] })
790 #[async_trait::async_trait(?Send)]
791 impl Perform for PasswordReset {
792 type Response = PasswordResetResponse;
794 #[tracing::instrument(skip(self, context, _websocket_id))]
797 context: &Data<LemmyContext>,
798 _websocket_id: Option<ConnectionId>,
799 ) -> Result<PasswordResetResponse, LemmyError> {
800 let data: &PasswordReset = self;
803 let email = data.email.clone();
804 let local_user_view = blocking(context.pool(), move |conn| {
805 LocalUserView::find_by_email(conn, &email)
808 .map_err(LemmyError::from)
809 .map_err(|e| e.with_message("couldnt_find_that_username_or_email"))?;
811 // Email the pure token to the user.
812 send_password_reset_email(&local_user_view, context.pool(), &context.settings()).await?;
813 Ok(PasswordResetResponse {})
817 #[async_trait::async_trait(?Send)]
818 impl Perform for PasswordChange {
819 type Response = LoginResponse;
821 #[tracing::instrument(skip(self, context, _websocket_id))]
824 context: &Data<LemmyContext>,
825 _websocket_id: Option<ConnectionId>,
826 ) -> Result<LoginResponse, LemmyError> {
827 let data: &PasswordChange = self;
829 // Fetch the user_id from the token
830 let token = data.token.clone();
831 let local_user_id = blocking(context.pool(), move |conn| {
832 PasswordResetRequest::read_from_token(conn, &token).map(|p| p.local_user_id)
836 password_length_check(&data.password)?;
838 // Make sure passwords match
839 if data.password != data.password_verify {
840 return Err(LemmyError::from_message("passwords_dont_match"));
843 // Update the user with the new password
844 let password = data.password.clone();
845 let updated_local_user = blocking(context.pool(), move |conn| {
846 LocalUser::update_password(conn, local_user_id, &password)
849 .map_err(LemmyError::from)
850 .map_err(|e| e.with_message("couldnt_update_user"))?;
856 updated_local_user.id.0,
857 &context.secret().jwt_secret,
858 &context.settings().hostname,
862 verify_email_sent: false,
863 registration_created: false,
868 #[async_trait::async_trait(?Send)]
869 impl Perform for GetReportCount {
870 type Response = GetReportCountResponse;
872 #[tracing::instrument(skip(context, _websocket_id))]
875 context: &Data<LemmyContext>,
876 _websocket_id: Option<ConnectionId>,
877 ) -> Result<GetReportCountResponse, LemmyError> {
878 let data: &GetReportCount = self;
879 let local_user_view =
880 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
882 let person_id = local_user_view.person.id;
883 let admin = local_user_view.person.admin;
884 let community_id = data.community_id;
886 let comment_reports = blocking(context.pool(), move |conn| {
887 CommentReportView::get_report_count(conn, person_id, admin, community_id)
891 let post_reports = blocking(context.pool(), move |conn| {
892 PostReportView::get_report_count(conn, person_id, admin, community_id)
896 let res = GetReportCountResponse {
906 #[async_trait::async_trait(?Send)]
907 impl Perform for GetUnreadCount {
908 type Response = GetUnreadCountResponse;
910 #[tracing::instrument(skip(context, _websocket_id))]
913 context: &Data<LemmyContext>,
914 _websocket_id: Option<ConnectionId>,
915 ) -> Result<Self::Response, LemmyError> {
917 let local_user_view =
918 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
920 let person_id = local_user_view.person.id;
922 let replies = blocking(context.pool(), move |conn| {
923 CommentView::get_unread_replies(conn, person_id)
927 let mentions = blocking(context.pool(), move |conn| {
928 PersonMentionView::get_unread_mentions(conn, person_id)
932 let private_messages = blocking(context.pool(), move |conn| {
933 PrivateMessageView::get_unread_messages(conn, person_id)
937 let res = Self::Response {
947 #[async_trait::async_trait(?Send)]
948 impl Perform for VerifyEmail {
949 type Response = VerifyEmailResponse;
953 context: &Data<LemmyContext>,
954 _websocket_id: Option<usize>,
955 ) -> Result<Self::Response, LemmyError> {
956 let token = self.token.clone();
957 let verification = blocking(context.pool(), move |conn| {
958 EmailVerification::read_for_token(conn, &token)
961 .map_err(LemmyError::from)
962 .map_err(|e| e.with_message("token_not_found"))?;
964 let form = LocalUserForm {
965 // necessary in case this is a new signup
966 email_verified: Some(true),
967 // necessary in case email of an existing user was changed
968 email: Some(Some(verification.email)),
969 ..LocalUserForm::default()
971 let local_user_id = verification.local_user_id;
972 blocking(context.pool(), move |conn| {
973 LocalUser::update(conn, local_user_id, &form)
977 let local_user_view = blocking(context.pool(), move |conn| {
978 LocalUserView::read(conn, local_user_id)
982 send_email_verification_success(&local_user_view, &context.settings())?;
984 blocking(context.pool(), move |conn| {
985 EmailVerification::delete_old_tokens_for_local_user(conn, local_user_id)
989 Ok(VerifyEmailResponse {})