-
-
Notifications
You must be signed in to change notification settings - Fork 948
Expand file tree
/
Copy pathcontext.rs
More file actions
113 lines (104 loc) · 3.02 KB
/
context.rs
File metadata and controls
113 lines (104 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::request::client_builder;
use activitypub_federation::config::{Data, FederationConfig};
use lemmy_db_schema::{
source::secret::Secret,
utils::{build_db_pool_for_tests, ActualDbPool, DbPool},
};
use lemmy_utils::{
rate_limit::RateLimit,
settings::{structs::Settings, SETTINGS},
};
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
use std::sync::Arc;
use lemmy_db_schema::utils::ReusableDbPool;
#[derive(Clone)]
pub enum ContextPool {
Actual(ActualDbPool),
Reusable(ReusableDbPool), // TODO it's not cloneable
}
#[derive(Clone)]
pub struct LemmyContext {
pool: ContextPool,
client: Arc<ClientWithMiddleware>,
/// Pictrs requests must bypass proxy. Unfortunately no_proxy can only be set on ClientBuilder
/// and not on RequestBuilder, so we need a separate client here.
pictrs_client: Arc<ClientWithMiddleware>,
secret: Arc<Secret>,
rate_limit_cell: RateLimit,
}
impl LemmyContext {
pub fn create(
pool: ContextPool,
client: ClientWithMiddleware,
pictrs_client: ClientWithMiddleware,
secret: Secret,
rate_limit_cell: RateLimit,
) -> LemmyContext {
LemmyContext {
pool,
client: Arc::new(client),
pictrs_client: Arc::new(pictrs_client),
secret: Arc::new(secret),
rate_limit_cell,
}
}
pub fn pool(&self) -> DbPool<'_> {
match &self.pool {
ContextPool::Actual(pool) => DbPool::Pool(pool),
ContextPool::Reusable(pool) => DbPool::ReusablePool(pool),
}
}
pub fn inner_pool(&self) -> &ContextPool {
&self.pool
}
pub fn client(&self) -> &ClientWithMiddleware {
&self.client
}
pub fn pictrs_client(&self) -> &ClientWithMiddleware {
&self.pictrs_client
}
pub fn settings(&self) -> &'static Settings {
&SETTINGS
}
pub fn secret(&self) -> &Secret {
&self.secret
}
pub fn rate_limit_cell(&self) -> &RateLimit {
&self.rate_limit_cell
}
/// Initialize a context for use in tests which blocks federation network calls.
///
/// Do not use this in production code.
#[allow(clippy::expect_used)]
pub async fn init_test_federation_config() -> FederationConfig<LemmyContext> {
// call this to run migrations
let pool = build_db_pool_for_tests().await;
let client = client_builder(&SETTINGS).build().expect("build client");
let client = ClientBuilder::new(client).build();
let secret = Secret {
id: 0,
jwt_secret: String::new().into(),
};
let rate_limit_cell = RateLimit::with_test_config();
let context = LemmyContext::create(
ContextPool::Reusable(pool),
client.clone(),
client,
secret,
rate_limit_cell.clone(),
);
FederationConfig::builder()
.domain(context.settings().hostname.clone())
.app_data(context)
.debug(true)
// Dont allow any network fetches
.http_fetch_limit(0)
.build()
.await
.expect("build federation config")
}
pub async fn init_test_context() -> Data<LemmyContext> {
let config = Self::init_test_federation_config().await;
config.to_request_data()
}
}