Include local posts in search results

This commit is contained in:
silverpill 2022-11-21 18:55:06 +00:00
parent 5789a4f021
commit 126c04febb
6 changed files with 63 additions and 29 deletions

View file

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

View file

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

View file

@ -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::{
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<String>,
limit: u16,
) -> Result<Vec<DbActorProfile>, HttpError> {
) -> Result<Vec<DbActorProfile>, DatabaseError> {
if let Some(ref hostname) = maybe_hostname {
if hostname == &config.instance().host() {
// This is a local profile
@ -129,14 +134,28 @@ 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<Option<Post>, HttpError> {
let maybe_post = match import_post(
config, db_client,
) -> Result<Option<Post>, 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 {
@ -145,6 +164,8 @@ async fn search_post(
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];

View file

@ -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<Post, DatabaseError> {
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(_) => {

View file

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

View file

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