Add handler function for Create() activity

This commit is contained in:
silverpill 2022-12-07 18:46:00 +00:00
parent c218d3ebce
commit e0053f19c7
2 changed files with 28 additions and 18 deletions

View file

@ -7,18 +7,19 @@ use tokio_postgres::GenericClient;
use uuid::Uuid; use uuid::Uuid;
use crate::activitypub::{ use crate::activitypub::{
activity::{Attachment, Link, Object, Tag}, activity::{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::{
get_or_import_profile_by_actor_id, get_or_import_profile_by_actor_id,
import_post,
import_profile_by_actor_address, import_profile_by_actor_address,
}, },
identifiers::parse_local_actor_id, identifiers::parse_local_actor_id,
receiver::{parse_array, parse_property_value, HandlerError}, receiver::{parse_array, parse_property_value, HandlerError},
vocabulary::*, vocabulary::*,
}; };
use crate::config::Instance; use crate::config::{Config, Instance};
use crate::database::DatabaseError; use crate::database::DatabaseError;
use crate::errors::{ConversionError, ValidationError}; use crate::errors::{ConversionError, ValidationError};
use crate::models::attachments::queries::create_attachment; use crate::models::attachments::queries::create_attachment;
@ -34,6 +35,7 @@ use crate::models::profiles::queries::get_profile_by_acct;
use crate::models::profiles::types::DbActorProfile; use crate::models::profiles::types::DbActorProfile;
use crate::models::users::queries::get_user_by_name; use crate::models::users::queries::get_user_by_name;
use crate::utils::html::clean_html; use crate::utils::html::clean_html;
use super::HandlerResult;
fn get_note_author_id(object: &Object) -> Result<String, ValidationError> { fn get_note_author_id(object: &Object) -> Result<String, ValidationError> {
let attributed_to = object.attributed_to.as_ref() let attributed_to = object.attributed_to.as_ref()
@ -386,6 +388,26 @@ pub async fn handle_note(
Ok(post) Ok(post)
} }
pub async fn handle_create(
config: &Config,
db_client: &mut impl GenericClient,
activity: Activity,
signer_id: &str,
) -> HandlerResult {
let object: Object = serde_json::from_value(activity.object)
.map_err(|_| ValidationError("invalid object"))?;
let object_id = object.id.clone();
let object_received = if activity.actor == signer_id {
Some(object)
} else {
// Fetch forwarded object, don't trust the sender.
// Most likely it's a forwarded reply.
None
};
import_post(config, db_client, object_id, object_received).await?;
Ok(Some(NOTE))
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serde_json::json; use serde_json::json;

View file

@ -10,20 +10,18 @@ use crate::errors::{
HttpError, HttpError,
ValidationError, ValidationError,
}; };
use super::activity::{Activity, Object}; use super::activity::Activity;
use super::authentication::{ use super::authentication::{
verify_signed_activity, verify_signed_activity,
verify_signed_request, verify_signed_request,
AuthenticationError, AuthenticationError,
}; };
use super::fetcher::{ use super::fetcher::fetchers::FetchError;
fetchers::FetchError,
helpers::import_post,
};
use super::handlers::{ use super::handlers::{
accept_follow::handle_accept_follow, accept_follow::handle_accept_follow,
add::handle_add, add::handle_add,
announce::handle_announce, announce::handle_announce,
create_note::handle_create,
delete::handle_delete, delete::handle_delete,
follow::handle_follow, follow::handle_follow,
like::handle_like, like::handle_like,
@ -220,17 +218,7 @@ pub async fn receive_activity(
handle_reject_follow(config, db_client, activity).await? handle_reject_follow(config, db_client, activity).await?
}, },
CREATE => { CREATE => {
let object: Object = serde_json::from_value(activity.object) handle_create(config, db_client, activity, &signer_id).await?
.map_err(|_| ValidationError("invalid object"))?;
let object_id = object.id.clone();
let object_received = if activity.actor == signer_id {
Some(object)
} else {
// Fetch forwarded note, don't trust the sender
None
};
import_post(config, db_client, object_id, object_received).await?;
Some(NOTE)
}, },
ANNOUNCE => { ANNOUNCE => {
require_actor_signature(&activity.actor, &signer_id)?; require_actor_signature(&activity.actor, &signer_id)?;