Move activity parsing to handlers
This commit is contained in:
parent
e247941695
commit
04ee3e5106
13 changed files with 58 additions and 24 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
use serde_json::Value;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::activitypub::{
|
use crate::activitypub::{
|
||||||
|
@ -19,9 +20,11 @@ use super::HandlerResult;
|
||||||
pub async fn handle_accept(
|
pub async fn handle_accept(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
activity: Activity,
|
activity: Value,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
// Accept(Follow)
|
// Accept(Follow)
|
||||||
|
let activity: Activity = serde_json::from_value(activity)
|
||||||
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
||||||
let actor_profile = get_profile_by_remote_actor_id(
|
let actor_profile = get_profile_by_remote_actor_id(
|
||||||
db_client,
|
db_client,
|
||||||
&activity.actor,
|
&activity.actor,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use serde_json::Value;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::activitypub::{
|
use crate::activitypub::{
|
||||||
|
@ -16,8 +17,10 @@ use super::{HandlerError, HandlerResult};
|
||||||
pub async fn handle_add(
|
pub async fn handle_add(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
activity: Activity,
|
activity: Value,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
|
let activity: Activity = serde_json::from_value(activity)
|
||||||
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
||||||
let actor_profile = get_profile_by_remote_actor_id(
|
let actor_profile = get_profile_by_remote_actor_id(
|
||||||
db_client,
|
db_client,
|
||||||
&activity.actor,
|
&activity.actor,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use serde_json::Value;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::activitypub::{
|
use crate::activitypub::{
|
||||||
|
@ -9,6 +10,7 @@ use crate::activitypub::{
|
||||||
};
|
};
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::database::DatabaseError;
|
use crate::database::DatabaseError;
|
||||||
|
use crate::errors::ValidationError;
|
||||||
use crate::models::posts::queries::{
|
use crate::models::posts::queries::{
|
||||||
create_post,
|
create_post,
|
||||||
get_post_by_remote_object_id,
|
get_post_by_remote_object_id,
|
||||||
|
@ -19,8 +21,10 @@ use super::HandlerResult;
|
||||||
pub async fn handle_announce(
|
pub async fn handle_announce(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
activity: Activity,
|
activity: Value,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
|
let activity: Activity = serde_json::from_value(activity)
|
||||||
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
||||||
let repost_object_id = activity.id;
|
let repost_object_id = activity.id;
|
||||||
match get_post_by_remote_object_id(
|
match get_post_by_remote_object_id(
|
||||||
db_client,
|
db_client,
|
||||||
|
|
|
@ -7,7 +7,7 @@ use tokio_postgres::GenericClient;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::activitypub::{
|
use crate::activitypub::{
|
||||||
activity::{Activity, Attachment, Link, Object, Tag},
|
activity::{Attachment, Link, Object, Tag},
|
||||||
constants::{AP_MEDIA_TYPE, AP_PUBLIC, AS_MEDIA_TYPE},
|
constants::{AP_MEDIA_TYPE, AP_PUBLIC, AS_MEDIA_TYPE},
|
||||||
fetcher::fetchers::fetch_file,
|
fetcher::fetchers::fetch_file,
|
||||||
fetcher::helpers::{
|
fetcher::helpers::{
|
||||||
|
@ -391,10 +391,10 @@ pub async fn handle_note(
|
||||||
pub async fn handle_create(
|
pub async fn handle_create(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
activity: Activity,
|
activity: JsonValue,
|
||||||
is_authenticated: bool,
|
is_authenticated: bool,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
let object: Object = serde_json::from_value(activity.object)
|
let object: Object = serde_json::from_value(activity["object"].to_owned())
|
||||||
.map_err(|_| ValidationError("invalid object"))?;
|
.map_err(|_| ValidationError("invalid object"))?;
|
||||||
let object_id = object.id.clone();
|
let object_id = object.id.clone();
|
||||||
let object_received = if is_authenticated {
|
let object_received = if is_authenticated {
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use serde_json::Value;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::activitypub::{
|
use crate::activitypub::{
|
||||||
|
@ -21,8 +22,10 @@ use super::HandlerResult;
|
||||||
pub async fn handle_delete(
|
pub async fn handle_delete(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
activity: Activity,
|
activity: Value,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
|
let activity: Activity = serde_json::from_value(activity)
|
||||||
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
||||||
let object_id = find_object_id(&activity.object)?;
|
let object_id = find_object_id(&activity.object)?;
|
||||||
if object_id == activity.actor {
|
if object_id == activity.actor {
|
||||||
// Self-delete
|
// Self-delete
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use serde_json::Value;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::activitypub::{
|
use crate::activitypub::{
|
||||||
|
@ -10,6 +11,7 @@ use crate::activitypub::{
|
||||||
};
|
};
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::database::DatabaseError;
|
use crate::database::DatabaseError;
|
||||||
|
use crate::errors::ValidationError;
|
||||||
use crate::models::relationships::queries::follow;
|
use crate::models::relationships::queries::follow;
|
||||||
use crate::models::users::queries::get_user_by_name;
|
use crate::models::users::queries::get_user_by_name;
|
||||||
use super::{HandlerError, HandlerResult};
|
use super::{HandlerError, HandlerResult};
|
||||||
|
@ -17,8 +19,10 @@ use super::{HandlerError, HandlerResult};
|
||||||
pub async fn handle_follow(
|
pub async fn handle_follow(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
activity: Activity,
|
activity: Value,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
|
let activity: Activity = serde_json::from_value(activity)
|
||||||
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
||||||
let source_profile = get_or_import_profile_by_actor_id(
|
let source_profile = get_or_import_profile_by_actor_id(
|
||||||
db_client,
|
db_client,
|
||||||
&config.instance(),
|
&config.instance(),
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use serde_json::Value;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::activitypub::{
|
use crate::activitypub::{
|
||||||
|
@ -9,6 +10,7 @@ use crate::activitypub::{
|
||||||
};
|
};
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::database::DatabaseError;
|
use crate::database::DatabaseError;
|
||||||
|
use crate::errors::ValidationError;
|
||||||
use crate::models::reactions::queries::create_reaction;
|
use crate::models::reactions::queries::create_reaction;
|
||||||
use crate::models::posts::queries::get_post_by_remote_object_id;
|
use crate::models::posts::queries::get_post_by_remote_object_id;
|
||||||
use super::HandlerResult;
|
use super::HandlerResult;
|
||||||
|
@ -16,8 +18,10 @@ use super::HandlerResult;
|
||||||
pub async fn handle_like(
|
pub async fn handle_like(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
activity: Activity,
|
activity: Value,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
|
let activity: Activity = serde_json::from_value(activity)
|
||||||
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
||||||
let author = get_or_import_profile_by_actor_id(
|
let author = get_or_import_profile_by_actor_id(
|
||||||
db_client,
|
db_client,
|
||||||
&config.instance(),
|
&config.instance(),
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use serde_json::Value;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::activitypub::{
|
use crate::activitypub::{
|
||||||
|
@ -29,9 +30,11 @@ use super::HandlerResult;
|
||||||
pub async fn handle_move(
|
pub async fn handle_move(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
activity: Activity,
|
activity: Value,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
// Move(Person)
|
// Move(Person)
|
||||||
|
let activity: Activity = serde_json::from_value(activity)
|
||||||
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
||||||
let object_id = find_object_id(&activity.object)?;
|
let object_id = find_object_id(&activity.object)?;
|
||||||
let target_value = activity.target
|
let target_value = activity.target
|
||||||
.ok_or(ValidationError("target is missing"))?;
|
.ok_or(ValidationError("target is missing"))?;
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use serde_json::Value;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::activitypub::{
|
use crate::activitypub::{
|
||||||
|
@ -19,9 +20,11 @@ use super::HandlerResult;
|
||||||
pub async fn handle_reject(
|
pub async fn handle_reject(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &impl GenericClient,
|
db_client: &impl GenericClient,
|
||||||
activity: Activity,
|
activity: Value,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
// Reject(Follow)
|
// Reject(Follow)
|
||||||
|
let activity: Activity = serde_json::from_value(activity)
|
||||||
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
||||||
let actor_profile = get_profile_by_remote_actor_id(
|
let actor_profile = get_profile_by_remote_actor_id(
|
||||||
db_client,
|
db_client,
|
||||||
&activity.actor,
|
&activity.actor,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use serde_json::Value;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::activitypub::{
|
use crate::activitypub::{
|
||||||
|
@ -20,8 +21,10 @@ use super::{HandlerError, HandlerResult};
|
||||||
pub async fn handle_remove(
|
pub async fn handle_remove(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
activity: Activity,
|
activity: Value,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
|
let activity: Activity = serde_json::from_value(activity)
|
||||||
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
||||||
let actor_profile = get_profile_by_remote_actor_id(
|
let actor_profile = get_profile_by_remote_actor_id(
|
||||||
db_client,
|
db_client,
|
||||||
&activity.actor,
|
&activity.actor,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use serde_json::Value;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::activitypub::{
|
use crate::activitypub::{
|
||||||
|
@ -54,8 +55,10 @@ async fn handle_undo_follow(
|
||||||
pub async fn handle_undo(
|
pub async fn handle_undo(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
activity: Activity,
|
activity: Value,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
|
let activity: Activity = serde_json::from_value(activity)
|
||||||
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
||||||
if let Some(FOLLOW) = activity.object["type"].as_str() {
|
if let Some(FOLLOW) = activity.object["type"].as_str() {
|
||||||
// Object type is currently required for processing Undo(Follow)
|
// Object type is currently required for processing Undo(Follow)
|
||||||
// because activity IDs of remote follow requests are not stored.
|
// because activity IDs of remote follow requests are not stored.
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
|
use serde_json::Value;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
|
|
||||||
use crate::activitypub::{
|
use crate::activitypub::{
|
||||||
|
@ -122,8 +123,10 @@ async fn handle_update_person(
|
||||||
pub async fn handle_update(
|
pub async fn handle_update(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
activity: Activity,
|
activity: Value,
|
||||||
) -> HandlerResult {
|
) -> HandlerResult {
|
||||||
|
let activity: Activity = serde_json::from_value(activity)
|
||||||
|
.map_err(|_| ValidationError("unexpected activity structure"))?;
|
||||||
let object_type = activity.object["type"].as_str()
|
let object_type = activity.object["type"].as_str()
|
||||||
.ok_or(ValidationError("unknown object type"))?;
|
.ok_or(ValidationError("unknown object type"))?;
|
||||||
match object_type {
|
match object_type {
|
||||||
|
|
|
@ -10,7 +10,6 @@ use crate::errors::{
|
||||||
HttpError,
|
HttpError,
|
||||||
ValidationError,
|
ValidationError,
|
||||||
};
|
};
|
||||||
use super::activity::Activity;
|
|
||||||
use super::authentication::{
|
use super::authentication::{
|
||||||
verify_signed_activity,
|
verify_signed_activity,
|
||||||
verify_signed_request,
|
verify_signed_request,
|
||||||
|
@ -131,15 +130,15 @@ pub async fn receive_activity(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
request: &HttpRequest,
|
request: &HttpRequest,
|
||||||
activity_raw: &Value,
|
activity: &Value,
|
||||||
) -> Result<(), HandlerError> {
|
) -> Result<(), HandlerError> {
|
||||||
let activity_type = activity_raw["type"].as_str()
|
let activity_type = activity["type"].as_str()
|
||||||
.ok_or(ValidationError("type property is missing"))?;
|
.ok_or(ValidationError("type property is missing"))?;
|
||||||
let activity_actor = activity_raw["actor"].as_str()
|
let activity_actor = activity["actor"].as_str()
|
||||||
.ok_or(ValidationError("actor property is missing"))?;
|
.ok_or(ValidationError("actor property is missing"))?;
|
||||||
|
|
||||||
let is_self_delete = if activity_type == DELETE {
|
let is_self_delete = if activity_type == DELETE {
|
||||||
let object_id = find_object_id(&activity_raw["object"])?;
|
let object_id = find_object_id(&activity["object"])?;
|
||||||
object_id == activity_actor
|
object_id == activity_actor
|
||||||
} else { false };
|
} else { false };
|
||||||
|
|
||||||
|
@ -174,7 +173,7 @@ pub async fn receive_activity(
|
||||||
match verify_signed_activity(
|
match verify_signed_activity(
|
||||||
config,
|
config,
|
||||||
db_client,
|
db_client,
|
||||||
activity_raw,
|
activity,
|
||||||
// Don't fetch actor if this is Delete(Person) activity
|
// Don't fetch actor if this is Delete(Person) activity
|
||||||
is_self_delete,
|
is_self_delete,
|
||||||
).await {
|
).await {
|
||||||
|
@ -200,7 +199,7 @@ pub async fn receive_activity(
|
||||||
if config.blocked_instances.iter()
|
if config.blocked_instances.iter()
|
||||||
.any(|instance| signer.hostname.as_ref() == Some(instance))
|
.any(|instance| signer.hostname.as_ref() == Some(instance))
|
||||||
{
|
{
|
||||||
log::warn!("ignoring activity from blocked instance: {}", activity_raw);
|
log::warn!("ignoring activity from blocked instance: {}", activity);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -225,8 +224,7 @@ pub async fn receive_activity(
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
let activity: Activity = serde_json::from_value(activity_raw.clone())
|
let activity = activity.clone();
|
||||||
.map_err(|_| ValidationError("invalid activity"))?;
|
|
||||||
let maybe_object_type = match activity_type {
|
let maybe_object_type = match activity_type {
|
||||||
ACCEPT => {
|
ACCEPT => {
|
||||||
handle_accept(config, db_client, activity).await?
|
handle_accept(config, db_client, activity).await?
|
||||||
|
@ -265,7 +263,7 @@ pub async fn receive_activity(
|
||||||
handle_update(config, db_client, activity).await?
|
handle_update(config, db_client, activity).await?
|
||||||
},
|
},
|
||||||
_ => {
|
_ => {
|
||||||
log::warn!("activity type is not supported: {}", activity_raw);
|
log::warn!("activity type is not supported: {}", activity);
|
||||||
return Ok(());
|
return Ok(());
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue