Add get_post_by_object_id helper

This commit is contained in:
silverpill 2022-10-15 11:54:20 +00:00
parent a74736d29f
commit 22d4695614
2 changed files with 44 additions and 39 deletions

View file

@ -15,7 +15,7 @@ use crate::activitypub::{
import_profile_by_actor_address, import_profile_by_actor_address,
ImportError, ImportError,
}, },
identifiers::{parse_local_actor_id, parse_local_object_id}, identifiers::parse_local_actor_id,
receiver::{parse_array, parse_property_value}, receiver::{parse_array, parse_property_value},
vocabulary::{DOCUMENT, HASHTAG, IMAGE, LINK, MENTION, NOTE}, vocabulary::{DOCUMENT, HASHTAG, IMAGE, LINK, MENTION, NOTE},
}; };
@ -23,12 +23,9 @@ use crate::config::Instance;
use crate::errors::{ConversionError, DatabaseError, ValidationError}; use crate::errors::{ConversionError, DatabaseError, ValidationError};
use crate::models::attachments::queries::create_attachment; use crate::models::attachments::queries::create_attachment;
use crate::models::posts::hashtags::normalize_hashtag; use crate::models::posts::hashtags::normalize_hashtag;
use crate::models::posts::helpers::get_post_by_object_id;
use crate::models::posts::mentions::mention_to_address; use crate::models::posts::mentions::mention_to_address;
use crate::models::posts::queries::{ use crate::models::posts::queries::create_post;
create_post,
get_post_by_id,
get_post_by_remote_object_id,
};
use crate::models::posts::types::{Post, PostCreateData, Visibility}; use crate::models::posts::types::{Post, PostCreateData, Visibility};
use crate::models::profiles::queries::get_profile_by_acct; use crate::models::profiles::queries::get_profile_by_acct;
use crate::models::profiles::types::DbActorProfile; use crate::models::profiles::types::DbActorProfile;
@ -122,27 +119,6 @@ fn get_note_visibility(
Visibility::Direct Visibility::Direct
} }
async fn get_internal_post_id(
db_client: &impl GenericClient,
instance_url: &str,
object_id: &String,
redirects: &HashMap<String, String>,
) -> Result<Uuid, ImportError> {
match parse_local_object_id(instance_url, object_id) {
Ok(post_id) => {
// Local post
let post = get_post_by_id(db_client, &post_id).await?;
Ok(post.id)
},
Err(_) => {
let real_object_id = redirects.get(object_id)
.unwrap_or(object_id);
let post = get_post_by_remote_object_id(db_client, real_object_id).await?;
Ok(post.id)
},
}
}
pub async fn handle_note( pub async fn handle_note(
db_client: &mut impl GenericClient, db_client: &mut impl GenericClient,
instance: &Instance, instance: &Instance,
@ -316,37 +292,41 @@ pub async fn handle_note(
// Unknown media type // Unknown media type
continue; continue;
}; };
if let Some(href) = tag.href { if let Some(ref href) = tag.href {
let linked_id = get_internal_post_id( let href = redirects.get(href).unwrap_or(href);
let linked = get_post_by_object_id(
db_client, db_client,
&instance.url(), &instance.url(),
&href, href,
redirects,
).await?; ).await?;
links.push(linked_id); if !links.contains(&linked.id) {
links.push(linked.id);
};
}; };
}; };
}; };
}; };
if let Some(ref object_id) = object.quote_url { if let Some(ref object_id) = object.quote_url {
let linked_id = get_internal_post_id( let object_id = redirects.get(object_id).unwrap_or(object_id);
let linked = get_post_by_object_id(
db_client, db_client,
&instance.url(), &instance.url(),
object_id, object_id,
redirects,
).await?; ).await?;
links.push(linked_id); if !links.contains(&linked.id) {
links.push(linked.id);
};
}; };
let in_reply_to_id = match object.in_reply_to { let in_reply_to_id = match object.in_reply_to {
Some(ref object_id) => { Some(ref object_id) => {
let in_reply_to_id = get_internal_post_id( let object_id = redirects.get(object_id).unwrap_or(object_id);
let in_reply_to = get_post_by_object_id(
db_client, db_client,
&instance.url(), &instance.url(),
object_id, object_id,
redirects,
).await?; ).await?;
Some(in_reply_to_id) Some(in_reply_to.id)
}, },
None => None, None => None,
}; };

View file

@ -1,12 +1,18 @@
use tokio_postgres::GenericClient; use tokio_postgres::GenericClient;
use uuid::Uuid; use uuid::Uuid;
use crate::activitypub::identifiers::parse_local_object_id;
use crate::errors::DatabaseError; use crate::errors::DatabaseError;
use crate::models::reactions::queries::find_favourited_by_user; use crate::models::reactions::queries::find_favourited_by_user;
use crate::models::relationships::queries::has_relationship; use crate::models::relationships::queries::has_relationship;
use crate::models::relationships::types::RelationshipType; use crate::models::relationships::types::RelationshipType;
use crate::models::users::types::User; use crate::models::users::types::User;
use super::queries::{get_related_posts, find_reposted_by_user}; use super::queries::{
get_post_by_id,
get_post_by_remote_object_id,
get_related_posts,
find_reposted_by_user,
};
use super::types::{Post, PostActions, Visibility}; use super::types::{Post, PostActions, Visibility};
pub async fn add_related_posts( pub async fn add_related_posts(
@ -118,6 +124,25 @@ pub async fn can_view_post(
Ok(result) Ok(result)
} }
pub async fn get_post_by_object_id(
db_client: &impl GenericClient,
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_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;