use chrono::NaiveDate; use serde::{Deserialize, Serialize}; use uuid::Uuid; pub type PageId = Uuid; pub type BlockId = Uuid; /// Free-form properties (`key:: value` in block Markdown). pub type Properties = serde_json::Map; pub fn new_id() -> Uuid { Uuid::now_v7() } /// What a page is: a note (an outline of blocks), a hub (a container of pages with a short /// description and an ordered member list, and no blocks) or a template (the outline that new /// notes of a hub start from; every hub owns exactly one, see `Graph::hub_template`). #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum PageKind { /// Also what the retired `tag` kind of schema v5 reads as (tags no longer have pages). #[default] #[serde(alias = "tag")] Note, Hub, Template, } impl PageKind { pub fn as_str(self) -> &'static str { match self { PageKind::Note => "note", PageKind::Hub => "hub", PageKind::Template => "template", } } /// Unknown values read as notes. pub fn parse(s: &str) -> Self { match s { "hub" => PageKind::Hub, "template" => PageKind::Template, _ => PageKind::Note, } } } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Page { pub id: PageId, pub title: String, /// Set for journal pages: the day they represent. pub journal_date: Option, /// Journal day in which this page was born (episodic anchor). pub origin_journal: Option, pub properties: Properties, pub created_at: i64, pub updated_at: i64, /// Defaults to `Note` so ops and snapshots from older versions parse. #[serde(default)] pub kind: PageKind, /// Short plain-text description (hubs). #[serde(default)] pub description: String, /// Hubs this page belongs to (its "parents", like tags). Ids may dangle (deleted pages, /// pages no longer hubs); readers ignore those. #[serde(default)] pub hubs: Vec, /// Tag names this page is filed under, any number (names only: tags have no pages); see /// `tags`. Its types count as tags too without being listed here. #[serde(default)] pub tags: Vec, /// What kind of thing this is ("persona", "lugar"...): a name, not a page; see `types`. /// Holds zero or one entry (a list only for compatibility with older replicas: readers /// use the first, see [`Page::page_type`]). The type implies a tag of the same name. #[serde(default)] pub types: Vec, } impl Page { pub fn is_journal(&self) -> bool { self.journal_date.is_some() } /// The page's single type, if any. pub fn page_type(&self) -> Option<&str> { self.types.first().map(String::as_str) } pub fn is_hub(&self) -> bool { self.kind == PageKind::Hub } pub fn is_template(&self) -> bool { self.kind == PageKind::Template } } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Block { pub id: BlockId, pub page_id: PageId, pub parent_id: Option, /// Fractional index among siblings (see [`crate::position`]). pub position: String, /// Synapse outline Markdown (rich text source of truth). pub content: String, pub properties: Properties, pub collapsed: bool, pub created_at: i64, pub updated_at: i64, } /// Kind of a link from a block to a page. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum RefKind { /// `[[Page]]` Link, /// `#tag` / `#[[tag]]` Tag, /// `key:: [[Page]]` inside properties Property, } impl RefKind { pub fn as_str(self) -> &'static str { match self { RefKind::Link => "link", RefKind::Tag => "tag", RefKind::Property => "property", } } pub fn parse(s: &str) -> Option { Some(match s { "link" => RefKind::Link, "tag" => RefKind::Tag, "property" => RefKind::Property, _ => return None, }) } } /// A backlink: `block` (on `page`) references the queried page. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Backlink { pub block: Block, pub page_title: String, pub kind: RefKind, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Asset { /// blake3 hex of the content. pub hash: String, pub size: u64, pub mime: Option, pub original_name: Option, pub created_at: i64, } /// One day on the timeline view. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct TimelineDay { pub date: NaiveDate, pub journal: Option, /// Pages born on this day. pub created: Vec, /// Pages edited on this day (excluding the journal itself), with edit counts. pub edited: Vec<(PageId, u32)>, } /// Journal neighbours (episodic chain). #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct JournalNeighbors { pub prev: Option, pub next: Option, } /// Entry of the op log, the change feed for indexing, backup and sync. #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct OpRecord { pub seq: i64, pub hlc: String, pub op: Op, } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "snake_case")] pub enum Op { UpsertPage { page: Page, }, DeletePage { id: PageId, }, UpsertBlock { block: Block, }, DeleteBlock { id: BlockId, }, PutAsset { asset: Asset, }, SetAlias { alias: String, page: Option, }, /// Metadata of a type name (its icon), last writer wins per [`title_key`] of the name. PutTypeDef { def: TypeDef, }, } /// Graph-wide metadata of a type name: types are names, not pages, so this is where their /// icon lives (`type_defs` table, synced by [`Op::PutTypeDef`]). A definition also keeps a /// type in the vocabulary while no page uses it. `removed` is a tombstone: the type was /// deleted (it also hides a seeded suggestion of that name). #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct TypeDef { pub name: String, /// An emoji (a short string) shown next to the names of pages of this type. #[serde(default)] pub icon: Option, #[serde(default)] pub removed: bool, } /// Fields for creating a page; defaults come from the graph clock. #[derive(Debug, Clone, Default)] pub struct NewPage { pub title: String, pub origin_journal: Option, pub properties: Properties, /// Override timestamps (importer). Millis since epoch. pub created_at: Option, pub updated_at: Option, } impl NewPage { pub fn titled(title: impl Into) -> Self { Self { title: title.into(), ..Default::default() } } } /// Where to insert a new block. #[derive(Debug, Clone, Copy)] pub enum Placement { /// Last child of `parent` (or last root block when `None`). LastChildOf(Option), /// Right after a sibling. After(BlockId), /// Right before a sibling. Before(BlockId), } /// Journal page title for a date (ISO; the UI formats it for display). pub fn journal_title(date: NaiveDate) -> String { date.format("%Y-%m-%d").to_string() } /// Normalised key used for title/alias lookups. pub fn title_key(title: &str) -> String { title.trim().to_lowercase() }