2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 site::{ApproveRegistrationApplication, RegistrationApplicationResponse},
5 utils::{blocking, get_local_user_view_from_jwt, is_admin, send_application_approved_email},
9 local_user::{LocalUser, LocalUserForm},
10 registration_application::{RegistrationApplication, RegistrationApplicationForm},
13 utils::diesel_option_overwrite,
15 use lemmy_db_views::structs::{LocalUserView, RegistrationApplicationView};
16 use lemmy_utils::{error::LemmyError, ConnectionId};
17 use lemmy_websocket::LemmyContext;
19 #[async_trait::async_trait(?Send)]
20 impl Perform for ApproveRegistrationApplication {
21 type Response = RegistrationApplicationResponse;
25 context: &Data<LemmyContext>,
26 _websocket_id: Option<ConnectionId>,
27 ) -> Result<Self::Response, LemmyError> {
30 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34 // Only let admins do this
35 is_admin(&local_user_view)?;
37 // Update the registration with reason, admin_id
38 let deny_reason = diesel_option_overwrite(&data.deny_reason);
39 let app_form = RegistrationApplicationForm {
40 admin_id: Some(local_user_view.person.id),
42 ..RegistrationApplicationForm::default()
45 let registration_application = blocking(context.pool(), move |conn| {
46 RegistrationApplication::update(conn, app_id, &app_form)
50 // Update the local_user row
51 let local_user_form = LocalUserForm {
52 accepted_application: Some(data.approve),
53 ..LocalUserForm::default()
56 let approved_user_id = registration_application.local_user_id;
57 blocking(context.pool(), move |conn| {
58 LocalUser::update(conn, approved_user_id, &local_user_form)
63 let approved_local_user_view = blocking(context.pool(), move |conn| {
64 LocalUserView::read(conn, approved_user_id)
68 if approved_local_user_view.local_user.email.is_some() {
69 send_application_approved_email(&approved_local_user_view, &context.settings())?;
74 let registration_application = blocking(context.pool(), move |conn| {
75 RegistrationApplicationView::read(conn, app_id)
80 registration_application,