use crate::{
- activities::{
- community::announce::{AnnouncableActivities, AnnounceActivity},
- extract_community,
- following::{follow::FollowCommunity, undo::UndoFollowCommunity},
- report::Report,
+ activity_lists::GroupInboxActivities,
+ collections::{
+ community_featured::ApubCommunityFeatured,
+ community_moderators::ApubCommunityModerators,
+ community_outbox::ApubCommunityOutbox,
},
- context::lemmy_context,
- generate_moderators_url,
- generate_outbox_url,
- http::{
- create_apub_response,
- create_apub_tombstone_response,
- payload_to_string,
- receive_activity,
- },
- objects::ToApub,
-};
-use activitystreams::{
- base::{AnyBase, BaseExt},
- collection::{CollectionExt, OrderedCollection, UnorderedCollection},
- url::Url,
+ http::{create_apub_response, create_apub_tombstone_response},
+ objects::{community::ApubCommunity, person::ApubPerson},
+ protocol::collections::group_followers::GroupFollowers,
};
-use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
-use lemmy_api_common::blocking;
-use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler};
-use lemmy_db_schema::source::{activity::Activity, community::Community};
-use lemmy_db_views_actor::{
- community_follower_view::CommunityFollowerView,
- community_moderator_view::CommunityModeratorView,
+use activitypub_federation::{
+ actix_web::inbox::receive_activity,
+ config::Data,
+ protocol::context::WithContext,
+ traits::{Collection, Object},
};
-use lemmy_utils::LemmyError;
-use lemmy_websocket::LemmyContext;
-use log::trace;
-use serde::{Deserialize, Serialize};
+use actix_web::{web, web::Bytes, HttpRequest, HttpResponse};
+use lemmy_api_common::context::LemmyContext;
+use lemmy_db_schema::{source::community::Community, traits::ApubActor};
+use lemmy_utils::error::{LemmyError, LemmyErrorType};
+use serde::Deserialize;
#[derive(Deserialize)]
pub(crate) struct CommunityQuery {
}
/// Return the ActivityPub json representation of a local community over HTTP.
+#[tracing::instrument(skip_all)]
pub(crate) async fn get_apub_community_http(
info: web::Path<CommunityQuery>,
- context: web::Data<LemmyContext>,
-) -> Result<HttpResponse<Body>, LemmyError> {
- let community = blocking(context.pool(), move |conn| {
- Community::read_from_name(conn, &info.community_name)
- })
- .await??;
+ context: Data<LemmyContext>,
+) -> Result<HttpResponse, LemmyError> {
+ let community: ApubCommunity =
+ Community::read_from_name(&mut context.pool(), &info.community_name, true)
+ .await?
+ .into();
- if !community.deleted {
- let apub = community.to_apub(context.pool()).await?;
+ if !community.deleted && !community.removed {
+ let apub = community.into_json(&context).await?;
- Ok(create_apub_response(&apub))
+ create_apub_response(&apub)
} else {
- Ok(create_apub_tombstone_response(&community.to_tombstone()?))
+ create_apub_tombstone_response(community.actor_id.clone())
}
}
-#[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
-#[serde(untagged)]
-#[activity_handler(LemmyContext)]
-pub enum GroupInboxActivities {
- FollowCommunity(FollowCommunity),
- UndoFollowCommunity(UndoFollowCommunity),
- AnnouncableActivities(AnnouncableActivities),
- Report(Report),
-}
-
/// Handler for all incoming receive to community inboxes.
+#[tracing::instrument(skip_all)]
pub async fn community_inbox(
request: HttpRequest,
- payload: Payload,
- _path: web::Path<String>,
- context: web::Data<LemmyContext>,
-) -> Result<HttpResponse, LemmyError> {
- let unparsed = payload_to_string(payload).await?;
- trace!("Received community inbox activity {}", unparsed);
- let activity = serde_json::from_str::<GroupInboxActivities>(&unparsed)?;
-
- receive_group_inbox(activity.clone(), request, &context).await?;
-
- if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
- let community = extract_community(&announcable.cc(), &context, &mut 0).await?;
- if community.local {
- AnnounceActivity::send(announcable, &community, vec![], &context).await?;
- }
- }
- Ok(HttpResponse::Ok().finish())
-}
-
-pub(in crate::http) async fn receive_group_inbox(
- activity: GroupInboxActivities,
- request: HttpRequest,
- context: &LemmyContext,
+ body: Bytes,
+ data: Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> {
- receive_activity(request, activity.clone(), context).await
+ receive_activity::<WithContext<GroupInboxActivities>, ApubPerson, LemmyContext>(
+ request, body, &data,
+ )
+ .await
}
/// Returns an empty followers collection, only populating the size (for privacy).
pub(crate) async fn get_apub_community_followers(
info: web::Path<CommunityQuery>,
- context: web::Data<LemmyContext>,
-) -> Result<HttpResponse<Body>, LemmyError> {
- let community = blocking(context.pool(), move |conn| {
- Community::read_from_name(conn, &info.community_name)
- })
- .await??;
-
- let community_id = community.id;
- let community_followers = blocking(context.pool(), move |conn| {
- CommunityFollowerView::for_community(conn, community_id)
- })
- .await??;
-
- let mut collection = UnorderedCollection::new();
- collection
- .set_many_contexts(lemmy_context())
- .set_id(community.followers_url.into())
- .set_total_items(community_followers.len() as u64);
- Ok(create_apub_response(&collection))
+ context: Data<LemmyContext>,
+) -> Result<HttpResponse, LemmyError> {
+ let community =
+ Community::read_from_name(&mut context.pool(), &info.community_name, false).await?;
+ let followers = GroupFollowers::new(community, &context).await?;
+ create_apub_response(&followers)
}
/// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
/// activites like votes or comments).
pub(crate) async fn get_apub_community_outbox(
info: web::Path<CommunityQuery>,
- context: web::Data<LemmyContext>,
-) -> Result<HttpResponse<Body>, LemmyError> {
- let community = blocking(context.pool(), move |conn| {
- Community::read_from_name(conn, &info.community_name)
- })
- .await??;
-
- let community_actor_id = community.actor_id.to_owned();
- let activities = blocking(context.pool(), move |conn| {
- Activity::read_community_outbox(conn, &community_actor_id)
- })
- .await??;
-
- let activities = activities
- .iter()
- .map(AnyBase::from_arbitrary_json)
- .collect::<Result<Vec<AnyBase>, serde_json::Error>>()?;
- let len = activities.len();
- let mut collection = OrderedCollection::new();
- collection
- .set_many_items(activities)
- .set_many_contexts(lemmy_context())
- .set_id(generate_outbox_url(&community.actor_id)?.into())
- .set_total_items(len as u64);
- Ok(create_apub_response(&collection))
+ context: Data<LemmyContext>,
+) -> Result<HttpResponse, LemmyError> {
+ let community: ApubCommunity =
+ Community::read_from_name(&mut context.pool(), &info.community_name, false)
+ .await?
+ .into();
+ if community.deleted || community.removed {
+ return Err(LemmyErrorType::Deleted)?;
+ }
+ let outbox = ApubCommunityOutbox::read_local(&community, &context).await?;
+ create_apub_response(&outbox)
}
-pub(crate) async fn get_apub_community_inbox(
+#[tracing::instrument(skip_all)]
+pub(crate) async fn get_apub_community_moderators(
info: web::Path<CommunityQuery>,
- context: web::Data<LemmyContext>,
-) -> Result<HttpResponse<Body>, LemmyError> {
- let community = blocking(context.pool(), move |conn| {
- Community::read_from_name(conn, &info.community_name)
- })
- .await??;
-
- let mut collection = OrderedCollection::new();
- collection
- .set_id(community.inbox_url.into())
- .set_many_contexts(lemmy_context());
- Ok(create_apub_response(&collection))
+ context: Data<LemmyContext>,
+) -> Result<HttpResponse, LemmyError> {
+ let community: ApubCommunity =
+ Community::read_from_name(&mut context.pool(), &info.community_name, false)
+ .await?
+ .into();
+ if community.deleted || community.removed {
+ return Err(LemmyErrorType::Deleted)?;
+ }
+ let moderators = ApubCommunityModerators::read_local(&community, &context).await?;
+ create_apub_response(&moderators)
}
-pub(crate) async fn get_apub_community_moderators(
+/// Returns collection of featured (stickied) posts.
+pub(crate) async fn get_apub_community_featured(
info: web::Path<CommunityQuery>,
- context: web::Data<LemmyContext>,
-) -> Result<HttpResponse<Body>, LemmyError> {
- let community = blocking(context.pool(), move |conn| {
- Community::read_from_name(conn, &info.community_name)
- })
- .await??;
-
- // The attributed to, is an ordered vector with the creator actor_ids first,
- // then the rest of the moderators
- // TODO Technically the instance admins can mod the community, but lets
- // ignore that for now
- let cid = community.id;
- let moderators = blocking(context.pool(), move |conn| {
- CommunityModeratorView::for_community(conn, cid)
- })
- .await??;
-
- let moderators: Vec<Url> = moderators
- .into_iter()
- .map(|m| m.moderator.actor_id.into())
- .collect();
- let mut collection = OrderedCollection::new();
- collection
- .set_id(generate_moderators_url(&community.actor_id)?.into())
- .set_total_items(moderators.len() as u64)
- .set_many_items(moderators)
- .set_many_contexts(lemmy_context());
- Ok(create_apub_response(&collection))
+ context: Data<LemmyContext>,
+) -> Result<HttpResponse, LemmyError> {
+ let community: ApubCommunity =
+ Community::read_from_name(&mut context.pool(), &info.community_name, false)
+ .await?
+ .into();
+ if community.deleted || community.removed {
+ return Err(LemmyErrorType::Deleted)?;
+ }
+ let featured = ApubCommunityFeatured::read_local(&community, &context).await?;
+ create_apub_response(&featured)
}