2021-07-17 16:20:44 +00:00
|
|
|
use crate::{
|
2021-10-29 10:32:42 +00:00
|
|
|
activity_lists::SharedInboxActivities,
|
2021-11-05 00:24:10 +00:00
|
|
|
fetcher::user_or_community::UserOrCommunity,
|
2021-07-17 16:20:44 +00:00
|
|
|
insert_activity,
|
2022-06-02 14:33:41 +00:00
|
|
|
local_instance,
|
|
|
|
protocol::objects::tombstone::Tombstone,
|
|
|
|
CONTEXT,
|
2021-07-17 16:20:44 +00:00
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use activitypub_federation::{
|
2022-06-08 15:45:39 +00:00
|
|
|
core::inbox::receive_activity,
|
2021-10-06 20:20:05 +00:00
|
|
|
data::Data,
|
2022-06-02 14:33:41 +00:00
|
|
|
deser::context::WithContext,
|
2022-06-08 15:45:39 +00:00
|
|
|
traits::{ActivityHandler, Actor, ApubObject},
|
2021-10-06 20:20:05 +00:00
|
|
|
APUB_JSON_CONTENT_TYPE,
|
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use actix_web::{web, HttpRequest, HttpResponse};
|
|
|
|
use http::StatusCode;
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2021-11-22 18:57:03 +00:00
|
|
|
use lemmy_db_schema::source::activity::Activity;
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
|
|
|
use once_cell::sync::OnceCell;
|
|
|
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
|
|
|
use serde_json::Value;
|
|
|
|
use std::ops::Deref;
|
|
|
|
use tracing::{debug, log::info};
|
2021-03-02 12:41:48 +00:00
|
|
|
use 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
|
|
|
mod comment;
|
|
|
|
mod community;
|
|
|
|
mod person;
|
|
|
|
mod post;
|
|
|
|
pub mod routes;
|
2022-02-07 19:23:12 +00:00
|
|
|
pub mod site;
|
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 shared_inbox(
|
|
|
|
request: HttpRequest,
|
2022-06-02 14:33:41 +00:00
|
|
|
payload: String,
|
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
|
|
|
context: web::Data<LemmyContext>,
|
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2022-06-02 14:33:41 +00:00
|
|
|
receive_lemmy_activity::<SharedInboxActivities, UserOrCommunity>(request, payload, 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
|
|
|
}
|
|
|
|
|
2022-06-08 15:45:39 +00:00
|
|
|
pub async fn receive_lemmy_activity<Activity, ActorT>(
|
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
|
|
|
request: HttpRequest,
|
2022-06-02 14:33:41 +00:00
|
|
|
payload: String,
|
|
|
|
context: web::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>
|
|
|
|
where
|
2022-06-02 14:33:41 +00:00
|
|
|
Activity: ActivityHandler<DataType = LemmyContext, Error = LemmyError>
|
|
|
|
+ DeserializeOwned
|
2021-08-19 21:24:33 +00:00
|
|
|
+ Send
|
|
|
|
+ 'static,
|
2022-06-08 15:45:39 +00:00
|
|
|
ActorT: ApubObject<DataType = LemmyContext, Error = LemmyError> + Actor + Send + 'static,
|
|
|
|
for<'de2> <ActorT as ApubObject>::ApubType: serde::Deserialize<'de2>,
|
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
|
|
|
{
|
2022-11-19 04:33:54 +00:00
|
|
|
static DATA: OnceCell<Data<LemmyContext>> = OnceCell::new();
|
2022-06-02 14:33:41 +00:00
|
|
|
let activity_value: Value = serde_json::from_str(&payload)?;
|
2022-11-16 22:51:05 +00:00
|
|
|
debug!("Parsing activity {}", payload);
|
2022-06-02 14:33:41 +00:00
|
|
|
let activity: Activity = serde_json::from_value(activity_value.clone())?;
|
|
|
|
// Log the activity, so we avoid receiving and parsing it twice.
|
|
|
|
let insert = insert_activity(activity.id(), activity_value, false, true, context.pool()).await?;
|
2022-03-24 16:05:27 +00:00
|
|
|
if !insert {
|
2022-06-02 14:33:41 +00:00
|
|
|
debug!("Received duplicate activity {}", activity.id().to_string());
|
2022-03-24 16:05:27 +00:00
|
|
|
return Ok(HttpResponse::BadRequest().finish());
|
|
|
|
}
|
2022-06-02 14:33:41 +00:00
|
|
|
info!("Received activity {}", payload);
|
|
|
|
|
|
|
|
let data = DATA.get_or_init(|| Data::new(context.get_ref().clone()));
|
2022-06-08 15:45:39 +00:00
|
|
|
receive_activity::<Activity, ActorT, LemmyContext>(
|
2022-06-02 14:33:41 +00:00
|
|
|
request,
|
|
|
|
activity,
|
2022-11-09 10:05:00 +00:00
|
|
|
local_instance(&context).await,
|
2022-06-02 14:33:41 +00:00
|
|
|
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
|
|
|
|
|
|
|
/// Convert the data to json and turn it into an HTTP Response with the correct ActivityPub
|
|
|
|
/// headers.
|
2021-12-14 13:30:37 +00:00
|
|
|
fn create_apub_response<T>(data: &T) -> HttpResponse
|
2020-10-12 14:10:09 +00:00
|
|
|
where
|
|
|
|
T: Serialize,
|
|
|
|
{
|
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type(APUB_JSON_CONTENT_TYPE)
|
2022-06-02 14:33:41 +00:00
|
|
|
.json(WithContext::new(data, CONTEXT.deref().clone()))
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
|
|
|
|
2021-12-14 13:30:37 +00:00
|
|
|
fn create_json_apub_response(data: serde_json::Value) -> HttpResponse {
|
2021-11-15 20:26:48 +00:00
|
|
|
HttpResponse::Ok()
|
|
|
|
.content_type(APUB_JSON_CONTENT_TYPE)
|
2021-11-16 02:07:07 +00:00
|
|
|
.json(data)
|
2021-11-15 20:26:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-02 14:33:41 +00:00
|
|
|
fn create_apub_tombstone_response<T: Into<Url>>(id: T) -> HttpResponse {
|
|
|
|
let tombstone = Tombstone::new(id.into());
|
2020-10-12 14:10:09 +00:00
|
|
|
HttpResponse::Gone()
|
|
|
|
.content_type(APUB_JSON_CONTENT_TYPE)
|
2021-01-04 18:53:15 +00:00
|
|
|
.status(StatusCode::GONE)
|
2022-06-02 14:33:41 +00:00
|
|
|
.json(WithContext::new(tombstone, CONTEXT.deref().clone()))
|
2020-10-12 14:10:09 +00:00
|
|
|
}
|
2020-10-23 13:02:45 +00:00
|
|
|
|
2023-02-08 19:45:29 +00:00
|
|
|
fn err_object_not_local() -> LemmyError {
|
|
|
|
LemmyError::from_message("Object not local, fetch it from original instance")
|
|
|
|
}
|
|
|
|
|
2020-10-23 13:02:45 +00:00
|
|
|
#[derive(Deserialize)]
|
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 struct ActivityQuery {
|
2020-10-23 13:02:45 +00:00
|
|
|
type_: String,
|
|
|
|
id: String,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/// Return the ActivityPub json representation of a local activity 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_activity(
|
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
|
|
|
info: web::Path<ActivityQuery>,
|
2020-10-23 13:02:45 +00:00
|
|
|
context: web::Data<LemmyContext>,
|
2021-12-14 13:30:37 +00:00
|
|
|
) -> Result<HttpResponse, LemmyError> {
|
2021-09-22 15:57:09 +00:00
|
|
|
let settings = context.settings();
|
2021-03-02 12:41:48 +00:00
|
|
|
let activity_id = Url::parse(&format!(
|
2020-10-23 13:02:45 +00:00
|
|
|
"{}/activities/{}/{}",
|
|
|
|
settings.get_protocol_and_hostname(),
|
|
|
|
info.type_,
|
|
|
|
info.id
|
2021-03-02 12:41:48 +00:00
|
|
|
))?
|
|
|
|
.into();
|
2022-11-09 10:05:00 +00:00
|
|
|
let activity = Activity::read_from_apub_id(context.pool(), &activity_id).await?;
|
2020-10-23 13:02:45 +00:00
|
|
|
|
2020-11-10 16:11:08 +00:00
|
|
|
let sensitive = activity.sensitive.unwrap_or(true);
|
2023-02-08 19:45:29 +00:00
|
|
|
if !activity.local {
|
|
|
|
return Err(err_object_not_local());
|
|
|
|
} else if sensitive {
|
|
|
|
Ok(HttpResponse::Forbidden().finish())
|
2020-11-06 13:06:47 +00:00
|
|
|
} else {
|
2021-11-15 20:26:48 +00:00
|
|
|
Ok(create_json_apub_response(activity.data))
|
2020-11-06 13:06:47 +00:00
|
|
|
}
|
2020-10-23 13:02:45 +00:00
|
|
|
}
|