]> Untitled Git - lemmy.git/blob - crates/apub/src/objects/mod.rs
Federation: dont overwrite local object from Announce activity (#2232)
[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_apub_lib::activity_queue::create_activity_queue;
66   use lemmy_db_schema::{
67     establish_unpooled_connection,
68     get_database_url_from_env,
69     source::secret::Secret,
70   };
71   use lemmy_utils::{
72     rate_limit::{rate_limiter::RateLimiter, RateLimit},
73     request::build_user_agent,
74     settings::structs::Settings,
75     LemmyError,
76   };
77   use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
78   use parking_lot::Mutex;
79   use reqwest::Client;
80   use reqwest_middleware::ClientBuilder;
81   use std::sync::Arc;
82
83   // TODO: would be nice if we didnt have to use a full context for tests.
84   //       or at least write a helper function so this code is shared with main.rs
85   pub(crate) fn init_context() -> LemmyContext {
86     let client = reqwest::Client::new().into();
87     // activity queue isnt used in tests, so worker count makes no difference
88     let queue_manager = create_activity_queue(client, 4);
89     let activity_queue = queue_manager.queue_handle().clone();
90     // call this to run migrations
91     establish_unpooled_connection();
92     let settings = Settings::init().unwrap();
93     let rate_limiter = RateLimit {
94       rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
95       rate_limit_config: settings.rate_limit.to_owned().unwrap_or_default(),
96     };
97     let client = Client::builder()
98       .user_agent(build_user_agent(&settings))
99       .build()
100       .unwrap();
101
102     let client = ClientBuilder::new(client).build();
103     let secret = Secret {
104       id: 0,
105       jwt_secret: "".to_string(),
106     };
107     let db_url = match get_database_url_from_env() {
108       Ok(url) => url,
109       Err(_) => settings.get_database_url(),
110     };
111     let manager = ConnectionManager::<PgConnection>::new(&db_url);
112     let pool = Pool::builder()
113       .max_size(settings.database.pool_size)
114       .build(manager)
115       .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
116     async fn x() -> Result<String, LemmyError> {
117       Ok("".to_string())
118     }
119     let chat_server = ChatServer::startup(
120       pool.clone(),
121       rate_limiter,
122       |_, _, _, _| Box::pin(x()),
123       |_, _, _, _| Box::pin(x()),
124       client.clone(),
125       activity_queue.clone(),
126       settings.clone(),
127       secret.clone(),
128     )
129     .start();
130     LemmyContext::create(pool, chat_server, client, activity_queue, settings, secret)
131   }
132 }