Move DbActorProfile::actor_url function to activitypub::identifiers

This commit is contained in:
silverpill 2023-03-18 16:36:15 +00:00
parent a515af1111
commit 306fd7b75b
5 changed files with 35 additions and 15 deletions

View file

@ -2,7 +2,10 @@ use regex::Regex;
use uuid::Uuid; use uuid::Uuid;
use crate::errors::ValidationError; use crate::errors::ValidationError;
use crate::models::posts::types::Post; use crate::models::{
posts::types::Post,
profiles::types::DbActorProfile,
};
const ACTOR_KEY_SUFFIX: &str = "#main-key"; const ACTOR_KEY_SUFFIX: &str = "#main-key";
@ -122,6 +125,15 @@ pub fn post_object_id(instance_url: &str, post: &Post) -> String {
} }
} }
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)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use mitra_utils::id::generate_ulid; use mitra_utils::id::generate_ulid;
@ -188,4 +200,17 @@ mod tests {
).unwrap_err(); ).unwrap_err();
assert_eq!(error.to_string(), "invalid object ID"); assert_eq!(error.to_string(), "invalid object ID");
} }
#[test]
fn test_profile_actor_url() {
let profile = DbActorProfile {
username: "test".to_string(),
..Default::default()
};
let profile_url = profile_actor_url(INSTANCE_URL, &profile);
assert_eq!(
profile_url,
"https://example.org/users/test",
);
}
} }

View file

@ -9,7 +9,10 @@ use mitra_utils::{
markdown::markdown_basic_to_html, markdown::markdown_basic_to_html,
}; };
use crate::activitypub::actors::helpers::ACTOR_IMAGE_MAX_SIZE; use crate::activitypub::{
actors::helpers::ACTOR_IMAGE_MAX_SIZE,
identifiers::profile_actor_url,
};
use crate::errors::ValidationError; use crate::errors::ValidationError;
use crate::mastodon_api::{ use crate::mastodon_api::{
custom_emojis::types::CustomEmoji, custom_emojis::types::CustomEmoji,
@ -127,7 +130,7 @@ impl Account {
instance_url: &str, instance_url: &str,
profile: DbActorProfile, profile: DbActorProfile,
) -> Self { ) -> Self {
let profile_url = profile.actor_url(instance_url); let profile_url = profile_actor_url(instance_url, &profile);
let avatar_url = profile.avatar let avatar_url = profile.avatar
.map(|image| get_file_url(base_url, &image.file_name)); .map(|image| get_file_url(base_url, &image.file_name));
let header_url = profile.banner let header_url = profile.banner

View file

@ -5,6 +5,7 @@ use uuid::Uuid;
use crate::activitypub::identifiers::{ use crate::activitypub::identifiers::{
local_tag_collection, local_tag_collection,
post_object_id, post_object_id,
profile_actor_url,
}; };
use crate::mastodon_api::{ use crate::mastodon_api::{
accounts::types::Account, accounts::types::Account,
@ -32,7 +33,7 @@ impl Mention {
id: profile.id.to_string(), id: profile.id.to_string(),
username: profile.username.clone(), username: profile.username.clone(),
acct: profile.acct.clone(), acct: profile.acct.clone(),
url: profile.actor_url(instance_url), url: profile_actor_url(instance_url, &profile),
} }
} }
} }

View file

@ -2,6 +2,7 @@ use std::collections::HashMap;
use regex::{Captures, Regex}; use regex::{Captures, Regex};
use crate::activitypub::identifiers::profile_actor_url;
use crate::database::{DatabaseClient, DatabaseError}; use crate::database::{DatabaseClient, DatabaseError};
use crate::errors::ValidationError; use crate::errors::ValidationError;
use crate::models::{ use crate::models::{
@ -85,7 +86,7 @@ pub fn replace_mentions(
if let Some(profile) = mention_map.get(&acct) { if let Some(profile) = mention_map.get(&acct) {
// Replace with a link to profile. // Replace with a link to profile.
// Actor URL may differ from actor ID. // Actor URL may differ from actor ID.
let url = profile.actor_url(instance_url); let url = profile_actor_url(instance_url, profile);
#[allow(clippy::to_string_in_format_args)] #[allow(clippy::to_string_in_format_args)]
return format!( return format!(
// https://microformats.org/wiki/h-card // https://microformats.org/wiki/h-card

View file

@ -406,16 +406,6 @@ impl DbActorProfile {
} }
} }
/// Profile URL
pub fn actor_url(&self, instance_url: &str) -> String {
if let Some(ref actor) = self.actor_json {
if let Some(ref actor_url) = actor.url {
return actor_url.to_string();
};
};
self.actor_id(instance_url)
}
pub fn actor_address(&self, local_hostname: &str) -> ActorAddress { pub fn actor_address(&self, local_hostname: &str) -> ActorAddress {
assert_eq!(self.hostname.is_none(), self.is_local()); assert_eq!(self.hostname.is_none(), self.is_local());
ActorAddress { ActorAddress {