2021-04-01 17:30:24 +00:00
|
|
|
use crate::{captcha_as_wav_base64, Perform};
|
2020-08-18 13:43:50 +00:00
|
|
|
use actix_web::web::Data;
|
2020-08-13 15:46:31 +00:00
|
|
|
use anyhow::Context;
|
2020-07-10 18:15:41 +00:00
|
|
|
use bcrypt::verify;
|
2020-07-29 13:02:46 +00:00
|
|
|
use captcha::{gen, Difficulty};
|
|
|
|
use chrono::Duration;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
blocking,
|
2021-12-15 19:49:59 +00:00
|
|
|
check_registration_application,
|
2021-03-25 19:19:40 +00:00
|
|
|
get_local_user_view_from_jwt,
|
|
|
|
is_admin,
|
|
|
|
password_length_check,
|
|
|
|
person::*,
|
2021-12-15 19:49:59 +00:00
|
|
|
send_email_verification_success,
|
|
|
|
send_password_reset_email,
|
|
|
|
send_verification_email,
|
2021-02-04 16:34:58 +00:00
|
|
|
};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2020-08-05 16:03:46 +00:00
|
|
|
diesel_option_overwrite,
|
2021-03-02 12:41:48 +00:00
|
|
|
diesel_option_overwrite_to_url,
|
2021-04-15 03:37:51 +00:00
|
|
|
from_opt_str_to_opt_enum,
|
2021-03-11 04:43:11 +00:00
|
|
|
naive_now,
|
|
|
|
source::{
|
|
|
|
comment::Comment,
|
2021-08-13 17:39:56 +00:00
|
|
|
community::Community,
|
2021-12-15 19:49:59 +00:00
|
|
|
email_verification::EmailVerification,
|
2021-03-11 04:43:11 +00:00
|
|
|
local_user::{LocalUser, LocalUserForm},
|
|
|
|
moderator::*,
|
|
|
|
password_reset_request::*,
|
|
|
|
person::*,
|
2021-08-19 20:54:15 +00:00
|
|
|
person_block::{PersonBlock, PersonBlockForm},
|
2021-03-11 04:43:11 +00:00
|
|
|
person_mention::*,
|
|
|
|
post::Post,
|
2021-03-25 19:19:40 +00:00
|
|
|
private_message::PrivateMessage,
|
2021-03-11 04:43:11 +00:00
|
|
|
site::*,
|
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::{Blockable, Crud},
|
|
|
|
SortType,
|
2021-03-11 04:43:11 +00:00
|
|
|
};
|
2020-12-21 16:30:34 +00:00
|
|
|
use lemmy_db_views::{
|
|
|
|
comment_report_view::CommentReportView,
|
2021-10-16 10:43:41 +00:00
|
|
|
comment_view::{CommentQueryBuilder, CommentView},
|
2021-03-11 04:43:11 +00:00
|
|
|
local_user_view::LocalUserView,
|
2020-12-21 16:30:34 +00:00
|
|
|
post_report_view::PostReportView,
|
2021-10-16 10:43:41 +00:00
|
|
|
private_message_view::PrivateMessageView,
|
2020-12-21 23:27:42 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_views_actor::{
|
2021-08-13 17:39:56 +00:00
|
|
|
community_moderator_view::CommunityModeratorView,
|
2021-03-10 22:33:55 +00:00
|
|
|
person_mention_view::{PersonMentionQueryBuilder, PersonMentionView},
|
|
|
|
person_view::PersonViewSafe,
|
2020-12-21 16:30:34 +00:00
|
|
|
};
|
2020-07-10 18:15:41 +00:00
|
|
|
use lemmy_utils::{
|
2021-02-09 18:26:06 +00:00
|
|
|
claims::Claims,
|
2020-08-13 15:46:31 +00:00
|
|
|
location_info,
|
2021-12-15 19:49:59 +00:00
|
|
|
utils::{is_valid_display_name, is_valid_matrix_id, naive_from_unix},
|
2020-09-01 14:25:34 +00:00
|
|
|
ConnectionId,
|
|
|
|
LemmyError,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2020-09-24 13:53:21 +00:00
|
|
|
use lemmy_websocket::{
|
2021-09-28 10:36:17 +00:00
|
|
|
messages::{CaptchaItem, SendAllMessage},
|
2020-09-24 13:53:21 +00:00
|
|
|
LemmyContext,
|
|
|
|
UserOperation,
|
|
|
|
};
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for Login {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = LoginResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
_websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<LoginResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &Login = self;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
// Fetch that username / email
|
2020-07-01 12:54:29 +00:00
|
|
|
let username_or_email = data.username_or_email.clone();
|
2021-04-16 13:10:43 +00:00
|
|
|
let local_user_view = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
LocalUserView::find_by_email_or_name(conn, &username_or_email)
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_find_that_username_or_email"))?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
// Verify the password
|
2021-03-11 04:43:11 +00:00
|
|
|
let valid: bool = verify(
|
|
|
|
&data.password,
|
|
|
|
&local_user_view.local_user.password_encrypted,
|
|
|
|
)
|
|
|
|
.unwrap_or(false);
|
2019-05-05 05:20:38 +00:00
|
|
|
if !valid {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("password_incorrect"));
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2021-12-15 19:49:59 +00:00
|
|
|
let site = blocking(context.pool(), Site::read_simple).await??;
|
|
|
|
if site.require_email_verification && !local_user_view.local_user.email_verified {
|
|
|
|
return Err(LemmyError::from_message("email_not_verified"));
|
|
|
|
}
|
|
|
|
|
|
|
|
check_registration_application(&site, &local_user_view, context.pool()).await?;
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
// Return the jwt
|
2020-07-10 18:15:41 +00:00
|
|
|
Ok(LoginResponse {
|
2021-12-15 19:49:59 +00:00
|
|
|
jwt: Some(
|
|
|
|
Claims::jwt(
|
|
|
|
local_user_view.local_user.id.0,
|
|
|
|
&context.secret().jwt_secret,
|
|
|
|
&context.settings().hostname,
|
|
|
|
)?
|
|
|
|
.into(),
|
|
|
|
),
|
|
|
|
verify_email_sent: false,
|
|
|
|
registration_created: false,
|
2020-07-10 18:15:41 +00:00
|
|
|
})
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 13:02:46 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for GetCaptcha {
|
2020-07-29 13:02:46 +00:00
|
|
|
type Response = GetCaptchaResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2020-07-29 13:02:46 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
2020-08-24 11:58:24 +00:00
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
2020-07-29 13:02:46 +00:00
|
|
|
) -> Result<Self::Response, LemmyError> {
|
2021-09-22 15:57:09 +00:00
|
|
|
let captcha_settings = context.settings().captcha;
|
2020-07-29 13:02:46 +00:00
|
|
|
|
|
|
|
if !captcha_settings.enabled {
|
|
|
|
return Ok(GetCaptchaResponse { ok: None });
|
|
|
|
}
|
|
|
|
|
|
|
|
let captcha = match captcha_settings.difficulty.as_str() {
|
|
|
|
"easy" => gen(Difficulty::Easy),
|
|
|
|
"medium" => gen(Difficulty::Medium),
|
|
|
|
"hard" => gen(Difficulty::Hard),
|
|
|
|
_ => gen(Difficulty::Medium),
|
|
|
|
};
|
|
|
|
|
|
|
|
let answer = captcha.chars_as_string();
|
|
|
|
|
2021-04-01 17:30:24 +00:00
|
|
|
let png = captcha.as_base64().expect("failed to generate captcha");
|
2020-07-29 13:02:46 +00:00
|
|
|
|
|
|
|
let uuid = uuid::Uuid::new_v4().to_string();
|
|
|
|
|
2021-04-01 17:30:24 +00:00
|
|
|
let wav = captcha_as_wav_base64(&captcha);
|
2020-07-29 13:02:46 +00:00
|
|
|
|
|
|
|
let captcha_item = CaptchaItem {
|
|
|
|
answer,
|
|
|
|
uuid: uuid.to_owned(),
|
|
|
|
expires: naive_now() + Duration::minutes(10), // expires in 10 minutes
|
|
|
|
};
|
|
|
|
|
2020-08-24 11:58:24 +00:00
|
|
|
// Stores the captcha item on the queue
|
|
|
|
context.chat_server().do_send(captcha_item);
|
2020-07-29 13:02:46 +00:00
|
|
|
|
|
|
|
Ok(GetCaptchaResponse {
|
2021-03-25 19:30:15 +00:00
|
|
|
ok: Some(CaptchaResponse { png, wav, uuid }),
|
2020-07-29 13:02:46 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for SaveUserSettings {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = LoginResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
_websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<LoginResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &SaveUserSettings = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2019-08-14 02:52:43 +00:00
|
|
|
|
2021-03-02 12:41:48 +00:00
|
|
|
let avatar = diesel_option_overwrite_to_url(&data.avatar)?;
|
|
|
|
let banner = diesel_option_overwrite_to_url(&data.banner)?;
|
2020-09-25 15:16:49 +00:00
|
|
|
let bio = diesel_option_overwrite(&data.bio);
|
2021-04-01 17:57:45 +00:00
|
|
|
let display_name = diesel_option_overwrite(&data.display_name);
|
2020-09-25 15:16:49 +00:00
|
|
|
let matrix_user_id = diesel_option_overwrite(&data.matrix_user_id);
|
2021-04-21 21:41:14 +00:00
|
|
|
let bot_account = data.bot_account;
|
2021-12-15 19:49:59 +00:00
|
|
|
let email_deref = data.email.as_deref().map(|e| e.to_owned());
|
|
|
|
let email = diesel_option_overwrite(&email_deref);
|
|
|
|
|
|
|
|
if let Some(Some(email)) = &email {
|
|
|
|
let previous_email = local_user_view.local_user.email.unwrap_or_default();
|
|
|
|
// Only send the verification email if there was an email change
|
|
|
|
if previous_email.ne(email) {
|
|
|
|
send_verification_email(
|
|
|
|
local_user_view.local_user.id,
|
|
|
|
email,
|
|
|
|
&local_user_view.person.name,
|
|
|
|
context.pool(),
|
|
|
|
&context.settings(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When the site requires email, make sure email is not Some(None). IE, an overwrite to a None value
|
|
|
|
if let Some(email) = &email {
|
|
|
|
let site_fut = blocking(context.pool(), Site::read_simple);
|
|
|
|
if email.is_none() && site_fut.await??.require_email_verification {
|
|
|
|
return Err(LemmyError::from_message("email_required"));
|
|
|
|
}
|
|
|
|
}
|
2020-08-05 16:03:46 +00:00
|
|
|
|
2020-09-25 15:16:49 +00:00
|
|
|
if let Some(Some(bio)) = &bio {
|
|
|
|
if bio.chars().count() > 300 {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("bio_length_overflow"));
|
2020-08-12 11:13:44 +00:00
|
|
|
}
|
2020-09-25 15:16:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-01 17:57:45 +00:00
|
|
|
if let Some(Some(display_name)) = &display_name {
|
2021-09-22 15:57:09 +00:00
|
|
|
if !is_valid_display_name(
|
|
|
|
display_name.trim(),
|
|
|
|
context.settings().actor_name_max_length,
|
|
|
|
) {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("invalid_username"));
|
2020-09-25 15:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-10 00:04:09 +00:00
|
|
|
|
2021-04-07 11:38:00 +00:00
|
|
|
if let Some(Some(matrix_user_id)) = &matrix_user_id {
|
|
|
|
if !is_valid_matrix_id(matrix_user_id) {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("invalid_matrix_id"));
|
2021-04-07 11:38:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
let local_user_id = local_user_view.local_user.id;
|
|
|
|
let person_id = local_user_view.person.id;
|
2020-12-26 03:22:25 +00:00
|
|
|
let default_listing_type = data.default_listing_type;
|
|
|
|
let default_sort_type = data.default_sort_type;
|
2021-04-01 21:39:01 +00:00
|
|
|
let password_encrypted = local_user_view.local_user.password_encrypted;
|
2021-11-22 15:10:18 +00:00
|
|
|
let public_key = local_user_view.person.public_key;
|
2020-12-20 01:10:47 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
let person_form = PersonForm {
|
|
|
|
name: local_user_view.person.name,
|
2020-07-10 00:04:09 +00:00
|
|
|
avatar,
|
2020-08-05 16:03:46 +00:00
|
|
|
banner,
|
2021-02-04 16:34:58 +00:00
|
|
|
inbox_url: None,
|
2021-04-01 17:57:45 +00:00
|
|
|
display_name,
|
2021-03-11 04:43:11 +00:00
|
|
|
published: None,
|
2019-08-14 02:52:43 +00:00
|
|
|
updated: Some(naive_now()),
|
2021-03-11 04:43:11 +00:00
|
|
|
banned: None,
|
|
|
|
deleted: None,
|
|
|
|
actor_id: None,
|
|
|
|
bio,
|
|
|
|
local: None,
|
2021-03-22 14:28:00 +00:00
|
|
|
admin: None,
|
2021-03-11 04:43:11 +00:00
|
|
|
private_key: None,
|
2021-11-22 15:10:18 +00:00
|
|
|
public_key,
|
2021-03-11 04:43:11 +00:00
|
|
|
last_refreshed_at: None,
|
|
|
|
shared_inbox_url: None,
|
2021-03-20 19:21:51 +00:00
|
|
|
matrix_user_id,
|
2021-04-21 21:41:14 +00:00
|
|
|
bot_account,
|
2021-03-11 04:43:11 +00:00
|
|
|
};
|
|
|
|
|
2021-10-13 19:50:21 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2021-03-11 04:43:11 +00:00
|
|
|
Person::update(conn, person_id, &person_form)
|
|
|
|
})
|
2021-10-13 19:50:21 +00:00
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("user_already_exists"))?;
|
2021-03-11 04:43:11 +00:00
|
|
|
|
|
|
|
let local_user_form = LocalUserForm {
|
2021-12-15 19:49:59 +00:00
|
|
|
person_id: Some(person_id),
|
2021-03-11 04:43:11 +00:00
|
|
|
email,
|
2021-12-15 19:49:59 +00:00
|
|
|
password_encrypted: Some(password_encrypted),
|
2019-08-14 02:52:43 +00:00
|
|
|
show_nsfw: data.show_nsfw,
|
2021-04-21 21:41:14 +00:00
|
|
|
show_bot_accounts: data.show_bot_accounts,
|
2021-03-31 10:54:46 +00:00
|
|
|
show_scores: data.show_scores,
|
2019-10-15 19:21:27 +00:00
|
|
|
theme: data.theme.to_owned(),
|
2020-12-20 01:10:47 +00:00
|
|
|
default_sort_type,
|
|
|
|
default_listing_type,
|
2019-12-09 08:24:53 +00:00
|
|
|
lang: data.lang.to_owned(),
|
2020-01-02 21:55:54 +00:00
|
|
|
show_avatars: data.show_avatars,
|
2021-04-24 22:26:50 +00:00
|
|
|
show_read_posts: data.show_read_posts,
|
2021-07-22 20:07:40 +00:00
|
|
|
show_new_post_notifs: data.show_new_post_notifs,
|
2020-01-02 21:55:54 +00:00
|
|
|
send_notifications_to_email: data.send_notifications_to_email,
|
2021-12-15 19:49:59 +00:00
|
|
|
email_verified: None,
|
|
|
|
accepted_application: None,
|
2019-08-14 02:52:43 +00:00
|
|
|
};
|
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
let local_user_res = blocking(context.pool(), move |conn| {
|
|
|
|
LocalUser::update(conn, local_user_id, &local_user_form)
|
2020-08-18 13:43:50 +00:00
|
|
|
})
|
|
|
|
.await?;
|
2021-03-11 22:47:44 +00:00
|
|
|
let updated_local_user = match local_user_res {
|
|
|
|
Ok(u) => u,
|
2020-01-18 01:34:16 +00:00
|
|
|
Err(e) => {
|
|
|
|
let err_type = if e.to_string()
|
2021-03-11 04:43:11 +00:00
|
|
|
== "duplicate key value violates unique constraint \"local_user_email_key\""
|
2020-01-18 01:34:16 +00:00
|
|
|
{
|
|
|
|
"email_already_exists"
|
|
|
|
} else {
|
|
|
|
"user_already_exists"
|
|
|
|
};
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from(e).with_message(err_type));
|
2020-01-18 01:34:16 +00:00
|
|
|
}
|
2019-08-14 02:52:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Return the jwt
|
2019-09-07 15:35:05 +00:00
|
|
|
Ok(LoginResponse {
|
2021-12-15 19:49:59 +00:00
|
|
|
jwt: Some(
|
|
|
|
Claims::jwt(
|
|
|
|
updated_local_user.id.0,
|
|
|
|
&context.secret().jwt_secret,
|
|
|
|
&context.settings().hostname,
|
|
|
|
)?
|
|
|
|
.into(),
|
|
|
|
),
|
|
|
|
verify_email_sent: false,
|
|
|
|
registration_created: false,
|
2019-09-07 15:35:05 +00:00
|
|
|
})
|
2019-08-14 02:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2021-04-01 21:39:01 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for ChangePassword {
|
|
|
|
type Response = LoginResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(self, context, _websocket_id))]
|
2021-04-01 21:39:01 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<LoginResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &ChangePassword = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
2021-12-06 14:54:47 +00:00
|
|
|
get_local_user_view_from_jwt(data.auth.as_ref(), context.pool(), context.secret()).await?;
|
2021-04-01 21:39:01 +00:00
|
|
|
|
|
|
|
password_length_check(&data.new_password)?;
|
|
|
|
|
|
|
|
// Make sure passwords match
|
|
|
|
if data.new_password != data.new_password_verify {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("passwords_dont_match"));
|
2021-04-01 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the old password
|
|
|
|
let valid: bool = verify(
|
|
|
|
&data.old_password,
|
|
|
|
&local_user_view.local_user.password_encrypted,
|
|
|
|
)
|
|
|
|
.unwrap_or(false);
|
|
|
|
if !valid {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("password_incorrect"));
|
2021-04-01 21:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let local_user_id = local_user_view.local_user.id;
|
|
|
|
let new_password = data.new_password.to_owned();
|
|
|
|
let updated_local_user = blocking(context.pool(), move |conn| {
|
|
|
|
LocalUser::update_password(conn, local_user_id, &new_password)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Return the jwt
|
|
|
|
Ok(LoginResponse {
|
2021-12-15 19:49:59 +00:00
|
|
|
jwt: Some(
|
|
|
|
Claims::jwt(
|
|
|
|
updated_local_user.id.0,
|
|
|
|
&context.secret().jwt_secret,
|
|
|
|
&context.settings().hostname,
|
|
|
|
)?
|
|
|
|
.into(),
|
|
|
|
),
|
|
|
|
verify_email_sent: false,
|
|
|
|
registration_created: false,
|
2021-04-01 21:39:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for AddAdmin {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = AddAdminResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, websocket_id))]
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<AddAdminResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &AddAdmin = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
// Make sure user is an admin
|
2021-03-11 04:43:11 +00:00
|
|
|
is_admin(&local_user_view)?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let added = data.added;
|
2021-03-15 18:02:27 +00:00
|
|
|
let added_person_id = data.person_id;
|
2021-10-13 19:50:21 +00:00
|
|
|
let added_admin = blocking(context.pool(), move |conn| {
|
2021-03-22 14:28:00 +00:00
|
|
|
Person::add_admin(conn, added_person_id, added)
|
2021-03-11 04:43:11 +00:00
|
|
|
})
|
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_update_user"))?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModAddForm {
|
2021-03-11 04:43:11 +00:00
|
|
|
mod_person_id: local_user_view.person.id,
|
2021-03-22 14:28:00 +00:00
|
|
|
other_person_id: added_admin.id,
|
2019-05-05 05:20:38 +00:00
|
|
|
removed: Some(!data.added),
|
|
|
|
};
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let site_creator_id = blocking(context.pool(), move |conn| {
|
|
|
|
Site::read(conn, 1).map(|s| s.creator_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2020-07-01 12:54:29 +00:00
|
|
|
|
2021-10-12 16:46:26 +00:00
|
|
|
let mut admins = blocking(context.pool(), PersonViewSafe::admins).await??;
|
2020-08-13 15:46:31 +00:00
|
|
|
let creator_index = admins
|
|
|
|
.iter()
|
2021-03-11 04:43:11 +00:00
|
|
|
.position(|r| r.person.id == site_creator_id)
|
2020-08-13 15:46:31 +00:00
|
|
|
.context(location_info!())?;
|
2021-03-11 04:43:11 +00:00
|
|
|
let creator_person = admins.remove(creator_index);
|
|
|
|
admins.insert(0, creator_person);
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
let res = AddAdminResponse { admins };
|
|
|
|
|
2020-08-24 11:58:24 +00:00
|
|
|
context.chat_server().do_send(SendAllMessage {
|
|
|
|
op: UserOperation::AddAdmin,
|
|
|
|
response: res.clone(),
|
|
|
|
websocket_id,
|
|
|
|
});
|
2020-04-19 22:08:25 +00:00
|
|
|
|
|
|
|
Ok(res)
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2021-03-11 04:43:11 +00:00
|
|
|
impl Perform for BanPerson {
|
|
|
|
type Response = BanPersonResponse;
|
2020-04-20 03:59:07 +00:00
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, websocket_id))]
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2021-03-11 04:43:11 +00:00
|
|
|
) -> Result<BanPersonResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &BanPerson = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
// Make sure user is an admin
|
2021-03-11 04:43:11 +00:00
|
|
|
is_admin(&local_user_view)?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let ban = data.ban;
|
2021-03-11 04:43:11 +00:00
|
|
|
let banned_person_id = data.person_id;
|
|
|
|
let ban_person = move |conn: &'_ _| Person::ban_person(conn, banned_person_id, ban);
|
2021-10-13 19:50:21 +00:00
|
|
|
blocking(context.pool(), ban_person)
|
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_update_user"))?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-08-17 18:12:36 +00:00
|
|
|
// Remove their data if that's desired
|
2021-04-26 13:50:34 +00:00
|
|
|
if data.remove_data.unwrap_or(false) {
|
2020-08-17 18:12:36 +00:00
|
|
|
// Posts
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn: &'_ _| {
|
2021-03-11 04:43:11 +00:00
|
|
|
Post::update_removed_for_creator(conn, banned_person_id, None, true)
|
2020-08-17 18:12:36 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Communities
|
2021-04-08 11:29:08 +00:00
|
|
|
// Remove all communities where they're the top mod
|
|
|
|
// for now, remove the communities manually
|
2021-08-13 17:39:56 +00:00
|
|
|
let first_mod_communities = blocking(context.pool(), move |conn: &'_ _| {
|
|
|
|
CommunityModeratorView::get_community_first_mods(conn)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Filter to only this banned users top communities
|
|
|
|
let banned_user_first_communities: Vec<CommunityModeratorView> = first_mod_communities
|
|
|
|
.into_iter()
|
|
|
|
.filter(|fmc| fmc.moderator.id == banned_person_id)
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
for first_mod_community in banned_user_first_communities {
|
|
|
|
blocking(context.pool(), move |conn: &'_ _| {
|
|
|
|
Community::update_removed(conn, first_mod_community.community.id, true)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
}
|
2020-08-17 18:12:36 +00:00
|
|
|
|
|
|
|
// Comments
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn: &'_ _| {
|
2021-03-11 04:43:11 +00:00
|
|
|
Comment::update_removed_for_creator(conn, banned_person_id, true)
|
2020-08-17 18:12:36 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
}
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
// Mod tables
|
2021-03-25 19:30:15 +00:00
|
|
|
let expires = data.expires.map(naive_from_unix);
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
let form = ModBanForm {
|
2021-03-11 04:43:11 +00:00
|
|
|
mod_person_id: local_user_view.person.id,
|
|
|
|
other_person_id: data.person_id,
|
2019-05-05 05:20:38 +00:00
|
|
|
reason: data.reason.to_owned(),
|
|
|
|
banned: Some(data.ban),
|
2019-12-09 19:08:19 +00:00
|
|
|
expires,
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| ModBan::create(conn, &form)).await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
let person_id = data.person_id;
|
|
|
|
let person_view = blocking(context.pool(), move |conn| {
|
|
|
|
PersonViewSafe::read(conn, person_id)
|
2020-08-18 13:43:50 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
let res = BanPersonResponse {
|
|
|
|
person_view,
|
2019-09-07 15:35:05 +00:00
|
|
|
banned: data.ban,
|
2020-04-19 22:08:25 +00:00
|
|
|
};
|
|
|
|
|
2020-08-24 11:58:24 +00:00
|
|
|
context.chat_server().do_send(SendAllMessage {
|
2021-03-10 22:33:55 +00:00
|
|
|
op: UserOperation::BanPerson,
|
2020-08-24 11:58:24 +00:00
|
|
|
response: res.clone(),
|
|
|
|
websocket_id,
|
|
|
|
});
|
2020-04-19 22:08:25 +00:00
|
|
|
|
|
|
|
Ok(res)
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-19 20:56:41 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for GetBannedPersons {
|
|
|
|
type Response = BannedPersonsResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<Self::Response, LemmyError> {
|
|
|
|
let data: &GetBannedPersons = self;
|
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
|
|
|
|
|
|
|
// Make sure user is an admin
|
|
|
|
is_admin(&local_user_view)?;
|
|
|
|
|
|
|
|
let banned = blocking(context.pool(), PersonViewSafe::banned).await??;
|
|
|
|
|
|
|
|
let res = Self::Response { banned };
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 20:54:15 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for BlockPerson {
|
|
|
|
type Response = BlockPersonResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2021-08-19 20:54:15 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<BlockPersonResponse, LemmyError> {
|
|
|
|
let data: &BlockPerson = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2021-08-19 20:54:15 +00:00
|
|
|
|
|
|
|
let target_id = data.person_id;
|
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
|
|
|
|
// Don't let a person block themselves
|
|
|
|
if target_id == person_id {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("cant_block_yourself"));
|
2021-08-19 20:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let person_block_form = PersonBlockForm {
|
|
|
|
person_id,
|
|
|
|
target_id,
|
|
|
|
};
|
|
|
|
|
|
|
|
if data.block {
|
|
|
|
let block = move |conn: &'_ _| PersonBlock::block(conn, &person_block_form);
|
2021-10-13 19:50:21 +00:00
|
|
|
blocking(context.pool(), block)
|
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("person_block_already_exists"))?;
|
2021-08-19 20:54:15 +00:00
|
|
|
} else {
|
|
|
|
let unblock = move |conn: &'_ _| PersonBlock::unblock(conn, &person_block_form);
|
2021-10-13 19:50:21 +00:00
|
|
|
blocking(context.pool(), unblock)
|
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("person_block_already_exists"))?;
|
2021-08-19 20:54:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO does any federated stuff need to be done here?
|
|
|
|
|
|
|
|
let person_view = blocking(context.pool(), move |conn| {
|
|
|
|
PersonViewSafe::read(conn, target_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let res = BlockPersonResponse {
|
|
|
|
person_view,
|
|
|
|
blocked: data.block,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for GetReplies {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = GetRepliesResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
_websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<GetRepliesResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &GetReplies = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2021-04-15 03:37:51 +00:00
|
|
|
let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
|
|
|
let unread_only = data.unread_only;
|
2021-03-11 04:43:11 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
2021-04-21 21:41:14 +00:00
|
|
|
let show_bot_accounts = local_user_view.local_user.show_bot_accounts;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let replies = blocking(context.pool(), move |conn| {
|
2020-12-16 18:59:43 +00:00
|
|
|
CommentQueryBuilder::create(conn)
|
2021-04-15 03:37:51 +00:00
|
|
|
.sort(sort)
|
2020-07-01 12:54:29 +00:00
|
|
|
.unread_only(unread_only)
|
2021-03-11 04:43:11 +00:00
|
|
|
.recipient_id(person_id)
|
2021-04-21 21:41:14 +00:00
|
|
|
.show_bot_accounts(show_bot_accounts)
|
2021-03-11 04:43:11 +00:00
|
|
|
.my_person_id(person_id)
|
2020-07-01 12:54:29 +00:00
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-01-16 14:39:08 +00:00
|
|
|
Ok(GetRepliesResponse { replies })
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2021-03-11 04:43:11 +00:00
|
|
|
impl Perform for GetPersonMentions {
|
|
|
|
type Response = GetPersonMentionsResponse;
|
2020-04-20 03:59:07 +00:00
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
_websocket_id: Option<ConnectionId>,
|
2021-03-11 04:43:11 +00:00
|
|
|
) -> Result<GetPersonMentionsResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &GetPersonMentions = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2021-04-15 03:37:51 +00:00
|
|
|
let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
|
|
|
let unread_only = data.unread_only;
|
2021-03-11 04:43:11 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let mentions = blocking(context.pool(), move |conn| {
|
2021-03-11 04:43:11 +00:00
|
|
|
PersonMentionQueryBuilder::create(conn)
|
|
|
|
.recipient_id(person_id)
|
|
|
|
.my_person_id(person_id)
|
2021-04-15 03:37:51 +00:00
|
|
|
.sort(sort)
|
2020-07-01 12:54:29 +00:00
|
|
|
.unread_only(unread_only)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
Ok(GetPersonMentionsResponse { mentions })
|
2019-10-20 00:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2021-03-11 04:43:11 +00:00
|
|
|
impl Perform for MarkPersonMentionAsRead {
|
|
|
|
type Response = PersonMentionResponse;
|
2020-04-20 03:59:07 +00:00
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
_websocket_id: Option<ConnectionId>,
|
2021-03-11 04:43:11 +00:00
|
|
|
) -> Result<PersonMentionResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &MarkPersonMentionAsRead = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
let person_mention_id = data.person_mention_id;
|
|
|
|
let read_person_mention = blocking(context.pool(), move |conn| {
|
|
|
|
PersonMention::read(conn, person_mention_id)
|
2020-08-18 13:43:50 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
if local_user_view.person.id != read_person_mention.recipient_id {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("couldnt_update_comment"));
|
2020-07-14 16:12:04 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
let person_mention_id = read_person_mention.id;
|
2020-07-20 14:56:40 +00:00
|
|
|
let read = data.read;
|
2021-03-11 04:43:11 +00:00
|
|
|
let update_mention =
|
|
|
|
move |conn: &'_ _| PersonMention::update_read(conn, person_mention_id, read);
|
2021-10-13 19:50:21 +00:00
|
|
|
blocking(context.pool(), update_mention)
|
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_update_comment"))?;
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
let person_mention_id = read_person_mention.id;
|
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
let person_mention_view = blocking(context.pool(), move |conn| {
|
|
|
|
PersonMentionView::read(conn, person_mention_id, Some(person_id))
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2019-10-20 00:46:29 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
Ok(PersonMentionResponse {
|
|
|
|
person_mention_view,
|
|
|
|
})
|
2019-10-20 00:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for MarkAllAsRead {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = GetRepliesResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
_websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<GetRepliesResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &MarkAllAsRead = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
2020-08-18 13:43:50 +00:00
|
|
|
let replies = blocking(context.pool(), move |conn| {
|
2020-12-16 18:59:43 +00:00
|
|
|
CommentQueryBuilder::create(conn)
|
2021-03-11 04:43:11 +00:00
|
|
|
.my_person_id(person_id)
|
|
|
|
.recipient_id(person_id)
|
2020-07-01 12:54:29 +00:00
|
|
|
.unread_only(true)
|
|
|
|
.page(1)
|
|
|
|
.limit(999)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
// TODO: this should probably be a bulk operation
|
2020-07-21 01:37:44 +00:00
|
|
|
// Not easy to do as a bulk operation,
|
|
|
|
// because recipient_id isn't in the comment table
|
2020-12-15 19:39:18 +00:00
|
|
|
for comment_view in &replies {
|
|
|
|
let reply_id = comment_view.comment.id;
|
2020-07-21 01:37:44 +00:00
|
|
|
let mark_as_read = move |conn: &'_ _| Comment::update_read(conn, reply_id, true);
|
2021-10-13 19:50:21 +00:00
|
|
|
blocking(context.pool(), mark_as_read)
|
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_update_comment"))?;
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 14:56:40 +00:00
|
|
|
// Mark all user mentions as read
|
2021-03-11 04:43:11 +00:00
|
|
|
let update_person_mentions =
|
|
|
|
move |conn: &'_ _| PersonMention::mark_all_as_read(conn, person_id);
|
2021-10-13 19:50:21 +00:00
|
|
|
blocking(context.pool(), update_person_mentions)
|
2020-08-18 13:43:50 +00:00
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_update_comment"))?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-20 04:29:44 +00:00
|
|
|
// Mark all private_messages as read
|
2021-03-11 04:43:11 +00:00
|
|
|
let update_pm = move |conn: &'_ _| PrivateMessage::mark_all_as_read(conn, person_id);
|
2021-10-13 19:50:21 +00:00
|
|
|
blocking(context.pool(), update_pm)
|
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_update_private_message"))?;
|
2020-01-22 21:35:29 +00:00
|
|
|
|
2020-01-16 14:39:08 +00:00
|
|
|
Ok(GetRepliesResponse { replies: vec![] })
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-15 22:09:01 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for PasswordReset {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = PasswordResetResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(self, context, _websocket_id))]
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
_websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<PasswordResetResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &PasswordReset = self;
|
2019-10-30 03:35:39 +00:00
|
|
|
|
|
|
|
// Fetch that email
|
2020-07-01 12:54:29 +00:00
|
|
|
let email = data.email.clone();
|
2021-04-16 13:10:43 +00:00
|
|
|
let local_user_view = blocking(context.pool(), move |conn| {
|
2021-03-11 04:43:11 +00:00
|
|
|
LocalUserView::find_by_email(conn, &email)
|
2020-08-18 13:43:50 +00:00
|
|
|
})
|
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_find_that_username_or_email"))?;
|
2019-10-30 03:35:39 +00:00
|
|
|
|
|
|
|
// Email the pure token to the user.
|
2021-12-15 19:49:59 +00:00
|
|
|
send_password_reset_email(&local_user_view, context.pool(), &context.settings()).await?;
|
2020-01-16 14:39:08 +00:00
|
|
|
Ok(PasswordResetResponse {})
|
2019-10-30 03:35:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for PasswordChange {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = LoginResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(self, context, _websocket_id))]
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
_websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<LoginResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &PasswordChange = self;
|
2019-10-30 03:35:39 +00:00
|
|
|
|
|
|
|
// Fetch the user_id from the token
|
2020-07-01 12:54:29 +00:00
|
|
|
let token = data.token.clone();
|
2021-03-11 04:43:11 +00:00
|
|
|
let local_user_id = blocking(context.pool(), move |conn| {
|
2021-02-26 13:49:58 +00:00
|
|
|
PasswordResetRequest::read_from_token(conn, &token).map(|p| p.local_user_id)
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2019-10-30 03:35:39 +00:00
|
|
|
|
2021-03-02 15:36:10 +00:00
|
|
|
password_length_check(&data.password)?;
|
|
|
|
|
2019-10-30 03:35:39 +00:00
|
|
|
// Make sure passwords match
|
2020-01-02 11:30:00 +00:00
|
|
|
if data.password != data.password_verify {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("passwords_dont_match"));
|
2019-10-30 03:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the user with the new password
|
2020-07-01 12:54:29 +00:00
|
|
|
let password = data.password.clone();
|
2021-04-16 13:10:43 +00:00
|
|
|
let updated_local_user = blocking(context.pool(), move |conn| {
|
2021-03-11 04:43:11 +00:00
|
|
|
LocalUser::update_password(conn, local_user_id, &password)
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await?
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_update_user"))?;
|
2019-10-30 03:35:39 +00:00
|
|
|
|
|
|
|
// Return the jwt
|
|
|
|
Ok(LoginResponse {
|
2021-12-15 19:49:59 +00:00
|
|
|
jwt: Some(
|
|
|
|
Claims::jwt(
|
|
|
|
updated_local_user.id.0,
|
|
|
|
&context.secret().jwt_secret,
|
|
|
|
&context.settings().hostname,
|
|
|
|
)?
|
|
|
|
.into(),
|
|
|
|
),
|
|
|
|
verify_email_sent: false,
|
|
|
|
registration_created: false,
|
2019-10-30 03:35:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-01-22 21:35:29 +00:00
|
|
|
|
2020-10-25 02:59:13 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for GetReportCount {
|
|
|
|
type Response = GetReportCountResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2020-10-25 02:59:13 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
2021-09-28 10:36:17 +00:00
|
|
|
_websocket_id: Option<ConnectionId>,
|
2020-10-25 02:59:13 +00:00
|
|
|
) -> Result<GetReportCountResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &GetReportCount = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2020-10-25 02:59:13 +00:00
|
|
|
|
2021-03-11 04:43:11 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
2021-10-12 12:02:16 +00:00
|
|
|
let admin = local_user_view.person.admin;
|
2021-09-28 10:36:17 +00:00
|
|
|
let community_id = data.community_id;
|
2020-10-25 02:59:13 +00:00
|
|
|
|
2021-09-28 10:36:17 +00:00
|
|
|
let comment_reports = blocking(context.pool(), move |conn| {
|
2021-10-12 12:02:16 +00:00
|
|
|
CommentReportView::get_report_count(conn, person_id, admin, community_id)
|
2021-09-28 10:36:17 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2020-10-25 02:59:13 +00:00
|
|
|
|
2021-09-28 10:36:17 +00:00
|
|
|
let post_reports = blocking(context.pool(), move |conn| {
|
2021-10-12 12:02:16 +00:00
|
|
|
PostReportView::get_report_count(conn, person_id, admin, community_id)
|
2021-09-28 10:36:17 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2020-10-25 02:59:13 +00:00
|
|
|
|
2021-09-28 10:36:17 +00:00
|
|
|
let res = GetReportCountResponse {
|
|
|
|
community_id,
|
|
|
|
comment_reports,
|
|
|
|
post_reports,
|
|
|
|
};
|
2020-10-25 02:59:13 +00:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
2021-10-16 10:43:41 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for GetUnreadCount {
|
|
|
|
type Response = GetUnreadCountResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2021-10-16 10:43:41 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<Self::Response, LemmyError> {
|
|
|
|
let data = self;
|
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
|
|
|
|
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
|
|
|
|
let replies = blocking(context.pool(), move |conn| {
|
|
|
|
CommentView::get_unread_replies(conn, person_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let mentions = blocking(context.pool(), move |conn| {
|
|
|
|
PersonMentionView::get_unread_mentions(conn, person_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let private_messages = blocking(context.pool(), move |conn| {
|
|
|
|
PrivateMessageView::get_unread_messages(conn, person_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let res = Self::Response {
|
|
|
|
replies,
|
|
|
|
mentions,
|
|
|
|
private_messages,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
2021-12-15 19:49:59 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for VerifyEmail {
|
|
|
|
type Response = VerifyEmailResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<usize>,
|
|
|
|
) -> Result<Self::Response, LemmyError> {
|
|
|
|
let token = self.token.clone();
|
|
|
|
let verification = blocking(context.pool(), move |conn| {
|
|
|
|
EmailVerification::read_for_token(conn, &token)
|
|
|
|
})
|
|
|
|
.await?
|
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("token_not_found"))?;
|
|
|
|
|
|
|
|
let form = LocalUserForm {
|
|
|
|
// necessary in case this is a new signup
|
|
|
|
email_verified: Some(true),
|
|
|
|
// necessary in case email of an existing user was changed
|
|
|
|
email: Some(Some(verification.email)),
|
|
|
|
..LocalUserForm::default()
|
|
|
|
};
|
|
|
|
let local_user_id = verification.local_user_id;
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
LocalUser::update(conn, local_user_id, &form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let local_user_view = blocking(context.pool(), move |conn| {
|
|
|
|
LocalUserView::read(conn, local_user_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
send_email_verification_success(&local_user_view, &context.settings())?;
|
|
|
|
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
EmailVerification::delete_old_tokens_for_local_user(conn, local_user_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
Ok(VerifyEmailResponse {})
|
|
|
|
}
|
|
|
|
}
|