Refactor search::helpers module

This commit is contained in:
silverpill 2023-02-10 17:14:34 +00:00
parent 0b8553d0c2
commit 4ace00736b
3 changed files with 32 additions and 30 deletions

View file

@ -460,12 +460,14 @@ async fn search_by_acct(
query_params: web::Query<SearchAcctQueryParams>, query_params: web::Query<SearchAcctQueryParams>,
) -> Result<HttpResponse, HttpError> { ) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?; let db_client = &**get_database_client(&db_pool).await?;
let accounts = search_profiles_only( let profiles = search_profiles_only(
&config,
db_client, db_client,
&query_params.q, &query_params.q,
query_params.limit.inner(), query_params.limit.inner(),
).await?; ).await?;
let accounts: Vec<Account> = profiles.into_iter()
.map(|profile| Account::from_profile(profile, &config.instance_url()))
.collect();
Ok(HttpResponse::Ok().json(accounts)) Ok(HttpResponse::Ok().json(accounts))
} }

View file

@ -14,11 +14,8 @@ use crate::activitypub::{
}; };
use crate::config::Config; use crate::config::Config;
use crate::database::{DatabaseClient, DatabaseError}; use crate::database::{DatabaseClient, DatabaseError};
use crate::errors::{HttpError, ValidationError}; use crate::errors::ValidationError;
use crate::identity::did::Did; 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::{ use crate::models::posts::{
helpers::{can_view_post, get_local_post_by_id}, helpers::{can_view_post, get_local_post_by_id},
types::Post, types::Post,
@ -36,7 +33,6 @@ use crate::models::users::{
}; };
use crate::utils::currencies::{validate_wallet_address, Currency}; use crate::utils::currencies::{validate_wallet_address, Currency};
use crate::webfinger::types::ActorAddress; use crate::webfinger::types::ActorAddress;
use super::types::SearchResults;
enum SearchQuery { enum SearchQuery {
ProfileQuery(String, Option<String>), ProfileQuery(String, Option<String>),
@ -213,13 +209,15 @@ async fn find_profile_by_url(
Ok(profile) Ok(profile)
} }
type SearchResults = (Vec<DbActorProfile>, Vec<Post>, Vec<String>);
pub async fn search( pub async fn search(
config: &Config, config: &Config,
current_user: &User, current_user: &User,
db_client: &mut impl DatabaseClient, db_client: &mut impl DatabaseClient,
search_query: &str, search_query: &str,
limit: u16, limit: u16,
) -> Result<SearchResults, HttpError> { ) -> Result<SearchResults, DatabaseError> {
let mut profiles = vec![]; let mut profiles = vec![];
let mut posts = vec![]; let mut posts = vec![];
let mut tags = vec![]; let mut tags = vec![];
@ -275,27 +273,14 @@ pub async fn search(
}, },
SearchQuery::Unknown => (), // ignore SearchQuery::Unknown => (), // ignore
}; };
let accounts: Vec<Account> = profiles.into_iter() Ok((profiles, posts, tags))
.map(|profile| Account::from_profile(profile, &config.instance_url()))
.collect();
let statuses = build_status_list(
db_client,
&config.instance_url(),
Some(current_user),
posts,
).await?;
let hashtags = tags.into_iter()
.map(|tag_name| Tag::from_tag_name(&config.instance_url(), tag_name))
.collect();
Ok(SearchResults { accounts, statuses, hashtags })
} }
pub async fn search_profiles_only( pub async fn search_profiles_only(
config: &Config,
db_client: &impl DatabaseClient, db_client: &impl DatabaseClient,
search_query: &str, search_query: &str,
limit: u16, limit: u16,
) -> Result<Vec<Account>, HttpError> { ) -> Result<Vec<DbActorProfile>, DatabaseError> {
let (username, maybe_hostname) = match parse_profile_query(search_query) { let (username, maybe_hostname) = match parse_profile_query(search_query) {
Ok(result) => result, Ok(result) => result,
Err(_) => return Ok(vec![]), Err(_) => return Ok(vec![]),
@ -306,10 +291,7 @@ pub async fn search_profiles_only(
maybe_hostname.as_ref(), maybe_hostname.as_ref(),
limit, limit,
).await?; ).await?;
let accounts: Vec<Account> = profiles.into_iter() Ok(profiles)
.map(|profile| Account::from_profile(profile, &config.instance_url()))
.collect();
Ok(accounts)
} }
#[cfg(test)] #[cfg(test)]

View file

@ -5,9 +5,14 @@ use actix_web_httpauth::extractors::bearer::BearerAuth;
use crate::config::Config; use crate::config::Config;
use crate::database::{get_database_client, DbPool}; use crate::database::{get_database_client, DbPool};
use crate::errors::HttpError; use crate::errors::HttpError;
use crate::mastodon_api::oauth::auth::get_current_user; use crate::mastodon_api::{
accounts::types::Account,
oauth::auth::get_current_user,
statuses::helpers::build_status_list,
statuses::types::Tag,
};
use super::helpers::search; use super::helpers::search;
use super::types::SearchQueryParams; use super::types::{SearchQueryParams, SearchResults};
#[get("")] #[get("")]
async fn search_view( async fn search_view(
@ -18,13 +23,26 @@ async fn search_view(
) -> Result<HttpResponse, HttpError> { ) -> Result<HttpResponse, HttpError> {
let db_client = &mut **get_database_client(&db_pool).await?; let db_client = &mut **get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?; let current_user = get_current_user(db_client, auth.token()).await?;
let results = search( let (profiles, posts, tags) = search(
&config, &config,
&current_user, &current_user,
db_client, db_client,
query_params.q.trim(), query_params.q.trim(),
query_params.limit.inner(), query_params.limit.inner(),
).await?; ).await?;
let accounts: Vec<Account> = profiles.into_iter()
.map(|profile| Account::from_profile(profile, &config.instance_url()))
.collect();
let statuses = build_status_list(
db_client,
&config.instance_url(),
Some(&current_user),
posts,
).await?;
let hashtags = tags.into_iter()
.map(|tag_name| Tag::from_tag_name(&config.instance_url(), tag_name))
.collect();
let results = SearchResults { accounts, statuses, hashtags };
Ok(HttpResponse::Ok().json(results)) Ok(HttpResponse::Ok().json(results))
} }