2021-07-17 16:20:44 +00:00
|
|
|
use crate::{
|
2021-08-19 21:24:33 +00:00
|
|
|
activities::{
|
|
|
|
community::announce::{AnnouncableActivities, AnnounceActivity},
|
|
|
|
extract_community,
|
|
|
|
following::{follow::FollowCommunity, undo::UndoFollowCommunity},
|
2021-10-15 14:37:33 +00:00
|
|
|
report::Report,
|
2021-08-19 21:24:33 +00:00
|
|
|
},
|
2021-10-06 20:20:05 +00:00
|
|
|
context::lemmy_context,
|
2021-07-17 16:20:44 +00:00
|
|
|
generate_moderators_url,
|
2021-10-06 20:20:05 +00:00
|
|
|
generate_outbox_url,
|
2021-07-17 16:20:44 +00:00
|
|
|
http::{
|
|
|
|
create_apub_response,
|
|
|
|
create_apub_tombstone_response,
|
|
|
|
payload_to_string,
|
|
|
|
receive_activity,
|
|
|
|
},
|
2021-10-18 21:36:44 +00:00
|
|
|
objects::community::ApubCommunity,
|
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
|
|
|
use activitystreams::{
|
2021-01-27 14:02:28 +00:00
|
|
|
base::{AnyBase, BaseExt},
|
2020-10-12 14:10:09 +00:00
|
|
|
collection::{CollectionExt, OrderedCollection, UnorderedCollection},
|
2021-03-08 13:40:28 +00:00
|
|
|
url::Url,
|
2020-10-12 14:10:09 +00:00
|
|
|
};
|
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
|
|
|
use actix_web::{body::Body, web, web::Payload, HttpRequest, HttpResponse};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::blocking;
|
2021-10-18 21:36:44 +00:00
|
|
|
use lemmy_apub_lib::traits::{ActivityFields, ActivityHandler, ToApub};
|
2021-01-27 14:02:28 +00:00
|
|
|
use lemmy_db_schema::source::{activity::Activity, community::Community};
|
2021-03-08 13:40:28 +00:00
|
|
|
use lemmy_db_views_actor::{
|
|
|
|
community_follower_view::CommunityFollowerView,
|
|
|
|
community_moderator_view::CommunityModeratorView,
|
|
|
|
};
|
2020-10-12 14:10:09 +00:00
|
|
|
use lemmy_utils::LemmyError;
|
|
|
|
use lemmy_websocket::LemmyContext;
|
2021-08-19 21:24:33 +00:00
|
|
|
use log::trace;
|
|
|
|
use serde::{Deserialize, Serialize};
|
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 {
|
|
|
|
let apub = community.to_apub(context.pool()).await?;
|
|
|
|
|
|
|
|
Ok(create_apub_response(&apub))
|
|
|
|
} else {
|
|
|
|
Ok(create_apub_tombstone_response(&community.to_tombstone()?))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:24:33 +00:00
|
|
|
#[derive(Clone, Debug, Deserialize, Serialize, ActivityHandler, ActivityFields)]
|
|
|
|
#[serde(untagged)]
|
2021-10-06 20:20:05 +00:00
|
|
|
#[activity_handler(LemmyContext)]
|
2021-08-19 21:24:33 +00:00
|
|
|
pub enum GroupInboxActivities {
|
|
|
|
FollowCommunity(FollowCommunity),
|
|
|
|
UndoFollowCommunity(UndoFollowCommunity),
|
|
|
|
AnnouncableActivities(AnnouncableActivities),
|
2021-10-15 14:37:33 +00:00
|
|
|
Report(Report),
|
2021-08-19 21:24:33 +00:00
|
|
|
}
|
|
|
|
|
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-08-19 21:24:33 +00:00
|
|
|
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,
|
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
|
|
|
receive_activity(request, activity.clone(), context).await
|
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??;
|
|
|
|
|
|
|
|
let community_id = community.id;
|
|
|
|
let community_followers = blocking(context.pool(), move |conn| {
|
2021-07-05 16:07:26 +00:00
|
|
|
CommunityFollowerView::for_community(conn, community_id)
|
2020-10-12 14:10:09 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let mut collection = UnorderedCollection::new();
|
|
|
|
collection
|
2021-07-29 08:58:29 +00:00
|
|
|
.set_many_contexts(lemmy_context())
|
2021-02-04 16:34:58 +00:00
|
|
|
.set_id(community.followers_url.into())
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_total_items(community_followers.len() as u64);
|
|
|
|
Ok(create_apub_response(&collection))
|
|
|
|
}
|
|
|
|
|
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-01-27 14:02:28 +00:00
|
|
|
let community_actor_id = community.actor_id.to_owned();
|
|
|
|
let activities = blocking(context.pool(), move |conn| {
|
|
|
|
Activity::read_community_outbox(conn, &community_actor_id)
|
2020-10-12 14:10:09 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2021-01-27 14:02:28 +00:00
|
|
|
let activities = activities
|
|
|
|
.iter()
|
|
|
|
.map(AnyBase::from_arbitrary_json)
|
|
|
|
.collect::<Result<Vec<AnyBase>, serde_json::Error>>()?;
|
|
|
|
let len = activities.len();
|
2020-10-12 14:10:09 +00:00
|
|
|
let mut collection = OrderedCollection::new();
|
|
|
|
collection
|
2021-01-27 14:02:28 +00:00
|
|
|
.set_many_items(activities)
|
2021-07-29 08:58:29 +00:00
|
|
|
.set_many_contexts(lemmy_context())
|
2021-10-06 20:20:05 +00:00
|
|
|
.set_id(generate_outbox_url(&community.actor_id)?.into())
|
2020-10-12 14:10:09 +00:00
|
|
|
.set_total_items(len as u64);
|
|
|
|
Ok(create_apub_response(&collection))
|
|
|
|
}
|
2020-12-16 20:07:48 +00:00
|
|
|
|
2021-03-08 13:40:28 +00:00
|
|
|
pub(crate) async fn get_apub_community_inbox(
|
|
|
|
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)
|
2021-03-08 13:40:28 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let mut collection = OrderedCollection::new();
|
|
|
|
collection
|
|
|
|
.set_id(community.inbox_url.into())
|
2021-07-29 08:58:29 +00:00
|
|
|
.set_many_contexts(lemmy_context());
|
2021-03-08 13:40:28 +00:00
|
|
|
Ok(create_apub_response(&collection))
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2020-12-16 20:07:48 +00:00
|
|
|
|
2021-03-08 13:40:28 +00:00
|
|
|
// 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| {
|
2021-07-05 16:07:26 +00:00
|
|
|
CommunityModeratorView::for_community(conn, cid)
|
2021-03-08 13:40:28 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let moderators: Vec<Url> = moderators
|
|
|
|
.into_iter()
|
2021-07-31 20:58:11 +00:00
|
|
|
.map(|m| m.moderator.actor_id.into())
|
2021-03-08 13:40:28 +00:00
|
|
|
.collect();
|
2020-12-16 20:07:48 +00:00
|
|
|
let mut collection = OrderedCollection::new();
|
|
|
|
collection
|
2021-03-08 13:40:28 +00:00
|
|
|
.set_id(generate_moderators_url(&community.actor_id)?.into())
|
|
|
|
.set_total_items(moderators.len() as u64)
|
|
|
|
.set_many_items(moderators)
|
2021-07-29 08:58:29 +00:00
|
|
|
.set_many_contexts(lemmy_context());
|
2020-12-16 20:07:48 +00:00
|
|
|
Ok(create_apub_response(&collection))
|
|
|
|
}
|