From f76438b6f8bfed35842b3fe36f4bdffd45f02fe9 Mon Sep 17 00:00:00 2001 From: silverpill Date: Sat, 18 Mar 2023 17:25:52 +0000 Subject: [PATCH] Move DbActorProfile::actor_id function to activitypub::identifiers --- src/activitypub/builders/announce.rs | 5 +++-- src/activitypub/builders/create_note.rs | 8 ++++++-- src/activitypub/builders/delete_person.rs | 3 ++- src/activitypub/builders/like.rs | 9 +++++++-- src/activitypub/builders/undo_like.rs | 4 ++-- src/activitypub/handlers/create.rs | 4 ++-- src/activitypub/handlers/move.rs | 6 +++--- src/activitypub/identifiers.rs | 9 ++++++++- src/activitypub/receiver.rs | 3 ++- src/mastodon_api/accounts/views.rs | 23 ++++++++++++++++------- src/mastodon_api/settings/views.rs | 3 ++- src/models/profiles/types.rs | 8 -------- src/web_client/views.rs | 6 +++--- 13 files changed, 56 insertions(+), 35 deletions(-) diff --git a/src/activitypub/builders/announce.rs b/src/activitypub/builders/announce.rs index 06cc8a1..48ed3d4 100644 --- a/src/activitypub/builders/announce.rs +++ b/src/activitypub/builders/announce.rs @@ -12,6 +12,7 @@ use crate::activitypub::{ local_actor_id, local_object_id, post_object_id, + profile_actor_id, }, types::{build_default_context, Context}, vocabulary::ANNOUNCE, @@ -50,7 +51,7 @@ fn build_announce( .expect("repost_of field should be populated"); let object_id = post_object_id(instance_url, post); let activity_id = local_object_id(instance_url, &repost.id); - let recipient_id = post.author.actor_id(instance_url); + let recipient_id = profile_actor_id(instance_url, &post.author); let followers = local_actor_followers(instance_url, sender_username); Announce { context: build_default_context(), @@ -77,7 +78,7 @@ pub async fn get_announce_recipients( recipients.push(remote_actor); }; }; - let primary_recipient = post.author.actor_id(instance_url); + let primary_recipient = profile_actor_id(instance_url, &post.author); if let Some(remote_actor) = post.author.actor_json.as_ref() { recipients.push(remote_actor.clone()); }; diff --git a/src/activitypub/builders/create_note.rs b/src/activitypub/builders/create_note.rs index c58561f..9a976a7 100644 --- a/src/activitypub/builders/create_note.rs +++ b/src/activitypub/builders/create_note.rs @@ -15,6 +15,7 @@ use crate::activitypub::{ local_object_id, local_tag_collection, post_object_id, + profile_actor_id, }, types::{ build_default_context, @@ -134,7 +135,7 @@ pub fn build_note( let mut tags = vec![]; for profile in &post.mentions { let tag_name = format!("@{}", profile.actor_address(instance_hostname)); - let actor_id = profile.actor_id(instance_url); + let actor_id = profile_actor_id(instance_url, profile); if !primary_audience.contains(&actor_id) { primary_audience.push(actor_id.clone()); }; @@ -183,7 +184,10 @@ pub fn build_note( let in_reply_to = post.in_reply_to.as_ref() .expect("in_reply_to should be populated"); assert_eq!(in_reply_to.id, in_reply_to_id); - let in_reply_to_actor_id = in_reply_to.author.actor_id(instance_url); + let in_reply_to_actor_id = profile_actor_id( + instance_url, + &in_reply_to.author, + ); if !primary_audience.contains(&in_reply_to_actor_id) { primary_audience.push(in_reply_to_actor_id); }; diff --git a/src/activitypub/builders/delete_person.rs b/src/activitypub/builders/delete_person.rs index 5585240..218f585 100644 --- a/src/activitypub/builders/delete_person.rs +++ b/src/activitypub/builders/delete_person.rs @@ -7,6 +7,7 @@ use crate::activitypub::{ actors::types::Actor, constants::AP_PUBLIC, deliverer::OutgoingActivity, + identifiers::local_actor_id, types::{build_default_context, Context}, vocabulary::DELETE, }; @@ -35,7 +36,7 @@ fn build_delete_person( instance_url: &str, user: &User, ) -> DeletePerson { - let actor_id = user.profile.actor_id(instance_url); + let actor_id = local_actor_id(instance_url, &user.profile.username); let activity_id = format!("{}/delete", actor_id); DeletePerson { context: build_default_context(), diff --git a/src/activitypub/builders/like.rs b/src/activitypub/builders/like.rs index 7bafb30..9269fbe 100644 --- a/src/activitypub/builders/like.rs +++ b/src/activitypub/builders/like.rs @@ -7,7 +7,12 @@ use crate::activitypub::{ actors::types::Actor, constants::AP_PUBLIC, deliverer::OutgoingActivity, - identifiers::{local_actor_id, local_object_id, post_object_id}, + identifiers::{ + local_actor_id, + local_object_id, + post_object_id, + profile_actor_id, + }, types::{build_default_context, Context}, vocabulary::LIKE, }; @@ -94,7 +99,7 @@ pub async fn prepare_like( post, ).await?; let object_id = post_object_id(&instance.url(), post); - let post_author_id = post.author.actor_id(&instance.url()); + let post_author_id = profile_actor_id(&instance.url(), &post.author); let activity = build_like( &instance.url(), &sender.profile, diff --git a/src/activitypub/builders/undo_like.rs b/src/activitypub/builders/undo_like.rs index 680942a..213a7ed 100644 --- a/src/activitypub/builders/undo_like.rs +++ b/src/activitypub/builders/undo_like.rs @@ -5,7 +5,7 @@ use mitra_config::Instance; use crate::activitypub::{ deliverer::OutgoingActivity, - identifiers::{local_actor_id, local_object_id}, + identifiers::{local_actor_id, local_object_id, profile_actor_id}, types::{build_default_context, Context}, vocabulary::UNDO, }; @@ -71,7 +71,7 @@ pub async fn prepare_undo_like( &instance.url(), post, ).await?; - let post_author_id = post.author.actor_id(&instance.url()); + let post_author_id = profile_actor_id(&instance.url(), &post.author); let activity = build_undo_like( &instance.url(), &sender.profile, diff --git a/src/activitypub/handlers/create.rs b/src/activitypub/handlers/create.rs index 0f911f1..2b4acd3 100644 --- a/src/activitypub/handlers/create.rs +++ b/src/activitypub/handlers/create.rs @@ -19,7 +19,7 @@ use crate::activitypub::{ get_or_import_profile_by_actor_id, import_post, }, - identifiers::parse_local_actor_id, + identifiers::{parse_local_actor_id, profile_actor_id}, receiver::{parse_array, parse_property_value, HandlerError}, types::{Attachment, EmojiTag, Link, LinkTag, Object, Tag}, vocabulary::*, @@ -150,7 +150,7 @@ pub async fn get_object_attachments( }, }; if is_gnu_social_link( - &author.actor_id(&instance.url()), + &profile_actor_id(&instance.url(), author), &attachment, ) { // Don't fetch HTML pages attached by GNU Social diff --git a/src/activitypub/handlers/move.rs b/src/activitypub/handlers/move.rs index f2f3c66..2184cbf 100644 --- a/src/activitypub/handlers/move.rs +++ b/src/activitypub/handlers/move.rs @@ -9,7 +9,7 @@ use crate::activitypub::{ undo_follow::prepare_undo_follow, }, fetcher::helpers::get_or_import_profile_by_actor_id, - identifiers::parse_local_actor_id, + identifiers::{parse_local_actor_id, profile_actor_id}, receiver::parse_array, vocabulary::PERSON, }; @@ -64,7 +64,7 @@ pub async fn handle_move( &activity.object, ).await? }; - let old_actor_id = old_profile.actor_id(&instance.url()); + let old_actor_id = profile_actor_id(&instance.url(), &old_profile); let new_profile = get_or_import_profile_by_actor_id( db_client, &instance, @@ -77,7 +77,7 @@ pub async fn handle_move( // Find aliases by DIDs (signed) let mut aliases = find_aliases(db_client, &new_profile).await? .into_iter() - .map(|profile| profile.actor_id(&instance.url())) + .map(|profile| profile_actor_id(&instance.url(), &profile)) .collect::>(); // Read aliases from alsoKnownAs property // TODO: use new_profile.aliases.into_actor_ids() diff --git a/src/activitypub/identifiers.rs b/src/activitypub/identifiers.rs index d679f3a..f6d0259 100644 --- a/src/activitypub/identifiers.rs +++ b/src/activitypub/identifiers.rs @@ -125,13 +125,20 @@ pub fn post_object_id(instance_url: &str, post: &Post) -> String { } } +pub fn profile_actor_id(instance_url: &str, profile: &DbActorProfile) -> String { + match profile.actor_json { + Some(ref actor) => actor.id.clone(), + None => local_actor_id(instance_url, &profile.username), + } +} + pub fn profile_actor_url(instance_url: &str, profile: &DbActorProfile) -> String { if let Some(ref actor) = profile.actor_json { if let Some(ref actor_url) = actor.url { return actor_url.to_string(); }; }; - profile.actor_id(instance_url) + profile_actor_id(instance_url, profile) } #[cfg(test)] diff --git a/src/activitypub/receiver.rs b/src/activitypub/receiver.rs index d7d7ed9..54d7eca 100644 --- a/src/activitypub/receiver.rs +++ b/src/activitypub/receiver.rs @@ -35,6 +35,7 @@ use super::handlers::{ undo::handle_undo, update::handle_update, }; +use super::identifiers::profile_actor_id; use super::queues::IncomingActivityJobData; use super::vocabulary::*; @@ -298,7 +299,7 @@ pub async fn receive_activity( return Ok(()); }; - let signer_id = signer.actor_id(&config.instance_url()); + let signer_id = profile_actor_id(&config.instance_url(), &signer); let is_authenticated = activity_actor == signer_id; if !is_authenticated { match activity_type { diff --git a/src/mastodon_api/accounts/views.rs b/src/mastodon_api/accounts/views.rs index bd59aba..d275ee6 100644 --- a/src/mastodon_api/accounts/views.rs +++ b/src/mastodon_api/accounts/views.rs @@ -26,12 +26,15 @@ use mitra_utils::{ passwords::hash_password, }; -use crate::activitypub::builders::{ - undo_follow::prepare_undo_follow, - update_person::{ - build_update_person, - prepare_update_person, +use crate::activitypub::{ + builders::{ + undo_follow::prepare_undo_follow, + update_person::{ + build_update_person, + prepare_update_person, + }, }, + identifiers::local_actor_id, }; use crate::database::{get_database_client, DatabaseError, DbPool}; use crate::errors::ValidationError; @@ -369,7 +372,10 @@ async fn get_identity_claim( }, _ => return Err(ValidationError("unknown proof type").into()), }; - let actor_id = current_user.profile.actor_id(&config.instance_url()); + let actor_id = local_actor_id( + &config.instance_url(), + ¤t_user.profile.username, + ); let claim = create_identity_claim(&actor_id, &did) .map_err(|_| MastodonError::InternalError)?; let response = IdentityClaim { did, claim }; @@ -399,7 +405,10 @@ async fn create_identity_proof( Err(DatabaseError::NotFound(_)) => (), Err(other_error) => return Err(other_error.into()), }; - let actor_id = current_user.profile.actor_id(&config.instance_url()); + let actor_id = local_actor_id( + &config.instance_url(), + ¤t_user.profile.username, + ); let message = create_identity_claim(&actor_id, &did) .map_err(|_| ValidationError("invalid claim"))?; diff --git a/src/mastodon_api/settings/views.rs b/src/mastodon_api/settings/views.rs index 499d22b..339b026 100644 --- a/src/mastodon_api/settings/views.rs +++ b/src/mastodon_api/settings/views.rs @@ -11,6 +11,7 @@ use actix_web_httpauth::extractors::bearer::BearerAuth; use mitra_config::Config; use mitra_utils::passwords::hash_password; +use crate::activitypub::identifiers::profile_actor_id; use crate::database::{get_database_client, DatabaseError, DbPool}; use crate::errors::ValidationError; use crate::http::get_request_base_url; @@ -147,7 +148,7 @@ async fn move_followers( // Find known aliases of the current user let mut aliases = find_aliases(db_client, ¤t_user.profile).await? .into_iter() - .map(|profile| profile.actor_id(&instance.url())); + .map(|profile| profile_actor_id(&instance.url(), &profile)); if !aliases.any(|actor_id| actor_id == request_data.from_actor_id) { return Err(ValidationError("old profile is not an alias").into()); }; diff --git a/src/models/profiles/types.rs b/src/models/profiles/types.rs index 7ef701a..f03cf73 100644 --- a/src/models/profiles/types.rs +++ b/src/models/profiles/types.rs @@ -15,7 +15,6 @@ use mitra_utils::{ use crate::activitypub::{ actors::types::Actor, - identifiers::local_actor_id, }; use crate::database::{ json_macro::{json_from_sql, json_to_sql}, @@ -399,13 +398,6 @@ impl DbActorProfile { self.actor_json.is_none() } - pub fn actor_id(&self, instance_url: &str) -> String { - match self.actor_json { - Some(ref actor) => actor.id.clone(), - None => local_actor_id(instance_url, &self.username), - } - } - pub fn actor_address(&self, local_hostname: &str) -> ActorAddress { assert_eq!(self.hostname.is_none(), self.is_local()); ActorAddress { diff --git a/src/web_client/views.rs b/src/web_client/views.rs index 7032849..69a8fd0 100644 --- a/src/web_client/views.rs +++ b/src/web_client/views.rs @@ -14,7 +14,7 @@ use uuid::Uuid; use mitra_config::Config; use crate::activitypub::{ - identifiers::post_object_id, + identifiers::{post_object_id, profile_actor_id}, views::is_activitypub_request, }; use crate::database::{get_database_client, DbPool}; @@ -53,7 +53,7 @@ async fn profile_page_redirect_view( ) -> Result { let db_client = &**get_database_client(&db_pool).await?; let profile = get_profile_by_id(db_client, &profile_id).await?; - let actor_id = profile.actor_id(&config.instance_url()); + let actor_id = profile_actor_id(&config.instance_url(), &profile); let response = HttpResponse::Found() .append_header(("Location", actor_id)) .finish(); @@ -75,7 +75,7 @@ async fn profile_acct_page_redirect_view( ) -> Result { let db_client = &**get_database_client(&db_pool).await?; let profile = get_profile_by_acct(db_client, &acct).await?; - let actor_id = profile.actor_id(&config.instance_url()); + let actor_id = profile_actor_id(&config.instance_url(), &profile); let response = HttpResponse::Found() .append_header(("Location", actor_id)) .finish();