From 1d1618d6a236876e4ad36dc32b0622a53063603e Mon Sep 17 00:00:00 2001 From: silverpill Date: Sun, 21 Nov 2021 15:08:20 +0000 Subject: [PATCH] Add uri attribute to Status object --- src/activitypub/views.rs | 10 +++++----- src/mastodon_api/statuses/types.rs | 3 +++ src/models/posts/types.rs | 8 ++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/activitypub/views.rs b/src/activitypub/views.rs index 908b9e6..b3fd9ff 100644 --- a/src/activitypub/views.rs +++ b/src/activitypub/views.rs @@ -41,8 +41,8 @@ pub fn get_instance_actor_url(instance_url: &str) -> String { format!("{}/actor", instance_url) } -pub fn get_object_url(instance_url: &str, object_uuid: &Uuid) -> String { - format!("{}/objects/{}", instance_url, object_uuid) +pub fn get_object_url(instance_url: &str, internal_object_id: &Uuid) -> String { + format!("{}/objects/{}", instance_url, internal_object_id) } fn is_activitypub_request(request: &HttpRequest) -> bool { @@ -180,13 +180,13 @@ pub async fn object_view( config: web::Data, db_pool: web::Data, request: HttpRequest, - web::Path(object_id): web::Path, + web::Path(internal_object_id): web::Path, ) -> Result { let db_client = &**get_database_client(&db_pool).await?; // Try to find local post by ID, return 404 if not found - let thread = get_thread(db_client, &object_id, None).await?; + let thread = get_thread(db_client, &internal_object_id, None).await?; let post = thread.iter() - .find(|post| post.id == object_id && post.author.is_local()) + .find(|post| post.id == internal_object_id && post.author.is_local()) .ok_or(HttpError::NotFoundError("post"))?; if !is_activitypub_request(&request) { let page_url = get_post_page_url(&post.id, &config.instance_url()); diff --git a/src/mastodon_api/statuses/types.rs b/src/mastodon_api/statuses/types.rs index e8c9ae4..41883b4 100644 --- a/src/mastodon_api/statuses/types.rs +++ b/src/mastodon_api/statuses/types.rs @@ -31,6 +31,7 @@ impl Mention { #[derive(Serialize)] pub struct Status { pub id: Uuid, + pub uri: String, pub created_at: DateTime, pub account: Account, pub content: String, @@ -52,6 +53,7 @@ pub struct Status { impl Status { pub fn from_post(post: Post, instance_url: &str) -> Self { + let object_id = post.get_object_id(instance_url); let attachments: Vec = post.attachments.into_iter() .map(|item| Attachment::from_db(item, instance_url)) .collect(); @@ -65,6 +67,7 @@ impl Status { }; Self { id: post.id, + uri: object_id, created_at: post.created_at, account: account, content: post.content, diff --git a/src/models/posts/types.rs b/src/models/posts/types.rs index 76e9e0b..94756ad 100644 --- a/src/models/posts/types.rs +++ b/src/models/posts/types.rs @@ -5,6 +5,7 @@ use postgres_types::FromSql; use tokio_postgres::Row; use uuid::Uuid; +use crate::activitypub::views::get_object_url; use crate::database::int_enum::{int_enum_from_sql, int_enum_to_sql}; use crate::errors::{ConversionError, DatabaseError, ValidationError}; use crate::models::attachments::types::DbMediaAttachment; @@ -119,6 +120,13 @@ impl Post { pub fn is_public(&self) -> bool { matches!(self.visibility, Visibility::Public) } + + pub fn get_object_id(&self, instance_url: &str) -> String { + match &self.object_id { + Some(object_id) => object_id.to_string(), + None => get_object_url(instance_url, &self.id), + } + } } #[cfg(test)]