Move get_post_by_object_id to activitypub::fetcher::helpers module

This commit is contained in:
silverpill 2023-03-22 22:45:43 +00:00
parent dae9be1388
commit 21135d7704
5 changed files with 25 additions and 25 deletions

View file

@ -165,6 +165,25 @@ pub async fn get_or_import_profile_by_actor_address(
Ok(profile) Ok(profile)
} }
pub async fn get_post_by_object_id(
db_client: &impl DatabaseClient,
instance_url: &str,
object_id: &str,
) -> Result<Post, DatabaseError> {
match parse_local_object_id(instance_url, object_id) {
Ok(post_id) => {
// Local post
let post = get_local_post_by_id(db_client, &post_id).await?;
Ok(post)
},
Err(_) => {
// Remote post
let post = get_post_by_remote_object_id(db_client, object_id).await?;
Ok(post)
},
}
}
const RECURSION_DEPTH_MAX: usize = 50; const RECURSION_DEPTH_MAX: usize = 50;
pub async fn import_post( pub async fn import_post(

View file

@ -17,6 +17,7 @@ use crate::activitypub::{
fetcher::helpers::{ fetcher::helpers::{
get_or_import_profile_by_actor_address, get_or_import_profile_by_actor_address,
get_or_import_profile_by_actor_id, get_or_import_profile_by_actor_id,
get_post_by_object_id,
import_post, import_post,
}, },
identifiers::{parse_local_actor_id, profile_actor_id}, identifiers::{parse_local_actor_id, profile_actor_id},
@ -41,7 +42,6 @@ use crate::models::{
}, },
posts::{ posts::{
hashtags::normalize_hashtag, hashtags::normalize_hashtag,
helpers::get_post_by_object_id,
mentions::mention_to_address, mentions::mention_to_address,
queries::create_post, queries::create_post,
types::{Post, PostCreateData, Visibility}, types::{Post, PostCreateData, Visibility},

View file

@ -4,7 +4,10 @@ use serde_json::Value;
use mitra_config::Config; use mitra_config::Config;
use crate::activitypub::{ use crate::activitypub::{
fetcher::helpers::get_or_import_profile_by_actor_id, fetcher::helpers::{
get_or_import_profile_by_actor_id,
get_post_by_object_id,
},
receiver::deserialize_into_object_id, receiver::deserialize_into_object_id,
vocabulary::NOTE, vocabulary::NOTE,
}; };
@ -12,7 +15,6 @@ use crate::database::{DatabaseClient, DatabaseError};
use crate::errors::ValidationError; use crate::errors::ValidationError;
use crate::models::{ use crate::models::{
reactions::queries::create_reaction, reactions::queries::create_reaction,
posts::helpers::get_post_by_object_id,
}; };
use super::HandlerResult; use super::HandlerResult;

View file

@ -1,6 +1,5 @@
use uuid::Uuid; use uuid::Uuid;
use crate::activitypub::identifiers::parse_local_object_id;
use crate::database::{DatabaseClient, DatabaseError}; use crate::database::{DatabaseClient, DatabaseError};
use crate::models::{ use crate::models::{
reactions::queries::find_favourited_by_user, reactions::queries::find_favourited_by_user,
@ -10,7 +9,6 @@ use crate::models::{
}; };
use super::queries::{ use super::queries::{
get_post_by_id, get_post_by_id,
get_post_by_remote_object_id,
get_related_posts, get_related_posts,
find_reposted_by_user, find_reposted_by_user,
}; };
@ -142,25 +140,6 @@ pub async fn get_local_post_by_id(
Ok(post) Ok(post)
} }
pub async fn get_post_by_object_id(
db_client: &impl DatabaseClient,
instance_url: &str,
object_id: &str,
) -> Result<Post, DatabaseError> {
match parse_local_object_id(instance_url, object_id) {
Ok(post_id) => {
// Local post
let post = get_local_post_by_id(db_client, &post_id).await?;
Ok(post)
},
Err(_) => {
// Remote post
let post = get_post_by_remote_object_id(db_client, object_id).await?;
Ok(post)
},
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use serial_test::serial; use serial_test::serial;

View file

@ -2,8 +2,8 @@ use std::collections::HashMap;
use regex::{Captures, Match, Regex}; use regex::{Captures, Match, Regex};
use crate::activitypub::fetcher::helpers::get_post_by_object_id;
use crate::database::{DatabaseClient, DatabaseError}; use crate::database::{DatabaseClient, DatabaseError};
use super::helpers::get_post_by_object_id;
use super::types::{Post, Visibility}; use super::types::{Post, Visibility};
// MediaWiki-like syntax: [[url|text]] // MediaWiki-like syntax: [[url|text]]