fedimovies/src/activitypub/collections.rs

72 lines
1.7 KiB
Rust
Raw Normal View History

2021-12-23 00:32:00 +00:00
use serde::Serialize;
use serde_json::{json, Value};
use super::types::{build_default_context, Context};
2021-12-23 21:51:01 +00:00
use super::vocabulary::{ORDERED_COLLECTION, ORDERED_COLLECTION_PAGE};
2021-12-23 00:32:00 +00:00
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderedCollection {
#[serde(rename = "@context")]
pub context: Context,
2021-12-23 00:32:00 +00:00
pub id: String,
#[serde(rename = "type")]
pub object_type: String,
2021-12-23 21:51:01 +00:00
#[serde(skip_serializing_if = "Option::is_none")]
first: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
total_items: Option<i32>,
2021-12-23 00:32:00 +00:00
}
impl OrderedCollection {
2021-12-23 21:51:01 +00:00
pub fn new(
collection_id: String,
first_page_id: Option<String>,
total_items: Option<i32>,
2021-12-23 21:51:01 +00:00
) -> Self {
2021-12-23 00:32:00 +00:00
Self {
context: build_default_context(),
2021-12-23 00:32:00 +00:00
id: collection_id,
object_type: ORDERED_COLLECTION.to_string(),
2021-12-23 21:51:01 +00:00
first: first_page_id,
total_items,
2021-12-23 21:51:01 +00:00
}
}
}
2022-09-29 21:36:55 +00:00
pub const COLLECTION_PAGE_SIZE: u16 = 10;
2021-12-23 21:51:01 +00:00
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OrderedCollectionPage {
#[serde(rename = "@context")]
pub context: Context,
2021-12-23 21:51:01 +00:00
pub id: String,
#[serde(rename = "type")]
pub object_type: String,
ordered_items: Vec<Value>,
}
impl OrderedCollectionPage {
pub fn new(
collection_page_id: String,
items: Vec<impl Serialize>,
) -> Self {
let ordered_items = items.into_iter()
.map(|item| json!(item)).collect();
Self {
context: build_default_context(),
2021-12-23 21:51:01 +00:00
id: collection_page_id,
object_type: ORDERED_COLLECTION_PAGE.to_string(),
ordered_items,
2021-12-23 00:32:00 +00:00
}
}
}