Move Post::object_id function to activitypub::identifiers

This commit is contained in:
silverpill 2023-03-18 13:20:09 +00:00
parent f27b2e13eb
commit a515af1111
9 changed files with 45 additions and 29 deletions

View file

@ -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);

View file

@ -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,
};

View file

@ -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(

View file

@ -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(),

View file

@ -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;

View file

@ -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<Attachment> = post.attachments.into_iter()
.map(|item| Attachment::from_db(base_url, item))
.collect();

View file

@ -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(

View file

@ -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)]

View file

@ -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<HttpResponse, HttpError> {
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();