- expected_domain: Url,
- request_counter: &mut i32,
- _mod_action_allowed: bool,
- ) -> Result<Self, LemmyError> {
- let moderator_uris = fetch_community_mods(context, group, request_counter).await?;
- let creator = if let Some(creator_uri) = moderator_uris.first() {
- get_or_fetch_and_upsert_person(creator_uri, context, request_counter)
- } else {
- // NOTE: code for compatibility with lemmy v0.9.9
- let creator_uri = group
- .inner
- .attributed_to()
- .map(|a| a.as_many())
- .flatten()
- .map(|a| a.first())
- .flatten()
- .map(|a| a.as_xsd_any_uri())
- .flatten()
- .context(location_info!())?;
- get_or_fetch_and_upsert_person(creator_uri, context, request_counter)
- }
- .await?;
-
- let name = group
- .inner
- .preferred_username()
- .context(location_info!())?
- .to_string();
- let title = group
- .inner
- .name()
- .context(location_info!())?
- .as_one()
- .context(location_info!())?
- .as_xsd_string()
- .context(location_info!())?
- .to_string();
-
- let description = get_source_markdown_value(group)?;
-
- check_slurs(&name)?;
- check_slurs(&title)?;
- check_slurs_opt(&description)?;
-
- let icon = match group.icon() {
- Some(any_image) => Some(
- Image::from_any_base(any_image.as_one().context(location_info!())?.clone())
- .context(location_info!())?
- .context(location_info!())?
- .url()
- .context(location_info!())?
- .as_single_xsd_any_uri()
- .map(|u| u.to_owned().into()),
- ),
- None => None,
- };
- let banner = match group.image() {
- Some(any_image) => Some(
- Image::from_any_base(any_image.as_one().context(location_info!())?.clone())
- .context(location_info!())?
- .context(location_info!())?
- .url()
- .context(location_info!())?
- .as_single_xsd_any_uri()
- .map(|u| u.to_owned().into()),
- ),
- None => None,
- };
- let shared_inbox = group
- .inner
- .endpoints()?
- .map(|e| e.shared_inbox)
- .flatten()
- .map(|s| s.to_owned().into());
-
- Ok(CommunityForm {
- name,
- title,
- description,
- creator_id: creator.id,
- removed: None,
- published: group.inner.published().map(|u| u.to_owned().naive_local()),
- updated: group.inner.updated().map(|u| u.to_owned().naive_local()),
- deleted: None,
- nsfw: Some(group.ext_one.sensitive.unwrap_or(false)),
- actor_id: Some(check_object_domain(group, expected_domain)?),
- local: Some(false),
- private_key: None,
- public_key: Some(group.ext_two.to_owned().public_key.public_key_pem),
- last_refreshed_at: Some(naive_now()),
- icon,
- banner,
- followers_url: Some(
- group
- .inner
- .followers()?
- .context(location_info!())?
- .to_owned()
- .into(),
- ),
- inbox_url: Some(group.inner.inbox()?.to_owned().into()),
- shared_inbox_url: Some(shared_inbox),
- })
+ ) -> Result<Vec<Url>, LemmyError> {
+ let id = self.id;
+
+ let local_site_data = local_site_data_cached(&mut context.pool()).await?;
+ let follows =
+ CommunityFollowerView::get_community_follower_inboxes(&mut context.pool(), id).await?;
+ let inboxes: Vec<Url> = follows
+ .into_iter()
+ .map(Into::into)
+ .filter(|inbox: &Url| inbox.host_str() != Some(&context.settings().hostname))
+ // Don't send to blocked instances
+ .filter(|inbox| check_apub_id_valid(inbox, &local_site_data).is_ok())
+ .collect();
+
+ Ok(inboxes)
+ }
+}
+
+#[cfg(test)]
+pub(crate) mod tests {
+ #![allow(clippy::unwrap_used)]
+ #![allow(clippy::indexing_slicing)]
+
+ use super::*;
+ use crate::{
+ objects::{instance::tests::parse_lemmy_instance, tests::init_context},
+ protocol::tests::file_to_json_object,
+ };
+ use activitypub_federation::fetch::collection_id::CollectionId;
+ use lemmy_db_schema::{source::site::Site, traits::Crud};
+ use serial_test::serial;
+
+ pub(crate) async fn parse_lemmy_community(context: &Data<LemmyContext>) -> ApubCommunity {
+ // use separate counter so this doesnt affect tests
+ let context2 = context.reset_request_count();
+ let mut json: Group = file_to_json_object("assets/lemmy/objects/group.json").unwrap();
+ // change these links so they dont fetch over the network
+ json.attributed_to = None;
+ json.outbox =
+ CollectionId::parse("https://enterprise.lemmy.ml/c/tenforward/not_outbox").unwrap();
+
+ let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward").unwrap();
+ ApubCommunity::verify(&json, &url, &context2).await.unwrap();
+ let community = ApubCommunity::from_json(json, &context2).await.unwrap();
+ // this makes one requests to the (intentionally broken) outbox collection
+ assert_eq!(context2.request_count(), 1);
+ community
+ }
+
+ #[tokio::test]
+ #[serial]
+ async fn test_parse_lemmy_community() {
+ let context = init_context().await;
+ let site = parse_lemmy_instance(&context).await;
+ let community = parse_lemmy_community(&context).await;
+
+ assert_eq!(community.title, "Ten Forward");
+ assert!(!community.local);
+ assert_eq!(community.description.as_ref().unwrap().len(), 132);
+
+ Community::delete(&mut context.pool(), community.id)
+ .await
+ .unwrap();
+ Site::delete(&mut context.pool(), site.id).await.unwrap();