Refactor search::helpers module
This commit is contained in:
parent
333f90e293
commit
4797bacf32
1 changed files with 49 additions and 30 deletions
|
@ -1,5 +1,6 @@
|
|||
use regex::Regex;
|
||||
use tokio_postgres::GenericClient;
|
||||
use url::Url;
|
||||
|
||||
use crate::activitypub::actor::ActorAddress;
|
||||
use crate::activitypub::receiver::process_note;
|
||||
|
@ -13,6 +14,12 @@ use crate::models::profiles::queries::search_profile;
|
|||
use crate::models::profiles::types::DbActorProfile;
|
||||
use super::types::SearchResults;
|
||||
|
||||
enum SearchQuery {
|
||||
ProfileQuery(String, Option<String>),
|
||||
Url(String),
|
||||
Unknown,
|
||||
}
|
||||
|
||||
fn parse_profile_query(query: &str) ->
|
||||
Result<(String, Option<String>), ValidationError>
|
||||
{
|
||||
|
@ -27,25 +34,32 @@ fn parse_profile_query(query: &str) ->
|
|||
Ok((username, maybe_instance))
|
||||
}
|
||||
|
||||
fn parse_search_query(search_query: &str) -> SearchQuery {
|
||||
let search_query = search_query.trim();
|
||||
if Url::parse(search_query).is_ok() {
|
||||
return SearchQuery::Url(search_query.to_string());
|
||||
};
|
||||
match parse_profile_query(search_query) {
|
||||
Ok((username, instance)) => {
|
||||
return SearchQuery::ProfileQuery(username, instance);
|
||||
},
|
||||
Err(_) => {
|
||||
return SearchQuery::Unknown;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async fn search_profiles(
|
||||
config: &Config,
|
||||
db_client: &impl GenericClient,
|
||||
search_query: &str,
|
||||
username: String,
|
||||
mut instance: Option<String>,
|
||||
) -> Result<Vec<DbActorProfile>, HttpError> {
|
||||
let (username, instance) = match parse_profile_query(search_query) {
|
||||
Ok((username, mut instance)) => {
|
||||
if let Some(ref actor_host) = instance {
|
||||
if actor_host == &config.instance().host() {
|
||||
// This is a local profile
|
||||
instance = None;
|
||||
};
|
||||
};
|
||||
(username, instance)
|
||||
},
|
||||
Err(_) => {
|
||||
// Not an 'acct' query
|
||||
return Ok(vec![]);
|
||||
},
|
||||
if let Some(ref actor_host) = instance {
|
||||
if actor_host == &config.instance().host() {
|
||||
// This is a local profile
|
||||
instance = None;
|
||||
};
|
||||
};
|
||||
let mut profiles = search_profile(db_client, &username, instance.as_ref()).await?;
|
||||
if profiles.is_empty() && instance.is_some() {
|
||||
|
@ -75,15 +89,12 @@ async fn search_profiles(
|
|||
async fn search_note(
|
||||
config: &Config,
|
||||
db_client: &mut impl GenericClient,
|
||||
search_query: &str,
|
||||
url: String,
|
||||
) -> Result<Option<Post>, HttpError> {
|
||||
if url::Url::parse(search_query).is_err() {
|
||||
// Not a valid URL
|
||||
return Ok(None);
|
||||
};
|
||||
let maybe_post = match process_note(
|
||||
config, db_client,
|
||||
search_query.to_string(), None,
|
||||
url,
|
||||
None,
|
||||
).await {
|
||||
Ok(post) => Some(post),
|
||||
Err(err) => {
|
||||
|
@ -99,17 +110,25 @@ pub async fn search(
|
|||
db_client: &mut impl GenericClient,
|
||||
search_query: &str,
|
||||
) -> Result<SearchResults, HttpError> {
|
||||
let profiles = search_profiles(config, db_client, search_query).await?;
|
||||
let mut profiles = vec![];
|
||||
let mut posts = vec![];
|
||||
match parse_search_query(search_query) {
|
||||
SearchQuery::ProfileQuery(username, instance) => {
|
||||
profiles = search_profiles(config, db_client, username, instance).await?;
|
||||
},
|
||||
SearchQuery::Url(url) => {
|
||||
let maybe_post = search_note(config, db_client, url).await?;
|
||||
if let Some(post) = maybe_post {
|
||||
posts = vec![post];
|
||||
};
|
||||
},
|
||||
SearchQuery::Unknown => (), // ignore
|
||||
};
|
||||
let accounts: Vec<Account> = profiles.into_iter()
|
||||
.map(|profile| Account::from_profile(profile, &config.instance_url()))
|
||||
.collect();
|
||||
let maybe_post = search_note(config, db_client, search_query).await?;
|
||||
let statuses = match maybe_post {
|
||||
Some(post) => {
|
||||
let status = Status::from_post(post, &config.instance_url());
|
||||
vec![status]
|
||||
},
|
||||
None => vec![],
|
||||
};
|
||||
let statuses: Vec<Status> = posts.into_iter()
|
||||
.map(|post| Status::from_post(post, &config.instance_url()))
|
||||
.collect();
|
||||
Ok(SearchResults { accounts, statuses })
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue