]> Untitled Git - lemmy.git/blob - crates/apub/src/objects/mod.rs
Cache & Optimize Woodpecker CI (#3450)
[lemmy.git] / crates / apub / src / objects / mod.rs
1 use crate::protocol::Source;
2 use activitypub_federation::protocol::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   #![allow(clippy::unwrap_used)]
58   #![allow(clippy::indexing_slicing)]
59
60   use activitypub_federation::config::{Data, FederationConfig};
61   use anyhow::anyhow;
62   use lemmy_api_common::{context::LemmyContext, request::build_user_agent};
63   use lemmy_db_schema::{source::secret::Secret, utils::build_db_pool_for_tests};
64   use lemmy_utils::{
65     rate_limit::{RateLimitCell, RateLimitConfig},
66     settings::SETTINGS,
67   };
68   use reqwest::{Client, Request, Response};
69   use reqwest_middleware::{ClientBuilder, Middleware, Next};
70   use task_local_extensions::Extensions;
71
72   struct BlockedMiddleware;
73
74   /// A reqwest middleware which blocks all requests
75   #[async_trait::async_trait]
76   impl Middleware for BlockedMiddleware {
77     async fn handle(
78       &self,
79       _req: Request,
80       _extensions: &mut Extensions,
81       _next: Next<'_>,
82     ) -> reqwest_middleware::Result<Response> {
83       Err(anyhow!("Network requests not allowed").into())
84     }
85   }
86
87   // TODO: would be nice if we didnt have to use a full context for tests.
88   pub(crate) async fn init_context() -> Data<LemmyContext> {
89     // call this to run migrations
90     let pool = build_db_pool_for_tests().await;
91
92     let settings = SETTINGS.clone();
93     let client = Client::builder()
94       .user_agent(build_user_agent(&settings))
95       .build()
96       .unwrap();
97
98     let client = ClientBuilder::new(client).with(BlockedMiddleware).build();
99     let secret = Secret {
100       id: 0,
101       jwt_secret: String::new(),
102     };
103
104     let rate_limit_config = RateLimitConfig::builder().build();
105     let rate_limit_cell = RateLimitCell::new(rate_limit_config).await;
106
107     let context = LemmyContext::create(pool, client, secret, rate_limit_cell.clone());
108     let config = FederationConfig::builder()
109       .domain("example.com")
110       .app_data(context)
111       .build()
112       .await
113       .unwrap();
114     config.to_request_data()
115   }
116 }