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, 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())
56 pub(crate) mod tests {
59 use lemmy_api_common::{
60 context::LemmyContext,
61 request::build_user_agent,
62 websocket::chat_server::ChatServer,
64 use lemmy_db_schema::{source::secret::Secret, utils::build_db_pool_for_tests};
67 rate_limit::{RateLimitCell, RateLimitConfig},
70 use reqwest::{Client, Request, Response};
71 use reqwest_middleware::{ClientBuilder, Middleware, Next};
72 use task_local_extensions::Extensions;
74 struct BlockedMiddleware;
76 /// A reqwest middleware which blocks all requests
77 #[async_trait::async_trait]
78 impl Middleware for BlockedMiddleware {
82 _extensions: &mut Extensions,
84 ) -> reqwest_middleware::Result<Response> {
85 Err(anyhow!("Network requests not allowed").into())
89 // TODO: would be nice if we didnt have to use a full context for tests.
90 pub(crate) async fn init_context() -> LemmyContext {
91 async fn x() -> Result<String, LemmyError> {
94 // call this to run migrations
95 let pool = build_db_pool_for_tests().await;
97 let settings = SETTINGS.clone();
98 let client = Client::builder()
99 .user_agent(build_user_agent(&settings))
103 let client = ClientBuilder::new(client).with(BlockedMiddleware).build();
104 let secret = Secret {
106 jwt_secret: String::new(),
109 let rate_limit_config = RateLimitConfig::builder().build();
110 let rate_limit_cell = RateLimitCell::new(rate_limit_config).await;
112 let chat_server = ChatServer::startup(
114 |_, _, _, _| Box::pin(x()),
115 |_, _, _, _| Box::pin(x()),
116 |_, _, _, _| Box::pin(x()),
120 rate_limit_cell.clone(),
123 LemmyContext::create(
129 rate_limit_cell.clone(),