use crate::{
- activities::{
- community::announce::{AnnouncableActivities, AnnounceActivity},
- extract_community,
- following::{follow::FollowCommunity, undo::UndoFollowCommunity},
- report::Report,
- },
+ activity_lists::GroupInboxActivities,
collections::{
- community_moderators::ApubCommunityModerators, community_outbox::ApubCommunityOutbox,
- CommunityContext,
- },
- context::lemmy_context,
- fetcher::object_id::ObjectId,
- generate_outbox_url,
- http::{
- create_apub_response, create_apub_tombstone_response, payload_to_string, receive_activity,
+ community_featured::ApubCommunityFeatured,
+ community_moderators::ApubCommunityModerators,
+ community_outbox::ApubCommunityOutbox,
},
- objects::community::ApubCommunity,
+ http::{create_apub_response, create_apub_tombstone_response},
+ objects::{community::ApubCommunity, person::ApubPerson},
+ protocol::collections::group_followers::GroupFollowers,
};
-use activitystreams::{
- base::BaseExt,
- collection::{CollectionExt, UnorderedCollection},
+use activitypub_federation::{
+ actix_web::inbox::receive_activity,
+ config::Data,
+ protocol::context::WithContext,
+ traits::{Collection, Object},
};
-use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
-use lemmy_api_common::blocking;
-use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler, ApubObject};
-use lemmy_db_schema::source::community::Community;
-use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
-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: ApubCommunity = blocking(context.pool(), move |conn| {
- Community::read_from_name(conn, &info.community_name)
- })
- .await??
- .into();
+ 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).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?;
-
- 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> {
- let res = receive_activity(request, activity.clone(), context).await;
- if let GroupInboxActivities::AnnouncableActivities(announcable) = activity.clone() {
- let community = extract_community(&announcable.cc(), context, &mut 0).await?;
- if community.local {
- AnnounceActivity::send(announcable, &community, vec![], context).await?;
- }
- }
- res
+ 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 id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
- let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
- let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
- Ok(create_apub_response(&outbox.to_apub(&outbox_data).await?))
+ 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)
}
+#[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: ApubCommunity = blocking(context.pool(), move |conn| {
- Community::read_from_name(conn, &info.community_name)
- })
- .await??
- .into();
- let id = ObjectId::new(generate_outbox_url(&community.actor_id)?.into_inner());
- let outbox_data = CommunityContext(community, context.get_ref().clone());
- let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
- Ok(create_apub_response(
- &moderators.to_apub(&outbox_data).await?,
- ))
+ 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)
+}
+
+/// Returns collection of featured (stickied) posts.
+pub(crate) async fn get_apub_community_featured(
+ info: web::Path<CommunityQuery>,
+ 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)
}