2023-02-18 14:50:28 +00:00
|
|
|
use crate::{
|
2023-03-21 15:03:05 +00:00
|
|
|
objects::{community::ApubCommunity, post::ApubPost},
|
2023-02-18 14:50:28 +00:00
|
|
|
protocol::collections::group_featured::GroupFeatured,
|
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
2023-03-21 15:03:05 +00:00
|
|
|
config::Data,
|
|
|
|
kinds::collection::OrderedCollectionType,
|
|
|
|
protocol::verification::verify_domains_match,
|
|
|
|
traits::{ActivityHandler, Collection, Object},
|
2023-02-18 14:50:28 +00:00
|
|
|
};
|
|
|
|
use futures::future::{join_all, try_join_all};
|
2023-03-21 15:03:05 +00:00
|
|
|
use lemmy_api_common::{context::LemmyContext, utils::generate_featured_url};
|
2023-02-18 14:50:28 +00:00
|
|
|
use lemmy_db_schema::{source::post::Post, utils::FETCH_LIMIT_MAX};
|
|
|
|
use lemmy_utils::error::LemmyError;
|
|
|
|
use url::Url;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub(crate) struct ApubCommunityFeatured(Vec<ApubPost>);
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
|
|
|
impl Collection for ApubCommunityFeatured {
|
|
|
|
type Owner = ApubCommunity;
|
|
|
|
type DataType = LemmyContext;
|
|
|
|
type Kind = GroupFeatured;
|
2023-02-18 14:50:28 +00:00
|
|
|
type Error = LemmyError;
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn read_local(
|
|
|
|
owner: &Self::Owner,
|
|
|
|
data: &Data<Self::DataType>,
|
|
|
|
) -> Result<Self::Kind, Self::Error> {
|
|
|
|
let ordered_items = try_join_all(
|
2023-07-11 13:09:59 +00:00
|
|
|
Post::list_featured_for_community(&mut data.pool(), owner.id)
|
2023-02-18 14:50:28 +00:00
|
|
|
.await?
|
|
|
|
.into_iter()
|
2023-03-21 15:03:05 +00:00
|
|
|
.map(ApubPost::from)
|
|
|
|
.map(|p| p.into_json(data)),
|
|
|
|
)
|
|
|
|
.await?;
|
2023-02-18 14:50:28 +00:00
|
|
|
Ok(GroupFeatured {
|
|
|
|
r#type: OrderedCollectionType::OrderedCollection,
|
2023-03-21 15:03:05 +00:00
|
|
|
id: generate_featured_url(&owner.actor_id)?.into(),
|
2023-02-18 14:50:28 +00:00
|
|
|
total_items: ordered_items.len() as i32,
|
|
|
|
ordered_items,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn verify(
|
2023-03-21 15:03:05 +00:00
|
|
|
apub: &Self::Kind,
|
2023-02-18 14:50:28 +00:00
|
|
|
expected_domain: &Url,
|
2023-03-21 15:03:05 +00:00
|
|
|
_data: &Data<Self::DataType>,
|
2023-02-18 14:50:28 +00:00
|
|
|
) -> Result<(), Self::Error> {
|
|
|
|
verify_domains_match(expected_domain, &apub.id)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn from_json(
|
|
|
|
apub: Self::Kind,
|
|
|
|
_owner: &Self::Owner,
|
|
|
|
data: &Data<Self::DataType>,
|
2023-02-18 14:50:28 +00:00
|
|
|
) -> Result<Self, Self::Error>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
let mut posts = apub.ordered_items;
|
|
|
|
if posts.len() as i64 > FETCH_LIMIT_MAX {
|
2023-02-28 11:34:50 +00:00
|
|
|
posts = posts
|
|
|
|
.get(0..(FETCH_LIMIT_MAX as usize))
|
|
|
|
.unwrap_or_default()
|
|
|
|
.to_vec();
|
2023-02-18 14:50:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We intentionally ignore errors here. This is because the outbox might contain posts from old
|
|
|
|
// Lemmy versions, or from other software which we cant parse. In that case, we simply skip the
|
|
|
|
// item and only parse the ones that work.
|
|
|
|
// process items in parallel, to avoid long delay from fetch_site_metadata() and other processing
|
|
|
|
join_all(posts.into_iter().map(|post| {
|
|
|
|
async {
|
|
|
|
// use separate request counter for each item, otherwise there will be problems with
|
|
|
|
// parallel processing
|
2023-03-21 15:03:05 +00:00
|
|
|
let verify = post.verify(data).await;
|
2023-02-18 14:50:28 +00:00
|
|
|
if verify.is_ok() {
|
2023-03-21 15:03:05 +00:00
|
|
|
post.receive(data).await.ok();
|
2023-02-18 14:50:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// This return value is unused, so just set an empty vec
|
|
|
|
Ok(ApubCommunityFeatured(Vec::new()))
|
|
|
|
}
|
|
|
|
}
|