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