2021-07-17 16:20:44 +00:00
|
|
|
use crate::{
|
2021-10-29 10:32:42 +00:00
|
|
|
activity_lists::GroupInboxActivities,
|
2021-10-27 16:03:07 +00:00
|
|
|
collections::{
|
2023-02-18 14:50:28 +00:00
|
|
|
community_featured::ApubCommunityFeatured,
|
2023-10-17 14:34:38 +00:00
|
|
|
community_follower::ApubCommunityFollower,
|
2021-10-25 14:15:03 +00:00
|
|
|
community_moderators::ApubCommunityModerators,
|
|
|
|
community_outbox::ApubCommunityOutbox,
|
2021-10-27 16:03:07 +00:00
|
|
|
},
|
2024-01-25 16:04:25 +00:00
|
|
|
http::{check_community_public, create_apub_response, create_apub_tombstone_response},
|
2022-06-02 14:33:41 +00:00
|
|
|
objects::{community::ApubCommunity, person::ApubPerson},
|
|
|
|
};
|
|
|
|
use activitypub_federation::{
|
2023-03-21 15:03:05 +00:00
|
|
|
actix_web::inbox::receive_activity,
|
|
|
|
config::Data,
|
|
|
|
protocol::context::WithContext,
|
|
|
|
traits::{Collection, Object},
|
2023-02-18 14:50:28 +00:00
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use actix_web::{web, web::Bytes, HttpRequest, HttpResponse};
|
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2022-01-27 16:39:22 +00:00
|
|
|
use lemmy_db_schema::{source::community::Community, traits::ApubActor};
|
2024-01-25 16:04:25 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
2021-10-29 10:32:42 +00:00
|
|
|
use serde::Deserialize;
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2024-01-25 16:04:25 +00:00
|
|
|
#[derive(Deserialize, Clone)]
|
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-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
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>,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: Data<LemmyContext>,
|
2021-12-14 13:30:37 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let community: ApubCommunity =
|
2023-07-11 13:09:59 +00:00
|
|
|
Community::read_from_name(&mut context.pool(), &info.community_name, true)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await?
|
|
|
|
.into();
|
2020-10-12 14:10:09 +00:00
|
|
|
|
2024-01-25 16:04:25 +00:00
|
|
|
if community.deleted || community.removed {
|
|
|
|
return create_apub_tombstone_response(community.actor_id.clone());
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
2024-01-25 16:04:25 +00:00
|
|
|
check_community_public(&community)?;
|
|
|
|
|
|
|
|
let apub = community.into_json(&context).await?;
|
|
|
|
create_apub_response(&apub)
|
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
|
|
|
/// Handler for all incoming receive to community inboxes.
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
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
|
|
|
pub async fn community_inbox(
|
|
|
|
request: HttpRequest,
|
2023-03-21 15:03:05 +00:00
|
|
|
body: Bytes,
|
|
|
|
data: Data<LemmyContext>,
|
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
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2023-03-21 15:03:05 +00:00
|
|
|
receive_activity::<WithContext<GroupInboxActivities>, ApubPerson, LemmyContext>(
|
|
|
|
request, body, &data,
|
|
|
|
)
|
|
|
|
.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>,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: Data<LemmyContext>,
|
2021-12-14 13:30:37 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2023-07-11 13:09:59 +00:00
|
|
|
let community =
|
|
|
|
Community::read_from_name(&mut context.pool(), &info.community_name, false).await?;
|
2024-01-25 16:04:25 +00:00
|
|
|
check_community_public(&community)?;
|
2023-10-17 14:34:38 +00:00
|
|
|
let followers = ApubCommunityFollower::read_local(&community.into(), &context).await?;
|
2023-04-13 00:17:23 +00:00
|
|
|
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>,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: Data<LemmyContext>,
|
2021-12-14 13:30:37 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2023-03-21 15:03:05 +00:00
|
|
|
let community: ApubCommunity =
|
2023-07-11 13:09:59 +00:00
|
|
|
Community::read_from_name(&mut context.pool(), &info.community_name, false)
|
2023-03-21 15:03:05 +00:00
|
|
|
.await?
|
|
|
|
.into();
|
2024-01-25 16:04:25 +00:00
|
|
|
check_community_public(&community)?;
|
2023-03-21 15:03:05 +00:00
|
|
|
let outbox = ApubCommunityOutbox::read_local(&community, &context).await?;
|
2023-04-13 00:17:23 +00:00
|
|
|
create_apub_response(&outbox)
|
2021-03-08 13:40:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
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>,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: Data<LemmyContext>,
|
2021-12-14 13:30:37 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2022-11-09 10:05:00 +00:00
|
|
|
let community: ApubCommunity =
|
2023-07-11 13:09:59 +00:00
|
|
|
Community::read_from_name(&mut context.pool(), &info.community_name, false)
|
2022-11-09 10:05:00 +00:00
|
|
|
.await?
|
|
|
|
.into();
|
2024-01-25 16:04:25 +00:00
|
|
|
check_community_public(&community)?;
|
2023-03-21 15:03:05 +00:00
|
|
|
let moderators = ApubCommunityModerators::read_local(&community, &context).await?;
|
2023-04-13 00:17:23 +00:00
|
|
|
create_apub_response(&moderators)
|
2020-12-16 20:07:48 +00:00
|
|
|
}
|
2023-02-18 14:50:28 +00:00
|
|
|
|
|
|
|
/// Returns collection of featured (stickied) posts.
|
|
|
|
pub(crate) async fn get_apub_community_featured(
|
|
|
|
info: web::Path<CommunityQuery>,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: Data<LemmyContext>,
|
2023-02-18 14:50:28 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2023-03-21 15:03:05 +00:00
|
|
|
let community: ApubCommunity =
|
2023-07-11 13:09:59 +00:00
|
|
|
Community::read_from_name(&mut context.pool(), &info.community_name, false)
|
2023-03-21 15:03:05 +00:00
|
|
|
.await?
|
|
|
|
.into();
|
2024-01-25 16:04:25 +00:00
|
|
|
check_community_public(&community)?;
|
2023-03-21 15:03:05 +00:00
|
|
|
let featured = ApubCommunityFeatured::read_local(&community, &context).await?;
|
2023-04-13 00:17:23 +00:00
|
|
|
create_apub_response(&featured)
|
2023-02-18 14:50:28 +00:00
|
|
|
}
|
2024-01-25 16:04:25 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
#![allow(clippy::unwrap_used)]
|
|
|
|
#![allow(clippy::indexing_slicing)]
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
use crate::protocol::objects::{group::Group, tombstone::Tombstone};
|
|
|
|
use actix_web::body::to_bytes;
|
|
|
|
use lemmy_db_schema::{
|
2024-02-26 14:47:10 +00:00
|
|
|
source::{community::CommunityInsertForm, instance::Instance},
|
2024-01-25 16:04:25 +00:00
|
|
|
traits::Crud,
|
|
|
|
CommunityVisibility,
|
|
|
|
};
|
|
|
|
use lemmy_utils::error::LemmyResult;
|
|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
use serial_test::serial;
|
|
|
|
|
|
|
|
async fn init(
|
|
|
|
deleted: bool,
|
|
|
|
visibility: CommunityVisibility,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
) -> Result<(Instance, Community), LemmyError> {
|
|
|
|
let instance =
|
|
|
|
Instance::read_or_create(&mut context.pool(), "my_domain.tld".to_string()).await?;
|
|
|
|
let community_form = CommunityInsertForm::builder()
|
|
|
|
.name("testcom6".to_string())
|
|
|
|
.title("nada".to_owned())
|
|
|
|
.public_key("pubkey".to_string())
|
|
|
|
.instance_id(instance.id)
|
|
|
|
.deleted(Some(deleted))
|
|
|
|
.visibility(Some(visibility))
|
|
|
|
.build();
|
|
|
|
let community = Community::create(&mut context.pool(), &community_form).await?;
|
|
|
|
Ok((instance, community))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn decode_response<T: DeserializeOwned>(res: HttpResponse) -> Result<T, LemmyError> {
|
|
|
|
let body = to_bytes(res.into_body()).await.unwrap();
|
|
|
|
let body = std::str::from_utf8(&body)?;
|
|
|
|
Ok(serde_json::from_str(body)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_get_community() -> LemmyResult<()> {
|
|
|
|
let context = LemmyContext::init_test_context().await;
|
|
|
|
|
|
|
|
// fetch invalid community
|
|
|
|
let query = CommunityQuery {
|
|
|
|
community_name: "asd".to_string(),
|
|
|
|
};
|
|
|
|
let res = get_apub_community_http(query.into(), context.reset_request_count()).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
|
|
|
|
let (instance, community) = init(false, CommunityVisibility::Public, &context).await?;
|
|
|
|
|
|
|
|
// fetch valid community
|
|
|
|
let query = CommunityQuery {
|
|
|
|
community_name: community.name.clone(),
|
|
|
|
};
|
|
|
|
let res = get_apub_community_http(query.clone().into(), context.reset_request_count()).await?;
|
|
|
|
assert_eq!(200, res.status());
|
|
|
|
let res_group: Group = decode_response(res).await?;
|
|
|
|
let community: ApubCommunity = community.into();
|
|
|
|
let group = community.clone().into_json(&context).await?;
|
|
|
|
assert_eq!(group, res_group);
|
|
|
|
|
|
|
|
let res =
|
|
|
|
get_apub_community_featured(query.clone().into(), context.reset_request_count()).await?;
|
|
|
|
assert_eq!(200, res.status());
|
|
|
|
let res =
|
|
|
|
get_apub_community_followers(query.clone().into(), context.reset_request_count()).await?;
|
|
|
|
assert_eq!(200, res.status());
|
|
|
|
let res =
|
|
|
|
get_apub_community_moderators(query.clone().into(), context.reset_request_count()).await?;
|
|
|
|
assert_eq!(200, res.status());
|
|
|
|
let res = get_apub_community_outbox(query.into(), context.reset_request_count()).await?;
|
|
|
|
assert_eq!(200, res.status());
|
|
|
|
|
|
|
|
Instance::delete(&mut context.pool(), instance.id).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_get_deleted_community() -> LemmyResult<()> {
|
|
|
|
let context = LemmyContext::init_test_context().await;
|
|
|
|
let (instance, community) = init(true, CommunityVisibility::LocalOnly, &context).await?;
|
|
|
|
|
|
|
|
// should return tombstone
|
|
|
|
let query = CommunityQuery {
|
|
|
|
community_name: community.name.clone(),
|
|
|
|
};
|
|
|
|
let res = get_apub_community_http(query.clone().into(), context.reset_request_count()).await?;
|
|
|
|
assert_eq!(410, res.status());
|
|
|
|
let res_tombstone = decode_response::<Tombstone>(res).await;
|
|
|
|
assert!(res_tombstone.is_ok());
|
|
|
|
|
|
|
|
let res =
|
|
|
|
get_apub_community_featured(query.clone().into(), context.reset_request_count()).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
let res =
|
|
|
|
get_apub_community_followers(query.clone().into(), context.reset_request_count()).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
let res =
|
|
|
|
get_apub_community_moderators(query.clone().into(), context.reset_request_count()).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
let res = get_apub_community_outbox(query.into(), context.reset_request_count()).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
|
|
|
|
//Community::delete(&mut context.pool(), community.id).await?;
|
|
|
|
Instance::delete(&mut context.pool(), instance.id).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[serial]
|
|
|
|
async fn test_get_local_only_community() -> LemmyResult<()> {
|
|
|
|
let context = LemmyContext::init_test_context().await;
|
|
|
|
let (instance, community) = init(false, CommunityVisibility::LocalOnly, &context).await?;
|
|
|
|
|
|
|
|
let query = CommunityQuery {
|
|
|
|
community_name: community.name.clone(),
|
|
|
|
};
|
|
|
|
let res = get_apub_community_http(query.clone().into(), context.reset_request_count()).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
let res =
|
|
|
|
get_apub_community_featured(query.clone().into(), context.reset_request_count()).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
let res =
|
|
|
|
get_apub_community_followers(query.clone().into(), context.reset_request_count()).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
let res =
|
|
|
|
get_apub_community_moderators(query.clone().into(), context.reset_request_count()).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
let res = get_apub_community_outbox(query.into(), context.reset_request_count()).await;
|
|
|
|
assert!(res.is_err());
|
|
|
|
|
|
|
|
Instance::delete(&mut context.pool(), instance.id).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|