1 use crate::protocol::Source;
2 use activitypub_federation::deser::values::MediaTypeMarkdownOrHtml;
4 use html2md::parse_html;
5 use lemmy_utils::{error::LemmyError, settings::structs::Settings};
13 pub mod private_message;
15 pub(crate) fn read_from_string_or_source(
17 media_type: &Option<MediaTypeMarkdownOrHtml>,
18 source: &Option<Source>,
20 if let Some(s) = source {
21 // markdown sent by lemmy in source field
23 } else if media_type == &Some(MediaTypeMarkdownOrHtml::Markdown) {
24 // markdown sent by peertube in content field
27 // otherwise, convert content html to markdown
32 pub(crate) fn read_from_string_or_source_opt(
33 content: &Option<String>,
34 media_type: &Option<MediaTypeMarkdownOrHtml>,
35 source: &Option<Source>,
39 .map(|content| read_from_string_or_source(content, media_type, source))
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())
56 pub(crate) mod tests {
59 r2d2::{ConnectionManager, Pool},
62 use lemmy_api_common::request::build_user_agent;
63 use lemmy_db_schema::{
64 source::secret::Secret,
65 utils::{establish_unpooled_connection, get_database_url_from_env},
69 rate_limit::{rate_limiter::RateLimiter, RateLimit},
70 settings::structs::Settings,
72 use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
73 use parking_lot::Mutex;
75 use reqwest_middleware::ClientBuilder;
78 // TODO: would be nice if we didnt have to use a full context for tests.
79 pub(crate) fn init_context() -> LemmyContext {
80 // call this to run migrations
81 establish_unpooled_connection();
82 let settings = Settings::init().unwrap();
83 let rate_limiter = RateLimit {
84 rate_limiter: Arc::new(Mutex::new(RateLimiter::default())),
85 rate_limit_config: settings.rate_limit.to_owned().unwrap_or_default(),
87 let client = Client::builder()
88 .user_agent(build_user_agent(&settings))
92 let client = ClientBuilder::new(client).build();
95 jwt_secret: "".to_string(),
97 let db_url = match get_database_url_from_env() {
99 Err(_) => settings.get_database_url(),
101 let manager = ConnectionManager::<PgConnection>::new(&db_url);
102 let pool = Pool::builder()
103 .max_size(settings.database.pool_size)
105 .unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
106 async fn x() -> Result<String, LemmyError> {
109 let chat_server = ChatServer::startup(
112 |_, _, _, _| Box::pin(x()),
113 |_, _, _, _| Box::pin(x()),
119 LemmyContext::create(pool, chat_server, client, settings, secret)