Skip to content

Commit

Permalink
fix(apub/collection): fix total pages
Browse files Browse the repository at this point in the history
  • Loading branch information
kwaa committed Sep 28, 2024
1 parent ea87c6e commit f5ecbfc
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 26 deletions.
2 changes: 1 addition & 1 deletion crates/api_apub/src/users/user_followers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub async fn handler(
&hatsu_utils::url::generate_user_url(data.domain(), &name)?
.join(&format!("{name}/followers"))?,
total.number_of_items,
Some(total.number_of_pages),
total.number_of_pages,
)?),
))),
Some(page) =>
Expand Down
2 changes: 1 addition & 1 deletion crates/api_apub/src/users/user_following.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub async fn handler(
&hatsu_utils::url::generate_user_url(data.domain(), &name)?
.join(&format!("{name}/following"))?,
0,
Some(0),
0,
)?),
))),
Some(page) => Ok(FederationJson(WithContext::new_default(
Expand Down
2 changes: 1 addition & 1 deletion crates/api_apub/src/users/user_outbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub async fn handler(
&hatsu_utils::url::generate_user_url(data.domain(), &name)?
.join(&format!("{name}/outbox"))?,
total.number_of_items,
Some(total.number_of_pages),
total.number_of_pages,
)?),
))),
Some(page) =>
Expand Down
33 changes: 10 additions & 23 deletions crates/apub/src/collections/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,27 @@ use crate::collections::generate_collection_page_url;
#[serde(rename_all = "camelCase")]
pub struct Collection {
#[serde(rename = "type")]
kind: OrderedCollectionType,
pub kind: OrderedCollectionType,
// example: https://hatsu.local/users/example.com/collection
id: Url,

pub id: Url,
// example: https://hatsu.local/users/example.com/collection?page=1
first: Url,
pub first: Url,
// example: https://hatsu.local/users/example.com/collection?page=64
#[serde(skip_serializing_if = "Option::is_none")]
last: Option<Url>,

pub last: Url,
// collection count
total_items: u64,
pub total_items: u64,
}

impl Collection {
pub fn new(
collection_id: &Url,
total_items: u64,
total_pages: Option<u64>,
) -> Result<Self, AppError> {
pub fn new(collection_id: &Url, total_items: u64, total_pages: u64) -> Result<Self, AppError> {
Ok(Self {
kind: OrderedCollectionType::OrderedCollection,
id: collection_id.clone(),
first: generate_collection_page_url(collection_id, 1)?,
last: match total_pages {
Some(total_pages) => Some(generate_collection_page_url(
collection_id,
match total_pages {
page if total_pages > 0 => page + 1,
_ => 1,
},
)?),
None => None,
},
last: generate_collection_page_url(collection_id, match total_pages {
page if page > 0 => page,
_ => 1,
})?,
total_items,
})
}
Expand Down
34 changes: 34 additions & 0 deletions crates/apub/tests/collections_local.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use hatsu_apub::collections::{
Collection,
// TODO: test collection page
// CollectionPage
};
use hatsu_utils::AppError;
use url::Url;

#[test]
fn new_collection() -> Result<(), AppError> {
let collection_id = Url::parse("https://hatsu.local/collections/test1")?;
let collection = Collection::new(&collection_id, 100, 10)?;

let expected_first_url = Url::parse("https://hatsu.local/collections/test1?page=1")?;
let expected_last_url = Url::parse("https://hatsu.local/collections/test1?page=10")?;

assert_eq!(collection.first, expected_first_url);
assert_eq!(collection.last, expected_last_url);

Ok(())
}

#[test]
fn new_empty_collection() -> Result<(), AppError> {
let collection_id = Url::parse("https://hatsu.local/collections/test2")?;
let collection = Collection::new(&collection_id, 0, 0)?;

let expected_url = Url::parse("https://hatsu.local/collections/test2?page=1")?;

assert_eq!(collection.first, expected_url);
assert_eq!(collection.last, expected_url);

Ok(())
}

0 comments on commit f5ecbfc

Please sign in to comment.