-pub enum EndpointType {
- Community,
- Person,
- Post,
- Comment,
- PrivateMessage,
-}
-
-/// Generates an apub endpoint for a given domain, IE xyz.tld
-fn generate_apub_endpoint_for_domain(
- endpoint_type: EndpointType,
- name: &str,
- domain: &str,
-) -> Result<DbUrl, ParseError> {
- let point = match endpoint_type {
- EndpointType::Community => "c",
- EndpointType::Person => "u",
- EndpointType::Post => "post",
- EndpointType::Comment => "comment",
- EndpointType::PrivateMessage => "private_message",
- };
-
- Ok(Url::parse(&format!("{}/{}/{}", domain, point, name))?.into())
-}
-
-/// Generates the ActivityPub ID for a given object type and ID.
-pub fn generate_apub_endpoint(
- endpoint_type: EndpointType,
- name: &str,
- protocol_and_hostname: &str,
-) -> Result<DbUrl, ParseError> {
- generate_apub_endpoint_for_domain(endpoint_type, name, protocol_and_hostname)
-}
-
-pub fn generate_followers_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
- Ok(Url::parse(&format!("{}/followers", actor_id))?.into())
-}
-
-pub fn generate_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
- Ok(Url::parse(&format!("{}/inbox", actor_id))?.into())
-}
-
-pub fn generate_shared_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, LemmyError> {
- let actor_id: Url = actor_id.clone().into();
- let url = format!(
- "{}://{}{}/inbox",
- &actor_id.scheme(),
- &actor_id.host_str().context(location_info!())?,
- if let Some(port) = actor_id.port() {
- format!(":{}", port)
- } else {
- "".to_string()
- },
- );
- Ok(Url::parse(&url)?.into())
-}
-
-pub fn generate_outbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
- Ok(Url::parse(&format!("{}/outbox", actor_id))?.into())
-}
-
-fn generate_moderators_url(community_id: &DbUrl) -> Result<DbUrl, LemmyError> {
- Ok(Url::parse(&format!("{}/moderators", community_id))?.into())
-}
-
-/// Takes in a shortname of the type dessalines@xyz.tld or dessalines (assumed to be local), and outputs the actor id.
-/// Used in the API for communities and users.
-pub fn build_actor_id_from_shortname(
- endpoint_type: EndpointType,
- short_name: &str,
- settings: &Settings,
-) -> Result<DbUrl, ParseError> {
- let split = short_name.split('@').collect::<Vec<&str>>();
-
- let name = split[0];
-
- // If there's no @, its local
- let domain = if split.len() == 1 {
- settings.get_protocol_and_hostname()
- } else {
- format!("{}://{}", settings.get_protocol_string(), split[1])
- };
-
- generate_apub_endpoint_for_domain(endpoint_type, name, &domain)
-}
-
-/// Store a sent or received activity in the database, for logging purposes. These records are not
-/// persistent.
-async fn insert_activity<T>(
- ap_id: &Url,
- activity: T,
- local: bool,
- sensitive: bool,
- pool: &DbPool,
-) -> Result<(), LemmyError>
-where
- T: Serialize + std::fmt::Debug + Send + 'static,
-{
- let ap_id = ap_id.to_owned().into();
- blocking(pool, move |conn| {
- Activity::insert(conn, ap_id, &activity, local, sensitive)
- })
- .await??;
- Ok(())
+pub(crate) async fn local_site_data_cached(
+ pool: &mut DbPool<'_>,
+) -> LemmyResult<Arc<LocalSiteData>> {
+ static CACHE: Lazy<Cache<(), Arc<LocalSiteData>>> = Lazy::new(|| {
+ Cache::builder()
+ .max_capacity(1)
+ .time_to_live(BLOCKLIST_CACHE_DURATION)
+ .build()
+ });
+ Ok(
+ CACHE
+ .try_get_with((), async {
+ let (local_site, allowed_instances, blocked_instances) =
+ lemmy_db_schema::try_join_with_pool!(pool => (
+ // LocalSite may be missing
+ |pool| async {
+ Ok(LocalSite::read(pool).await.ok())
+ },
+ Instance::allowlist,
+ Instance::blocklist
+ ))?;
+
+ Ok::<_, diesel::result::Error>(Arc::new(LocalSiteData {
+ local_site,
+ allowed_instances,
+ blocked_instances,
+ }))
+ })
+ .await?,
+ )