From a515af1111e13bec66bd26933e9a2029a769d27b Mon Sep 17 00:00:00 2001 From: silverpill Date: Sat, 18 Mar 2023 13:20:09 +0000 Subject: [PATCH] Move Post::object_id function to activitypub::identifiers --- src/activitypub/builders/announce.rs | 12 +++++++++--- src/activitypub/builders/create_note.rs | 7 ++++--- src/activitypub/builders/delete_note.rs | 5 +++-- src/activitypub/builders/like.rs | 4 ++-- src/activitypub/identifiers.rs | 8 ++++++++ src/mastodon_api/statuses/types.rs | 7 +++++-- src/mastodon_api/statuses/views.rs | 20 ++++++++++++-------- src/models/posts/types.rs | 8 -------- src/web_client/views.rs | 3 ++- 9 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/activitypub/builders/announce.rs b/src/activitypub/builders/announce.rs index 0cb7658..06cc8a1 100644 --- a/src/activitypub/builders/announce.rs +++ b/src/activitypub/builders/announce.rs @@ -7,7 +7,12 @@ use crate::activitypub::{ actors::types::Actor, constants::AP_PUBLIC, deliverer::OutgoingActivity, - identifiers::{local_actor_followers, local_actor_id, local_object_id}, + identifiers::{ + local_actor_followers, + local_actor_id, + local_object_id, + post_object_id, + }, types::{build_default_context, Context}, vocabulary::ANNOUNCE, }; @@ -41,8 +46,9 @@ fn build_announce( repost: &Post, ) -> Announce { let actor_id = local_actor_id(instance_url, sender_username); - let post = repost.repost_of.as_ref().unwrap(); - let object_id = post.object_id(instance_url); + let post = repost.repost_of.as_ref() + .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 followers = local_actor_followers(instance_url, sender_username); diff --git a/src/activitypub/builders/create_note.rs b/src/activitypub/builders/create_note.rs index 22c3848..c58561f 100644 --- a/src/activitypub/builders/create_note.rs +++ b/src/activitypub/builders/create_note.rs @@ -14,6 +14,7 @@ use crate::activitypub::{ local_emoji_id, local_object_id, local_tag_collection, + post_object_id, }, types::{ build_default_context, @@ -158,7 +159,7 @@ pub fn build_note( for linked in &post.linked { // Build FEP-e232 object link // https://codeberg.org/fediverse/fep/src/branch/main/feps/fep-e232.md - let link_href = linked.object_id(instance_url); + let link_href = post_object_id(instance_url, linked); let tag = LinkTag { name: None, // no microsyntax tag_type: LINK.to_string(), @@ -170,7 +171,7 @@ pub fn build_note( }; }; let maybe_quote_url = post.linked.get(0) - .map(|linked| linked.object_id(instance_url)); + .map(|linked| post_object_id(instance_url, linked)); for emoji in &post.emojis { let tag = build_emoji_tag(instance_url, emoji); @@ -186,7 +187,7 @@ pub fn build_note( if !primary_audience.contains(&in_reply_to_actor_id) { primary_audience.push(in_reply_to_actor_id); }; - Some(in_reply_to.object_id(instance_url)) + Some(post_object_id(instance_url, in_reply_to)) }, None => None, }; diff --git a/src/activitypub/builders/delete_note.rs b/src/activitypub/builders/delete_note.rs index 43c2b7b..27da0cb 100644 --- a/src/activitypub/builders/delete_note.rs +++ b/src/activitypub/builders/delete_note.rs @@ -4,7 +4,7 @@ use mitra_config::Instance; use crate::activitypub::{ deliverer::OutgoingActivity, - identifiers::local_actor_id, + identifiers::{local_actor_id, local_object_id}, types::{build_default_context, Context}, vocabulary::{DELETE, NOTE, TOMBSTONE}, }; @@ -52,7 +52,8 @@ fn build_delete_note( instance_url: &str, post: &Post, ) -> DeleteNote { - let object_id = post.object_id(instance_url); + assert!(post.is_local()); + let object_id = local_object_id(instance_url, &post.id); let activity_id = format!("{}/delete", object_id); let actor_id = local_actor_id(instance_url, &post.author.username); let Note { to, cc, .. } = build_note( diff --git a/src/activitypub/builders/like.rs b/src/activitypub/builders/like.rs index 22c5cd3..7bafb30 100644 --- a/src/activitypub/builders/like.rs +++ b/src/activitypub/builders/like.rs @@ -7,7 +7,7 @@ use crate::activitypub::{ actors::types::Actor, constants::AP_PUBLIC, deliverer::OutgoingActivity, - identifiers::{local_actor_id, local_object_id}, + identifiers::{local_actor_id, local_object_id, post_object_id}, types::{build_default_context, Context}, vocabulary::LIKE, }; @@ -93,7 +93,7 @@ pub async fn prepare_like( &instance.url(), post, ).await?; - let object_id = post.object_id(&instance.url()); + let object_id = post_object_id(&instance.url(), post); let post_author_id = post.author.actor_id(&instance.url()); let activity = build_like( &instance.url(), diff --git a/src/activitypub/identifiers.rs b/src/activitypub/identifiers.rs index 67336b6..b8d2f9c 100644 --- a/src/activitypub/identifiers.rs +++ b/src/activitypub/identifiers.rs @@ -2,6 +2,7 @@ use regex::Regex; use uuid::Uuid; use crate::errors::ValidationError; +use crate::models::posts::types::Post; const ACTOR_KEY_SUFFIX: &str = "#main-key"; @@ -114,6 +115,13 @@ pub fn parse_local_object_id( Ok(internal_object_id) } +pub fn post_object_id(instance_url: &str, post: &Post) -> String { + match post.object_id { + Some(ref object_id) => object_id.to_string(), + None => local_object_id(instance_url, &post.id), + } +} + #[cfg(test)] mod tests { use mitra_utils::id::generate_ulid; diff --git a/src/mastodon_api/statuses/types.rs b/src/mastodon_api/statuses/types.rs index 877ebfa..0375ce0 100644 --- a/src/mastodon_api/statuses/types.rs +++ b/src/mastodon_api/statuses/types.rs @@ -2,7 +2,10 @@ use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; use uuid::Uuid; -use crate::activitypub::identifiers::local_tag_collection; +use crate::activitypub::identifiers::{ + local_tag_collection, + post_object_id, +}; use crate::mastodon_api::{ accounts::types::Account, custom_emojis::types::CustomEmoji, @@ -90,7 +93,7 @@ impl Status { instance_url: &str, post: Post, ) -> Self { - let object_id = post.object_id(instance_url); + let object_id = post_object_id(instance_url, &post); let attachments: Vec = post.attachments.into_iter() .map(|item| Attachment::from_db(base_url, item)) .collect(); diff --git a/src/mastodon_api/statuses/views.rs b/src/mastodon_api/statuses/views.rs index 628ccab..08d277a 100644 --- a/src/mastodon_api/statuses/views.rs +++ b/src/mastodon_api/statuses/views.rs @@ -15,13 +15,16 @@ use uuid::Uuid; use mitra_config::Config; use mitra_utils::markdown::markdown_lite_to_html; -use crate::activitypub::builders::{ - announce::prepare_announce, - create_note::prepare_create_note, - delete_note::prepare_delete_note, - like::prepare_like, - undo_announce::prepare_undo_announce, - undo_like::prepare_undo_like, +use crate::activitypub::{ + builders::{ + announce::prepare_announce, + create_note::prepare_create_note, + delete_note::prepare_delete_note, + like::prepare_like, + undo_announce::prepare_undo_announce, + undo_like::prepare_undo_like, + }, + identifiers::local_object_id, }; use crate::database::{get_database_client, DatabaseError, DbPool}; use crate::errors::ValidationError; @@ -565,7 +568,8 @@ async fn make_permanent( attachment.ipfs_cid = Some(image_cid.clone()); attachments.push((attachment.id, image_cid)); }; - let post_url = post.object_id(&config.instance_url()); + assert!(post.is_local()); + let post_url = local_object_id(&config.instance_url(), &post.id); let maybe_post_image_cid = post.attachments.first() .and_then(|attachment| attachment.ipfs_cid.as_deref()); let post_metadata = PostMetadata::new( diff --git a/src/models/posts/types.rs b/src/models/posts/types.rs index bcb8a40..8a960d5 100644 --- a/src/models/posts/types.rs +++ b/src/models/posts/types.rs @@ -3,7 +3,6 @@ use postgres_types::FromSql; use tokio_postgres::Row; use uuid::Uuid; -use crate::activitypub::identifiers::local_object_id; use crate::database::{ int_enum::{int_enum_from_sql, int_enum_to_sql}, DatabaseError, @@ -181,13 +180,6 @@ impl Post { pub fn is_public(&self) -> bool { matches!(self.visibility, Visibility::Public) } - - pub fn object_id(&self, instance_url: &str) -> String { - match &self.object_id { - Some(object_id) => object_id.to_string(), - None => local_object_id(instance_url, &self.id), - } - } } #[cfg(test)] diff --git a/src/web_client/views.rs b/src/web_client/views.rs index a95ef29..7032849 100644 --- a/src/web_client/views.rs +++ b/src/web_client/views.rs @@ -14,6 +14,7 @@ use uuid::Uuid; use mitra_config::Config; use crate::activitypub::{ + identifiers::post_object_id, views::is_activitypub_request, }; use crate::database::{get_database_client, DbPool}; @@ -96,7 +97,7 @@ async fn post_page_redirect_view( ) -> Result { let db_client = &**get_database_client(&db_pool).await?; let post = get_post_by_id(db_client, &post_id).await?; - let object_id = post.object_id(&config.instance_url()); + let object_id = post_object_id(&config.instance_url(), &post); let response = HttpResponse::Found() .append_header(("Location", object_id)) .finish();