This commit is contained in:
parent
80bfd23b4d
commit
7316dd281a
3 changed files with 62 additions and 17 deletions
|
@ -6,11 +6,14 @@ use activitypub_federation::{
|
|||
config::Data,
|
||||
kinds::collection::OrderedCollectionType,
|
||||
protocol::verification::verify_domains_match,
|
||||
traits::{ActivityHandler, Collection, Object},
|
||||
traits::{Collection, Object},
|
||||
};
|
||||
use futures::future::{join_all, try_join_all};
|
||||
use lemmy_api_common::{context::LemmyContext, utils::generate_featured_url};
|
||||
use lemmy_db_schema::{source::post::Post, utils::FETCH_LIMIT_MAX};
|
||||
use lemmy_db_schema::{
|
||||
source::{community::Community, post::Post},
|
||||
utils::FETCH_LIMIT_MAX,
|
||||
};
|
||||
use lemmy_utils::error::LemmyError;
|
||||
use url::Url;
|
||||
|
||||
|
@ -55,35 +58,36 @@ impl Collection for ApubCommunityFeatured {
|
|||
|
||||
async fn from_json(
|
||||
apub: Self::Kind,
|
||||
_owner: &Self::Owner,
|
||||
data: &Data<Self::DataType>,
|
||||
owner: &Self::Owner,
|
||||
context: &Data<Self::DataType>,
|
||||
) -> Result<Self, Self::Error>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
let mut posts = apub.ordered_items;
|
||||
if posts.len() as i64 > FETCH_LIMIT_MAX {
|
||||
posts = posts
|
||||
let mut pages = apub.ordered_items;
|
||||
if pages.len() as i64 > FETCH_LIMIT_MAX {
|
||||
pages = pages
|
||||
.get(0..(FETCH_LIMIT_MAX as usize))
|
||||
.unwrap_or_default()
|
||||
.to_vec();
|
||||
}
|
||||
|
||||
// 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| {
|
||||
let stickied_posts: Vec<Post> = join_all(pages.into_iter().map(|page| {
|
||||
async {
|
||||
// use separate request counter for each item, otherwise there will be problems with
|
||||
// parallel processing
|
||||
let verify = post.verify(data).await;
|
||||
if verify.is_ok() {
|
||||
post.receive(data).await.ok();
|
||||
}
|
||||
ApubPost::verify(&page, &apub.id, context).await?;
|
||||
ApubPost::from_json(page, context).await
|
||||
}
|
||||
}))
|
||||
.await;
|
||||
.await
|
||||
// ignore any failed or unparseable items
|
||||
.into_iter()
|
||||
.filter_map(|p| p.ok().map(|p| p.0))
|
||||
.collect();
|
||||
|
||||
Community::set_featured_posts(owner.id, stickied_posts, &mut context.pool()).await?;
|
||||
|
||||
// This return value is unused, so just set an empty vec
|
||||
Ok(ApubCommunityFeatured(()))
|
||||
|
|
|
@ -166,7 +166,7 @@ impl Object for ApubCommunity {
|
|||
moderators_url: group.attributed_to.clone().map(Into::into),
|
||||
posting_restricted_to_mods: group.posting_restricted_to_mods,
|
||||
instance_id,
|
||||
featured_url: group.featured.map(Into::into),
|
||||
featured_url: group.featured.clone().map(Into::into),
|
||||
..Default::default()
|
||||
};
|
||||
let languages =
|
||||
|
@ -184,6 +184,9 @@ impl Object for ApubCommunity {
|
|||
spawn_try_task(async move {
|
||||
group.outbox.dereference(&community_, &context_).await?;
|
||||
group.followers.dereference(&community_, &context_).await?;
|
||||
if let Some(featured) = group.featured {
|
||||
featured.dereference(&community_, &context_).await?;
|
||||
}
|
||||
if let Some(moderators) = group.attributed_to {
|
||||
moderators.dereference(&community_, &context_).await?;
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ use crate::{
|
|||
CommunityPersonBanForm,
|
||||
CommunityUpdateForm,
|
||||
},
|
||||
post::Post,
|
||||
},
|
||||
traits::{ApubActor, Bannable, Crud, Followable, Joinable},
|
||||
utils::{functions::lower, get_conn, DbPool},
|
||||
|
@ -27,6 +28,7 @@ use diesel::{
|
|||
result::Error,
|
||||
select,
|
||||
sql_types,
|
||||
update,
|
||||
ExpressionMethods,
|
||||
NullableExpressionMethods,
|
||||
QueryDsl,
|
||||
|
@ -137,6 +139,42 @@ impl Community {
|
|||
}
|
||||
Err(diesel::NotFound)
|
||||
}
|
||||
|
||||
pub async fn set_featured_posts(
|
||||
community_id: CommunityId,
|
||||
posts: Vec<Post>,
|
||||
pool: &mut DbPool<'_>,
|
||||
) -> Result<(), Error> {
|
||||
use crate::schema::post;
|
||||
let conn = &mut get_conn(pool).await?;
|
||||
for p in &posts {
|
||||
debug_assert!(p.community_id == community_id);
|
||||
}
|
||||
conn
|
||||
.build_transaction()
|
||||
.run(|conn| {
|
||||
Box::pin(async move {
|
||||
update(
|
||||
// first remove all existing featured posts
|
||||
post::table,
|
||||
)
|
||||
.filter(post::dsl::community_id.eq(community_id))
|
||||
.set(post::dsl::featured_community.eq(false))
|
||||
.execute(conn)
|
||||
.await?;
|
||||
|
||||
// then mark the given posts as featured
|
||||
let post_ids: Vec<_> = posts.iter().map(|p| p.id).collect();
|
||||
update(post::table)
|
||||
.filter(post::dsl::id.eq_any(post_ids))
|
||||
.set(post::dsl::featured_community.eq(true))
|
||||
.execute(conn)
|
||||
.await?;
|
||||
Ok(())
|
||||
}) as _
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
impl CommunityModerator {
|
||||
|
|
Loading…
Reference in a new issue