Show only local profiles in profile directory

This commit is contained in:
silverpill 2022-10-02 20:43:51 +00:00
parent 54c8f241e4
commit cde324c07d
4 changed files with 25 additions and 5 deletions

View file

@ -419,22 +419,29 @@ paths:
description: Profile not found description: Profile not found
/api/v1/directory: /api/v1/directory:
get: get:
summary: List accounts visible in the directory. summary: List profiles visible in the directory.
parameters: parameters:
- name: offset - name: offset
in: query in: query
description: How many accounts to skip before returning results. description: How many profiles to skip before returning results.
required: false required: false
schema: schema:
type: integer type: integer
default: 0 default: 0
- name: limit - name: limit
in: query in: query
description: How many accounts to load. description: How many profiles to load.
required: false required: false
schema: schema:
type: integer type: integer
default: 40 default: 40
- name: local
in: query
description: Only return local profiles.
required: false
schema:
type: boolean
default: true
responses: responses:
200: 200:
description: Successful operation description: Successful operation

View file

@ -4,6 +4,8 @@ use crate::mastodon_api::pagination::PageSize;
fn default_page_size() -> PageSize { PageSize::new(40) } fn default_page_size() -> PageSize { PageSize::new(40) }
fn default_only_local() -> bool { true }
/// https://docs.joinmastodon.org/methods/instance/directory/ /// https://docs.joinmastodon.org/methods/instance/directory/
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct DirectoryQueryParams { pub struct DirectoryQueryParams {
@ -12,4 +14,7 @@ pub struct DirectoryQueryParams {
#[serde(default = "default_page_size")] #[serde(default = "default_page_size")]
pub limit: PageSize, pub limit: PageSize,
#[serde(default = "default_only_local")]
pub local: bool,
} }

View file

@ -21,6 +21,7 @@ async fn profile_directory(
get_current_user(db_client, auth.token()).await?; get_current_user(db_client, auth.token()).await?;
let profiles = get_profiles( let profiles = get_profiles(
db_client, db_client,
query_params.local,
query_params.offset, query_params.offset,
query_params.limit.inner(), query_params.limit.inner(),
).await?; ).await?;

View file

@ -159,21 +159,28 @@ pub async fn get_profile_by_acct(
pub async fn get_profiles( pub async fn get_profiles(
db_client: &impl GenericClient, db_client: &impl GenericClient,
only_local: bool,
offset: u16, offset: u16,
limit: u16, limit: u16,
) -> Result<Vec<DbActorProfile>, DatabaseError> { ) -> Result<Vec<DbActorProfile>, DatabaseError> {
let rows = db_client.query( let condition = if only_local { "WHERE actor_id IS NULL" } else { "" };
let statement = format!(
" "
SELECT actor_profile SELECT actor_profile
FROM actor_profile FROM actor_profile
{condition}
ORDER BY username ORDER BY username
LIMIT $1 OFFSET $2 LIMIT $1 OFFSET $2
", ",
condition=condition,
);
let rows = db_client.query(
&statement,
&[&i64::from(limit), &i64::from(offset)], &[&i64::from(limit), &i64::from(offset)],
).await?; ).await?;
let profiles = rows.iter() let profiles = rows.iter()
.map(|row| row.try_get("actor_profile")) .map(|row| row.try_get("actor_profile"))
.collect::<Result<Vec<DbActorProfile>, _>>()?; .collect::<Result<_, _>>()?;
Ok(profiles) Ok(profiles)
} }