2021-07-17 16:20:44 +00:00
|
|
|
use crate::{
|
2021-10-29 10:32:42 +00:00
|
|
|
activities::{community::announce::GetCommunity, verify_person_in_community},
|
|
|
|
activity_lists::GroupInboxActivities,
|
2021-10-27 16:03:07 +00:00
|
|
|
collections::{
|
2021-10-25 14:15:03 +00:00
|
|
|
community_moderators::ApubCommunityModerators,
|
|
|
|
community_outbox::ApubCommunityOutbox,
|
2021-10-27 16:03:07 +00:00
|
|
|
CommunityContext,
|
|
|
|
},
|
2021-10-28 15:25:26 +00:00
|
|
|
context::WithContext,
|
2021-10-06 20:20:05 +00:00
|
|
|
generate_outbox_url,
|
2021-07-17 16:20:44 +00:00
|
|
|
http::{
|
2021-10-25 14:15:03 +00:00
|
|
|
create_apub_response,
|
|
|
|
create_apub_tombstone_response,
|
|
|
|
payload_to_string,
|
|
|
|
receive_activity,
|
2021-11-03 17:33:51 +00:00
|
|
|
ActivityCommonFields,
|
2021-07-17 16:20:44 +00:00
|
|
|
},
|
2021-10-18 21:36:44 +00:00
|
|
|
objects::community::ApubCommunity,
|
2021-10-29 10:32:42 +00:00
|
|
|
protocol::{
|
|
|
|
activities::community::announce::AnnounceActivity,
|
2021-11-01 13:05:20 +00:00
|
|
|
collections::group_followers::GroupFollowers,
|
2021-10-29 10:32:42 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
|
|
|
|
use lemmy_api_common::blocking;
|
2021-11-05 00:24:10 +00:00
|
|
|
use lemmy_apub_lib::{object_id::ObjectId, traits::ApubObject};
|
2021-10-29 10:32:42 +00:00
|
|
|
use lemmy_db_schema::source::community::Community;
|
|
|
|
use lemmy_utils::LemmyError;
|
|
|
|
use lemmy_websocket::LemmyContext;
|
|
|
|
use log::info;
|
|
|
|
use serde::Deserialize;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2021-03-08 13:40:28 +00:00
|
|
|
pub(crate) struct CommunityQuery {
|
2020-10-12 14:10:09 +00:00
|
|
|
community_name: String,
|
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// Return the ActivityPub json representation of a local community over HTTP.
|
2021-03-08 13:40:28 +00:00
|
|
|
pub(crate) async fn get_apub_community_http(
|
2020-10-12 14:10:09 +00:00
|
|
|
info: web::Path<CommunityQuery>,
|
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse<Body>, LemmyError> {
|
2021-10-18 21:36:44 +00:00
|
|
|
let community: ApubCommunity = blocking(context.pool(), move |conn| {
|
2020-10-12 14:10:09 +00:00
|
|
|
Community::read_from_name(conn, &info.community_name)
|
|
|
|
})
|
2021-10-18 21:36:44 +00:00
|
|
|
.await??
|
|
|
|
.into();
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
if !community.deleted {
|
2021-11-06 12:37:55 +00:00
|
|
|
let apub = community.into_apub(&**context).await?;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
|
|
|
Ok(create_apub_response(&apub))
|
|
|
|
} else {
|
|
|
|
Ok(create_apub_tombstone_response(&community.to_tombstone()?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
/// Handler for all incoming receive to community inboxes.
|
|
|
|
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?;
|
2021-10-25 17:22:34 +00:00
|
|
|
info!("Received community inbox activity {}", unparsed);
|
2021-11-03 17:33:51 +00:00
|
|
|
let activity_data: ActivityCommonFields = serde_json::from_str(&unparsed)?;
|
2021-10-28 15:25:26 +00:00
|
|
|
let activity = serde_json::from_str::<WithContext<GroupInboxActivities>>(&unparsed)?;
|
2021-08-19 21:24:33 +00:00
|
|
|
|
2021-11-03 17:33:51 +00:00
|
|
|
receive_group_inbox(activity.inner(), activity_data, request, &context).await?;
|
2021-08-19 21:24:33 +00:00
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().finish())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(in crate::http) async fn receive_group_inbox(
|
|
|
|
activity: GroupInboxActivities,
|
2021-11-03 17:33:51 +00:00
|
|
|
activity_data: ActivityCommonFields,
|
2021-08-19 21:24:33 +00:00
|
|
|
request: HttpRequest,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2021-11-03 17:33:51 +00:00
|
|
|
let actor_id = ObjectId::new(activity_data.actor.clone());
|
|
|
|
let res = receive_activity(request, activity.clone(), activity_data, context).await;
|
2021-10-28 11:46:48 +00:00
|
|
|
|
|
|
|
if let GroupInboxActivities::AnnouncableActivities(announcable) = activity {
|
|
|
|
let community = announcable.get_community(context, &mut 0).await?;
|
|
|
|
verify_person_in_community(&actor_id, &community, context, &mut 0).await?;
|
2021-10-28 20:46:24 +00:00
|
|
|
if community.local {
|
2021-11-06 13:25:34 +00:00
|
|
|
AnnounceActivity::send(*announcable, &community, vec![], context).await?;
|
2021-10-28 20:46:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-28 11:46:48 +00:00
|
|
|
|
2021-10-28 20:46:24 +00:00
|
|
|
res
|
Apub inbox rewrite (#1652)
* start to implement apub inbox routing lib
* got something that almost works
* it compiles!
* implemented some more
* move library code to separate crate (most of it)
* convert private message handlers
* convert all comment receivers (except undo comment)
* convert post receiver
* add verify trait
* convert community receivers
* add cc field for all activities which i forgot before
* convert inbox functions, add missing checks
* convert undo like/dislike receivers
* convert undo_delete and undo_remove receivers
* move block/unblock activities
* convert remaining activity receivers
* reimplement http signature verification and other checks
* also use actor type for routing, VerifyActivity and SendActivity traits
* cleanup and restructure apub_receive code
* wip: try to fix activity routing
* implement a (very bad) derive macro for activityhandler
* working activity routing!
* rework pm verify(), fix tests and confirm manually
also remove inbox username check which was broken
* rework following verify(), fix tests and test manually
* fix post/comment create/update, rework voting
* Rewrite remove/delete post/comment, fix tests, test manually
* Rework and fix (un)block user, announce, update post
* some code cleanup
* rework delete/remove activity receivers (still quite messy)
* rewrite, test and fix add/remove mod, update community handlers
* add docs for ActivityHandler derive macro
* dont try to compile macro comments
2021-07-17 16:08:46 +00:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:10:09 +00:00
|
|
|
/// Returns an empty followers collection, only populating the size (for privacy).
|
2021-03-08 13:40:28 +00:00
|
|
|
pub(crate) async fn get_apub_community_followers(
|
2020-10-12 14:10:09 +00:00
|
|
|
info: web::Path<CommunityQuery>,
|
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse<Body>, LemmyError> {
|
|
|
|
let community = blocking(context.pool(), move |conn| {
|
2021-07-05 16:07:26 +00:00
|
|
|
Community::read_from_name(conn, &info.community_name)
|
2020-10-12 14:10:09 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2021-11-01 13:05:20 +00:00
|
|
|
let followers = GroupFollowers::new(community, &context).await?;
|
2021-10-28 15:52:11 +00:00
|
|
|
Ok(create_apub_response(&followers))
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
2020-10-19 14:29:35 +00:00
|
|
|
/// Returns the community outbox, which is populated by a maximum of 20 posts (but no other
|
|
|
|
/// activites like votes or comments).
|
2021-03-08 13:40:28 +00:00
|
|
|
pub(crate) async fn get_apub_community_outbox(
|
2020-10-12 14:10:09 +00:00
|
|
|
info: web::Path<CommunityQuery>,
|
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse<Body>, LemmyError> {
|
|
|
|
let community = blocking(context.pool(), move |conn| {
|
2021-07-05 16:07:26 +00:00
|
|
|
Community::read_from_name(conn, &info.community_name)
|
2020-10-12 14:10:09 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2021-11-05 00:24:10 +00:00
|
|
|
let id = ObjectId::new(generate_outbox_url(&community.actor_id)?);
|
2021-10-27 16:03:07 +00:00
|
|
|
let outbox_data = CommunityContext(community.into(), context.get_ref().clone());
|
|
|
|
let outbox: ApubCommunityOutbox = id.dereference(&outbox_data, &mut 0).await?;
|
2021-11-06 12:37:55 +00:00
|
|
|
Ok(create_apub_response(&outbox.into_apub(&outbox_data).await?))
|
2021-03-08 13:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) async fn get_apub_community_moderators(
|
2020-12-16 20:07:48 +00:00
|
|
|
info: web::Path<CommunityQuery>,
|
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse<Body>, LemmyError> {
|
2021-10-18 21:36:44 +00:00
|
|
|
let community: ApubCommunity = blocking(context.pool(), move |conn| {
|
2021-07-05 16:07:26 +00:00
|
|
|
Community::read_from_name(conn, &info.community_name)
|
2020-12-16 20:07:48 +00:00
|
|
|
})
|
2021-10-18 21:36:44 +00:00
|
|
|
.await??
|
|
|
|
.into();
|
2021-11-05 00:24:10 +00:00
|
|
|
let id = ObjectId::new(generate_outbox_url(&community.actor_id)?);
|
2021-10-27 16:03:07 +00:00
|
|
|
let outbox_data = CommunityContext(community, context.get_ref().clone());
|
|
|
|
let moderators: ApubCommunityModerators = id.dereference(&outbox_data, &mut 0).await?;
|
|
|
|
Ok(create_apub_response(
|
2021-11-06 12:37:55 +00:00
|
|
|
&moderators.into_apub(&outbox_data).await?,
|
2021-10-27 16:03:07 +00:00
|
|
|
))
|
2020-12-16 20:07:48 +00:00
|
|
|
}
|