]> Untitled Git - lemmy.git/blob - crates/apub/src/objects/mod.rs
Reorganize federation tests (#2092)
[lemmy.git] / crates / apub / src / objects / mod.rs
1 use crate::protocol::Source;
2 use html2md::parse_html;
3
4 pub mod comment;
5 pub mod community;
6 pub mod instance;
7 pub mod person;
8 pub mod post;
9 pub mod private_message;
10
11 pub(crate) fn get_summary_from_string_or_source(
12   raw: &Option<String>,
13   source: &Option<Source>,
14 ) -> Option<String> {
15   if let Some(source) = &source {
16     Some(source.content.clone())
17   } else {
18     raw.as_ref().map(|s| parse_html(s))
19   }
20 }
21
22 #[cfg(test)]
23 pub(crate) mod tests {
24   use actix::Actor;
25   use background_jobs::QueueHandle;
26   use diesel::{
27     r2d2::{ConnectionManager, Pool},
28     PgConnection,
29   };
30   use lemmy_db_schema::{
31     establish_unpooled_connection,
32     get_database_url_from_env,
33     source::secret::Secret,
34   };
35   use lemmy_utils::{
36     rate_limit::{rate_limiter::RateLimiter, RateLimit},
37     request::build_user_agent,
38     settings::structs::Settings,
39     LemmyError,
40   };
41   use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
42   use reqwest::Client;
43   use reqwest_middleware::ClientBuilder;
44   use std::sync::Arc;
45   use tokio::sync::Mutex;
46
47   // TODO: would be nice if we didnt have to use a full context for tests.
48   //       or at least write a helper function so this code is shared with main.rs
49   pub(crate) fn init_context(activity_queue: QueueHandle) -> LemmyContext {
50     // call this to run migrations
51     establish_unpooled_connection();
52     let settings = Settings::init().unwrap();
53     let rate_limiter = RateLimit {
54       rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
55       rate_limit_config: settings.rate_limit.to_owned().unwrap_or_default(),
56     };
57     let client = Client::builder()
58       .user_agent(build_user_agent(&settings))
59       .build()
60       .unwrap();
61
62     let client = ClientBuilder::new(client).build();
63     let secret = Secret {
64       id: 0,
65       jwt_secret: "".to_string(),
66     };
67     let db_url = match get_database_url_from_env() {
68       Ok(url) => url,
69       Err(_) => settings.get_database_url(),
70     };
71     let manager = ConnectionManager::<PgConnection>::new(&db_url);
72     let pool = Pool::builder()
73       .max_size(settings.database.pool_size)
74       .build(manager)
75       .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
76     async fn x() -> Result<String, LemmyError> {
77       Ok("".to_string())
78     }
79     let chat_server = ChatServer::startup(
80       pool.clone(),
81       rate_limiter,
82       |_, _, _, _| Box::pin(x()),
83       |_, _, _, _| Box::pin(x()),
84       client.clone(),
85       activity_queue.clone(),
86       settings.clone(),
87       secret.clone(),
88     )
89     .start();
90     LemmyContext::create(pool, chat_server, client, activity_queue, settings, secret)
91   }
92 }