lemmy/crates/api/src/local_user.rs

785 lines
22 KiB
Rust
Raw Normal View History

use crate::{captcha_espeak_wav_base64, Perform};
use actix_web::web::Data;
use anyhow::Context;
use bcrypt::verify;
use captcha::{gen, Difficulty};
use chrono::Duration;
use lemmy_api_common::{
blocking,
collect_moderated_communities,
community::{GetFollowedCommunities, GetFollowedCommunitiesResponse},
get_local_user_view_from_jwt,
is_admin,
password_length_check,
person::*,
};
use lemmy_db_queries::{
diesel_option_overwrite,
diesel_option_overwrite_to_url,
source::{
comment::Comment_,
2020-12-21 12:28:12 +00:00
community::Community_,
2021-03-11 04:43:11 +00:00
local_user::LocalUser_,
password_reset_request::PasswordResetRequest_,
2021-03-11 04:43:11 +00:00
person::Person_,
person_mention::PersonMention_,
post::Post_,
private_message::PrivateMessage_,
},
Crud,
SortType,
};
2021-03-11 04:43:11 +00:00
use lemmy_db_schema::{
naive_now,
source::{
comment::Comment,
community::*,
local_user::{LocalUser, LocalUserForm},
moderator::*,
password_reset_request::*,
person::*,
person_mention::*,
post::Post,
private_message::PrivateMessage,
2021-03-11 04:43:11 +00:00
site::*,
},
};
use lemmy_db_views::{
comment_report_view::CommentReportView,
comment_view::CommentQueryBuilder,
2021-03-11 04:43:11 +00:00
local_user_view::LocalUserView,
post_report_view::PostReportView,
};
use lemmy_db_views_actor::{
community_follower_view::CommunityFollowerView,
2021-03-10 22:33:55 +00:00
person_mention_view::{PersonMentionQueryBuilder, PersonMentionView},
person_view::PersonViewSafe,
};
use lemmy_utils::{
claims::Claims,
email::send_email,
location_info,
settings::structs::Settings,
utils::{generate_random_string, is_valid_preferred_username, naive_from_unix},
2021-02-22 18:04:32 +00:00
ApiError,
ConnectionId,
LemmyError,
2020-05-16 14:04:08 +00:00
};
use lemmy_websocket::{
messages::{CaptchaItem, SendAllMessage, SendUserRoomMessage},
LemmyContext,
UserOperation,
};
2020-05-16 14:04:08 +00:00
use std::str::FromStr;
2019-05-05 05:20:38 +00:00
#[async_trait::async_trait(?Send)]
impl Perform for Login {
type Response = LoginResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<LoginResponse, LemmyError> {
let data: &Login = &self;
2019-05-05 05:20:38 +00:00
// Fetch that username / email
let username_or_email = data.username_or_email.clone();
2021-03-10 22:33:55 +00:00
let local_user_view = match blocking(context.pool(), move |conn| {
LocalUserView::find_by_email_or_name(conn, &username_or_email)
})
.await?
{
2021-03-10 22:33:55 +00:00
Ok(uv) => uv,
2021-02-22 18:04:32 +00:00
Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
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-02-22 18:04:32 +00:00
return Err(ApiError::err("password_incorrect").into());
2019-05-05 05:20:38 +00:00
}
// Return the jwt
Ok(LoginResponse {
jwt: Claims::jwt(local_user_view.local_user.id.0)?,
})
2019-05-05 05:20:38 +00:00
}
}
#[async_trait::async_trait(?Send)]
impl Perform for GetCaptcha {
type Response = GetCaptchaResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<Self::Response, LemmyError> {
let captcha_settings = Settings::get().captcha();
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();
let png_byte_array = captcha.as_png().expect("failed to generate captcha");
let png = base64::encode(png_byte_array);
let uuid = uuid::Uuid::new_v4().to_string();
let wav = captcha_espeak_wav_base64(&answer).ok();
let captcha_item = CaptchaItem {
answer,
uuid: uuid.to_owned(),
expires: naive_now() + Duration::minutes(10), // expires in 10 minutes
};
// Stores the captcha item on the queue
context.chat_server().do_send(captcha_item);
Ok(GetCaptchaResponse {
ok: Some(CaptchaResponse { png, uuid, wav }),
})
}
}
#[async_trait::async_trait(?Send)]
impl Perform for SaveUserSettings {
type Response = LoginResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<LoginResponse, LemmyError> {
let data: &SaveUserSettings = &self;
2021-03-11 04:43:11 +00:00
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let avatar = diesel_option_overwrite_to_url(&data.avatar)?;
let banner = diesel_option_overwrite_to_url(&data.banner)?;
let email = diesel_option_overwrite(&data.email);
let bio = diesel_option_overwrite(&data.bio);
let preferred_username = diesel_option_overwrite(&data.preferred_username);
let matrix_user_id = diesel_option_overwrite(&data.matrix_user_id);
if let Some(Some(bio)) = &bio {
if bio.chars().count() > 300 {
2021-02-22 18:04:32 +00:00
return Err(ApiError::err("bio_length_overflow").into());
}
}
if let Some(Some(preferred_username)) = &preferred_username {
if !is_valid_preferred_username(preferred_username.trim()) {
2021-02-22 18:04:32 +00:00
return Err(ApiError::err("invalid_username").into());
}
}
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;
let password_encrypted = match &data.new_password {
Some(new_password) => {
match &data.new_password_verify {
Some(new_password_verify) => {
password_length_check(&new_password)?;
// Make sure passwords match
if new_password != new_password_verify {
2021-02-22 18:04:32 +00:00
return Err(ApiError::err("passwords_dont_match").into());
}
// Check the old password
match &data.old_password {
Some(old_password) => {
2021-03-11 04:43:11 +00:00
let valid: bool =
verify(old_password, &local_user_view.local_user.password_encrypted)
.unwrap_or(false);
if !valid {
2021-02-22 18:04:32 +00:00
return Err(ApiError::err("password_incorrect").into());
}
let new_password = new_password.to_owned();
let user = blocking(context.pool(), move |conn| {
2021-03-11 04:43:11 +00:00
LocalUser::update_password(conn, local_user_id, &new_password)
})
.await??;
user.password_encrypted
}
2021-02-22 18:04:32 +00:00
None => return Err(ApiError::err("password_incorrect").into()),
}
}
2021-02-22 18:04:32 +00:00
None => return Err(ApiError::err("passwords_dont_match").into()),
}
}
2021-03-11 04:43:11 +00:00
None => local_user_view.local_user.password_encrypted,
};
let default_listing_type = data.default_listing_type;
let default_sort_type = data.default_sort_type;
2021-03-11 04:43:11 +00:00
let person_form = PersonForm {
name: local_user_view.person.name,
avatar,
banner,
inbox_url: None,
preferred_username,
2021-03-11 04:43:11 +00:00
published: None,
updated: Some(naive_now()),
2021-03-11 04:43:11 +00:00
banned: None,
deleted: None,
actor_id: None,
bio,
local: None,
private_key: None,
public_key: None,
last_refreshed_at: None,
shared_inbox_url: None,
};
let person_res = blocking(context.pool(), move |conn| {
Person::update(conn, person_id, &person_form)
})
.await?;
2021-03-11 22:47:44 +00:00
let _updated_person: Person = match person_res {
2021-03-11 04:43:11 +00:00
Ok(p) => p,
Err(_) => {
return Err(ApiError::err("user_already_exists").into());
}
};
let local_user_form = LocalUserForm {
person_id,
email,
matrix_user_id,
password_encrypted,
admin: None,
show_nsfw: data.show_nsfw,
theme: data.theme.to_owned(),
default_sort_type,
default_listing_type,
lang: data.lang.to_owned(),
show_avatars: data.show_avatars,
send_notifications_to_email: data.send_notifications_to_email,
};
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)
})
.await?;
2021-03-11 22:47:44 +00:00
let updated_local_user = match local_user_res {
Ok(u) => u,
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\""
{
"email_already_exists"
} else {
"user_already_exists"
};
2021-02-22 18:04:32 +00:00
return Err(ApiError::err(err_type).into());
}
};
// Return the jwt
Ok(LoginResponse {
jwt: Claims::jwt(updated_local_user.id.0)?,
})
}
}
2019-05-05 05:20:38 +00:00
#[async_trait::async_trait(?Send)]
impl Perform for AddAdmin {
type Response = AddAdminResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
websocket_id: Option<ConnectionId>,
) -> Result<AddAdminResponse, LemmyError> {
let data: &AddAdmin = &self;
2021-03-11 04:43:11 +00:00
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).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
let added = data.added;
let added_person_id = data.person_id;
2021-03-11 04:43:11 +00:00
let added_admin = match blocking(context.pool(), move |conn| {
LocalUser::add_admin(conn, added_person_id, added)
2021-03-11 04:43:11 +00:00
})
.await?
{
Ok(a) => a,
Err(_) => {
return Err(ApiError::err("couldnt_update_user").into());
}
};
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,
other_person_id: added_admin.person_id,
2019-05-05 05:20:38 +00:00
removed: Some(!data.added),
};
blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
2019-05-05 05:20:38 +00:00
let site_creator_id = blocking(context.pool(), move |conn| {
Site::read(conn, 1).map(|s| s.creator_id)
})
.await??;
2021-03-11 04:43:11 +00:00
let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
let creator_index = admins
.iter()
2021-03-11 04:43:11 +00:00
.position(|r| r.person.id == site_creator_id)
.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
let res = AddAdminResponse { admins };
context.chat_server().do_send(SendAllMessage {
op: UserOperation::AddAdmin,
response: res.clone(),
websocket_id,
});
Ok(res)
2019-05-05 05:20:38 +00:00
}
}
#[async_trait::async_trait(?Send)]
2021-03-11 04:43:11 +00:00
impl Perform for BanPerson {
type Response = BanPersonResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
websocket_id: Option<ConnectionId>,
2021-03-11 04:43:11 +00:00
) -> Result<BanPersonResponse, LemmyError> {
let data: &BanPerson = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).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
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);
if blocking(context.pool(), ban_person).await?.is_err() {
2021-02-22 18:04:32 +00:00
return Err(ApiError::err("couldnt_update_user").into());
}
2019-05-05 05:20:38 +00:00
// Remove their data if that's desired
if data.remove_data {
// Posts
blocking(context.pool(), move |conn: &'_ _| {
2021-03-11 04:43:11 +00:00
Post::update_removed_for_creator(conn, banned_person_id, None, true)
})
.await??;
// Communities
blocking(context.pool(), move |conn: &'_ _| {
2021-03-11 04:43:11 +00:00
Community::update_removed_for_creator(conn, banned_person_id, true)
})
.await??;
// Comments
blocking(context.pool(), move |conn: &'_ _| {
2021-03-11 04:43:11 +00:00
Comment::update_removed_for_creator(conn, banned_person_id, true)
})
.await??;
}
2019-05-05 05:20:38 +00:00
// Mod tables
let expires = match data.expires {
Some(time) => Some(naive_from_unix(time)),
None => None,
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),
expires,
2019-05-05 05:20:38 +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)
})
.await??;
2019-05-05 05:20:38 +00:00
2021-03-11 04:43:11 +00:00
let res = BanPersonResponse {
person_view,
banned: data.ban,
};
context.chat_server().do_send(SendAllMessage {
2021-03-10 22:33:55 +00:00
op: UserOperation::BanPerson,
response: res.clone(),
websocket_id,
});
Ok(res)
2019-05-05 05:20:38 +00:00
}
}
#[async_trait::async_trait(?Send)]
impl Perform for GetReplies {
type Response = GetRepliesResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<GetRepliesResponse, LemmyError> {
let data: &GetReplies = &self;
2021-03-11 04:43:11 +00:00
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
2019-05-05 05:20:38 +00:00
let sort = SortType::from_str(&data.sort)?;
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;
let replies = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn)
.sort(&sort)
.unread_only(unread_only)
2021-03-11 04:43:11 +00:00
.recipient_id(person_id)
.my_person_id(person_id)
.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
}
}
#[async_trait::async_trait(?Send)]
2021-03-11 04:43:11 +00:00
impl Perform for GetPersonMentions {
type Response = GetPersonMentionsResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
2021-03-11 04:43:11 +00:00
) -> Result<GetPersonMentionsResponse, LemmyError> {
let data: &GetPersonMentions = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let sort = SortType::from_str(&data.sort)?;
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;
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)
.sort(&sort)
.unread_only(unread_only)
.page(page)
.limit(limit)
.list()
})
.await??;
2021-03-11 04:43:11 +00:00
Ok(GetPersonMentionsResponse { mentions })
}
}
#[async_trait::async_trait(?Send)]
2021-03-11 04:43:11 +00:00
impl Perform for MarkPersonMentionAsRead {
type Response = PersonMentionResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
2021-03-11 04:43:11 +00:00
) -> Result<PersonMentionResponse, LemmyError> {
let data: &MarkPersonMentionAsRead = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
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)
})
.await??;
2021-03-11 04:43:11 +00:00
if local_user_view.person.id != read_person_mention.recipient_id {
2021-02-22 18:04:32 +00:00
return Err(ApiError::err("couldnt_update_comment").into());
2020-07-14 16:12:04 +00:00
}
2021-03-11 04:43:11 +00:00
let person_mention_id = read_person_mention.id;
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);
if blocking(context.pool(), update_mention).await?.is_err() {
2021-02-22 18:04:32 +00:00
return Err(ApiError::err("couldnt_update_comment").into());
};
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))
})
.await??;
2021-03-11 04:43:11 +00:00
Ok(PersonMentionResponse {
person_mention_view,
})
}
}
#[async_trait::async_trait(?Send)]
impl Perform for MarkAllAsRead {
type Response = GetRepliesResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<GetRepliesResponse, LemmyError> {
let data: &MarkAllAsRead = &self;
2021-03-11 04:43:11 +00:00
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).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;
let replies = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn)
2021-03-11 04:43:11 +00:00
.my_person_id(person_id)
.recipient_id(person_id)
.unread_only(true)
.page(1)
.limit(999)
.list()
})
.await??;
2019-05-05 05:20:38 +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);
if blocking(context.pool(), mark_as_read).await?.is_err() {
2021-02-22 18:04:32 +00:00
return Err(ApiError::err("couldnt_update_comment").into());
}
2019-05-05 05:20:38 +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);
if blocking(context.pool(), update_person_mentions)
.await?
.is_err()
{
2021-02-22 18:04:32 +00:00
return Err(ApiError::err("couldnt_update_comment").into());
}
2019-05-05 05:20:38 +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);
if blocking(context.pool(), update_pm).await?.is_err() {
2021-02-22 18:04:32 +00:00
return Err(ApiError::err("couldnt_update_private_message").into());
}
2020-01-16 14:39:08 +00:00
Ok(GetRepliesResponse { replies: vec![] })
2019-05-05 05:20:38 +00:00
}
}
#[async_trait::async_trait(?Send)]
impl Perform for PasswordReset {
type Response = PasswordResetResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<PasswordResetResponse, LemmyError> {
let data: &PasswordReset = &self;
// Fetch that email
let email = data.email.clone();
2021-03-11 04:43:11 +00:00
let local_user_view = match blocking(context.pool(), move |conn| {
LocalUserView::find_by_email(conn, &email)
})
.await?
{
2021-03-11 04:43:11 +00:00
Ok(lu) => lu,
2021-02-22 18:04:32 +00:00
Err(_e) => return Err(ApiError::err("couldnt_find_that_username_or_email").into()),
};
// Generate a random token
let token = generate_random_string();
2019-11-02 06:43:21 +00:00
// Insert the row
let token2 = token.clone();
2021-03-11 04:43:11 +00:00
let local_user_id = local_user_view.local_user.id;
blocking(context.pool(), move |conn| {
2021-03-11 04:43:11 +00:00
PasswordResetRequest::create_token(conn, local_user_id, &token2)
})
.await??;
// Email the pure token to the user.
// TODO no i18n support here.
2021-03-11 04:43:11 +00:00
let email = &local_user_view.local_user.email.expect("email");
let subject = &format!("Password reset for {}", local_user_view.person.name);
let hostname = &Settings::get().get_protocol_and_hostname();
2021-03-11 04:43:11 +00:00
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);
match send_email(subject, email, &local_user_view.person.name, html) {
Ok(_o) => _o,
2021-02-22 18:04:32 +00:00
Err(_e) => return Err(ApiError::err(&_e).into()),
};
2020-01-16 14:39:08 +00:00
Ok(PasswordResetResponse {})
}
}
#[async_trait::async_trait(?Send)]
impl Perform for PasswordChange {
type Response = LoginResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<LoginResponse, LemmyError> {
let data: &PasswordChange = &self;
// Fetch the user_id from the token
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)
})
.await??;
password_length_check(&data.password)?;
// Make sure passwords match
if data.password != data.password_verify {
2021-02-22 18:04:32 +00:00
return Err(ApiError::err("passwords_dont_match").into());
}
// Update the user with the new password
let password = data.password.clone();
2021-03-11 04:43:11 +00:00
let updated_local_user = match blocking(context.pool(), move |conn| {
LocalUser::update_password(conn, local_user_id, &password)
})
.await?
{
2021-03-11 04:43:11 +00:00
Ok(u) => u,
2021-02-22 18:04:32 +00:00
Err(_e) => return Err(ApiError::err("couldnt_update_user").into()),
};
// Return the jwt
Ok(LoginResponse {
jwt: Claims::jwt(updated_local_user.id.0)?,
})
}
}
#[async_trait::async_trait(?Send)]
impl Perform for GetReportCount {
type Response = GetReportCountResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
websocket_id: Option<ConnectionId>,
) -> Result<GetReportCountResponse, LemmyError> {
let data: &GetReportCount = &self;
2021-03-11 04:43:11 +00:00
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
2021-03-11 04:43:11 +00:00
let person_id = local_user_view.person.id;
let community_id = data.community;
let community_ids =
2021-03-11 04:43:11 +00:00
collect_moderated_communities(person_id, community_id, context.pool()).await?;
let res = {
if community_ids.is_empty() {
GetReportCountResponse {
community: None,
comment_reports: 0,
post_reports: 0,
}
} else {
let ids = community_ids.clone();
let comment_reports = blocking(context.pool(), move |conn| {
CommentReportView::get_report_count(conn, &ids)
})
.await??;
let ids = community_ids.clone();
let post_reports = blocking(context.pool(), move |conn| {
PostReportView::get_report_count(conn, &ids)
})
.await??;
GetReportCountResponse {
community: data.community,
comment_reports,
post_reports,
}
}
};
context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::GetReportCount,
response: res.clone(),
local_recipient_id: local_user_view.local_user.id,
websocket_id,
});
Ok(res)
}
}
#[async_trait::async_trait(?Send)]
impl Perform for GetFollowedCommunities {
type Response = GetFollowedCommunitiesResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<GetFollowedCommunitiesResponse, LemmyError> {
let data: &GetFollowedCommunities = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let person_id = local_user_view.person.id;
let communities = match blocking(context.pool(), move |conn| {
CommunityFollowerView::for_person(conn, person_id)
})
.await?
{
Ok(communities) => communities,
_ => return Err(ApiError::err("system_err_login").into()),
};
// Return the jwt
Ok(GetFollowedCommunitiesResponse { communities })
}
}