Compare commits
22
Commits
0101b05487
...
16ab4a13f5
@@ -35,13 +35,26 @@ private val COMPACT_IMAGE_WIDTH = 48.dp
|
||||
/**
|
||||
* A URL that is the WHOLE body, whitespace either side allowed.
|
||||
*
|
||||
* Mirrors `LONE_URL_RE` in `NoteCard.vue` deliberately — the two surfaces have to
|
||||
* agree on what counts as "this note is a link", or the same note reads as a card
|
||||
* on one and a paragraph on the other. Someone pasting a link rarely trims it,
|
||||
* Mirrors `LONE_URL_RE` in the web's `notes/links.ts` deliberately — the two surfaces
|
||||
* have to agree on what counts as "this note is a link", or the same note reads as a
|
||||
* card on one and a paragraph on the other. Someone pasting a link rarely trims it,
|
||||
* which is why the surrounding whitespace is tolerated rather than rejected.
|
||||
*/
|
||||
private val LONE_URL = Regex("""^\s*(https?://[^\s<>"'\]\)]+)\s*$""")
|
||||
|
||||
/** The sentence's punctuation, not the link's: the server trims it before unfurling. */
|
||||
private val TRAILING_PUNCTUATION = charArrayOf('.', ',', ';', ':', '!', '?')
|
||||
|
||||
/**
|
||||
* The link a note that is nothing but a link files its preview under, or null.
|
||||
*
|
||||
* Trimmed the way the server's `unfurl_queue.detect_urls` trims it, since that is the
|
||||
* key the preview arrives under (#5399). The web's `loneUrl` is the same rule, and
|
||||
* both run the `urls` cases in `core/testdata/grammar.json` (here, by hand).
|
||||
*/
|
||||
fun loneUrl(body: String): String? =
|
||||
LONE_URL.matchEntire(body)?.groupValues?.get(1)?.trimEnd(*TRAILING_PUNCTUATION)?.ifEmpty { null }
|
||||
|
||||
/**
|
||||
* The preview for a note that is nothing but a URL, or null.
|
||||
*
|
||||
@@ -56,13 +69,12 @@ private val LONE_URL = Regex("""^\s*(https?://[^\s<>"'\]\)]+)\s*$""")
|
||||
* the URL does.
|
||||
*/
|
||||
fun loneUrlPreview(note: Note): LinkPreview? {
|
||||
if (!LONE_URL.matches(note.body)) return null
|
||||
val url = note.body.trim()
|
||||
val url = loneUrl(note.body) ?: return null
|
||||
return note.previews.firstOrNull { it.url == url }
|
||||
}
|
||||
|
||||
/** True when the body is a lone URL, whether or not a preview has arrived for it. */
|
||||
fun isLoneUrl(note: Note): Boolean = LONE_URL.matches(note.body)
|
||||
fun isLoneUrl(note: Note): Boolean = loneUrl(note.body) != null
|
||||
|
||||
/**
|
||||
* A fetched link preview, in one of two sizes.
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.fabledsword.inkwell.ui
|
||||
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Test
|
||||
|
||||
/**
|
||||
* Pins [loneUrl] against the `urls` cases in `core/testdata/grammar.json`, written out
|
||||
* by hand as [DerivedTintTest] does the tint: these JVM tests do not read the fixture.
|
||||
* The web runs the same cases in `notes/grammar.test.ts`, the server in
|
||||
* `tests/test_grammar_fixture.py`, so a change to the rule changes the fixture and
|
||||
* this file together, or a suite goes red.
|
||||
*/
|
||||
class LoneUrlTest {
|
||||
@Test
|
||||
fun `lone links match the fixture shared with the web and the server`() {
|
||||
val cases =
|
||||
listOf(
|
||||
"https://example.com/a" to "https://example.com/a",
|
||||
" https://example.com/a \n" to "https://example.com/a",
|
||||
"https://example.com/a." to "https://example.com/a",
|
||||
"https://example.com/a?q=1!" to "https://example.com/a?q=1",
|
||||
"see https://example.com/a, then https://example.com/b." to null,
|
||||
"<https://example.com/a>" to null,
|
||||
"https://example.com/a)" to null,
|
||||
"https://example.com/a https://example.com/a" to null,
|
||||
"ftp://example.com/a" to null,
|
||||
"" to null,
|
||||
)
|
||||
for ((body, lone) in cases) {
|
||||
assertEquals("body [$body]", lone, loneUrl(body))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -533,7 +533,9 @@ impl Inkwell {
|
||||
Err(CoreError::NotLinked) => None,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
let revoked = link::unlink(&self.db, held).await.map_err(CoreError::store)?;
|
||||
let revoked = link::unlink(&self.db, held)
|
||||
.await
|
||||
.map_err(CoreError::store)?;
|
||||
Ok(revoked.into())
|
||||
}
|
||||
|
||||
@@ -739,7 +741,9 @@ impl Inkwell {
|
||||
url: &str,
|
||||
credential: link::Credential<'_>,
|
||||
) -> Result<Identity, CoreError> {
|
||||
let granted = link::authenticate(url, credential).await.map_err(CoreError::network)?;
|
||||
let granted = link::authenticate(url, credential)
|
||||
.await
|
||||
.map_err(CoreError::network)?;
|
||||
self.store_link(&granted.base_url, &granted.token, granted.retention_days)?;
|
||||
Ok(granted.identity.into())
|
||||
}
|
||||
|
||||
@@ -341,6 +341,32 @@ fn render_task_line(indent: &str, bullet: char, checked: bool, text: &str) -> St
|
||||
}
|
||||
}
|
||||
|
||||
/// The longest a note's name runs, in characters (not bytes): the server's
|
||||
/// `DISPLAY_TITLE_CAP`.
|
||||
pub const DISPLAY_TITLE_CAP: usize = 200;
|
||||
|
||||
/// The note's NAME: the first line of its body that says anything, cut to
|
||||
/// [`DISPLAY_TITLE_CAP`] characters.
|
||||
///
|
||||
/// Mirrors `derive_display_title` in the server's notes/helpers.py — one rule written
|
||||
/// twice, and they have to agree or a synced note is called different things on either
|
||||
/// side of the wire. Both run the `display_titles` cases in testdata/grammar.json.
|
||||
///
|
||||
/// It no longer needs the items, because the items ARE lines of the body now (M304).
|
||||
/// What it needs instead is to strip the task marker off: a list-only note is still
|
||||
/// named by its first item, and calling that note "- [ ] milk" would be showing
|
||||
/// someone the storage rather than the note. An empty item is skipped rather than
|
||||
/// naming the note "", which is what a half-typed list would otherwise do.
|
||||
pub fn display_title(body: &str) -> String {
|
||||
for line in body.split('\n') {
|
||||
let text = strip_marker(line.trim()).trim();
|
||||
if !text.is_empty() {
|
||||
return text.chars().take(DISPLAY_TITLE_CAP).collect();
|
||||
}
|
||||
}
|
||||
String::new()
|
||||
}
|
||||
|
||||
/// The text of a line with its task marker removed, or the line as it was.
|
||||
///
|
||||
/// For naming a note: a list-only note is named by its first item, and calling one
|
||||
@@ -759,6 +785,15 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixture_display_titles() {
|
||||
for case in fixture()["display_titles"].as_array().unwrap() {
|
||||
let body = case["body"].as_str().unwrap();
|
||||
let want = case["title"].as_str().unwrap();
|
||||
assert_eq!(display_title(body), want, "body {body:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fixture_lifts() {
|
||||
for case in fixture()["lifts"].as_array().unwrap() {
|
||||
|
||||
+13
-2
@@ -24,6 +24,11 @@ pub const DB_FILE: &str = "inkwell.db";
|
||||
/// Attachment bytes, filed by content hash, beside [`DB_FILE`].
|
||||
pub const BLOBS_DIR: &str = "blobs";
|
||||
|
||||
/// The type of bytes whose type isn't known, or isn't safe to say: stored for an
|
||||
/// attachment that declared none, and served for one that can't be shown inline.
|
||||
/// The schema's column default (`schema.rs`) is the same string, in SQL.
|
||||
pub const OPAQUE_MIME: &str = "application/octet-stream";
|
||||
|
||||
/// An instant in the store's own form: RFC 3339, UTC, milliseconds, `Z`. Every
|
||||
/// stored time is written this way, which is what makes lexical order chronological.
|
||||
pub fn iso(t: DateTime<Utc>) -> String {
|
||||
@@ -72,14 +77,20 @@ pub fn open(path: &Path) -> rusqlite::Result<Db> {
|
||||
/// crate was doing before the core was extracted. The Android bindings will want the
|
||||
/// same thing.
|
||||
pub fn open_in_memory() -> rusqlite::Result<Db> {
|
||||
Ok(Db(Mutex::new(memory_conn()?)))
|
||||
}
|
||||
|
||||
/// A migrated, in-memory connection: [`open_in_memory`]'s, and every core test's
|
||||
/// store.
|
||||
pub(crate) fn memory_conn() -> rusqlite::Result<Connection> {
|
||||
let conn = Connection::open_in_memory()?;
|
||||
schema::migrate(&conn)?;
|
||||
Ok(Db(Mutex::new(conn)))
|
||||
Ok(conn)
|
||||
}
|
||||
|
||||
/// A one-line count summary of the store, for the startup log.
|
||||
pub fn summary(db: &Db) -> String {
|
||||
let conn = match db.0.lock() {
|
||||
let conn = match db.conn() {
|
||||
Ok(c) => c,
|
||||
Err(_) => return "counts unavailable (lock poisoned)".to_string(),
|
||||
};
|
||||
|
||||
@@ -5,6 +5,23 @@
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// What this account may do with a note: it is ours, or shared with us to edit or
|
||||
/// only to view. The words the server's sharing uses, and the stored `permission`.
|
||||
pub mod access {
|
||||
pub const OWNER: &str = "owner";
|
||||
pub const EDIT: &str = "edit";
|
||||
pub const VIEW: &str = "view";
|
||||
}
|
||||
|
||||
/// The kinds of row a change names on the wire, and that `pending_deletes` files a
|
||||
/// local delete under until it is sent.
|
||||
pub mod entity {
|
||||
pub const NOTE: &str = "note";
|
||||
pub const LABEL: &str = "label";
|
||||
pub const ATTACHMENT: &str = "attachment";
|
||||
pub const PREVIEW: &str = "preview";
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Note {
|
||||
pub id: String,
|
||||
|
||||
@@ -559,7 +559,9 @@ fn strings(v: Option<&Value>) -> Vec<String> {
|
||||
/// Any RFC 3339 instant, in the store's own form (UTC, milliseconds, `Z`). Anything
|
||||
/// else is treated as absent, as the server's `parse_dt` does.
|
||||
fn instant(raw: &str) -> Option<String> {
|
||||
DateTime::parse_from_rfc3339(raw).ok().map(|t| iso(t.with_timezone(&Utc)))
|
||||
DateTime::parse_from_rfc3339(raw)
|
||||
.ok()
|
||||
.map(|t| iso(t.with_timezone(&Utc)))
|
||||
}
|
||||
|
||||
/// Keep's timestamps: integer microseconds since the epoch.
|
||||
@@ -591,13 +593,11 @@ fn dirname(path: &str) -> &str {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::local::schema;
|
||||
|
||||
fn store() -> (Connection, BlobStore, std::path::PathBuf) {
|
||||
let conn = Connection::open_in_memory().unwrap();
|
||||
schema::migrate(&conn).unwrap();
|
||||
let dir = std::env::temp_dir().join(format!("inkwell-portable-{}", uuid::Uuid::new_v4()));
|
||||
let blobs = BlobStore::new(dir.clone()).unwrap();
|
||||
let conn = crate::local::memory_conn().unwrap();
|
||||
let blobs = crate::sync::blobs::scratch("portable");
|
||||
let dir = blobs.root().to_path_buf();
|
||||
(conn, blobs, dir)
|
||||
}
|
||||
|
||||
|
||||
@@ -74,6 +74,19 @@ mod tests {
|
||||
use super::*;
|
||||
use chrono::TimeZone;
|
||||
|
||||
#[test]
|
||||
fn the_rules_are_the_shared_fixtures() {
|
||||
let fixture: serde_json::Value =
|
||||
serde_json::from_str(include_str!("../../testdata/grammar.json")).expect("parses");
|
||||
let want: Vec<&str> = fixture["recurrences"]
|
||||
.as_array()
|
||||
.expect("a list")
|
||||
.iter()
|
||||
.map(|r| r.as_str().expect("a string"))
|
||||
.collect();
|
||||
assert_eq!(RECURRENCES.to_vec(), want);
|
||||
}
|
||||
|
||||
fn utc(y: i32, m: u32, d: u32, h: u32, min: u32) -> DateTime<Utc> {
|
||||
Utc.with_ymd_and_hms(y, m, d, h, min, 0).unwrap()
|
||||
}
|
||||
|
||||
@@ -80,12 +80,9 @@ pub fn sweep_if_unlinked(conn: &Connection) -> rusqlite::Result<Option<usize>> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::local::schema;
|
||||
|
||||
fn db() -> Connection {
|
||||
let conn = Connection::open_in_memory().expect("in-memory db");
|
||||
schema::migrate(&conn).expect("migrate");
|
||||
conn
|
||||
crate::local::memory_conn().expect("in-memory db")
|
||||
}
|
||||
|
||||
/// A trashed note of a given age, stamped in the format the CLIENT writes
|
||||
|
||||
+35
-52
@@ -4,7 +4,8 @@
|
||||
//! `#tags` are NOT stored as such (derived at query time into labels), matching
|
||||
//! docs/sync.md.
|
||||
//!
|
||||
//! Migrations are gated on `PRAGMA user_version`; bump it and add a block per change.
|
||||
//! Migrations are gated on `PRAGMA user_version`; a change is a new entry at the end of
|
||||
//! [`STEPS`].
|
||||
|
||||
use rusqlite::{params, Connection, OptionalExtension};
|
||||
|
||||
@@ -331,60 +332,42 @@ const SCHEMA_V13: &str = r#"
|
||||
DROP TABLE saved_filters;
|
||||
"#;
|
||||
|
||||
/// One schema version's change: SQL, or code for a change SQL alone can't make.
|
||||
enum Step {
|
||||
Sql(&'static str),
|
||||
Code(fn(&Connection) -> rusqlite::Result<()>),
|
||||
}
|
||||
|
||||
/// Every version in order: entry N brings a store to version N + 1. A change is a
|
||||
/// new entry at the end; [`migrate`] applies each one a store hasn't had yet.
|
||||
const STEPS: &[Step] = &[
|
||||
Step::Sql(SCHEMA_V1),
|
||||
Step::Sql(SCHEMA_V2),
|
||||
Step::Sql(SCHEMA_V3),
|
||||
Step::Sql(SCHEMA_V4),
|
||||
Step::Sql(SCHEMA_V5),
|
||||
Step::Sql(SCHEMA_V6),
|
||||
Step::Sql(SCHEMA_V7),
|
||||
Step::Code(migrate_v8),
|
||||
Step::Sql(SCHEMA_V9),
|
||||
Step::Sql(SCHEMA_V10),
|
||||
Step::Sql(SCHEMA_V11),
|
||||
Step::Sql(SCHEMA_V12),
|
||||
Step::Sql(SCHEMA_V13),
|
||||
];
|
||||
|
||||
pub fn migrate(conn: &Connection) -> rusqlite::Result<()> {
|
||||
conn.execute_batch("PRAGMA foreign_keys = ON;")?;
|
||||
let version: i64 = conn.query_row("PRAGMA user_version", [], |r| r.get(0))?;
|
||||
if version < 1 {
|
||||
conn.execute_batch(SCHEMA_V1)?;
|
||||
conn.execute_batch("PRAGMA user_version = 1;")?;
|
||||
}
|
||||
if version < 2 {
|
||||
conn.execute_batch(SCHEMA_V2)?;
|
||||
conn.execute_batch("PRAGMA user_version = 2;")?;
|
||||
}
|
||||
if version < 3 {
|
||||
conn.execute_batch(SCHEMA_V3)?;
|
||||
conn.execute_batch("PRAGMA user_version = 3;")?;
|
||||
}
|
||||
if version < 4 {
|
||||
conn.execute_batch(SCHEMA_V4)?;
|
||||
conn.execute_batch("PRAGMA user_version = 4;")?;
|
||||
}
|
||||
if version < 5 {
|
||||
conn.execute_batch(SCHEMA_V5)?;
|
||||
conn.execute_batch("PRAGMA user_version = 5;")?;
|
||||
}
|
||||
if version < 6 {
|
||||
conn.execute_batch(SCHEMA_V6)?;
|
||||
conn.execute_batch("PRAGMA user_version = 6;")?;
|
||||
}
|
||||
if version < 7 {
|
||||
conn.execute_batch(SCHEMA_V7)?;
|
||||
conn.execute_batch("PRAGMA user_version = 7;")?;
|
||||
}
|
||||
if version < 8 {
|
||||
migrate_v8(conn)?;
|
||||
conn.execute_batch("PRAGMA user_version = 8;")?;
|
||||
}
|
||||
if version < 9 {
|
||||
conn.execute_batch(SCHEMA_V9)?;
|
||||
conn.execute_batch("PRAGMA user_version = 9;")?;
|
||||
}
|
||||
if version < 10 {
|
||||
conn.execute_batch(SCHEMA_V10)?;
|
||||
conn.execute_batch("PRAGMA user_version = 10;")?;
|
||||
}
|
||||
if version < 11 {
|
||||
conn.execute_batch(SCHEMA_V11)?;
|
||||
conn.execute_batch("PRAGMA user_version = 11;")?;
|
||||
}
|
||||
if version < 12 {
|
||||
conn.execute_batch(SCHEMA_V12)?;
|
||||
conn.execute_batch("PRAGMA user_version = 12;")?;
|
||||
}
|
||||
if version < 13 {
|
||||
conn.execute_batch(SCHEMA_V13)?;
|
||||
conn.execute_batch("PRAGMA user_version = 13;")?;
|
||||
for (to, step) in (1..).zip(STEPS) {
|
||||
if version >= to {
|
||||
continue;
|
||||
}
|
||||
match step {
|
||||
Step::Sql(sql) => conn.execute_batch(sql)?,
|
||||
Step::Code(apply) => apply(conn)?,
|
||||
}
|
||||
conn.execute_batch(&format!("PRAGMA user_version = {to};"))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
+40
-78
@@ -15,33 +15,12 @@ use uuid::Uuid;
|
||||
use crate::local::derive;
|
||||
use crate::local::models::*;
|
||||
use crate::local::recur;
|
||||
use crate::local::{iso, now};
|
||||
use crate::local::{iso, now, OPAQUE_MIME};
|
||||
|
||||
fn new_id() -> String {
|
||||
Uuid::new_v4().to_string()
|
||||
}
|
||||
|
||||
/// The note's NAME: the first line of its body that says anything.
|
||||
///
|
||||
/// Mirrors `derive_display_title` in the server's notes/helpers.py — one rule written
|
||||
/// twice, and they have to agree or a synced note is called different things on either
|
||||
/// side of the wire.
|
||||
///
|
||||
/// It no longer needs the items, because the items ARE lines of the body now (M304).
|
||||
/// What it needs instead is to strip the task marker off: a list-only note is still
|
||||
/// named by its first item, and calling that note "- [ ] milk" would be showing
|
||||
/// someone the storage rather than the note. An empty item is skipped rather than
|
||||
/// naming the note "", which is what a half-typed list would otherwise do.
|
||||
fn display_title(body: &str) -> String {
|
||||
for line in body.lines() {
|
||||
let text = derive::strip_marker(line.trim()).trim();
|
||||
if !text.is_empty() {
|
||||
return text.to_string();
|
||||
}
|
||||
}
|
||||
String::new()
|
||||
}
|
||||
|
||||
fn escape_like(s: &str) -> String {
|
||||
s.replace('\\', "\\\\")
|
||||
.replace('%', "\\%")
|
||||
@@ -183,7 +162,7 @@ fn load_note(conn: &Connection, id: &str) -> rusqlite::Result<Note> {
|
||||
note.items = items_of(¬e.body);
|
||||
note.attachments = load_attachments(conn, id)?;
|
||||
note.previews = load_previews(conn, id)?;
|
||||
note.display_title = display_title(¬e.body);
|
||||
note.display_title = derive::display_title(¬e.body);
|
||||
Ok(note)
|
||||
}
|
||||
|
||||
@@ -226,7 +205,7 @@ fn permission_of(conn: &Connection, id: &str) -> rusqlite::Result<String> {
|
||||
/// The note's text may change here: it is ours, or shared with us to edit.
|
||||
fn require_text(conn: &Connection, id: &str) -> rusqlite::Result<String> {
|
||||
let permission = permission_of(conn, id)?;
|
||||
if permission == "view" {
|
||||
if permission == access::VIEW {
|
||||
return Err(refuse(VIEW_ONLY));
|
||||
}
|
||||
Ok(permission)
|
||||
@@ -236,8 +215,8 @@ fn require_text(conn: &Connection, id: &str) -> rusqlite::Result<String> {
|
||||
/// owner.
|
||||
fn require_owner(conn: &Connection, id: &str) -> rusqlite::Result<()> {
|
||||
match permission_of(conn, id)?.as_str() {
|
||||
"owner" => Ok(()),
|
||||
"view" => Err(refuse(VIEW_ONLY)),
|
||||
access::OWNER => Ok(()),
|
||||
access::VIEW => Err(refuse(VIEW_ONLY)),
|
||||
_ => Err(refuse(OWNER_ONLY)),
|
||||
}
|
||||
}
|
||||
@@ -294,7 +273,7 @@ fn find_or_create_label(conn: &Connection, name: &str) -> rusqlite::Result<Strin
|
||||
/// so this overwrites what they wrote, on purpose.
|
||||
///
|
||||
/// `display_title` needs no attention here, unlike on the server: the core derives it
|
||||
/// on READ (see `display_title` above, called from `load_note`) rather than storing
|
||||
/// on READ (`derive::display_title`, called from `load_note`) rather than storing
|
||||
/// it, so there is no persisted copy to go stale.
|
||||
///
|
||||
/// The two kinds of tag are handled differently, and that difference IS what `via_tag`
|
||||
@@ -452,15 +431,14 @@ pub fn get_note(conn: &Connection, id: &str) -> rusqlite::Result<Note> {
|
||||
load_note(conn, id)
|
||||
}
|
||||
|
||||
/// Which notes' reminders are this device's to fire: the owner's, out of the trash.
|
||||
/// A note shared with us neither alerts us nor is ours to clear, the same as on the
|
||||
/// server.
|
||||
const OWN_REMINDERS: &str = "trashed = 0 AND remind_at IS NOT NULL AND permission = 'owner'";
|
||||
|
||||
pub fn reminders(conn: &Connection) -> rusqlite::Result<Vec<Note>> {
|
||||
// The owner's reminders: a note shared with us neither alerts us nor is ours to
|
||||
// clear, the same as on the server.
|
||||
notes_where(
|
||||
conn,
|
||||
"SELECT id FROM notes WHERE trashed = 0 AND remind_at IS NOT NULL AND permission = 'owner'
|
||||
ORDER BY remind_at ASC",
|
||||
[],
|
||||
)
|
||||
let sql = format!("SELECT id FROM notes WHERE {OWN_REMINDERS} ORDER BY remind_at ASC");
|
||||
notes_where(conn, &sql, [])
|
||||
}
|
||||
|
||||
/// Reminders due at or before `now_ms`, soonest first, trash excluded.
|
||||
@@ -470,11 +448,9 @@ pub fn reminders(conn: &Connection) -> rusqlite::Result<Vec<Note>> {
|
||||
/// parse is left out: it can never come due, and announcing it would be a guess.
|
||||
pub fn due_reminders(conn: &Connection, now_ms: i64) -> rusqlite::Result<Vec<DueReminder>> {
|
||||
let set: Vec<(String, String)> = {
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT id, remind_at FROM notes
|
||||
WHERE trashed = 0 AND remind_at IS NOT NULL AND permission = 'owner'
|
||||
ORDER BY remind_at ASC",
|
||||
)?;
|
||||
let mut stmt = conn.prepare(&format!(
|
||||
"SELECT id, remind_at FROM notes WHERE {OWN_REMINDERS} ORDER BY remind_at ASC"
|
||||
))?;
|
||||
let rows = stmt.query_map([], |r| Ok((r.get(0)?, r.get(1)?)))?;
|
||||
rows.collect::<rusqlite::Result<_>>()?
|
||||
};
|
||||
@@ -561,9 +537,7 @@ const REVISION_WINDOW_MINUTES: i64 = 10;
|
||||
/// own — no "commit" the client has to declare, and no protocol surface to carry it,
|
||||
/// which matters because sync-apply takes this same path.
|
||||
fn should_snapshot(conn: &Connection, id: &str, new_body: &str) -> rusqlite::Result<bool> {
|
||||
let current: String =
|
||||
conn.query_row("SELECT body FROM notes WHERE id = ?1", [id], |r| r.get(0))?;
|
||||
if current == new_body {
|
||||
if note_body(conn, id)? == new_body {
|
||||
return Ok(false);
|
||||
}
|
||||
// String comparison, not date maths: timestamps are RFC3339 UTC with a fixed
|
||||
@@ -578,8 +552,7 @@ fn should_snapshot(conn: &Connection, id: &str, new_body: &str) -> rusqlite::Res
|
||||
}
|
||||
|
||||
fn snapshot_revision(conn: &Connection, id: &str) -> rusqlite::Result<()> {
|
||||
let body: String =
|
||||
conn.query_row("SELECT body FROM notes WHERE id = ?1", [id], |r| r.get(0))?;
|
||||
let body = note_body(conn, id)?;
|
||||
conn.execute(
|
||||
"INSERT INTO note_revisions (id, note_id, body, created_at) VALUES (?1, ?2, ?3, ?4)",
|
||||
params![new_id(), id, body, now()],
|
||||
@@ -598,7 +571,7 @@ pub fn update_note(conn: &Connection, id: &str, changes: &Value) -> rusqlite::Re
|
||||
} else {
|
||||
permission_of(conn, id)?
|
||||
};
|
||||
let owned = permission == "owner";
|
||||
let owned = permission == access::OWNER;
|
||||
if !owned
|
||||
&& obj
|
||||
.keys()
|
||||
@@ -792,7 +765,7 @@ fn normalize_mime(raw: &str) -> String {
|
||||
.trim()
|
||||
.to_ascii_lowercase();
|
||||
if bare.is_empty() {
|
||||
"application/octet-stream".to_string()
|
||||
OPAQUE_MIME.to_string()
|
||||
} else {
|
||||
bare
|
||||
}
|
||||
@@ -873,7 +846,7 @@ pub fn delete_attachment(conn: &Connection, id: &str, att_id: &str) -> rusqlite:
|
||||
// Only a file the server holds needs a tombstone; without one the next pull would
|
||||
// bring it straight back. One still waiting to upload leaves the queue with its row.
|
||||
if uploaded == Some(true) {
|
||||
record_pending_delete(conn, "attachment", att_id)?;
|
||||
record_pending_delete(conn, entity::ATTACHMENT, att_id)?;
|
||||
}
|
||||
touch(conn, id)?;
|
||||
load_note(conn, id)
|
||||
@@ -887,7 +860,7 @@ pub fn delete_preview(conn: &Connection, id: &str, preview_id: &str) -> rusqlite
|
||||
)?;
|
||||
// Previews are made by the server, so every one it has is one it would send back.
|
||||
if removed > 0 {
|
||||
record_pending_delete(conn, "preview", preview_id)?;
|
||||
record_pending_delete(conn, entity::PREVIEW, preview_id)?;
|
||||
}
|
||||
touch(conn, id)?;
|
||||
load_note(conn, id)
|
||||
@@ -938,11 +911,11 @@ pub fn delete_forever(conn: &Connection, id: &str) -> rusqlite::Result<()> {
|
||||
// is someone else's is not ours to delete.
|
||||
if permission_of(conn, id)
|
||||
.optional()?
|
||||
.is_some_and(|p| p != "owner")
|
||||
.is_some_and(|p| p != access::OWNER)
|
||||
{
|
||||
return Err(refuse(OWNER_ONLY));
|
||||
}
|
||||
record_pending_delete(conn, "note", id)?;
|
||||
record_pending_delete(conn, entity::NOTE, id)?;
|
||||
conn.execute("DELETE FROM notes WHERE id = ?1", [id])?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -996,7 +969,7 @@ pub fn revisions(conn: &Connection, id: &str) -> rusqlite::Result<Vec<NoteRevisi
|
||||
}
|
||||
|
||||
pub fn restore_revision(conn: &Connection, id: &str, rev_id: &str) -> rusqlite::Result<Note> {
|
||||
let owned = require_text(conn, id)? == "owner";
|
||||
let owned = require_text(conn, id)? == access::OWNER;
|
||||
let body: String = conn.query_row(
|
||||
"SELECT body FROM note_revisions WHERE id = ?1 AND note_id = ?2",
|
||||
params![rev_id, id],
|
||||
@@ -1072,7 +1045,7 @@ pub fn rename_label(conn: &Connection, id: &str, name: &str) -> rusqlite::Result
|
||||
)
|
||||
.optional()?;
|
||||
|
||||
if let Some((other_id, other_created)) = clash {
|
||||
let renamed = if let Some((other_id, other_created)) = clash {
|
||||
let mine_created: String =
|
||||
conn.query_row("SELECT created_at FROM labels WHERE id = ?1", [id], |r| {
|
||||
r.get(0)
|
||||
@@ -1088,20 +1061,18 @@ pub fn rename_label(conn: &Connection, id: &str, name: &str) -> rusqlite::Result
|
||||
// knows to mark every affected NOTE dirty before the delete cascades the
|
||||
// membership rows away, which is what makes the merge reach the server.
|
||||
merge_labels(conn, &doomed, &survivor)?;
|
||||
// The survivor may still carry the old spelling — it is the one that keeps
|
||||
// existing, so it is the one that has to end up named what was asked for.
|
||||
conn.execute(
|
||||
"UPDATE labels SET name = ?1, updated_at = ?2, dirty = 1 WHERE id = ?3",
|
||||
params![name, now(), survivor],
|
||||
)?;
|
||||
return load_label(conn, &survivor);
|
||||
}
|
||||
survivor
|
||||
} else {
|
||||
id.to_string()
|
||||
};
|
||||
|
||||
// A merge's survivor may still carry the old spelling — it is the one that keeps
|
||||
// existing, so it is the one that has to end up named what was asked for.
|
||||
conn.execute(
|
||||
"UPDATE labels SET name = ?1, updated_at = ?2, dirty = 1 WHERE id = ?3",
|
||||
params![name, now(), id],
|
||||
params![name, now(), renamed],
|
||||
)?;
|
||||
load_label(conn, id)
|
||||
load_label(conn, &renamed)
|
||||
}
|
||||
|
||||
pub fn set_label_color(conn: &Connection, id: &str, color: &str) -> rusqlite::Result<Label> {
|
||||
@@ -1113,7 +1084,7 @@ pub fn set_label_color(conn: &Connection, id: &str, color: &str) -> rusqlite::Re
|
||||
}
|
||||
|
||||
pub fn remove_label(conn: &Connection, id: &str) -> rusqlite::Result<()> {
|
||||
record_pending_delete(conn, "label", id)?;
|
||||
record_pending_delete(conn, entity::LABEL, id)?;
|
||||
conn.execute("DELETE FROM labels WHERE id = ?1", [id])?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -1137,7 +1108,7 @@ pub fn merge_labels(
|
||||
WHERE id IN (SELECT note_id FROM note_labels WHERE label_id = ?1)",
|
||||
[source_id],
|
||||
)?;
|
||||
record_pending_delete(conn, "label", source_id)?;
|
||||
record_pending_delete(conn, entity::LABEL, source_id)?;
|
||||
conn.execute("DELETE FROM labels WHERE id = ?1", [source_id])?;
|
||||
load_label(conn, target_id)
|
||||
}
|
||||
@@ -1151,12 +1122,9 @@ mod tests {
|
||||
//! against a real schema catches, which is what every test here runs.
|
||||
|
||||
use super::*;
|
||||
use crate::local::schema;
|
||||
|
||||
fn db() -> Connection {
|
||||
let conn = Connection::open_in_memory().expect("in-memory db");
|
||||
schema::migrate(&conn).expect("migrate");
|
||||
conn
|
||||
crate::local::memory_conn().expect("in-memory db")
|
||||
}
|
||||
|
||||
fn note(conn: &Connection, body: &str) -> Note {
|
||||
@@ -1821,12 +1789,6 @@ mod tests {
|
||||
assert_eq!(list_labels(&conn).expect("labels")[0].count, Some(1));
|
||||
}
|
||||
|
||||
fn blobs(tag: &str) -> crate::sync::blobs::BlobStore {
|
||||
let dir = std::env::temp_dir().join(format!("ts-store-blobs-{}-{tag}", std::process::id()));
|
||||
let _ = std::fs::remove_dir_all(&dir);
|
||||
crate::sync::blobs::BlobStore::new(dir).expect("blobs")
|
||||
}
|
||||
|
||||
fn tombstones(conn: &Connection) -> Vec<(String, String)> {
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT entity, id FROM pending_deletes ORDER BY entity, id")
|
||||
@@ -1840,7 +1802,7 @@ mod tests {
|
||||
#[test]
|
||||
fn an_attached_file_is_kept_here_and_queued_to_upload() {
|
||||
let conn = db();
|
||||
let blobs = blobs("attach");
|
||||
let blobs = crate::sync::blobs::scratch("attach");
|
||||
let n = note(&conn, "with a receipt");
|
||||
conn.execute("UPDATE notes SET dirty = 0", [])
|
||||
.expect("clean");
|
||||
@@ -1884,7 +1846,7 @@ mod tests {
|
||||
#[test]
|
||||
fn attaching_to_a_note_that_is_gone_writes_nothing() {
|
||||
let conn = db();
|
||||
let blobs = blobs("gone");
|
||||
let blobs = crate::sync::blobs::scratch("gone");
|
||||
assert!(add_attachment(&conn, &blobs, "missing", "a.txt", "text/plain", b"hi").is_err());
|
||||
let rows: i64 = conn
|
||||
.query_row("SELECT COUNT(*) FROM attachments", [], |r| r.get(0))
|
||||
@@ -1897,7 +1859,7 @@ mod tests {
|
||||
#[test]
|
||||
fn only_a_file_the_server_holds_leaves_a_tombstone_when_removed() {
|
||||
let conn = db();
|
||||
let blobs = blobs("remove");
|
||||
let blobs = crate::sync::blobs::scratch("remove");
|
||||
let n = note(&conn, "two files");
|
||||
conn.execute(
|
||||
"INSERT INTO attachments (id, note_id, url) VALUES ('synced', ?1, '/x')",
|
||||
|
||||
+23
-16
@@ -12,6 +12,8 @@ use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::OnceLock;
|
||||
|
||||
use crate::local::OPAQUE_MIME;
|
||||
|
||||
/// A sha256 in lowercase hex, and nothing else.
|
||||
///
|
||||
/// This is a **path-safety** check, not a formatting nicety: the hash is taken
|
||||
@@ -189,7 +191,7 @@ fn content_type_for(mime: &str) -> String {
|
||||
if familiar && printable {
|
||||
mime.to_string()
|
||||
} else {
|
||||
"application/octet-stream".to_string()
|
||||
OPAQUE_MIME.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,18 +215,23 @@ pub fn serve(path: &str, query: Option<&str>) -> (u16, String, Vec<u8>) {
|
||||
(200, content_type_for(&claimed), bytes)
|
||||
}
|
||||
|
||||
/// A blob store in a fresh throwaway directory, for any core test that needs one.
|
||||
/// No tempfile dependency for one fixture: the process id and a counter keep
|
||||
/// concurrent tests and runs apart, and `tag` says whose it was.
|
||||
#[cfg(test)]
|
||||
pub(crate) fn scratch(tag: &str) -> BlobStore {
|
||||
use std::sync::atomic::{AtomicU32, Ordering};
|
||||
static SEQ: AtomicU32 = AtomicU32::new(0);
|
||||
let n = SEQ.fetch_add(1, Ordering::Relaxed);
|
||||
let dir = std::env::temp_dir().join(format!("ts-blobs-{}-{n}-{tag}", std::process::id()));
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
BlobStore::new(dir).expect("scratch blob store")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
/// A blob store in a throwaway directory. No tempfile dependency for one test
|
||||
/// fixture — the process id keeps concurrent runs apart.
|
||||
fn store(tag: &str) -> BlobStore {
|
||||
let dir = std::env::temp_dir().join(format!("ts-blobs-{}-{tag}", std::process::id()));
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
BlobStore::new(dir).expect("store")
|
||||
}
|
||||
|
||||
/// sha256("hello") — a fixed vector, so a broken digest can't agree with itself.
|
||||
const HELLO: &str = "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824";
|
||||
|
||||
@@ -235,7 +242,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn stores_and_reads_back() {
|
||||
let store = store("roundtrip");
|
||||
let store = scratch("roundtrip");
|
||||
assert!(!store.has(HELLO));
|
||||
store.store(HELLO, b"hello").expect("store");
|
||||
assert!(store.has(HELLO));
|
||||
@@ -246,7 +253,7 @@ mod tests {
|
||||
fn refuses_bytes_that_dont_match_the_hash() {
|
||||
// A corrupted or substituted transfer must never be filed under a name that
|
||||
// claims it's genuine.
|
||||
let store = store("mismatch");
|
||||
let store = scratch("mismatch");
|
||||
let err = store.store(HELLO, b"goodbye").expect_err("must reject");
|
||||
assert!(err.contains("integrity"), "got {err}");
|
||||
assert!(!store.has(HELLO), "nothing should have been written");
|
||||
@@ -255,7 +262,7 @@ mod tests {
|
||||
#[test]
|
||||
fn rejects_a_hash_that_could_escape_the_directory() {
|
||||
// The hash arrives from a server response and becomes a filename.
|
||||
let store = store("traversal");
|
||||
let store = scratch("traversal");
|
||||
assert!(store.path("../../etc/passwd").is_none());
|
||||
assert!(store.store("../../etc/passwd", b"x").is_err());
|
||||
assert!(store.path("").is_none());
|
||||
@@ -265,7 +272,7 @@ mod tests {
|
||||
#[test]
|
||||
fn accepts_an_uppercase_hash() {
|
||||
// The wire format isn't guaranteed to be lowercase; the filename is.
|
||||
let store = store("case");
|
||||
let store = scratch("case");
|
||||
store
|
||||
.store(&HELLO.to_ascii_uppercase(), b"hello")
|
||||
.expect("store");
|
||||
@@ -306,7 +313,7 @@ mod tests {
|
||||
fn a_scriptable_type_is_served_as_a_download() {
|
||||
// This scheme is an origin of its own. An attachment claiming to be HTML
|
||||
// must not be handed back as a document that can run there.
|
||||
let opaque = "application/octet-stream";
|
||||
let opaque = OPAQUE_MIME;
|
||||
assert_eq!(content_type_for("text/html"), opaque);
|
||||
assert_eq!(content_type_for("application/javascript"), opaque);
|
||||
// An image family member that is really a document with script in it.
|
||||
@@ -327,14 +334,14 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn put_files_bytes_under_their_own_hash() {
|
||||
let store = store("put");
|
||||
let store = scratch("put");
|
||||
assert_eq!(store.put(b"hello").expect("put"), HELLO);
|
||||
assert_eq!(store.read(HELLO).as_deref(), Some(&b"hello"[..]));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn missing_blob_reads_as_none() {
|
||||
let store = store("missing");
|
||||
let store = scratch("missing");
|
||||
assert!(store.read(HELLO).is_none());
|
||||
assert!(!store.has(HELLO));
|
||||
}
|
||||
|
||||
+56
-98
@@ -11,7 +11,7 @@
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
|
||||
use reqwest::{RequestBuilder, StatusCode};
|
||||
use reqwest::{RequestBuilder, Response, StatusCode};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::compat::{self, Compatibility, ServerInfo};
|
||||
@@ -154,6 +154,46 @@ fn unexpected_status(base_url: &str, status: StatusCode) -> String {
|
||||
)
|
||||
}
|
||||
|
||||
/// Send `request`, failing the way every call to the server fails alike: a
|
||||
/// transport error, described for a person, and a 401, as `unauthorized` (what a
|
||||
/// rejected credential means depends on the call). Any other status is the
|
||||
/// caller's to read; [`send`] is this plus "anything but success is unexpected".
|
||||
async fn send_raw(
|
||||
base_url: &str,
|
||||
request: RequestBuilder,
|
||||
unauthorized: &str,
|
||||
) -> Result<Response, String> {
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| describe_transport_error(base_url, &e))?;
|
||||
if response.status() == StatusCode::UNAUTHORIZED {
|
||||
return Err(unauthorized.to_string());
|
||||
}
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
/// [`send_raw`], and anything but a success is [`unexpected_status`].
|
||||
async fn send(
|
||||
base_url: &str,
|
||||
request: RequestBuilder,
|
||||
unauthorized: &str,
|
||||
) -> Result<Response, String> {
|
||||
let response = send_raw(base_url, request, unauthorized).await?;
|
||||
let status = response.status();
|
||||
if !status.is_success() {
|
||||
return Err(unexpected_status(base_url, status));
|
||||
}
|
||||
Ok(response)
|
||||
}
|
||||
|
||||
/// The server's own words for a refusal, when it gave some (`{"error": "…"}`).
|
||||
/// "file is too large (max 25 MB)" is what a person can act on; a status is not.
|
||||
async fn server_reason(response: Response) -> Option<String> {
|
||||
let body = response.json::<serde_json::Value>().await.ok()?;
|
||||
body.get("error")?.as_str().map(String::from)
|
||||
}
|
||||
|
||||
/// Ask a server who it is and whether we can sync with it.
|
||||
///
|
||||
/// `Err` means we never got a usable answer (bad address, unreachable, not an
|
||||
@@ -211,18 +251,8 @@ pub async fn device_login(
|
||||
"name": device_name,
|
||||
});
|
||||
let request = prepare(http()?.post(device_login_url(base_url)), None).json(&body);
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| describe_transport_error(base_url, &e))?;
|
||||
|
||||
let status = response.status();
|
||||
if status == StatusCode::UNAUTHORIZED {
|
||||
return Err("That email and password didn't match an account on this server.".to_string());
|
||||
}
|
||||
if !status.is_success() {
|
||||
return Err(unexpected_status(base_url, status));
|
||||
}
|
||||
let refused = "That email and password didn't match an account on this server.";
|
||||
let response = send(base_url, request, refused).await?;
|
||||
|
||||
let parsed: DeviceLoginResponse = response
|
||||
.json()
|
||||
@@ -238,20 +268,9 @@ pub async fn device_login(
|
||||
/// far from the thing that caused it.
|
||||
pub async fn fetch_identity(base_url: &str, token: &str) -> Result<Identity, String> {
|
||||
let request = prepare(http()?.get(me_url(base_url)), Some(token));
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| describe_transport_error(base_url, &e))?;
|
||||
|
||||
let status = response.status();
|
||||
if status == StatusCode::UNAUTHORIZED {
|
||||
let message = "That token isn't valid on this server — it may have been revoked. \
|
||||
Issue a new one from the web app under Account → Linked devices.";
|
||||
return Err(message.to_string());
|
||||
}
|
||||
if !status.is_success() {
|
||||
return Err(unexpected_status(base_url, status));
|
||||
}
|
||||
let refused = "That token isn't valid on this server — it may have been revoked. \
|
||||
Issue a new one from the web app under Account → Linked devices.";
|
||||
let response = send(base_url, request, refused).await?;
|
||||
|
||||
response
|
||||
.json()
|
||||
@@ -277,18 +296,7 @@ pub async fn fetch_changes(
|
||||
format!("{base_url}/api/sync/changes?since={since}")
|
||||
};
|
||||
let request = prepare(http_with(SYNC_TIMEOUT)?.get(url), Some(token));
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| describe_transport_error(base_url, &e))?;
|
||||
|
||||
let status = response.status();
|
||||
if status == StatusCode::UNAUTHORIZED {
|
||||
return Err(TOKEN_REJECTED.to_string());
|
||||
}
|
||||
if !status.is_success() {
|
||||
return Err(unexpected_status(base_url, status));
|
||||
}
|
||||
let response = send(base_url, request, TOKEN_REJECTED).await?;
|
||||
|
||||
response
|
||||
.json()
|
||||
@@ -308,18 +316,7 @@ pub async fn fetch_attachment(
|
||||
) -> Result<Vec<u8>, String> {
|
||||
let url = format!("{base_url}/api/notes/{note_id}/attachments/{attachment_id}");
|
||||
let request = prepare(http_with(SYNC_TIMEOUT)?.get(url), Some(token));
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| describe_transport_error(base_url, &e))?;
|
||||
|
||||
let status = response.status();
|
||||
if status == StatusCode::UNAUTHORIZED {
|
||||
return Err(TOKEN_REJECTED.to_string());
|
||||
}
|
||||
if !status.is_success() {
|
||||
return Err(unexpected_status(base_url, status));
|
||||
}
|
||||
let response = send(base_url, request, TOKEN_REJECTED).await?;
|
||||
|
||||
response
|
||||
.bytes()
|
||||
@@ -378,11 +375,8 @@ pub async fn upload_attachment(
|
||||
}
|
||||
// The server's own words, when it gave some: "file is too large (max 25 MB)" is
|
||||
// what a person can act on, and a bare status code is not.
|
||||
let reason = response
|
||||
.json::<serde_json::Value>()
|
||||
let reason = server_reason(response)
|
||||
.await
|
||||
.ok()
|
||||
.and_then(|v| v.get("error").and_then(|e| e.as_str()).map(String::from))
|
||||
.unwrap_or_else(|| format!("HTTP {}", status.as_u16()));
|
||||
if status.is_client_error() && status != StatusCode::NOT_FOUND {
|
||||
Err(UploadError::Refused(reason))
|
||||
@@ -406,18 +400,7 @@ pub async fn push_changes<T: Serialize>(
|
||||
let body = serde_json::json!({ "changes": changes });
|
||||
let url = format!("{base_url}/api/sync/push");
|
||||
let request = prepare(http_with(SYNC_TIMEOUT)?.post(url), Some(token)).json(&body);
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| describe_transport_error(base_url, &e))?;
|
||||
|
||||
let status = response.status();
|
||||
if status == StatusCode::UNAUTHORIZED {
|
||||
return Err(TOKEN_REJECTED.to_string());
|
||||
}
|
||||
if !status.is_success() {
|
||||
return Err(unexpected_status(base_url, status));
|
||||
}
|
||||
let response = send(base_url, request, TOKEN_REJECTED).await?;
|
||||
|
||||
response
|
||||
.text()
|
||||
@@ -509,24 +492,14 @@ async fn read_reply<T: serde::de::DeserializeOwned>(
|
||||
base_url: &str,
|
||||
request: RequestBuilder,
|
||||
) -> Result<T, String> {
|
||||
let response = request
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| describe_transport_error(base_url, &e))?;
|
||||
let response = send_raw(base_url, request, TOKEN_REJECTED).await?;
|
||||
let status = response.status();
|
||||
if status == StatusCode::UNAUTHORIZED {
|
||||
return Err(TOKEN_REJECTED.to_string());
|
||||
}
|
||||
if status == StatusCode::NOT_FOUND {
|
||||
return Err(SHARE_NOT_FOUND.to_string());
|
||||
}
|
||||
if !status.is_success() {
|
||||
// The server's own words when it gave some ("choose someone to share with").
|
||||
let reason = response
|
||||
.json::<serde_json::Value>()
|
||||
.await
|
||||
.ok()
|
||||
.and_then(|v| v.get("error").and_then(|e| e.as_str()).map(String::from));
|
||||
let reason = server_reason(response).await;
|
||||
return Err(reason.unwrap_or_else(|| unexpected_status(base_url, status)));
|
||||
}
|
||||
response
|
||||
@@ -717,18 +690,13 @@ pub async fn fetch_client_release(
|
||||
token: &str,
|
||||
) -> Result<Option<ClientRelease>, String> {
|
||||
let url = format!("{base_url}/api/client/android");
|
||||
let response = prepare(http()?.get(url), Some(token))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| describe_transport_error(base_url, &e))?;
|
||||
let request = prepare(http()?.get(url), Some(token));
|
||||
let response = send_raw(base_url, request, TOKEN_REJECTED).await?;
|
||||
|
||||
let status = response.status();
|
||||
if status == StatusCode::NOT_FOUND {
|
||||
return Ok(None);
|
||||
}
|
||||
if status == StatusCode::UNAUTHORIZED {
|
||||
return Err(TOKEN_REJECTED.to_string());
|
||||
}
|
||||
if !status.is_success() {
|
||||
return Err(unexpected_status(base_url, status));
|
||||
}
|
||||
@@ -768,18 +736,8 @@ pub async fn download_client(
|
||||
let path = release.url.trim_start_matches('/');
|
||||
let url = format!("{base_url}/{path}");
|
||||
|
||||
let mut response = prepare(http_with(SYNC_TIMEOUT)?.get(url), Some(token))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| describe_transport_error(base_url, &e))?;
|
||||
|
||||
let status = response.status();
|
||||
if status == StatusCode::UNAUTHORIZED {
|
||||
return Err(TOKEN_REJECTED.to_string());
|
||||
}
|
||||
if !status.is_success() {
|
||||
return Err(unexpected_status(base_url, status));
|
||||
}
|
||||
let request = prepare(http_with(SYNC_TIMEOUT)?.get(url), Some(token));
|
||||
let mut response = send(base_url, request, TOKEN_REJECTED).await?;
|
||||
|
||||
let partial = dest.with_extension("part");
|
||||
if let Some(parent) = partial.parent() {
|
||||
|
||||
+7
-29
@@ -13,6 +13,7 @@ use super::blobs::BlobStore;
|
||||
use super::client;
|
||||
use super::state;
|
||||
use super::wire;
|
||||
use crate::local::models::{access, entity};
|
||||
use crate::local::{now, Db};
|
||||
|
||||
/// Backstop against a server that never stops saying `has_more`. At the server's
|
||||
@@ -248,9 +249,9 @@ fn upsert_note(conn: &Connection, note: &wire::Note) -> rusqlite::Result<()> {
|
||||
// never changes, and the server's copy is the same value anyway.
|
||||
// A server without `shares` sends no permission, and every note it sends is ours.
|
||||
let permission = match note.permission.as_deref() {
|
||||
Some("edit") => "edit",
|
||||
Some("view") => "view",
|
||||
_ => "owner",
|
||||
Some(access::EDIT) => access::EDIT,
|
||||
Some(access::VIEW) => access::VIEW,
|
||||
_ => access::OWNER,
|
||||
};
|
||||
let (shared_by_id, shared_by_name) = match ¬e.shared_by {
|
||||
Some(by) => (Some(by.id.as_str()), Some(by.display_name.as_str())),
|
||||
@@ -330,7 +331,7 @@ fn replace_attachments(conn: &Connection, note: &wire::Note) -> rusqlite::Result
|
||||
params![note.id],
|
||||
)?;
|
||||
for (index, att) in note.attachments.iter().enumerate() {
|
||||
if removed_here(conn, "attachment", &att.id)? {
|
||||
if removed_here(conn, entity::ATTACHMENT, &att.id)? {
|
||||
continue;
|
||||
}
|
||||
// The server listing an id this device is still waiting to upload means the
|
||||
@@ -506,36 +507,13 @@ pub async fn run(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::local::schema;
|
||||
|
||||
fn db() -> Connection {
|
||||
let conn = Connection::open_in_memory().expect("in-memory db");
|
||||
schema::migrate(&conn).expect("migrate");
|
||||
conn
|
||||
crate::local::memory_conn().expect("in-memory db")
|
||||
}
|
||||
|
||||
fn note(id: &str, revision: i64) -> wire::Note {
|
||||
wire::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,
|
||||
}
|
||||
wire::sample_note(id, revision)
|
||||
}
|
||||
|
||||
fn attachment(id: &str) -> wire::Attachment {
|
||||
|
||||
+30
-52
@@ -9,12 +9,13 @@
|
||||
//! patch (docs/sync.md). The server resolves conflicts last-write-wins by the client's
|
||||
//! `edited_at`, snapshotting anything it overwrites into the note's version history.
|
||||
|
||||
use rusqlite::{params, Connection, OptionalExtension};
|
||||
use rusqlite::{params, Connection};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use super::blobs::BlobStore;
|
||||
use super::client::{self, UploadError};
|
||||
use super::state;
|
||||
use crate::local::models::{access, entity};
|
||||
use crate::local::Db;
|
||||
|
||||
/// The server rejects a batch larger than this (`MAX_PUSH` in `sync.py`).
|
||||
@@ -61,6 +62,10 @@ impl PushSummary {
|
||||
|
||||
// --- outgoing shapes ---------------------------------------------------------
|
||||
|
||||
/// A change's operation on the wire.
|
||||
const UPSERT: &str = "upsert";
|
||||
const DELETE: &str = "delete";
|
||||
|
||||
/// One entry in the `changes` array. Notes and labels share the envelope; serde skips
|
||||
/// the fields that don't apply, so the server sees exactly the shape docs/sync.md
|
||||
/// describes for each entity.
|
||||
@@ -108,7 +113,7 @@ impl Change {
|
||||
Change {
|
||||
entity,
|
||||
id,
|
||||
op: "delete",
|
||||
op: DELETE,
|
||||
edited_at,
|
||||
..Default::default()
|
||||
}
|
||||
@@ -201,10 +206,10 @@ fn collect_deletes(
|
||||
// Only these exist on the wire; anything else is a bug in a writer, and
|
||||
// shipping it would earn a rejection that no retry could clear.
|
||||
let entity: &'static str = match entity.as_str() {
|
||||
"note" => "note",
|
||||
"label" => "label",
|
||||
"attachment" => "attachment",
|
||||
"preview" => "preview",
|
||||
entity::NOTE => entity::NOTE,
|
||||
entity::LABEL => entity::LABEL,
|
||||
entity::ATTACHMENT => entity::ATTACHMENT,
|
||||
entity::PREVIEW => entity::PREVIEW,
|
||||
_ => continue,
|
||||
};
|
||||
out.push(Change::delete(entity, id, deleted_at));
|
||||
@@ -220,9 +225,9 @@ fn collect_labels(conn: &Connection, out: &mut Vec<Change>, limit: usize) -> rus
|
||||
)?;
|
||||
let rows = stmt.query_map(params![remaining as i64], |r| {
|
||||
Ok(Change {
|
||||
entity: "label",
|
||||
entity: entity::LABEL,
|
||||
id: r.get(0)?,
|
||||
op: "upsert",
|
||||
op: UPSERT,
|
||||
name: Some(r.get(1)?),
|
||||
color: Some(r.get(2)?),
|
||||
edited_at: r.get(3)?,
|
||||
@@ -313,15 +318,15 @@ fn note_change(conn: &Connection, id: &str, shared_state: bool) -> rusqlite::Res
|
||||
// pin, archive and place once they have been changed here, to a server that keeps
|
||||
// them. Trash, reminders and labels are the owner's, and this account's labels
|
||||
// were never on it.
|
||||
if row.permission != "owner" {
|
||||
if row.permission != access::OWNER {
|
||||
let state_at = row.state_at.filter(|_| shared_state);
|
||||
let own = state_at.is_some();
|
||||
return Ok(Change {
|
||||
entity: "note",
|
||||
entity: entity::NOTE,
|
||||
id: id.to_string(),
|
||||
op: "upsert",
|
||||
op: UPSERT,
|
||||
edited_at: row.updated_at,
|
||||
body: (row.permission == "edit").then_some(row.body),
|
||||
body: (row.permission == access::EDIT).then_some(row.body),
|
||||
pinned: own.then_some(row.pinned),
|
||||
archived: own.then_some(row.archived),
|
||||
position: own.then_some(row.position),
|
||||
@@ -341,9 +346,9 @@ fn note_change(conn: &Connection, id: &str, shared_state: bool) -> rusqlite::Res
|
||||
};
|
||||
|
||||
Ok(Change {
|
||||
entity: "note",
|
||||
entity: entity::NOTE,
|
||||
id: id.to_string(),
|
||||
op: "upsert",
|
||||
op: UPSERT,
|
||||
// The local `updated_at` IS the client's edit time, which is what the
|
||||
// server's last-write-wins comparison runs against.
|
||||
edited_at: row.updated_at,
|
||||
@@ -387,7 +392,7 @@ pub fn apply_results(
|
||||
} else {
|
||||
summary.applied += 1;
|
||||
}
|
||||
if change.op == "delete" {
|
||||
if change.op == DELETE {
|
||||
forget_pending_delete(&tx, change)?;
|
||||
}
|
||||
}
|
||||
@@ -403,7 +408,7 @@ pub fn apply_results(
|
||||
// lose to the same comparison forever — and let the next pull bring
|
||||
// the server's copy down.
|
||||
clear_dirty(&tx, change, None)?;
|
||||
if change.op == "delete" {
|
||||
if change.op == DELETE {
|
||||
// Our delete lost to a newer server edit; the note lives on, and
|
||||
// the pull will restore it locally. Drop the tombstone so we
|
||||
// don't keep trying to delete a note the user has since edited.
|
||||
@@ -448,11 +453,11 @@ pub fn apply_results(
|
||||
|
||||
fn clear_dirty(conn: &Connection, change: &Change, revision: Option<i64>) -> rusqlite::Result<()> {
|
||||
// A delete has no local row left to update.
|
||||
if change.op == "delete" {
|
||||
if change.op == DELETE {
|
||||
return Ok(());
|
||||
}
|
||||
let table = match change.entity {
|
||||
"label" => "labels",
|
||||
entity::LABEL => "labels",
|
||||
_ => "notes",
|
||||
};
|
||||
match revision {
|
||||
@@ -469,7 +474,7 @@ fn clear_dirty(conn: &Connection, change: &Change, revision: Option<i64>) -> rus
|
||||
}
|
||||
|
||||
fn forget_pending_delete(conn: &Connection, change: &Change) -> rusqlite::Result<()> {
|
||||
if change.op != "delete" {
|
||||
if change.op != DELETE {
|
||||
return Ok(());
|
||||
}
|
||||
conn.execute(
|
||||
@@ -481,18 +486,10 @@ fn forget_pending_delete(conn: &Connection, change: &Change) -> rusqlite::Result
|
||||
|
||||
/// True when anything is waiting to go up. Cheap enough to call before a cycle.
|
||||
pub fn has_pending(conn: &Connection) -> rusqlite::Result<bool> {
|
||||
let pending: Option<i64> = conn
|
||||
.query_row(
|
||||
"SELECT 1 FROM notes WHERE dirty = 1
|
||||
UNION ALL SELECT 1 FROM labels WHERE dirty = 1
|
||||
UNION ALL SELECT 1 FROM pending_deletes
|
||||
UNION ALL SELECT 1 FROM attachments WHERE uploaded = 0 AND upload_error IS NULL
|
||||
LIMIT 1",
|
||||
[],
|
||||
|r| r.get(0),
|
||||
)
|
||||
.optional()?;
|
||||
Ok(pending.is_some())
|
||||
// One definition of "pending": the fingerprint's. Counting where a LIMIT 1 would
|
||||
// do costs nothing on a local store, and two lists of the same four predicates
|
||||
// would drift.
|
||||
Ok(pending_fingerprint(conn)?.is_some())
|
||||
}
|
||||
|
||||
/// A value that changes whenever the set of pending changes does, and stays put
|
||||
@@ -705,13 +702,10 @@ pub fn parse_results(raw: &str) -> Result<Vec<PushResult>, String> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::local::schema;
|
||||
use crate::local::store;
|
||||
|
||||
fn db() -> Connection {
|
||||
let conn = Connection::open_in_memory().expect("in-memory db");
|
||||
schema::migrate(&conn).expect("migrate");
|
||||
conn
|
||||
crate::local::memory_conn().expect("in-memory db")
|
||||
}
|
||||
|
||||
fn seed_note(conn: &Connection, id: &str, dirty: i64) {
|
||||
@@ -1065,25 +1059,9 @@ mod tests {
|
||||
use crate::sync::{pull, wire};
|
||||
let conn = db();
|
||||
let server_note = |revision: i64, attachments: Vec<wire::Attachment>| wire::Note {
|
||||
id: "n1".into(),
|
||||
body: "a note".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,
|
||||
previews: vec![],
|
||||
permission: None,
|
||||
shared: false,
|
||||
shared_by: None,
|
||||
..wire::sample_note("n1", revision)
|
||||
};
|
||||
let page = |note: wire::Note, cursor: i64| wire::ChangesPage {
|
||||
notes: vec![note],
|
||||
|
||||
@@ -85,15 +85,10 @@ pub async fn unshare(
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::local::schema;
|
||||
use crate::sync::client::Member;
|
||||
use rusqlite::Connection;
|
||||
use std::sync::Mutex;
|
||||
|
||||
fn db() -> Db {
|
||||
let conn = Connection::open_in_memory().expect("in-memory db");
|
||||
schema::migrate(&conn).expect("migrate");
|
||||
Db(Mutex::new(conn))
|
||||
crate::local::open_in_memory().expect("in-memory db")
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -105,7 +100,7 @@ mod tests {
|
||||
fn the_shared_flag_follows_the_servers_answer_without_dirtying_the_note() {
|
||||
let db = db();
|
||||
{
|
||||
let conn = db.0.lock().unwrap();
|
||||
let conn = db.conn().unwrap();
|
||||
conn.execute(
|
||||
"INSERT INTO notes (id, body, created_at, updated_at, dirty)
|
||||
VALUES ('n1', 'x', '2026-01-01', '2026-01-01', 0)",
|
||||
@@ -125,7 +120,7 @@ mod tests {
|
||||
created_at: None,
|
||||
};
|
||||
let read = |db: &Db| -> (bool, i64) {
|
||||
let conn = db.0.lock().unwrap();
|
||||
let conn = db.conn().unwrap();
|
||||
conn.query_row("SELECT shared, dirty FROM notes WHERE id = 'n1'", [], |r| {
|
||||
Ok((r.get(0)?, r.get(1)?))
|
||||
})
|
||||
|
||||
@@ -308,12 +308,9 @@ pub fn status(conn: &Connection) -> rusqlite::Result<Status> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::local::schema;
|
||||
|
||||
fn db() -> Connection {
|
||||
let conn = Connection::open_in_memory().expect("in-memory db");
|
||||
schema::migrate(&conn).expect("migrate");
|
||||
conn
|
||||
crate::local::memory_conn().expect("in-memory db")
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+30
-1
@@ -8,6 +8,8 @@
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::local::OPAQUE_MIME;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Default)]
|
||||
pub struct ChangesPage {
|
||||
#[serde(default)]
|
||||
@@ -161,5 +163,32 @@ fn default_color() -> String {
|
||||
}
|
||||
|
||||
fn default_mime() -> String {
|
||||
"application/octet-stream".to_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,
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+31
-1
@@ -1,9 +1,12 @@
|
||||
{
|
||||
"_about": [
|
||||
"The cases every implementation of the note grammar is tested against.",
|
||||
"One file, read by four test suites: core (derive.rs), the server (checklist.py, tags.py), the web (markdown.ts, colors.ts) and, for the tint, Android (DerivedTint.kt, which pins the same values by hand).",
|
||||
"One file, read by four test suites: core (derive.rs), the server (checklist.py, tags.py, helpers.py), the web (markdown.ts, colors.ts) and, for the tint, Android (DerivedTint.kt, which pins the same values by hand).",
|
||||
"A difference between any two implementations is a note that changes shape when it syncs. Add a case here, not to one suite.",
|
||||
"Lives under core/ because core is the definition, and because core/ is in the desktop, Android and server file sets, so changing a case reruns every suite that reads it.",
|
||||
"display_titles: a note's name is its first line that says anything once a task marker is stripped, cut to 200 characters (not bytes). Lines split on \\n alone, as the grammar does everywhere.",
|
||||
"urls: the links in a body the server unfurls (found), and the one a note that is nothing but a link files its preview under (lone). Trailing .,;:!? is the sentence's, not the link's, on every surface.",
|
||||
"recurrences: the repeat rules a reminder can carry, as stored. The server, the core and the web's editor each hold the list; Android's picker pins it by hand, as it does the tint.",
|
||||
"Tag cases are plain text on purpose. The web's renderer lets `code` and **bold** claim a `#` first, which the derivers do not; that is a rendering choice, not grammar, and is tested where it lives."
|
||||
],
|
||||
|
||||
@@ -68,6 +71,33 @@
|
||||
{ "body": "(#todo)\nnotes", "standalone": [], "inline": [], "lifted": "(#todo)\nnotes" }
|
||||
],
|
||||
|
||||
"display_titles": [
|
||||
{ "body": "", "title": "" },
|
||||
{ "body": "\n\n Shopping \nmilk", "title": "Shopping" },
|
||||
{ "body": "- [ ] milk\n- [x] eggs", "title": "milk" },
|
||||
{ "body": "- [ ]\n- [ ] bread", "title": "bread" },
|
||||
{ "body": " \n\t\nlate start", "title": "late start" },
|
||||
{ "body": "one\r\ntwo", "title": "one" },
|
||||
{ "body": "a\rb\nc", "title": "a\rb" },
|
||||
{ "body": "a\u2028b\nc", "title": "a\u2028b" },
|
||||
{ "body": "ééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééé", "title": "éééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééééé" }
|
||||
],
|
||||
|
||||
"urls": [
|
||||
{ "body": "https://example.com/a", "found": ["https://example.com/a"], "lone": "https://example.com/a" },
|
||||
{ "body": " https://example.com/a \n", "found": ["https://example.com/a"], "lone": "https://example.com/a" },
|
||||
{ "body": "https://example.com/a.", "found": ["https://example.com/a"], "lone": "https://example.com/a" },
|
||||
{ "body": "https://example.com/a?q=1!", "found": ["https://example.com/a?q=1"], "lone": "https://example.com/a?q=1" },
|
||||
{ "body": "see https://example.com/a, then https://example.com/b.", "found": ["https://example.com/a", "https://example.com/b"], "lone": null },
|
||||
{ "body": "<https://example.com/a>", "found": ["https://example.com/a"], "lone": null },
|
||||
{ "body": "https://example.com/a)", "found": ["https://example.com/a"], "lone": null },
|
||||
{ "body": "https://example.com/a https://example.com/a", "found": ["https://example.com/a"], "lone": null },
|
||||
{ "body": "ftp://example.com/a", "found": [], "lone": null },
|
||||
{ "body": "", "found": [], "lone": null }
|
||||
],
|
||||
|
||||
"recurrences": ["daily", "weekly", "monthly", "yearly"],
|
||||
|
||||
"tint": {
|
||||
"keys": ["red", "orange", "yellow", "green", "teal", "blue", "purple", "pink", "gray"],
|
||||
"hashes": [
|
||||
|
||||
@@ -25,7 +25,7 @@ pub fn config_get(db: State<'_, Db>) -> PublicConfig {
|
||||
// be enforced. A store error falls back to the default rather than failing the
|
||||
// call — the app must still boot.
|
||||
let fallback = retention::LOCAL_RETENTION_DAYS;
|
||||
let retention_days = match db.0.lock() {
|
||||
let retention_days = match db.conn() {
|
||||
Ok(conn) => state::effective_retention_days(&conn, fallback).unwrap_or(fallback),
|
||||
Err(_) => fallback,
|
||||
};
|
||||
|
||||
@@ -269,7 +269,7 @@ fn paint_window_before_the_webview_does(app: &tauri::App) {
|
||||
/// user is present for is one they can see the result of. A failure here is logged and
|
||||
/// stepped over — housekeeping must never be the reason the app won't open.
|
||||
fn sweep_local_trash(db: &local::Db) {
|
||||
let conn = match db.0.lock() {
|
||||
let conn = match db.conn() {
|
||||
Ok(conn) => conn,
|
||||
Err(_) => {
|
||||
log::warn!("skipping the trash sweep: store lock poisoned");
|
||||
|
||||
@@ -553,7 +553,7 @@ mod tests {
|
||||
|
||||
/// The channel `update_check` would actually use.
|
||||
fn effective(db: &Db) -> Channel {
|
||||
let conn = db.0.lock().expect("lock");
|
||||
let conn = db.conn().expect("lock");
|
||||
store::pref(&conn, CHANNEL_PREF)
|
||||
.expect("read pref")
|
||||
.as_deref()
|
||||
@@ -562,7 +562,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn set_channel(db: &Db, channel: Channel) {
|
||||
let conn = db.0.lock().expect("lock");
|
||||
let conn = db.conn().expect("lock");
|
||||
store::set_pref(&conn, CHANNEL_PREF, channel.as_str()).expect("set pref");
|
||||
}
|
||||
|
||||
@@ -702,7 +702,7 @@ mod tests {
|
||||
}
|
||||
|
||||
fn set_server(db: &Db, value: &str) {
|
||||
let conn = db.0.lock().expect("lock");
|
||||
let conn = db.conn().expect("lock");
|
||||
store::set_pref(&conn, SERVER_PREF, value).expect("set pref");
|
||||
}
|
||||
|
||||
@@ -780,7 +780,7 @@ mod tests {
|
||||
let db = db();
|
||||
assert_eq!(token_for(&db, "https://notes.example.com"), Ok(None));
|
||||
{
|
||||
let conn = db.0.lock().expect("lock");
|
||||
let conn = db.conn().expect("lock");
|
||||
state::set_link(&conn, "https://notes.example.com/", "tok").expect("link");
|
||||
}
|
||||
assert_eq!(
|
||||
|
||||
@@ -3,6 +3,7 @@ import { computed, ref, watch } from "vue";
|
||||
import { useNotesStore } from "../stores/notes";
|
||||
import { NOTE_CARD_SURFACE, labelChipClasses } from "../notes/colors";
|
||||
import { rendersInline } from "../notes/attachments";
|
||||
import { loneUrl } from "../notes/links";
|
||||
import type { Note } from "../stores/notes";
|
||||
import Icon from "./Icon.vue";
|
||||
import LinkPreview from "./LinkPreview.vue";
|
||||
@@ -84,18 +85,14 @@ const PREVIEW_LINES = 8;
|
||||
// A note that is NOTHING but a URL is a link, and its preview is the whole card —
|
||||
// showing the raw URL underneath a card that already says where it goes is saying the
|
||||
// same thing twice, badly. A URL mentioned *inside* a note is a footnote to it, and
|
||||
// gets a compact strip at the bottom instead.
|
||||
//
|
||||
// Whitespace either side still counts as lone: someone pasting a link rarely trims it.
|
||||
const LONE_URL_RE = /^\s*(https?:\/\/[^\s<>"'\])]+)\s*$/;
|
||||
|
||||
const isLoneUrl = computed(() => LONE_URL_RE.test(props.note.body) && !props.note.items.length);
|
||||
// gets a compact strip at the bottom instead. What counts as lone is notes/links.ts.
|
||||
const isLoneUrl = computed(() => loneUrl(props.note.body) !== null && !props.note.items.length);
|
||||
|
||||
/** The preview for a lone-URL note — null while it is still being fetched, or if it
|
||||
* could never be fetched at all. */
|
||||
const loneUrlPreview = computed(() => {
|
||||
if (!isLoneUrl.value) return null;
|
||||
const url = props.note.body.trim();
|
||||
const url = loneUrl(props.note.body);
|
||||
return props.note.previews.find((p) => p.url === url) ?? null;
|
||||
});
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import ReminderActions from "./ReminderActions.vue";
|
||||
import LabelPicker from "./LabelPicker.vue";
|
||||
import LinkPreview from "./LinkPreview.vue";
|
||||
import { formatShortDateTime, fromLocalInput, toLocalInput } from "../notes/datetime";
|
||||
import { RECURRENCES } from "../notes/recurrence";
|
||||
import { takeMorphOrigin } from "../composables/useEditorMorph";
|
||||
import { prefersReducedMotion } from "../composables/useReducedMotion";
|
||||
import type { Note, NoteLabel, NoteRevision } from "../stores/notes";
|
||||
@@ -749,10 +750,7 @@ function revPreview(rev: NoteRevision): string {
|
||||
@change="onRecurrenceChange"
|
||||
>
|
||||
<option value="">Does not repeat</option>
|
||||
<option value="daily">Daily</option>
|
||||
<option value="weekly">Weekly</option>
|
||||
<option value="monthly">Monthly</option>
|
||||
<option value="yearly">Yearly</option>
|
||||
<option v-for="r in RECURRENCES" :key="r.value" :value="r.value">{{ r.label }}</option>
|
||||
</select>
|
||||
<ReminderActions :note-id="liveNote.id" />
|
||||
</div>
|
||||
|
||||
@@ -8,6 +8,8 @@ import { describe, expect, it } from "vitest";
|
||||
import fixture from "../../../core/testdata/grammar.json";
|
||||
import { DERIVED_TINT_KEYS, derivedTint, resolveLabelColor, tintHash } from "./colors";
|
||||
import { parseInline, parseTaskLine, renderTaskLine } from "./markdown";
|
||||
import { loneUrl } from "./links";
|
||||
import { RECURRENCES } from "./recurrence";
|
||||
|
||||
describe("checklist lines", () => {
|
||||
it.each(fixture.task_lines)("$line", ({ line, item }) => {
|
||||
@@ -52,3 +54,15 @@ describe("derived tag colour", () => {
|
||||
expect(resolveLabelColor({ name })).toBe(color);
|
||||
});
|
||||
});
|
||||
|
||||
describe("recurrences", () => {
|
||||
it("offers the stored rules every surface holds", () => {
|
||||
expect(RECURRENCES.map((r) => r.value)).toEqual(fixture.recurrences);
|
||||
});
|
||||
});
|
||||
|
||||
describe("links", () => {
|
||||
it.each(fixture.urls)("lone link in $body", ({ body, lone }) => {
|
||||
expect(loneUrl(body)).toBe(lone);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// A note that is nothing but a link renders as a link card.
|
||||
//
|
||||
// The characters a link runs over, and the punctuation trimmed off its end, are the
|
||||
// server's (unfurl_queue.detect_urls): the server files the preview under the trimmed
|
||||
// link, so the card has to look it up under the same one (#5399). Android's
|
||||
// LinkPreviewRow holds the same rule, and all three run the `urls` cases in
|
||||
// core/testdata/grammar.json.
|
||||
//
|
||||
// Whitespace either side still counts as lone: someone pasting a link rarely trims it.
|
||||
const LONE_URL_RE = /^\s*(https?:\/\/[^\s<>"'\])]+)\s*$/;
|
||||
|
||||
/** The link a lone-link note's preview is filed under, or null for any other body. */
|
||||
export function loneUrl(body: string): string | null {
|
||||
const match = LONE_URL_RE.exec(body);
|
||||
if (!match) return null;
|
||||
return match[1].replace(/[.,;:!?]+$/, "") || null;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// The repeat rules a reminder can carry, as stored, and what the editor calls each.
|
||||
// The server (REMINDER_RECURRENCES) and the core (recur::RECURRENCES) hold the same
|
||||
// list, and all three run the `recurrences` case in core/testdata/grammar.json.
|
||||
export const RECURRENCES: ReadonlyArray<{ value: string; label: string }> = [
|
||||
{ value: "daily", label: "Daily" },
|
||||
{ value: "weekly", label: "Weekly" },
|
||||
{ value: "monthly", label: "Monthly" },
|
||||
{ value: "yearly", label: "Yearly" },
|
||||
];
|
||||
@@ -46,6 +46,9 @@ export interface PublicConfig {
|
||||
// Whether this server can email a reset link, so sign-in offers "Forgot password?"
|
||||
// (#5266). Absent on an older server and on the offline desktop: no.
|
||||
password_reset_by_email?: boolean;
|
||||
// The shortest password an account may have (the server's MIN_PASSWORD_LEN).
|
||||
// Absent on an older server and on the offline desktop, which has no accounts.
|
||||
min_password_length?: number;
|
||||
// Every client this server holds, keyed by platform id. Absent on a server that
|
||||
// holds none, and absent on the desktop's own offline config — the Tauri build
|
||||
// answers `config_get` locally and has no clients to hand out.
|
||||
@@ -66,6 +69,8 @@ export const useConfigStore = defineStore("config", () => {
|
||||
// that never had the field, both correctly offer no downloads.
|
||||
const clients = ref<Record<string, ClientRelease>>({});
|
||||
const passwordResetByEmail = ref(false);
|
||||
// The server's own number; 8 only until /api/config answers, or if it never does.
|
||||
const minPasswordLength = ref(8);
|
||||
const loaded = ref(false);
|
||||
|
||||
async function load(): Promise<void> {
|
||||
@@ -79,6 +84,7 @@ export const useConfigStore = defineStore("config", () => {
|
||||
trashRetentionDays.value = cfg.trash_retention_days ?? 30;
|
||||
clients.value = cfg.clients ?? {};
|
||||
passwordResetByEmail.value = cfg.password_reset_by_email ?? false;
|
||||
minPasswordLength.value = cfg.min_password_length ?? 8;
|
||||
} catch {
|
||||
// Keep defaults if the config endpoint is unreachable.
|
||||
} finally {
|
||||
@@ -99,6 +105,7 @@ export const useConfigStore = defineStore("config", () => {
|
||||
trashRetentionDays,
|
||||
clients,
|
||||
passwordResetByEmail,
|
||||
minPasswordLength,
|
||||
loaded,
|
||||
load,
|
||||
reload,
|
||||
|
||||
@@ -264,7 +264,7 @@ onMounted(() => {
|
||||
type="password"
|
||||
label="New password"
|
||||
autocomplete="new-password"
|
||||
placeholder="At least 8 characters"
|
||||
:placeholder="`At least ${config.minPasswordLength} characters`"
|
||||
:error="passwordError"
|
||||
required
|
||||
/>
|
||||
|
||||
@@ -24,8 +24,8 @@ const loading = ref(false);
|
||||
|
||||
async function submit() {
|
||||
error.value = "";
|
||||
if (password.value.length < 8) {
|
||||
error.value = "Password must be at least 8 characters.";
|
||||
if (password.value.length < config.minPasswordLength) {
|
||||
error.value = `Password must be at least ${config.minPasswordLength} characters.`;
|
||||
return;
|
||||
}
|
||||
loading.value = true;
|
||||
@@ -80,7 +80,7 @@ async function submit() {
|
||||
label="Password"
|
||||
type="password"
|
||||
autocomplete="new-password"
|
||||
placeholder="At least 8 characters"
|
||||
:placeholder="`At least ${config.minPasswordLength} characters`"
|
||||
required
|
||||
/>
|
||||
<p
|
||||
|
||||
@@ -25,8 +25,8 @@ const loading = ref(false);
|
||||
|
||||
async function submit() {
|
||||
error.value = "";
|
||||
if (password.value.length < 8) {
|
||||
error.value = "Password must be at least 8 characters.";
|
||||
if (password.value.length < config.minPasswordLength) {
|
||||
error.value = `Password must be at least ${config.minPasswordLength} characters.`;
|
||||
return;
|
||||
}
|
||||
if (password.value !== confirm.value) {
|
||||
@@ -77,7 +77,7 @@ async function submit() {
|
||||
label="New password"
|
||||
type="password"
|
||||
autocomplete="new-password"
|
||||
placeholder="At least 8 characters"
|
||||
:placeholder="`At least ${config.minPasswordLength} characters`"
|
||||
required
|
||||
/>
|
||||
<BaseInput
|
||||
|
||||
@@ -6,7 +6,6 @@ What a reset link is, and how it is used, is in `password_resets.py`.
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import uuid
|
||||
|
||||
from quart import Blueprint, g, jsonify, request
|
||||
from sqlalchemy import select
|
||||
@@ -18,6 +17,7 @@ from .db import session_scope
|
||||
from .models.user import User
|
||||
from .password_resets import issue
|
||||
from .proxy import client_address
|
||||
from .responses import not_found, parse_uuid
|
||||
|
||||
bp = Blueprint("accounts", __name__, url_prefix="/api/accounts")
|
||||
|
||||
@@ -46,14 +46,13 @@ async def list_accounts():
|
||||
@bp.post("/<account_id>/reset-link")
|
||||
@require_admin
|
||||
async def create_reset_link(account_id: str):
|
||||
try:
|
||||
uid = uuid.UUID(account_id)
|
||||
except ValueError:
|
||||
return jsonify({"error": "not found"}), 404
|
||||
uid = parse_uuid(account_id)
|
||||
if uid is None:
|
||||
return not_found()
|
||||
async with session_scope() as db:
|
||||
user = await db.get(User, uid)
|
||||
if user is None:
|
||||
return jsonify({"error": "not found"}), 404
|
||||
return not_found()
|
||||
token, expires_at = await issue(db, uid, g.user_id)
|
||||
await db.commit()
|
||||
email = user.email
|
||||
|
||||
+3
-2
@@ -23,6 +23,7 @@ from .invites_api import bp as invites_bp
|
||||
from .labels import bp as labels_bp
|
||||
from .notes import bp as notes_bp
|
||||
from .proxy import is_https
|
||||
from .responses import json_error, not_found
|
||||
from .retention import run_sweeper
|
||||
from .settings import MAX_BODY_MB, get_public_config, get_setting, load_or_create_secret_key, refresh_live
|
||||
from .settings_api import bp as settings_bp
|
||||
@@ -225,13 +226,13 @@ def create_app() -> Quart:
|
||||
@app.get("/<path:path>")
|
||||
async def spa(path: str):
|
||||
if path.startswith("api/"):
|
||||
return jsonify({"error": "not found"}), 404
|
||||
return not_found()
|
||||
candidate = os.path.join(STATIC_DIR, path)
|
||||
if path and os.path.isfile(candidate):
|
||||
return await send_from_directory(STATIC_DIR, path)
|
||||
index = os.path.join(STATIC_DIR, "index.html")
|
||||
if os.path.isfile(index):
|
||||
return await send_from_directory(STATIC_DIR, "index.html")
|
||||
return jsonify({"error": "frontend not built"}), 404
|
||||
return json_error("frontend not built", 404)
|
||||
|
||||
return app
|
||||
|
||||
+31
-19
@@ -10,7 +10,7 @@ from quart import Blueprint, current_app, g, jsonify, request, session
|
||||
from sqlalchemy import delete, func, select
|
||||
|
||||
from . import audit
|
||||
from .common import iso
|
||||
from .common import iso, normalize_email
|
||||
from .db import session_scope
|
||||
from .invites import INVALID as INVALID_INVITE, record_redeemer, redeem
|
||||
from .mailer import send_later
|
||||
@@ -26,7 +26,7 @@ from .ratelimit import (
|
||||
sign_in_by_address,
|
||||
)
|
||||
from .security import dummy_verify, generate_token, hash_password, hash_token, verify_password
|
||||
from .settings import get_setting, mail_configured, set_settings
|
||||
from .settings import MIN_PASSWORD_LEN, get_setting, mail_configured, set_settings
|
||||
from .storage import storage_limit, storage_used
|
||||
|
||||
bp = Blueprint("auth", __name__, url_prefix="/api/auth")
|
||||
@@ -46,7 +46,6 @@ SESSION_KEY = "user_id"
|
||||
# signed cookies in other people's browsers. A cookie from before epochs existed has
|
||||
# no key and reads as 0, the epoch every account started at.
|
||||
EPOCH_KEY = "epoch"
|
||||
MIN_PASSWORD_LEN = 8
|
||||
|
||||
# The first account can only be created this long after the server starts (family
|
||||
# idea #5105, practice 8). Without it, a fresh server on a public address belongs to
|
||||
@@ -79,6 +78,19 @@ def _sign_in(user: User) -> None:
|
||||
session.permanent = True
|
||||
|
||||
|
||||
def _unauthenticated():
|
||||
"""401 for a caller who isn't signed in, or no longer is."""
|
||||
return json_error("authentication required", 401)
|
||||
|
||||
|
||||
def _password_refusal(password: str):
|
||||
"""The 400 for a password too short to keep, or None. Sign-up, reset and change
|
||||
all ask this one question."""
|
||||
if len(password) < MIN_PASSWORD_LEN:
|
||||
return json_error(f"password must be at least {MIN_PASSWORD_LEN} characters", 400)
|
||||
return None
|
||||
|
||||
|
||||
def _sign_out() -> None:
|
||||
session.pop(SESSION_KEY, None)
|
||||
session.pop(EPOCH_KEY, None)
|
||||
@@ -147,7 +159,7 @@ def login_required(fn):
|
||||
if uid is None:
|
||||
uid = await _user_id_from_bearer()
|
||||
if uid is None:
|
||||
return json_error("authentication required", 401)
|
||||
return _unauthenticated()
|
||||
g.user_id = uid
|
||||
return await fn(*args, **kwargs)
|
||||
|
||||
@@ -162,11 +174,11 @@ def require_admin(fn):
|
||||
async def wrapper(*args, **kwargs):
|
||||
uid = _session_user_id()
|
||||
if uid is None:
|
||||
return json_error("authentication required", 401)
|
||||
return _unauthenticated()
|
||||
async with session_scope() as db:
|
||||
user = await db.get(User, uid)
|
||||
if not _session_is_current(user.session_epoch if user else None):
|
||||
return json_error("authentication required", 401)
|
||||
return _unauthenticated()
|
||||
if not user.is_admin:
|
||||
return json_error("admin access required", 403)
|
||||
g.user_id = uid
|
||||
@@ -221,15 +233,15 @@ def _sign_in_succeeded(email: str) -> None:
|
||||
@bp.post("/register")
|
||||
async def register():
|
||||
data = await request.get_json(silent=True) or {}
|
||||
email = (data.get("email") or "").strip().lower()
|
||||
email = normalize_email(data.get("email"))
|
||||
password = data.get("password") or ""
|
||||
display_name = (data.get("display_name") or "").strip()
|
||||
invite = (data.get("invite") or "").strip()
|
||||
|
||||
if not email or "@" not in email:
|
||||
return json_error("a valid email is required", 400)
|
||||
if len(password) < MIN_PASSWORD_LEN:
|
||||
return json_error(f"password must be at least {MIN_PASSWORD_LEN} characters", 400)
|
||||
if (refusal := _password_refusal(password)) is not None:
|
||||
return refusal
|
||||
if not display_name:
|
||||
display_name = email.split("@", 1)[0]
|
||||
|
||||
@@ -336,7 +348,7 @@ def _bad_credentials():
|
||||
@bp.post("/login")
|
||||
async def login():
|
||||
data = await request.get_json(silent=True) or {}
|
||||
email = (data.get("email") or "").strip().lower()
|
||||
email = normalize_email(data.get("email"))
|
||||
password = data.get("password") or ""
|
||||
|
||||
wait = _sign_in_block(email)
|
||||
@@ -366,7 +378,7 @@ async def me():
|
||||
user = await db.get(User, g.user_id)
|
||||
if user is None:
|
||||
_sign_out()
|
||||
return json_error("authentication required", 401)
|
||||
return _unauthenticated()
|
||||
return jsonify(_serialize_user(user))
|
||||
|
||||
|
||||
@@ -398,7 +410,7 @@ async def forgot_password():
|
||||
the answer is unchanged and nothing is sent, so the cap is no oracle either.
|
||||
"""
|
||||
data = await request.get_json(silent=True) or {}
|
||||
email = (data.get("email") or "").strip().lower()
|
||||
email = normalize_email(data.get("email"))
|
||||
if not email or "@" not in email:
|
||||
return json_error("a valid email is required", 400)
|
||||
|
||||
@@ -433,8 +445,8 @@ async def reset_password():
|
||||
data = await request.get_json(silent=True) or {}
|
||||
token = (data.get("token") or "").strip()
|
||||
password = data.get("password") or ""
|
||||
if len(password) < MIN_PASSWORD_LEN:
|
||||
return json_error(f"password must be at least {MIN_PASSWORD_LEN} characters", 400)
|
||||
if (refusal := _password_refusal(password)) is not None:
|
||||
return refusal
|
||||
|
||||
# The sign-in budget for this address. A token can't be guessed, but each attempt
|
||||
# is still a credential attempt, and the log should show a run of them.
|
||||
@@ -503,13 +515,13 @@ async def change_password():
|
||||
data = await request.get_json(silent=True) or {}
|
||||
current = data.get("current_password") or ""
|
||||
password = data.get("new_password") or ""
|
||||
if len(password) < MIN_PASSWORD_LEN:
|
||||
return json_error(f"password must be at least {MIN_PASSWORD_LEN} characters", 400)
|
||||
if (refusal := _password_refusal(password)) is not None:
|
||||
return refusal
|
||||
|
||||
async with session_scope() as db:
|
||||
user = await db.get(User, g.user_id)
|
||||
if user is None:
|
||||
return json_error("authentication required", 401)
|
||||
return _unauthenticated()
|
||||
wait = _sign_in_block(user.email)
|
||||
if wait is not None:
|
||||
return _throttled(wait)
|
||||
@@ -537,7 +549,7 @@ async def sign_out_elsewhere():
|
||||
async with session_scope() as db:
|
||||
user = await db.get(User, g.user_id)
|
||||
if user is None:
|
||||
return json_error("authentication required", 401)
|
||||
return _unauthenticated()
|
||||
unlinked = await _sign_out_elsewhere(db, user, keep_token=_bearer_token())
|
||||
_stay_signed_in(user)
|
||||
logger.info("signed out elsewhere email=%s devices_unlinked=%s from=%s", user.email, unlinked, client_address())
|
||||
@@ -583,7 +595,7 @@ async def device_login():
|
||||
"""Native first-link: exchange email+password for a device bearer token. Public
|
||||
(no existing session) — this is how a fresh native install authenticates."""
|
||||
data = await request.get_json(silent=True) or {}
|
||||
email = (data.get("email") or "").strip().lower()
|
||||
email = normalize_email(data.get("email"))
|
||||
password = data.get("password") or ""
|
||||
if not email or not password:
|
||||
return json_error("email and password are required", 400)
|
||||
|
||||
@@ -40,3 +40,10 @@ def coerce_bool(raw: object) -> bool:
|
||||
if isinstance(raw, str):
|
||||
return raw.strip().lower() in ("true", "1", "yes", "on")
|
||||
return bool(raw)
|
||||
|
||||
|
||||
def normalize_email(raw: str | None) -> str:
|
||||
"""An address as accounts hold it: trimmed and lowercased. Every route that reads
|
||||
one from a request goes through here, so "Ana@Example.com " and "ana@example.com"
|
||||
are one account at sign-up, sign-in, reset and invite alike."""
|
||||
return (raw or "").strip().lower()
|
||||
|
||||
@@ -5,7 +5,6 @@ What an invite is, and how it is redeemed, is in `invites.py`.
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import uuid
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from quart import Blueprint, g, jsonify, request
|
||||
@@ -14,11 +13,13 @@ from sqlalchemy.orm import aliased
|
||||
|
||||
from . import audit
|
||||
from .auth import require_admin
|
||||
from .common import normalize_email
|
||||
from .db import session_scope
|
||||
from .invites import MAX_DAYS, lifetime_days, serialize
|
||||
from .models.invite import Invite
|
||||
from .models.user import User
|
||||
from .proxy import client_address
|
||||
from .responses import json_error, not_found, parse_uuid
|
||||
from .security import generate_token, hash_token
|
||||
|
||||
bp = Blueprint("invites", __name__, url_prefix="/api/invites")
|
||||
@@ -31,12 +32,12 @@ logger = logging.getLogger(__name__)
|
||||
@require_admin
|
||||
async def create_invite():
|
||||
data = await request.get_json(silent=True) or {}
|
||||
email = (data.get("email") or "").strip().lower() or None
|
||||
email = normalize_email(data.get("email")) or None
|
||||
if email is not None and "@" not in email:
|
||||
return jsonify({"error": "that doesn't look like an email address"}), 400
|
||||
return json_error("that doesn't look like an email address", 400)
|
||||
days = lifetime_days(data.get("days"))
|
||||
if days is None:
|
||||
return jsonify({"error": f"an invite lasts between 1 and {MAX_DAYS} days"}), 400
|
||||
return json_error(f"an invite lasts between 1 and {MAX_DAYS} days", 400)
|
||||
|
||||
token = generate_token()
|
||||
now = datetime.now(timezone.utc)
|
||||
@@ -82,15 +83,14 @@ async def list_invites():
|
||||
@bp.delete("/<invite_id>")
|
||||
@require_admin
|
||||
async def revoke_invite(invite_id: str):
|
||||
try:
|
||||
iid = uuid.UUID(invite_id)
|
||||
except ValueError:
|
||||
return jsonify({"error": "not found"}), 404
|
||||
iid = parse_uuid(invite_id)
|
||||
if iid is None:
|
||||
return not_found()
|
||||
now = datetime.now(timezone.utc)
|
||||
async with session_scope() as db:
|
||||
invite = await db.get(Invite, iid)
|
||||
if invite is None:
|
||||
return jsonify({"error": "not found"}), 404
|
||||
return not_found()
|
||||
# A used invite has done its work and its record stays as it is; an already
|
||||
# revoked one keeps its first revocation time.
|
||||
if invite.redeemed_at is None and invite.revoked_at is None:
|
||||
|
||||
@@ -35,7 +35,7 @@ from ..note_state import OWN_FIELDS, archived_for, join_state, pinned_for, posit
|
||||
from .checklist import append_item, parse_items, set_item_checked
|
||||
from ..responses import json_error, not_found, parse_uuid
|
||||
from ..retention import purge_note
|
||||
from ..storage import OVER_LIMIT_STATUS, storage_room, upload_refusal
|
||||
from ..storage import OVER_LIMIT_STATUS, storage_room, unlink_media, upload_refusal
|
||||
from ..unfurl_queue import schedule as schedule_unfurls
|
||||
from ._bp import bp
|
||||
from .body import write_body
|
||||
@@ -54,7 +54,6 @@ from .helpers import (
|
||||
is_empty_note,
|
||||
store_attachment,
|
||||
top_position,
|
||||
unlink_media,
|
||||
parse_list_items,
|
||||
)
|
||||
from .import_export import (
|
||||
@@ -256,7 +255,7 @@ async def export_notes():
|
||||
payload: dict = {
|
||||
"app": "inkwell",
|
||||
"version": 1,
|
||||
"exported_at": datetime.now(timezone.utc).isoformat(),
|
||||
"exported_at": iso(datetime.now(timezone.utc)),
|
||||
"labels": [{"name": lb.name, "color": lb.color} for lb in all_labels],
|
||||
"notes": [],
|
||||
}
|
||||
@@ -273,10 +272,10 @@ async def export_notes():
|
||||
"body": n.body,
|
||||
"pinned": n.pinned,
|
||||
"archived": n.archived,
|
||||
"remind_at": n.remind_at.isoformat() if n.remind_at else None,
|
||||
"remind_at": iso(n.remind_at),
|
||||
"recurrence": n.recurrence,
|
||||
"created_at": n.created_at.isoformat() if n.created_at else None,
|
||||
"updated_at": n.updated_at.isoformat() if n.updated_at else None,
|
||||
"created_at": iso(n.created_at),
|
||||
"updated_at": iso(n.updated_at),
|
||||
"labels": [lb["name"] for lb in labels],
|
||||
"attachments": [
|
||||
{"file": f"attachments/{short}/{os.path.basename(a.path)}", "mime": a.mime} for a in atts
|
||||
|
||||
@@ -43,10 +43,13 @@ def derive_display_title(body: str | None) -> str:
|
||||
would show someone the storage instead of the note — and skipping an EMPTY item, so
|
||||
a half-typed list does not leave a note with no name.
|
||||
|
||||
Mirrors `display_title` in core/src/local/store.rs. Deterministic — a literal first
|
||||
line, never generated.
|
||||
Lines split on "\n" alone, as every other reading of the grammar does;
|
||||
`splitlines` also broke on a lone "\r" or a U+2028, so a phone and the server named
|
||||
the same note differently. Mirrors `derive::display_title` in the core, and both
|
||||
run the `display_titles` cases in core/testdata/grammar.json. Deterministic — a
|
||||
literal first line, never generated.
|
||||
"""
|
||||
for line in (body or "").splitlines():
|
||||
for line in (body or "").split("\n"):
|
||||
stripped = strip_marker(line.strip()).strip()
|
||||
if stripped:
|
||||
return stripped[:DISPLAY_TITLE_CAP]
|
||||
@@ -208,16 +211,6 @@ def store_attachment(
|
||||
return row
|
||||
|
||||
|
||||
def unlink_media(rel: str) -> None:
|
||||
"""Remove an attachment's file, after its row is gone. A file that is already
|
||||
missing or can't be removed is logged, never raised: the row was what the person
|
||||
asked to delete, and failing here would undo a deletion that has committed."""
|
||||
try:
|
||||
(Config.media_root() / rel).unlink(missing_ok=True)
|
||||
except OSError:
|
||||
logger.warning("couldn't remove attachment file %s", rel, exc_info=True)
|
||||
|
||||
|
||||
def _header_filename(name: str) -> str:
|
||||
"""Sanitize a filename for a Content-Disposition header (drop quotes/newlines)."""
|
||||
return re.sub(r'[\r\n"]', "", name or "")[:255] or "file"
|
||||
|
||||
@@ -11,7 +11,7 @@ from datetime import datetime, timezone
|
||||
|
||||
from sqlalchemy import select
|
||||
|
||||
from ..common import parse_dt
|
||||
from ..common import iso, parse_dt
|
||||
from ..models.label import NoteLabel
|
||||
from ..models.note import Note
|
||||
from .helpers import (
|
||||
@@ -37,9 +37,9 @@ def _note_markdown(note: Note, labels: list) -> str:
|
||||
if note.archived:
|
||||
fm.append("archived: true")
|
||||
if note.remind_at:
|
||||
fm.append(f"remind_at: {note.remind_at.isoformat()}")
|
||||
fm.append(f"created: {note.created_at.isoformat() if note.created_at else ''}")
|
||||
fm.append(f"updated: {note.updated_at.isoformat() if note.updated_at else ''}")
|
||||
fm.append(f"remind_at: {iso(note.remind_at)}")
|
||||
fm.append(f"created: {iso(note.created_at) or ''}")
|
||||
fm.append(f"updated: {iso(note.updated_at) or ''}")
|
||||
fm.append("---")
|
||||
fm.append("")
|
||||
# Body and checklist are no longer alternatives — a note can carry both, so both
|
||||
|
||||
@@ -38,6 +38,7 @@ from .models.note_revision import NoteRevision
|
||||
from .models.share import Share
|
||||
from .settings import get_setting
|
||||
from .share_sync import recipients, revoke
|
||||
from .storage import unlink_media
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -79,13 +80,7 @@ async def purge_note(db, note: Note, edited_at: datetime | None = None) -> None:
|
||||
"""
|
||||
atts = (await db.scalars(select(NoteAttachment).where(NoteAttachment.note_id == note.id))).all()
|
||||
for a in atts:
|
||||
try:
|
||||
(Config.media_root() / a.path).unlink(missing_ok=True)
|
||||
except OSError:
|
||||
# A missing or unreadable file must not strand the row: the DB record is
|
||||
# what the user asked us to destroy, and a failed unlink leaving it in
|
||||
# place would make the note reappear whole on the next sweep.
|
||||
logger.warning("couldn't remove attachment file %s during purge", a.path, exc_info=True)
|
||||
unlink_media(a.path)
|
||||
await db.execute(sa_delete(NoteAttachment).where(NoteAttachment.note_id == note.id))
|
||||
await db.execute(sa_delete(NoteLabel).where(NoteLabel.note_id == note.id))
|
||||
await db.execute(sa_delete(NoteLinkPreview).where(NoteLinkPreview.note_id == note.id))
|
||||
|
||||
@@ -16,6 +16,10 @@ SettingType = Literal["string", "bool", "int"]
|
||||
# attachment an admin may allow is a megabyte under it.
|
||||
MAX_BODY_MB = 64
|
||||
|
||||
# The shortest password an account may have. Enforced by auth.py, and served in the
|
||||
# public config so the web's forms say the same number before a round-trip does.
|
||||
MIN_PASSWORD_LEN = 8
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class SettingDef:
|
||||
@@ -320,6 +324,7 @@ async def get_public_config(db) -> dict:
|
||||
"trash_retention_days": await get_setting(db, "trash_retention_days"),
|
||||
# Whether the sign-in screen offers "Forgot password?" (#5266).
|
||||
"password_reset_by_email": await mail_configured(db),
|
||||
"min_password_length": MIN_PASSWORD_LEN,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -64,9 +64,7 @@ async def test_email():
|
||||
async with session_scope() as db:
|
||||
cfg = await mail_settings(db)
|
||||
if cfg is None:
|
||||
return jsonify(
|
||||
{"error": "Email is off: it needs an SMTP server, a from address and the public address."}
|
||||
), 400
|
||||
return json_error("Email is off: it needs an SMTP server, a from address and the public address.", 400)
|
||||
admin = await db.get(User, g.user_id)
|
||||
site = await get_setting(db, "site_name")
|
||||
try:
|
||||
|
||||
@@ -12,18 +12,24 @@ An upload that would go over the limit is answered 507 Insufficient Storage, not
|
||||
(`client::upload_attachment` in the core), and a 5xx as worth another try. Over the
|
||||
limit is neither: freeing space should let the file through on the next sync, with
|
||||
nothing for the person to redo.
|
||||
|
||||
The one way an attachment's file leaves the disk is here too (`unlink_media`).
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import uuid
|
||||
|
||||
from sqlalchemy import func, select
|
||||
|
||||
from .config import Config
|
||||
from .models.note import Note
|
||||
from .models.note_attachment import NoteAttachment
|
||||
from .models.user import User
|
||||
from .settings import get_setting
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
GB = 1024**3
|
||||
OVER_LIMIT_STATUS = 507
|
||||
|
||||
@@ -76,3 +82,14 @@ async def upload_refusal(db, user_id: uuid.UUID, size: int) -> tuple[str, int] |
|
||||
if limit is not None and size > limit - await storage_used(db, user_id):
|
||||
return over_limit_message(limit), OVER_LIMIT_STATUS
|
||||
return None
|
||||
|
||||
|
||||
def unlink_media(rel: str) -> None:
|
||||
"""Remove an attachment's file, after its row is gone. A file that is already
|
||||
missing or can't be removed is logged, never raised: the row was what the person
|
||||
asked to delete, and failing here would undo a deletion that has committed, or
|
||||
strand the row so the note reappears whole on the next sweep."""
|
||||
try:
|
||||
(Config.media_root() / rel).unlink(missing_ok=True)
|
||||
except OSError:
|
||||
logger.warning("couldn't remove attachment file %s", rel, exc_info=True)
|
||||
|
||||
+2
-2
@@ -38,11 +38,11 @@ from .notes import (
|
||||
normalize_recurrence,
|
||||
)
|
||||
from .notes.body import write_body
|
||||
from .notes.helpers import _get_editable, _get_visible, store_attachment, unlink_media
|
||||
from .notes.helpers import _get_editable, _get_visible, store_attachment
|
||||
from .responses import json_error, not_found, parse_uuid
|
||||
from .retention import purge_note
|
||||
from .serialize import serialize_label_sync
|
||||
from .storage import upload_refusal
|
||||
from .storage import unlink_media, upload_refusal
|
||||
from .unfurl_queue import schedule as schedule_unfurls
|
||||
|
||||
bp = Blueprint("sync", __name__, url_prefix="/api/sync")
|
||||
|
||||
@@ -12,8 +12,12 @@ from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from inkwell.colors import NOTE_COLORS
|
||||
from inkwell.notes.checklist import parse_items, render_item
|
||||
from inkwell.notes.helpers import derive_display_title
|
||||
from inkwell.notes.recurrence import REMINDER_RECURRENCES
|
||||
from inkwell.notes.tags import parse_tags, split_body_tags
|
||||
from inkwell.unfurl_queue import detect_urls
|
||||
|
||||
FIXTURE = json.loads(
|
||||
(Path(__file__).resolve().parents[1] / "core" / "testdata" / "grammar.json").read_text(encoding="utf-8")
|
||||
@@ -41,3 +45,23 @@ def test_tags(case):
|
||||
def test_lifts(case):
|
||||
standalone, inline, lifted = split_body_tags(case["body"])
|
||||
assert (standalone, inline, lifted) == (case["standalone"], case["inline"], case["lifted"])
|
||||
|
||||
|
||||
@pytest.mark.parametrize("case", FIXTURE["display_titles"], ids=lambda c: repr(c["body"][:40]))
|
||||
def test_display_titles(case):
|
||||
assert derive_display_title(case["body"]) == case["title"]
|
||||
|
||||
|
||||
def test_recurrences():
|
||||
assert REMINDER_RECURRENCES == set(FIXTURE["recurrences"])
|
||||
|
||||
|
||||
def test_palette_keys():
|
||||
"""The palette the server accepts is the fixture's hues plus `default`, the key
|
||||
unrecognised input lands on. The web checks the same hues (DERIVED_TINT_KEYS)."""
|
||||
assert NOTE_COLORS == {"default", *FIXTURE["tint"]["keys"]}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("case", FIXTURE["urls"], ids=lambda c: repr(c["body"]))
|
||||
def test_urls(case):
|
||||
assert detect_urls(case["body"]) == case["found"]
|
||||
|
||||
Reference in New Issue
Block a user