Add actor() method to DbActorProfile type

This commit is contained in:
silverpill 2021-10-31 20:46:29 +00:00
parent e10804be64
commit 5c10ffc590
5 changed files with 36 additions and 28 deletions

View file

@ -2,7 +2,8 @@ use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use crate::config::Config;
use crate::models::profiles::types::ExtraField;
use crate::errors::ConversionError;
use crate::models::profiles::types::{DbActorProfile, ExtraField};
use crate::models::users::types::User;
use crate::utils::crypto::{deserialize_private_key, get_public_key_pem};
use crate::utils::files::get_file_url;
@ -101,6 +102,20 @@ impl Actor {
}
}
impl DbActorProfile {
pub fn actor(&self) -> Result<Option<Actor>, ConversionError> {
let actor = match self.actor_json {
Some(ref value) => {
let actor: Actor = serde_json::from_value(value.clone())
.map_err(|_| ConversionError)?;
Some(actor)
},
None => None,
};
Ok(actor)
}
}
pub fn get_actor_object(
config: &Config,
user: &User,

View file

@ -240,9 +240,8 @@ pub async fn receive_activity(
&activity.actor,
&config.media_dir(),
).await?;
let source_actor_value = source_profile.actor_json.ok_or(HttpError::InternalError)?;
let source_actor: Actor = serde_json::from_value(source_actor_value)
.map_err(|_| HttpError::InternalError)?;
let source_actor = source_profile.actor().ok().flatten()
.ok_or(HttpError::InternalError)?;
let target_actor_id = match activity.object.as_str() {
Some(object_id) => object_id.to_owned(),
None => {

View file

@ -4,7 +4,6 @@ use actix_web::{
};
use regex::Regex;
use crate::activitypub::actor::Actor;
use crate::activitypub::fetcher::fetch_profile_by_actor_id;
use crate::config::Config;
use crate::database::{Pool, get_database_client};
@ -141,9 +140,8 @@ pub async fn verify_http_signature(
},
},
};
let actor_value = actor_profile.actor_json.ok_or(VerificationError::ActorError)?;
let actor: Actor = serde_json::from_value(actor_value)
.map_err(|_| VerificationError::ActorError)?;
let actor = actor_profile.actor().ok().flatten()
.ok_or(VerificationError::ActorError)?;
let public_key = deserialize_public_key(&actor.public_key.public_key_pem)?;
let is_valid_signature = verify_signature(

View file

@ -7,7 +7,6 @@ use crate::activitypub::activity::{
create_activity_follow,
create_activity_undo_follow,
};
use crate::activitypub::actor::Actor;
use crate::activitypub::deliverer::deliver_activity;
use crate::config::Config;
use crate::database::{Pool, get_database_client};
@ -119,18 +118,17 @@ async fn follow(
let db_client = &mut **get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?;
let profile = get_profile_by_id(db_client, &account_id).await?;
let relationship = if let Some(actor_value) = profile.actor_json {
let maybe_remote_actor = profile.actor().map_err(|_| HttpError::InternalError)?;
let relationship = if let Some(remote_actor) = maybe_remote_actor {
// Remote follow
let request = follows::create_follow_request(db_client, &current_user.id, &profile.id).await?;
let actor: Actor = serde_json::from_value(actor_value)
.map_err(|_| HttpError::InternalError)?;
let activity = create_activity_follow(
&config.instance_url(),
&current_user.profile,
&request.id,
&actor.id,
&remote_actor.id,
);
deliver_activity(&config, &current_user, activity, vec![actor]);
deliver_activity(&config, &current_user, activity, vec![remote_actor]);
follows::get_relationship(db_client, &current_user.id, &profile.id).await?
} else {
follows::follow(db_client, &current_user.id, &profile.id).await?
@ -148,7 +146,8 @@ async fn unfollow(
let db_client = &mut **get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?;
let target_profile = get_profile_by_id(db_client, &account_id).await?;
let relationship = if let Some(actor_value) = target_profile.actor_json {
let maybe_remote_actor = target_profile.actor().map_err(|_| HttpError::InternalError)?;
let relationship = if let Some(remote_actor) = maybe_remote_actor {
// Remote follow
let follow_request = follows::get_follow_request_by_path(
db_client,
@ -161,15 +160,13 @@ async fn unfollow(
&target_profile.id,
).await?;
// Federate
let actor: Actor = serde_json::from_value(actor_value)
.map_err(|_| HttpError::InternalError)?;
let activity = create_activity_undo_follow(
&config.instance_url(),
&current_user.profile,
&follow_request.id,
&actor.id,
&remote_actor.id,
);
deliver_activity(&config, &current_user, activity, vec![actor]);
deliver_activity(&config, &current_user, activity, vec![remote_actor]);
// TODO: uncouple unfollow and get_relationship
relationship
} else {

View file

@ -64,11 +64,10 @@ async fn create_status(
let followers = get_followers(db_client, &current_user.id).await?;
let mut recipients: Vec<Actor> = Vec::new();
for follower in followers {
if let Some(actor_value) = follower.actor_json {
// Remote
let actor: Actor = serde_json::from_value(actor_value)
let maybe_remote_actor = follower.actor()
.map_err(|_| HttpError::InternalError)?;
recipients.push(actor);
if let Some(remote_actor) = maybe_remote_actor {
recipients.push(remote_actor);
};
};
deliver_activity(&config, &current_user, activity, recipients);
@ -143,7 +142,9 @@ async fn favourite(
get_actions_for_post(db_client, &current_user.id, &mut post).await?;
if reaction_created {
if let Some(actor_value) = &post.author.actor_json {
let maybe_remote_actor = post.author.actor()
.map_err(|_| HttpError::InternalError)?;
if let Some(remote_actor) = maybe_remote_actor {
// Federate
let object_id = post.object_id.as_ref().ok_or(HttpError::InternalError)?;
let activity = create_activity_like(
@ -151,9 +152,7 @@ async fn favourite(
&current_user.profile,
&object_id,
);
let recipient: Actor = serde_json::from_value(actor_value.clone())
.map_err(|_| HttpError::InternalError)?;
deliver_activity(&config, &current_user, activity, vec![recipient]);
deliver_activity(&config, &current_user, activity, vec![remote_actor]);
}
}