Include local posts in search results
This commit is contained in:
parent
5789a4f021
commit
126c04febb
6 changed files with 63 additions and 29 deletions
|
@ -15,10 +15,8 @@ use crate::activitypub::{
|
||||||
};
|
};
|
||||||
use crate::config::{Config, Instance};
|
use crate::config::{Config, Instance};
|
||||||
use crate::errors::{DatabaseError, ValidationError};
|
use crate::errors::{DatabaseError, ValidationError};
|
||||||
use crate::models::posts::queries::{
|
use crate::models::posts::helpers::get_local_post_by_id;
|
||||||
get_post_by_id,
|
use crate::models::posts::queries::get_post_by_remote_object_id;
|
||||||
get_post_by_remote_object_id,
|
|
||||||
};
|
|
||||||
use crate::models::posts::types::Post;
|
use crate::models::posts::types::Post;
|
||||||
use crate::models::profiles::queries::{
|
use crate::models::profiles::queries::{
|
||||||
get_profile_by_acct,
|
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) {
|
if let Ok(post_id) = parse_local_object_id(&instance.url(), &object_id) {
|
||||||
// Object is a local post
|
// Object is a local post
|
||||||
// Verify post exists, return error if it doesn't
|
// 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;
|
continue;
|
||||||
};
|
};
|
||||||
match get_post_by_remote_object_id(
|
match get_post_by_remote_object_id(
|
||||||
|
|
|
@ -304,7 +304,7 @@ pub async fn object_view(
|
||||||
// Try to find local post by ID,
|
// Try to find local post by ID,
|
||||||
// return 404 if not found, or not public, or it is a repost
|
// 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?;
|
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"));
|
return Err(HttpError::NotFoundError("post"));
|
||||||
};
|
};
|
||||||
if !is_activitypub_request(request.headers()) {
|
if !is_activitypub_request(request.headers()) {
|
||||||
|
|
|
@ -4,19 +4,24 @@ use regex::Regex;
|
||||||
use tokio_postgres::GenericClient;
|
use tokio_postgres::GenericClient;
|
||||||
use url::Url;
|
use url::Url;
|
||||||
|
|
||||||
use crate::activitypub::actors::types::ActorAddress;
|
use crate::activitypub::{
|
||||||
use crate::activitypub::fetcher::helpers::{
|
actors::types::ActorAddress,
|
||||||
|
fetcher::helpers::{
|
||||||
import_post,
|
import_post,
|
||||||
import_profile_by_actor_address,
|
import_profile_by_actor_address,
|
||||||
|
},
|
||||||
|
identifiers::parse_local_object_id,
|
||||||
};
|
};
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::errors::{ValidationError, HttpError};
|
use crate::errors::{DatabaseError, HttpError, ValidationError};
|
||||||
use crate::identity::did::Did;
|
use crate::identity::did::Did;
|
||||||
use crate::mastodon_api::accounts::types::Account;
|
use crate::mastodon_api::accounts::types::Account;
|
||||||
use crate::mastodon_api::statuses::helpers::build_status_list;
|
use crate::mastodon_api::statuses::helpers::build_status_list;
|
||||||
use crate::mastodon_api::statuses::types::Tag;
|
use crate::mastodon_api::statuses::types::Tag;
|
||||||
use crate::models::posts::helpers::can_view_post;
|
use crate::models::posts::{
|
||||||
use crate::models::posts::types::Post;
|
helpers::{can_view_post, get_local_post_by_id},
|
||||||
|
types::Post,
|
||||||
|
};
|
||||||
use crate::models::profiles::queries::{
|
use crate::models::profiles::queries::{
|
||||||
search_profiles,
|
search_profiles,
|
||||||
search_profiles_by_did_only,
|
search_profiles_by_did_only,
|
||||||
|
@ -94,7 +99,7 @@ async fn search_profiles_or_import(
|
||||||
username: String,
|
username: String,
|
||||||
mut maybe_hostname: Option<String>,
|
mut maybe_hostname: Option<String>,
|
||||||
limit: u16,
|
limit: u16,
|
||||||
) -> Result<Vec<DbActorProfile>, HttpError> {
|
) -> Result<Vec<DbActorProfile>, DatabaseError> {
|
||||||
if let Some(ref hostname) = maybe_hostname {
|
if let Some(ref hostname) = maybe_hostname {
|
||||||
if hostname == &config.instance().host() {
|
if hostname == &config.instance().host() {
|
||||||
// This is a local profile
|
// This is a local profile
|
||||||
|
@ -129,14 +134,28 @@ async fn search_profiles_or_import(
|
||||||
Ok(profiles)
|
Ok(profiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Finds public post by its object ID
|
/// Finds post by its object ID
|
||||||
async fn search_post(
|
async fn find_post_by_url(
|
||||||
config: &Config,
|
config: &Config,
|
||||||
db_client: &mut impl GenericClient,
|
db_client: &mut impl GenericClient,
|
||||||
url: String,
|
url: String,
|
||||||
) -> Result<Option<Post>, HttpError> {
|
) -> Result<Option<Post>, DatabaseError> {
|
||||||
let maybe_post = match import_post(
|
let maybe_post = match parse_local_object_id(
|
||||||
config, db_client,
|
&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,
|
url,
|
||||||
None,
|
None,
|
||||||
).await {
|
).await {
|
||||||
|
@ -145,6 +164,8 @@ async fn search_post(
|
||||||
log::warn!("{}", err);
|
log::warn!("{}", err);
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
};
|
};
|
||||||
Ok(maybe_post)
|
Ok(maybe_post)
|
||||||
}
|
}
|
||||||
|
@ -177,7 +198,7 @@ pub async fn search(
|
||||||
).await?;
|
).await?;
|
||||||
},
|
},
|
||||||
SearchQuery::Url(url) => {
|
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 let Some(post) = maybe_post {
|
||||||
if can_view_post(db_client, Some(current_user), &post).await? {
|
if can_view_post(db_client, Some(current_user), &post).await? {
|
||||||
posts = vec![post];
|
posts = vec![post];
|
||||||
|
|
|
@ -124,6 +124,17 @@ pub async fn can_view_post(
|
||||||
Ok(result)
|
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(
|
pub async fn get_post_by_object_id(
|
||||||
db_client: &impl GenericClient,
|
db_client: &impl GenericClient,
|
||||||
instance_url: &str,
|
instance_url: &str,
|
||||||
|
@ -132,7 +143,7 @@ pub async fn get_post_by_object_id(
|
||||||
match parse_local_object_id(instance_url, object_id) {
|
match parse_local_object_id(instance_url, object_id) {
|
||||||
Ok(post_id) => {
|
Ok(post_id) => {
|
||||||
// Local post
|
// 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)
|
Ok(post)
|
||||||
},
|
},
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
|
|
|
@ -167,6 +167,10 @@ impl Post {
|
||||||
Ok(post)
|
Ok(post)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_local(&self) -> bool {
|
||||||
|
self.author.is_local()
|
||||||
|
}
|
||||||
|
|
||||||
pub fn is_public(&self) -> bool {
|
pub fn is_public(&self) -> bool {
|
||||||
matches!(self.visibility, Visibility::Public)
|
matches!(self.visibility, Visibility::Public)
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,7 +110,7 @@ pub async fn send_monero(
|
||||||
// TODO: transaction can fail
|
// TODO: transaction can fail
|
||||||
// https://github.com/monero-project/monero/issues/8372
|
// https://github.com/monero-project/monero/issues/8372
|
||||||
let maybe_transfer = wallet_client.get_transfer(
|
let maybe_transfer = wallet_client.get_transfer(
|
||||||
tx_hash.clone(),
|
tx_hash,
|
||||||
Some(DEFAULT_ACCOUNT),
|
Some(DEFAULT_ACCOUNT),
|
||||||
).await?;
|
).await?;
|
||||||
let transfer_status = maybe_transfer
|
let transfer_status = maybe_transfer
|
||||||
|
|
Loading…
Reference in a new issue