1 use crate::protocol::{ImageObject, Source};
3 use html2md::parse_html;
4 use lemmy_apub_lib::verify::verify_domains_match;
5 use lemmy_utils::{settings::structs::Settings, LemmyError};
13 pub mod private_message;
15 pub(crate) fn read_from_string_or_source(raw: &str, source: &Option<Source>) -> String {
16 if let Some(s) = source {
23 pub(crate) fn read_from_string_or_source_opt(
25 source: &Option<Source>,
27 if let Some(s2) = source {
28 Some(s2.content.clone())
30 raw.as_ref().map(|s| parse_html(s))
34 pub(crate) fn verify_image_domain_matches(
36 b: &Option<ImageObject>,
37 ) -> Result<(), LemmyError> {
39 verify_domains_match(a, &b.url)
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())
59 pub(crate) mod tests {
62 r2d2::{ConnectionManager, Pool},
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,
72 rate_limit::{rate_limiter::RateLimiter, RateLimit},
73 request::build_user_agent,
74 settings::structs::Settings,
77 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
78 use parking_lot::Mutex;
80 use reqwest_middleware::ClientBuilder;
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(),
97 let client = Client::builder()
98 .user_agent(build_user_agent(&settings))
102 let client = ClientBuilder::new(client).build();
103 let secret = Secret {
105 jwt_secret: "".to_string(),
107 let db_url = match get_database_url_from_env() {
109 Err(_) => settings.get_database_url(),
111 let manager = ConnectionManager::<PgConnection>::new(&db_url);
112 let pool = Pool::builder()
113 .max_size(settings.database.pool_size)
115 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
116 async fn x() -> Result<String, LemmyError> {
119 let chat_server = ChatServer::startup(
122 |_, _, _, _| Box::pin(x()),
123 |_, _, _, _| Box::pin(x()),
125 activity_queue.clone(),
130 LemmyContext::create(pool, chat_server, client, activity_queue, settings, secret)