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
/api/v1/directory:
get:
summary: List accounts visible in the directory.
summary: List profiles visible in the directory.
parameters:
- name: offset
in: query
description: How many accounts to skip before returning results.
description: How many profiles to skip before returning results.
required: false
schema:
type: integer
default: 0
- name: limit
in: query
description: How many accounts to load.
description: How many profiles to load.
required: false
schema:
type: integer
default: 40
- name: local
in: query
description: Only return local profiles.
required: false
schema:
type: boolean
default: true
responses:
200:
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_only_local() -> bool { true }
/// https://docs.joinmastodon.org/methods/instance/directory/
#[derive(Deserialize)]
pub struct DirectoryQueryParams {
@ -12,4 +14,7 @@ pub struct DirectoryQueryParams {
#[serde(default = "default_page_size")]
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?;
let profiles = get_profiles(
db_client,
query_params.local,
query_params.offset,
query_params.limit.inner(),
).await?;

View file

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