pull's note() and push's server_note each wrote out all nineteen fields of a wire::Note. wire::sample_note(id, revision) is that note; push's version changes only the body and attachments, by struct update. DRY pass #2, batch 2, F9 (#5372). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
195 lines
5.7 KiB
Rust
195 lines
5.7 KiB
Rust
//! The delta-feed JSON shapes, exactly as `GET /api/sync/changes` sends them.
|
|
//!
|
|
//! Mirrors the server's serializers (`notes/serialize.py` + `serialize.py`) — see
|
|
//! `docs/sync.md` for the contract. Every field is `#[serde(default)]` or `Option`
|
|
//! so a NEWER server adding fields, or an older one omitting one, degrades to a
|
|
//! partial note rather than failing the whole page. Losing one attribute is
|
|
//! recoverable; refusing a page stalls sync permanently at that cursor.
|
|
|
|
use serde::Deserialize;
|
|
|
|
use crate::local::OPAQUE_MIME;
|
|
|
|
#[derive(Debug, Clone, Deserialize, Default)]
|
|
pub struct ChangesPage {
|
|
#[serde(default)]
|
|
pub notes: Vec<Note>,
|
|
#[serde(default)]
|
|
pub labels: Vec<Label>,
|
|
#[serde(default)]
|
|
pub cursor: i64,
|
|
#[serde(default)]
|
|
pub has_more: bool,
|
|
/// Notes shared with this account that stopped being shared, or that their owner
|
|
/// deleted (`shares`, protocol 6). The note no longer reaches this account's feed,
|
|
/// so this is the only word its devices get to delete their copy.
|
|
#[serde(default)]
|
|
pub revoked: Vec<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct Note {
|
|
pub id: String,
|
|
#[serde(default)]
|
|
pub body: String,
|
|
#[serde(default)]
|
|
pub position: i64,
|
|
#[serde(default)]
|
|
pub pinned: bool,
|
|
#[serde(default)]
|
|
pub archived: bool,
|
|
/// The server derives this from `deleted_at` — trash, NOT a tombstone.
|
|
#[serde(default)]
|
|
pub trashed: bool,
|
|
/// WHEN it was trashed. The trash-retention clock runs from here, so it has to be
|
|
/// the server's timestamp rather than anything this device invents. Absent from an
|
|
/// older server, which is why it's optional rather than required.
|
|
#[serde(default)]
|
|
pub deleted_at: Option<String>,
|
|
#[serde(default)]
|
|
pub remind_at: Option<String>,
|
|
#[serde(default)]
|
|
pub recurrence: Option<String>,
|
|
#[serde(default)]
|
|
pub created_at: Option<String>,
|
|
#[serde(default)]
|
|
pub updated_at: Option<String>,
|
|
#[serde(default)]
|
|
pub sync_revision: i64,
|
|
/// Set means the row was permanently purged: a content-less tombstone whose only
|
|
/// job is to tell clients to delete their copy.
|
|
#[serde(default)]
|
|
pub purged_at: Option<String>,
|
|
#[serde(default)]
|
|
pub labels: Vec<NoteLabel>,
|
|
#[serde(default)]
|
|
pub attachments: Vec<Attachment>,
|
|
#[serde(default)]
|
|
pub previews: Vec<Preview>,
|
|
/// How this account holds the note: `owner`, `edit` or `view` (`shares`). Absent
|
|
/// from a server without shares, where every note in the feed is the caller's own.
|
|
#[serde(default)]
|
|
pub permission: Option<String>,
|
|
/// Whether the owner has shared it with anyone.
|
|
#[serde(default)]
|
|
pub shared: bool,
|
|
/// Who shared it with us; absent on our own notes.
|
|
#[serde(default)]
|
|
pub shared_by: Option<SharedBy>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct SharedBy {
|
|
pub id: String,
|
|
#[serde(default)]
|
|
pub display_name: String,
|
|
}
|
|
|
|
impl Note {
|
|
pub fn is_tombstone(&self) -> bool {
|
|
self.purged_at.is_some()
|
|
}
|
|
}
|
|
|
|
/// A label as it appears attached to a note. Carries enough to materialize the label
|
|
/// row itself, which is what lets a membership be applied even if the label's own
|
|
/// delta hasn't arrived (see `pull::apply_page`).
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct NoteLabel {
|
|
pub id: String,
|
|
#[serde(default)]
|
|
pub name: String,
|
|
#[serde(default = "default_color")]
|
|
pub color: String,
|
|
/// True when the membership came from a `#tag` in the body rather than a manual
|
|
/// assignment. Applied verbatim rather than re-derived — see `pull::apply_page`.
|
|
#[serde(default)]
|
|
pub via_tag: bool,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct Attachment {
|
|
pub id: String,
|
|
#[serde(default)]
|
|
pub url: String,
|
|
#[serde(default)]
|
|
pub filename: Option<String>,
|
|
#[serde(default = "default_mime")]
|
|
pub mime: String,
|
|
#[serde(default)]
|
|
pub size: Option<i64>,
|
|
#[serde(default)]
|
|
pub sha256: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct Preview {
|
|
pub id: String,
|
|
#[serde(default)]
|
|
pub url: String,
|
|
#[serde(default)]
|
|
pub title: Option<String>,
|
|
#[serde(default)]
|
|
pub description: Option<String>,
|
|
#[serde(default)]
|
|
pub image_url: Option<String>,
|
|
#[serde(default)]
|
|
pub site_name: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct Label {
|
|
pub id: String,
|
|
#[serde(default)]
|
|
pub name: String,
|
|
#[serde(default = "default_color")]
|
|
pub color: String,
|
|
#[serde(default)]
|
|
pub sync_revision: i64,
|
|
#[serde(default)]
|
|
pub purged_at: Option<String>,
|
|
#[serde(default)]
|
|
pub created_at: Option<String>,
|
|
}
|
|
|
|
impl Label {
|
|
pub fn is_tombstone(&self) -> bool {
|
|
self.purged_at.is_some()
|
|
}
|
|
}
|
|
|
|
fn default_color() -> String {
|
|
"default".to_string()
|
|
}
|
|
|
|
fn default_mime() -> String {
|
|
OPAQUE_MIME.to_string()
|
|
}
|
|
|
|
/// A plain note, this account's own and live, as a server sends it. For tests in
|
|
/// pull and push, which each wrote the whole literal out.
|
|
#[cfg(test)]
|
|
pub(crate) fn sample_note(id: &str, revision: i64) -> Note {
|
|
Note {
|
|
id: id.to_string(),
|
|
body: "Body".into(),
|
|
position: 0,
|
|
pinned: false,
|
|
archived: false,
|
|
trashed: false,
|
|
deleted_at: None,
|
|
remind_at: None,
|
|
recurrence: None,
|
|
created_at: Some("2026-07-26T00:00:00.000Z".into()),
|
|
updated_at: Some("2026-07-26T00:00:00.000Z".into()),
|
|
sync_revision: revision,
|
|
purged_at: None,
|
|
labels: vec![],
|
|
attachments: vec![],
|
|
previews: vec![],
|
|
permission: None,
|
|
shared: false,
|
|
shared_by: None,
|
|
}
|
|
}
|