]> Untitled Git - lemmy.git/blob - crates/apub/src/objects/mod.rs
Add cargo feature for building lemmy_api_common with mininum deps (#2243)
[lemmy.git] / crates / apub / src / objects / mod.rs
1 use crate::protocol::{ImageObject, Source};
2 use anyhow::anyhow;
3 use html2md::parse_html;
4 use lemmy_apub_lib::verify::verify_domains_match;
5 use lemmy_utils::{settings::structs::Settings, LemmyError};
6 use url::Url;
7
8 pub mod comment;
9 pub mod community;
10 pub mod instance;
11 pub mod person;
12 pub mod post;
13 pub mod private_message;
14
15 pub(crate) fn read_from_string_or_source(raw: &str, source: &Option<Source>) -> String {
16   if let Some(s) = source {
17     s.content.clone()
18   } else {
19     parse_html(raw)
20   }
21 }
22
23 pub(crate) fn read_from_string_or_source_opt(
24   raw: &Option<String>,
25   source: &Option<Source>,
26 ) -> Option<String> {
27   if let Some(s2) = source {
28     Some(s2.content.clone())
29   } else {
30     raw.as_ref().map(|s| parse_html(s))
31   }
32 }
33
34 pub(crate) fn verify_image_domain_matches(
35   a: &Url,
36   b: &Option<ImageObject>,
37 ) -> Result<(), LemmyError> {
38   if let Some(b) = b {
39     verify_domains_match(a, &b.url)
40   } else {
41     Ok(())
42   }
43 }
44
45 /// When for example a Post is made in a remote community, the community will send it back,
46 /// wrapped in Announce. If we simply receive this like any other federated object, overwrite the
47 /// existing, local Post. In particular, it will set the field local = false, so that the object
48 /// can't be fetched from the Activitypub HTTP endpoint anymore (which only serves local objects).
49 pub(crate) fn verify_is_remote_object(id: &Url) -> Result<(), LemmyError> {
50   let local_domain = Settings::get().get_hostname_without_port()?;
51   if id.domain() == Some(&local_domain) {
52     Err(anyhow!("cant accept local object from remote instance").into())
53   } else {
54     Ok(())
55   }
56 }
57
58 #[cfg(test)]
59 pub(crate) mod tests {
60   use actix::Actor;
61   use diesel::{
62     r2d2::{ConnectionManager, Pool},
63     PgConnection,
64   };
65   use lemmy_api_common::request::build_user_agent;
66   use lemmy_apub_lib::activity_queue::create_activity_queue;
67   use lemmy_db_schema::{
68     source::secret::Secret,
69     utils::{establish_unpooled_connection, get_database_url_from_env},
70   };
71   use lemmy_utils::{
72     rate_limit::{rate_limiter::RateLimiter, RateLimit},
73     settings::structs::Settings,
74     LemmyError,
75   };
76   use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
77   use parking_lot::Mutex;
78   use reqwest::Client;
79   use reqwest_middleware::ClientBuilder;
80   use std::sync::Arc;
81
82   // TODO: would be nice if we didnt have to use a full context for tests.
83   //       or at least write a helper function so this code is shared with main.rs
84   pub(crate) fn init_context() -> LemmyContext {
85     let client = reqwest::Client::new().into();
86     // activity queue isnt used in tests, so worker count makes no difference
87     let queue_manager = create_activity_queue(client, 4);
88     let activity_queue = queue_manager.queue_handle().clone();
89     // call this to run migrations
90     establish_unpooled_connection();
91     let settings = Settings::init().unwrap();
92     let rate_limiter = RateLimit {
93       rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
94       rate_limit_config: settings.rate_limit.to_owned().unwrap_or_default(),
95     };
96     let client = Client::builder()
97       .user_agent(build_user_agent(&settings))
98       .build()
99       .unwrap();
100
101     let client = ClientBuilder::new(client).build();
102     let secret = Secret {
103       id: 0,
104       jwt_secret: "".to_string(),
105     };
106     let db_url = match get_database_url_from_env() {
107       Ok(url) => url,
108       Err(_) => settings.get_database_url(),
109     };
110     let manager = ConnectionManager::<PgConnection>::new(&db_url);
111     let pool = Pool::builder()
112       .max_size(settings.database.pool_size)
113       .build(manager)
114       .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
115     async fn x() -> Result<String, LemmyError> {
116       Ok("".to_string())
117     }
118     let chat_server = ChatServer::startup(
119       pool.clone(),
120       rate_limiter,
121       |_, _, _, _| Box::pin(x()),
122       |_, _, _, _| Box::pin(x()),
123       client.clone(),
124       activity_queue.clone(),
125       settings.clone(),
126       secret.clone(),
127     )
128     .start();
129     LemmyContext::create(pool, chat_server, client, activity_queue, settings, secret)
130   }
131 }