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