Skip to content

Commit

Permalink
Dont pass settings as parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Oct 18, 2024
1 parent 859dfb3 commit e87da6c
Show file tree
Hide file tree
Showing 55 changed files with 211 additions and 358 deletions.
1 change: 0 additions & 1 deletion crates/api/src/comment_report/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pub async fn create_comment_report(
&comment_report_view.creator.name,
&comment_report_view.comment_creator.name,
&mut context.pool(),
context.settings(),
)
.await?;
}
Expand Down
11 changes: 5 additions & 6 deletions crates/api/src/local_user/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use lemmy_api_common::{
utils::{check_email_verified, check_registration_application, check_user_valid},
};
use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_utils::error::{LemmyErrorType, LemmyResult};
use lemmy_utils::{
error::{LemmyErrorType, LemmyResult},
settings::SETTINGS,
};

#[tracing::instrument(skip(context))]
pub async fn login(
Expand Down Expand Up @@ -44,11 +47,7 @@ pub async fn login(

// Check the totp if enabled
if local_user_view.local_user.totp_2fa_enabled {
check_totp_2fa_valid(
&local_user_view,
&data.totp_2fa_token,
&context.settings().hostname,
)?;
check_totp_2fa_valid(&local_user_view, &data.totp_2fa_token, &SETTINGS.hostname)?;
}

let jwt = Claims::generate(local_user_view.local_user.id, req, &context).await?;
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/local_user/reset_password.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ pub async fn reset_password(
check_email_verified(&local_user_view, &site_view)?;

// Email the pure token to the user.
send_password_reset_email(&local_user_view, &mut context.pool(), context.settings()).await?;
send_password_reset_email(&local_user_view, &mut context.pool()).await?;
Ok(Json(SuccessResponse::default()))
}
8 changes: 1 addition & 7 deletions crates/api/src/local_user/save_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,7 @@ pub async fn save_user_settings(
// if email was changed, check that it is not taken and send verification mail
if previous_email.deref() != email {
LocalUser::check_is_email_taken(&mut context.pool(), email).await?;
send_verification_email(
&local_user_view,
email,
&mut context.pool(),
context.settings(),
)
.await?;
send_verification_email(&local_user_view, email, &mut context.pool()).await?;
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/local_user/update_totp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use lemmy_api_common::{
};
use lemmy_db_schema::source::local_user::{LocalUser, LocalUserUpdateForm};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyResult;
use lemmy_utils::{error::LemmyResult, settings::SETTINGS};

/// Enable or disable two-factor-authentication. The current setting is determined from
/// [LocalUser.totp_2fa_enabled].
Expand All @@ -25,7 +25,7 @@ pub async fn update_totp(
check_totp_2fa_valid(
&local_user_view,
&Some(data.totp_token.clone()),
&context.settings().hostname,
&SETTINGS.hostname,
)?;

// toggle the 2fa setting
Expand Down
7 changes: 1 addition & 6 deletions crates/api/src/local_user/verify_email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,7 @@ pub async fn verify_email(
if site_view.local_site.application_email_admins {
let local_user = LocalUserView::read(&mut context.pool(), local_user_id).await?;

send_new_applicant_email_to_admins(
&local_user.person.name,
&mut context.pool(),
context.settings(),
)
.await?;
send_new_applicant_email_to_admins(&local_user.person.name, &mut context.pool()).await?;
}

Ok(Json(SuccessResponse::default()))
Expand Down
1 change: 0 additions & 1 deletion crates/api/src/post_report/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ pub async fn create_post_report(
&post_report_view.creator.name,
&post_report_view.post_creator.name,
&mut context.pool(),
context.settings(),
)
.await?;
}
Expand Down
1 change: 0 additions & 1 deletion crates/api/src/private_message_report/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub async fn create_pm_report(
&private_message_report_view.creator.name,
&private_message_report_view.private_message_creator.name,
&mut context.pool(),
context.settings(),
)
.await?;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/api/src/site/registration_applications/approve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub async fn approve_registration_application(
LocalUserView::read(&mut context.pool(), approved_user_id).await?;
if approved_local_user_view.local_user.email.is_some() {
// Email sending may fail, but this won't revert the application approval
send_application_approved_email(&approved_local_user_view, context.settings()).await?;
send_application_approved_email(&approved_local_user_view).await?;
}
};

Expand Down
8 changes: 3 additions & 5 deletions crates/api_common/src/build_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use lemmy_db_views::structs::{CommentView, LocalUserView, PostView};
use lemmy_db_views_actor::structs::CommunityView;
use lemmy_utils::{
error::LemmyResult,
settings::SETTINGS,
utils::{markdown::markdown_to_html, mention::MentionData},
};

Expand Down Expand Up @@ -99,7 +100,7 @@ pub async fn send_local_notifs(
local_user_view: Option<&LocalUserView>,
) -> LemmyResult<Vec<LocalUserId>> {
let mut recipient_ids = Vec::new();
let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname());
let inbox_link = format!("{}/inbox", SETTINGS.get_protocol_and_hostname());

// let person = my_local_user.person;
// Read the comment view to get extra info
Expand All @@ -116,7 +117,7 @@ pub async fn send_local_notifs(
// Send the local mentions
for mention in mentions
.iter()
.filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
.filter(|m| m.is_local() && m.name.ne(&person.name))
{
let mention_name = mention.name.clone();
let user_view = LocalUserView::read_from_name(&mut context.pool(), &mention_name).await;
Expand Down Expand Up @@ -147,7 +148,6 @@ pub async fn send_local_notifs(
&mention_user_view,
&lang.notification_mentioned_by_subject(&person.name),
&lang.notification_mentioned_by_body(&content, &inbox_link, &person.name),
context.settings(),
)
.await
}
Expand Down Expand Up @@ -199,7 +199,6 @@ pub async fn send_local_notifs(
&parent_user_view,
&lang.notification_comment_reply_subject(&person.name),
&lang.notification_comment_reply_body(&content, &inbox_link, &person.name),
context.settings(),
)
.await
}
Expand Down Expand Up @@ -245,7 +244,6 @@ pub async fn send_local_notifs(
&parent_user_view,
&lang.notification_post_reply_subject(&person.name),
&lang.notification_post_reply_body(&content, &inbox_link, &person.name),
context.settings(),
)
.await
}
Expand Down
7 changes: 5 additions & 2 deletions crates/api_common/src/claims.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use lemmy_db_schema::{
sensitive::SensitiveString,
source::login_token::{LoginToken, LoginTokenCreateForm},
};
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult};
use lemmy_utils::{
error::{LemmyErrorExt, LemmyErrorType, LemmyResult},
settings::SETTINGS,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -38,7 +41,7 @@ impl Claims {
req: HttpRequest,
context: &LemmyContext,
) -> LemmyResult<SensitiveString> {
let hostname = context.settings().hostname.clone();
let hostname = SETTINGS.hostname.clone();
let my_claims = Claims {
sub: user_id.0.to_string(),
iss: hostname,
Expand Down
10 changes: 2 additions & 8 deletions crates/api_common/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use lemmy_db_schema::{
source::secret::Secret,
utils::{build_db_pool_for_tests, ActualDbPool, DbPool},
};
use lemmy_utils::{
rate_limit::RateLimitCell,
settings::{structs::Settings, SETTINGS},
};
use lemmy_utils::{rate_limit::RateLimitCell, settings::SETTINGS};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use std::sync::Arc;

Expand Down Expand Up @@ -42,9 +39,6 @@ impl LemmyContext {
pub fn client(&self) -> &ClientWithMiddleware {
&self.client
}
pub fn settings(&self) -> &'static Settings {
&SETTINGS
}
pub fn secret(&self) -> &Secret {
&self.secret
}
Expand Down Expand Up @@ -72,7 +66,7 @@ impl LemmyContext {
let context = LemmyContext::create(pool, client, secret, rate_limit_cell.clone());

FederationConfig::builder()
.domain(context.settings().hostname.clone())
.domain(SETTINGS.hostname.clone())
.app_data(context)
.debug(true)
// Dont allow any network fetches
Expand Down
17 changes: 10 additions & 7 deletions crates/api_common/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use lemmy_db_schema::{
};
use lemmy_utils::{
error::{LemmyError, LemmyErrorType, LemmyResult},
settings::structs::{PictrsImageMode, Settings},
settings::{
structs::{PictrsImageMode, Settings},
SETTINGS,
},
REQWEST_TIMEOUT,
VERSION,
};
Expand Down Expand Up @@ -296,7 +299,7 @@ pub async fn purge_image_from_pictrs(image_url: &Url, context: &LemmyContext) ->
.next_back()
.ok_or(LemmyErrorType::ImageUrlMissingLastPathSegment)?;

let pictrs_config = context.settings().pictrs_config()?;
let pictrs_config = SETTINGS.pictrs_config()?;
let purge_url = format!("{}internal/purge?alias={}", pictrs_config.url, alias);

let pictrs_api_key = pictrs_config
Expand All @@ -323,7 +326,7 @@ pub async fn delete_image_from_pictrs(
delete_token: &str,
context: &LemmyContext,
) -> LemmyResult<()> {
let pictrs_config = context.settings().pictrs_config()?;
let pictrs_config = SETTINGS.pictrs_config()?;
let url = format!(
"{}image/delete/{}/{}",
pictrs_config.url, &delete_token, &alias
Expand All @@ -341,7 +344,7 @@ pub async fn delete_image_from_pictrs(
/// Retrieves the image with local pict-rs and generates a thumbnail. Returns the thumbnail url.
#[tracing::instrument(skip_all)]
async fn generate_pictrs_thumbnail(image_url: &Url, context: &LemmyContext) -> LemmyResult<Url> {
let pictrs_config = context.settings().pictrs_config()?;
let pictrs_config = SETTINGS.pictrs_config()?;

match pictrs_config.image_mode() {
PictrsImageMode::None => return Ok(image_url.clone()),
Expand All @@ -357,7 +360,7 @@ async fn generate_pictrs_thumbnail(image_url: &Url, context: &LemmyContext) -> L
"{}image/download?url={}&resize={}",
pictrs_config.url,
encode(image_url.as_str()),
context.settings().pictrs_config()?.max_thumbnail_size
SETTINGS.pictrs_config()?.max_thumbnail_size
);

let res = context
Expand All @@ -382,7 +385,7 @@ async fn generate_pictrs_thumbnail(image_url: &Url, context: &LemmyContext) -> L
pictrs_alias: image.file.clone(),
pictrs_delete_token: image.delete_token.clone(),
};
let protocol_and_hostname = context.settings().get_protocol_and_hostname();
let protocol_and_hostname = SETTINGS.get_protocol_and_hostname();
let thumbnail_url = image.thumbnail_url(&protocol_and_hostname)?;

// Also store the details for the image
Expand All @@ -400,7 +403,7 @@ pub async fn fetch_pictrs_proxied_image_details(
image_url: &Url,
context: &LemmyContext,
) -> LemmyResult<PictrsFileDetails> {
let pictrs_url = context.settings().pictrs_config()?.url;
let pictrs_url = SETTINGS.pictrs_config()?.url;
let encoded_image_url = encode(image_url.as_str());

// Pictrs needs you to fetch the proxied image before you can fetch the details
Expand Down
Loading

0 comments on commit e87da6c

Please sign in to comment.