2 pub(crate) mod activity_lists;
3 pub(crate) mod collections;
12 extern crate lazy_static;
14 use crate::fetcher::post_or_comment::PostOrComment;
15 use anyhow::{anyhow, Context};
16 use lemmy_api_common::blocking;
17 use lemmy_apub_lib::webfinger::{webfinger_resolve_actor, WebfingerType};
18 use lemmy_db_schema::{newtypes::DbUrl, source::activity::Activity, DbPool};
19 use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
20 use lemmy_websocket::LemmyContext;
23 use url::{ParseError, Url};
25 /// Checks if the ID is allowed for sending or receiving.
27 /// In particular, it checks for:
28 /// - federation being enabled (if its disabled, only local URLs are allowed)
29 /// - the correct scheme (either http or https)
30 /// - URL being in the allowlist (if it is active)
31 /// - URL not being in the blocklist (if it is active)
33 pub(crate) fn check_is_apub_id_valid(
35 use_strict_allowlist: bool,
37 ) -> Result<(), LemmyError> {
38 let domain = apub_id.domain().context(location_info!())?.to_string();
39 let local_instance = settings.get_hostname_without_port()?;
41 if !settings.federation.enabled {
42 return if domain == local_instance {
47 "Trying to connect with {}, but federation is disabled",
55 let host = apub_id.host_str().context(location_info!())?;
56 let host_as_ip = host.parse::<IpAddr>();
57 if host == "localhost" || host_as_ip.is_ok() {
58 return Err(anyhow!("invalid hostname {}: {}", host, apub_id).into());
61 if apub_id.scheme() != settings.get_protocol_string() {
62 return Err(anyhow!("invalid apub id scheme {}: {}", apub_id.scheme(), apub_id).into());
65 // TODO: might be good to put the part above in one method, and below in another
66 // (which only gets called in apub::objects)
67 // -> no that doesnt make sense, we still need the code below for blocklist and strict allowlist
68 if let Some(blocked) = settings.to_owned().federation.blocked_instances {
69 if blocked.contains(&domain) {
70 return Err(anyhow!("{} is in federation blocklist", domain).into());
74 if let Some(mut allowed) = settings.to_owned().federation.allowed_instances {
75 // Only check allowlist if this is a community, or strict allowlist is enabled.
76 let strict_allowlist = settings.to_owned().federation.strict_allowlist;
77 if use_strict_allowlist || strict_allowlist {
78 // need to allow this explicitly because apub receive might contain objects from our local
80 allowed.push(local_instance);
82 if !allowed.contains(&domain) {
83 return Err(anyhow!("{} not in federation allowlist", domain).into());
91 pub enum EndpointType {
99 /// Generates an apub endpoint for a given domain, IE xyz.tld
100 pub fn generate_local_apub_endpoint(
101 endpoint_type: EndpointType,
104 ) -> Result<DbUrl, ParseError> {
105 let point = match endpoint_type {
106 EndpointType::Community => "c",
107 EndpointType::Person => "u",
108 EndpointType::Post => "post",
109 EndpointType::Comment => "comment",
110 EndpointType::PrivateMessage => "private_message",
113 Ok(Url::parse(&format!("{}/{}/{}", domain, point, name))?.into())
116 pub fn generate_followers_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
117 Ok(Url::parse(&format!("{}/followers", actor_id))?.into())
120 pub fn generate_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
121 Ok(Url::parse(&format!("{}/inbox", actor_id))?.into())
124 pub fn generate_shared_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, LemmyError> {
125 let actor_id: Url = actor_id.clone().into();
129 &actor_id.host_str().context(location_info!())?,
130 if let Some(port) = actor_id.port() {
136 Ok(Url::parse(&url)?.into())
139 pub fn generate_outbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
140 Ok(Url::parse(&format!("{}/outbox", actor_id))?.into())
143 fn generate_moderators_url(community_id: &DbUrl) -> Result<DbUrl, LemmyError> {
144 Ok(Url::parse(&format!("{}/moderators", community_id))?.into())
147 /// Takes in a shortname of the type dessalines@xyz.tld or dessalines (assumed to be local), and outputs the actor id.
148 /// Used in the API for communities and users.
149 pub async fn get_actor_id_from_name(
150 webfinger_type: WebfingerType,
152 context: &LemmyContext,
153 ) -> Result<DbUrl, LemmyError> {
154 let split = short_name.split('@').collect::<Vec<&str>>();
158 // If there's no @, its local
159 if split.len() == 1 {
160 let domain = context.settings().get_protocol_and_hostname();
161 let endpoint_type = match webfinger_type {
162 WebfingerType::Person => EndpointType::Person,
163 WebfingerType::Group => EndpointType::Community,
165 Ok(generate_local_apub_endpoint(endpoint_type, name, &domain)?)
167 let protocol = context.settings().get_protocol_string();
169 webfinger_resolve_actor(name, split[1], webfinger_type, context.client(), protocol)
176 /// Store a sent or received activity in the database, for logging purposes. These records are not
178 async fn insert_activity<T>(
184 ) -> Result<(), LemmyError>
186 T: Serialize + std::fmt::Debug + Send + 'static,
188 let ap_id = ap_id.to_owned().into();
189 blocking(pool, move |conn| {
190 Activity::insert(conn, ap_id, &activity, local, sensitive)