From 126c04febb4fe7ed4a5beaa00ab928c4c3e25745 Mon Sep 17 00:00:00 2001 From: silverpill Date: Mon, 21 Nov 2022 18:55:06 +0000 Subject: [PATCH] Include local posts in search results --- src/activitypub/fetcher/helpers.rs | 8 ++-- src/activitypub/views.rs | 2 +- src/mastodon_api/search/helpers.rs | 63 ++++++++++++++++++++---------- src/models/posts/helpers.rs | 13 +++++- src/models/posts/types.rs | 4 ++ src/monero/wallet.rs | 2 +- 6 files changed, 63 insertions(+), 29 deletions(-) diff --git a/src/activitypub/fetcher/helpers.rs b/src/activitypub/fetcher/helpers.rs index f1e23c6..ef1dd4c 100644 --- a/src/activitypub/fetcher/helpers.rs +++ b/src/activitypub/fetcher/helpers.rs @@ -15,10 +15,8 @@ use crate::activitypub::{ }; use crate::config::{Config, Instance}; use crate::errors::{DatabaseError, ValidationError}; -use crate::models::posts::queries::{ - get_post_by_id, - get_post_by_remote_object_id, -}; +use crate::models::posts::helpers::get_local_post_by_id; +use crate::models::posts::queries::get_post_by_remote_object_id; use crate::models::posts::types::Post; use crate::models::profiles::queries::{ get_profile_by_acct, @@ -207,7 +205,7 @@ pub async fn import_post( if let Ok(post_id) = parse_local_object_id(&instance.url(), &object_id) { // Object is a local post // Verify post exists, return error if it doesn't - get_post_by_id(db_client, &post_id).await?; + get_local_post_by_id(db_client, &post_id).await?; continue; }; match get_post_by_remote_object_id( diff --git a/src/activitypub/views.rs b/src/activitypub/views.rs index 6d2af87..a18ebdc 100644 --- a/src/activitypub/views.rs +++ b/src/activitypub/views.rs @@ -304,7 +304,7 @@ pub async fn object_view( // Try to find local post by ID, // return 404 if not found, or not public, or it is a repost let mut post = get_post_by_id(db_client, &internal_object_id).await?; - if !post.author.is_local() || !can_view_post(db_client, None, &post).await? { + if !post.is_local() || !can_view_post(db_client, None, &post).await? { return Err(HttpError::NotFoundError("post")); }; if !is_activitypub_request(request.headers()) { diff --git a/src/mastodon_api/search/helpers.rs b/src/mastodon_api/search/helpers.rs index 364878e..3fa74d3 100644 --- a/src/mastodon_api/search/helpers.rs +++ b/src/mastodon_api/search/helpers.rs @@ -4,19 +4,24 @@ use regex::Regex; use tokio_postgres::GenericClient; use url::Url; -use crate::activitypub::actors::types::ActorAddress; -use crate::activitypub::fetcher::helpers::{ - import_post, - import_profile_by_actor_address, +use crate::activitypub::{ + actors::types::ActorAddress, + fetcher::helpers::{ + import_post, + import_profile_by_actor_address, + }, + identifiers::parse_local_object_id, }; use crate::config::Config; -use crate::errors::{ValidationError, HttpError}; +use crate::errors::{DatabaseError, HttpError, ValidationError}; use crate::identity::did::Did; use crate::mastodon_api::accounts::types::Account; use crate::mastodon_api::statuses::helpers::build_status_list; use crate::mastodon_api::statuses::types::Tag; -use crate::models::posts::helpers::can_view_post; -use crate::models::posts::types::Post; +use crate::models::posts::{ + helpers::{can_view_post, get_local_post_by_id}, + types::Post, +}; use crate::models::profiles::queries::{ search_profiles, search_profiles_by_did_only, @@ -94,7 +99,7 @@ async fn search_profiles_or_import( username: String, mut maybe_hostname: Option, limit: u16, -) -> Result, HttpError> { +) -> Result, DatabaseError> { if let Some(ref hostname) = maybe_hostname { if hostname == &config.instance().host() { // This is a local profile @@ -129,21 +134,37 @@ async fn search_profiles_or_import( Ok(profiles) } -/// Finds public post by its object ID -async fn search_post( +/// Finds post by its object ID +async fn find_post_by_url( config: &Config, db_client: &mut impl GenericClient, url: String, -) -> Result, HttpError> { - let maybe_post = match import_post( - config, db_client, - url, - None, - ).await { - Ok(post) => Some(post), - Err(err) => { - log::warn!("{}", err); - None +) -> Result, DatabaseError> { + let maybe_post = match parse_local_object_id( + &config.instance_url(), + &url, + ) { + Ok(post_id) => { + // Local URL + match get_local_post_by_id(db_client, &post_id).await { + Ok(post) => Some(post), + Err(DatabaseError::NotFound(_)) => None, + Err(other_error) => return Err(other_error), + } + }, + Err(_) => { + match import_post( + config, + db_client, + url, + None, + ).await { + Ok(post) => Some(post), + Err(err) => { + log::warn!("{}", err); + None + }, + } }, }; Ok(maybe_post) @@ -177,7 +198,7 @@ pub async fn search( ).await?; }, SearchQuery::Url(url) => { - let maybe_post = search_post(config, db_client, url).await?; + let maybe_post = find_post_by_url(config, db_client, url).await?; if let Some(post) = maybe_post { if can_view_post(db_client, Some(current_user), &post).await? { posts = vec![post]; diff --git a/src/models/posts/helpers.rs b/src/models/posts/helpers.rs index d888834..217b7b9 100644 --- a/src/models/posts/helpers.rs +++ b/src/models/posts/helpers.rs @@ -124,6 +124,17 @@ pub async fn can_view_post( Ok(result) } +pub async fn get_local_post_by_id( + db_client: &impl GenericClient, + post_id: &Uuid, +) -> Result { + let post = get_post_by_id(db_client, post_id).await?; + if !post.is_local() { + return Err(DatabaseError::NotFound("post")); + }; + Ok(post) +} + pub async fn get_post_by_object_id( db_client: &impl GenericClient, instance_url: &str, @@ -132,7 +143,7 @@ pub async fn get_post_by_object_id( 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?; + let post = get_local_post_by_id(db_client, &post_id).await?; Ok(post) }, Err(_) => { diff --git a/src/models/posts/types.rs b/src/models/posts/types.rs index 87e33ad..6a40ca5 100644 --- a/src/models/posts/types.rs +++ b/src/models/posts/types.rs @@ -167,6 +167,10 @@ impl Post { Ok(post) } + pub fn is_local(&self) -> bool { + self.author.is_local() + } + pub fn is_public(&self) -> bool { matches!(self.visibility, Visibility::Public) } diff --git a/src/monero/wallet.rs b/src/monero/wallet.rs index 6879fa0..0055ce3 100644 --- a/src/monero/wallet.rs +++ b/src/monero/wallet.rs @@ -110,7 +110,7 @@ pub async fn send_monero( // TODO: transaction can fail // https://github.com/monero-project/monero/issues/8372 let maybe_transfer = wallet_client.get_transfer( - tx_hash.clone(), + tx_hash, Some(DEFAULT_ACCOUNT), ).await?; let transfer_status = maybe_transfer