]> Untitled Git - lemmy.git/blob - crates/apub/src/objects/mod.rs
Moving settings to Database. (#2492)
[lemmy.git] / crates / apub / src / objects / mod.rs
1 use crate::protocol::Source;
2 use activitypub_federation::deser::values::MediaTypeMarkdownOrHtml;
3 use anyhow::anyhow;
4 use html2md::parse_html;
5 use lemmy_utils::{error::LemmyError, settings::structs::Settings};
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, settings: &Settings) -> Result<(), LemmyError> {
47   let local_domain = settings.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 anyhow::anyhow;
59   use diesel::{
60     r2d2::{ConnectionManager, Pool},
61     PgConnection,
62   };
63   use lemmy_api_common::request::build_user_agent;
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     error::LemmyError,
70     rate_limit::{rate_limiter::RateLimiter, RateLimit, RateLimitConfig},
71     settings::SETTINGS,
72   };
73   use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
74   use reqwest::{Client, Request, Response};
75   use reqwest_middleware::{ClientBuilder, Middleware, Next};
76   use std::sync::{Arc, Mutex};
77   use task_local_extensions::Extensions;
78
79   struct BlockedMiddleware;
80
81   /// A reqwest middleware which blocks all requests
82   #[async_trait::async_trait]
83   impl Middleware for BlockedMiddleware {
84     async fn handle(
85       &self,
86       _req: Request,
87       _extensions: &mut Extensions,
88       _next: Next<'_>,
89     ) -> reqwest_middleware::Result<Response> {
90       Err(anyhow!("Network requests not allowed").into())
91     }
92   }
93
94   // TODO: would be nice if we didnt have to use a full context for tests.
95   pub(crate) fn init_context() -> LemmyContext {
96     // call this to run migrations
97     establish_unpooled_connection();
98     let settings = SETTINGS.to_owned();
99     let client = Client::builder()
100       .user_agent(build_user_agent(&settings))
101       .build()
102       .unwrap();
103
104     let client = ClientBuilder::new(client).with(BlockedMiddleware).build();
105     let secret = Secret {
106       id: 0,
107       jwt_secret: "".to_string(),
108     };
109     let db_url = match get_database_url_from_env() {
110       Ok(url) => url,
111       Err(_) => settings.get_database_url(),
112     };
113     let manager = ConnectionManager::<PgConnection>::new(&db_url);
114     let pool = Pool::builder()
115       .max_size(settings.database.pool_size)
116       .build(manager)
117       .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
118     async fn x() -> Result<String, LemmyError> {
119       Ok("".to_string())
120     }
121
122     let rate_limit_config = RateLimitConfig::builder().build();
123
124     let rate_limiter = RateLimit {
125       rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
126       rate_limit_config,
127     };
128
129     let chat_server = ChatServer::startup(
130       pool.clone(),
131       rate_limiter,
132       |_, _, _, _| Box::pin(x()),
133       |_, _, _, _| Box::pin(x()),
134       client.clone(),
135       settings.clone(),
136       secret.clone(),
137     )
138     .start();
139     LemmyContext::create(pool, chat_server, client, settings, secret)
140   }
141 }