mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 15:26:17 +00:00
feat(core): tags and types are names, one type per page implying a tag
Tags no longer have pages. A page has at most one type, which implies a tag of the same name. Tags are stored sorted. Adds rename_tag and remove_tag. Schema v6 turns tag pages into notes and trims types to the first one. History exports synapse-type:: and reads the old forms. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
39e7e43071
commit
e2ed9054c6
11 files changed
+373
-328
No files matched your search
@@ -17,12 +17,12 @@ pub fn new_id() -> Uuid {
|
||||
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum PageKind {
|
||||
/// Also what the retired `tag` kind of schema v5 reads as (tags no longer have pages).
|
||||
#[default]
|
||||
#[serde(alias = "tag")]
|
||||
Note,
|
||||
Hub,
|
||||
Template,
|
||||
/// A topic page: notes point at it from their `tags`.
|
||||
Tag,
|
||||
}
|
||||
|
||||
impl PageKind {
|
||||
@@ -31,7 +31,6 @@ impl PageKind {
|
||||
PageKind::Note => "note",
|
||||
PageKind::Hub => "hub",
|
||||
PageKind::Template => "template",
|
||||
PageKind::Tag => "tag",
|
||||
}
|
||||
}
|
||||
/// Unknown values read as notes.
|
||||
@@ -39,7 +38,6 @@ impl PageKind {
|
||||
match s {
|
||||
"hub" => PageKind::Hub,
|
||||
"template" => PageKind::Template,
|
||||
"tag" => PageKind::Tag,
|
||||
_ => PageKind::Note,
|
||||
}
|
||||
}
|
||||
@@ -66,11 +64,13 @@ pub struct Page {
|
||||
/// pages no longer hubs); readers ignore those.
|
||||
#[serde(default)]
|
||||
pub hubs: Vec<PageId>,
|
||||
/// Titles of the tag pages ([`PageKind::Tag`]) this page is filed under, any number; see
|
||||
/// `tags`. Always in step with the tag pages (created on first use).
|
||||
/// Tag names this page is filed under, any number (names only: tags have no pages); see
|
||||
/// `tags`. Its types count as tags too without being listed here.
|
||||
#[serde(default)]
|
||||
pub tags: Vec<String>,
|
||||
/// What kind of thing this is ("persona", "lugar"...): plain names, not pages; see `types`.
|
||||
/// What kind of thing this is ("persona", "lugar"...): a name, not a page; see `types`.
|
||||
/// Holds zero or one entry (a list only for compatibility with older replicas: readers
|
||||
/// use the first, see [`Page::page_type`]). The type implies a tag of the same name.
|
||||
#[serde(default)]
|
||||
pub types: Vec<String>,
|
||||
}
|
||||
@@ -79,15 +79,16 @@ impl Page {
|
||||
pub fn is_journal(&self) -> bool {
|
||||
self.journal_date.is_some()
|
||||
}
|
||||
/// The page's single type, if any.
|
||||
pub fn page_type(&self) -> Option<&str> {
|
||||
self.types.first().map(String::as_str)
|
||||
}
|
||||
pub fn is_hub(&self) -> bool {
|
||||
self.kind == PageKind::Hub
|
||||
}
|
||||
pub fn is_template(&self) -> bool {
|
||||
self.kind == PageKind::Template
|
||||
}
|
||||
pub fn is_tag(&self) -> bool {
|
||||
self.kind == PageKind::Tag
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use rusqlite::OptionalExtension;
|
||||
|
||||
pub const VERSION: i64 = 5;
|
||||
pub const VERSION: i64 = 6;
|
||||
|
||||
pub const SCHEMA: &str = r#"
|
||||
CREATE TABLE IF NOT EXISTS meta (key TEXT PRIMARY KEY, value TEXT NOT NULL);
|
||||
@@ -116,11 +116,18 @@ CREATE TABLE IF NOT EXISTS ops (
|
||||
);
|
||||
"#;
|
||||
|
||||
/// Rebuilds the `page_tags` index from every page's `tags` column.
|
||||
/// Rebuilds the `page_tags` index from every page's `tags` column and first type (the type
|
||||
/// implies a tag of the same name).
|
||||
pub fn rebuild_page_tags(conn: &rusqlite::Connection) -> rusqlite::Result<()> {
|
||||
conn.execute("DELETE FROM page_tags", [])?;
|
||||
let rows: Vec<(String, String)> = {
|
||||
let mut st = conn.prepare("SELECT id, tags FROM pages WHERE tags != '[]'")?;
|
||||
let mut st = conn.prepare(
|
||||
"SELECT id, json_group_array(value) FROM (
|
||||
SELECT pages.id AS id, j.value AS value FROM pages, json_each(pages.tags) j
|
||||
UNION ALL
|
||||
SELECT pages.id AS id, j.value AS value FROM pages, json_each(pages.types) j WHERE j.key = 0
|
||||
) GROUP BY id",
|
||||
)?;
|
||||
st.query_map([], |r| Ok((r.get(0)?, r.get(1)?)))?
|
||||
.collect::<rusqlite::Result<_>>()?
|
||||
};
|
||||
@@ -192,6 +199,18 @@ pub fn migrate(conn: &rusqlite::Connection) -> rusqlite::Result<()> {
|
||||
if version.is_some_and(|v| v < 4) {
|
||||
rebuild_page_tags(conn)?;
|
||||
}
|
||||
if version.is_some_and(|v| v < 6) {
|
||||
// v6: tags and types are names only, one type per page. Old tag pages become plain notes (title,
|
||||
// outline and links kept); their members keep the name in `tags`.
|
||||
conn.execute("UPDATE pages SET kind='note' WHERE kind='tag'", [])?;
|
||||
// A page has one type: keep the first of several.
|
||||
conn.execute(
|
||||
"UPDATE pages SET types = json_array(json_extract(types, '$[0]'))
|
||||
WHERE json_array_length(types) > 1",
|
||||
[],
|
||||
)?;
|
||||
rebuild_page_tags(conn)?;
|
||||
}
|
||||
conn.execute(
|
||||
"INSERT OR REPLACE INTO meta(key, value) VALUES ('schema', ?1)",
|
||||
[VERSION.to_string()],
|
||||
|
||||
@@ -82,7 +82,6 @@ impl Graph {
|
||||
dir,
|
||||
now_override: Cell::new(None),
|
||||
};
|
||||
graph.repair_tag_pages()?;
|
||||
Ok(graph)
|
||||
}
|
||||
|
||||
@@ -358,12 +357,9 @@ impl Graph {
|
||||
st.query_map([&old_key], |r| r.get(0))?
|
||||
.collect::<std::result::Result<_, _>>()?
|
||||
};
|
||||
let old_title = std::mem::replace(&mut page.title, new_title.to_string());
|
||||
page.title = new_title.to_string();
|
||||
page.updated_at = self.now_ms();
|
||||
self.write_page(&page, &self.tick())?;
|
||||
if page.is_tag() {
|
||||
self.retitle_tag(&old_title, new_title)?;
|
||||
}
|
||||
for bid in referencing {
|
||||
if let Some(mut b) = self.get_block(parse_id(&bid)?)? {
|
||||
let rewritten = parse::rewrite_refs(&b.content, &old_key, new_title);
|
||||
@@ -411,11 +407,6 @@ impl Graph {
|
||||
pub(crate) fn delete_page_rows(&self, id: PageId) -> Result<()> {
|
||||
// Members of a deleted hub leave it (each member page is rewritten and logged).
|
||||
self.drop_hub_from_members(id)?;
|
||||
if let Some(p) = self.get_page(id)?
|
||||
&& p.is_tag()
|
||||
{
|
||||
self.drop_tag_from_members(&p.title)?;
|
||||
}
|
||||
let ids: Vec<String> = {
|
||||
let mut st = self
|
||||
.conn
|
||||
@@ -1222,7 +1213,9 @@ impl Graph {
|
||||
)?;
|
||||
let id = p.id.to_string();
|
||||
self.exec("DELETE FROM page_tags WHERE page_id=?1", [&id])?;
|
||||
for t in &p.tags {
|
||||
// The type implies a tag of the same name, so the index covers both (a page has one
|
||||
// type: older replicas may sync more, and only the first counts).
|
||||
for t in p.tags.iter().chain(p.types.iter().take(1)) {
|
||||
self.exec(
|
||||
"INSERT OR IGNORE INTO page_tags(page_id, tag_key) VALUES (?1, ?2)",
|
||||
params![id, title_key(t)],
|
||||
|
||||
+122
-115
@@ -1,27 +1,26 @@
|
||||
//! Tags: topic pages ("neurociencia", "finanzas") that notes are filed under.
|
||||
//! Tags: topic names ("neurociencia", "finanzas") that notes are filed under.
|
||||
//!
|
||||
//! A tag is a page of kind [`PageKind::Tag`]: it has an outline, backlinks and a place in the
|
||||
//! graph like any note. A page lists the titles of its tag pages in [`Page::tags`] (synced
|
||||
//! with the page, last writer wins), any number of them, and a derived `page_tags` index
|
||||
//! answers "which pages have this tag". Tagging with a title that has no page creates the tag
|
||||
//! page; tagging with an existing note turns that note into a tag page. Renaming or deleting
|
||||
//! a tag page keeps its members in step. Names compare case-insensitively ([`title_key`]).
|
||||
//! What a page *is* ("persona", "lugar") is a different, name-only mechanism: see `types`.
|
||||
//! A tag is only a name: it has no page. A page lists its tags in [`Page::tags`] (synced with
|
||||
//! the page, last writer wins), any number of them. Its type ([`Page::types`], at most one) implies a tag of
|
||||
//! the same name without being stored twice, and a derived `page_tags` index (tags and types,
|
||||
//! one row per page and [`title_key`]) answers "which pages have this tag". Names compare
|
||||
//! case-insensitively. What a page *is* ("persona", "lugar") is the name-only mechanism in
|
||||
//! `types`.
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
use crate::model::{NewPage, Page, PageId, PageKind, title_key};
|
||||
use crate::model::{Page, PageId, title_key};
|
||||
use crate::store::{Graph, parse_id};
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
/// Longest tag (or type) name, in characters.
|
||||
pub const MAX_TAG_CHARS: usize = 48;
|
||||
|
||||
/// A tag with the number of pages filed under it (tag pages with no members count 0).
|
||||
/// A tag with the number of pages filed under it (explicitly or through a type).
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct TagInfo {
|
||||
pub name: String,
|
||||
pub count: u32,
|
||||
/// Part of the seeded suggestions (types only; tag pages are never seeded).
|
||||
/// Part of the seeded suggestions (types only).
|
||||
pub suggested: bool,
|
||||
}
|
||||
|
||||
@@ -33,34 +32,14 @@ pub fn normalize_tag(raw: &str) -> Option<String> {
|
||||
}
|
||||
|
||||
impl Graph {
|
||||
/// The tag page called `name`, created (or made out of a note of that name) on demand.
|
||||
fn ensure_tag_page(&self, name: &str) -> Result<Page> {
|
||||
if let Some(mut p) = self.page_by_title(name)? {
|
||||
return match p.kind {
|
||||
PageKind::Tag => Ok(p),
|
||||
PageKind::Note if !p.is_journal() => {
|
||||
p.kind = PageKind::Tag;
|
||||
p.updated_at = self.now_ms();
|
||||
self.write_page(&p, &self.tick())?;
|
||||
Ok(p)
|
||||
}
|
||||
_ => Err(Error::Invalid(format!(
|
||||
"{name:?} is a hub, template or journal and cannot be a tag"
|
||||
))),
|
||||
};
|
||||
}
|
||||
self.create_page_kind_tx(NewPage::titled(name), PageKind::Tag, String::new())
|
||||
}
|
||||
|
||||
/// Replaces the tags of `page` (names cleaned, duplicates dropped case-insensitively,
|
||||
/// order kept; each becomes the title of a tag page). Journals, templates and tag pages
|
||||
/// cannot be tagged, nor can a page be tagged with itself.
|
||||
/// kept sorted by [`title_key`]). Journals and templates cannot be tagged.
|
||||
pub fn set_page_tags(&self, page: PageId, tags: Vec<String>) -> Result<Page> {
|
||||
let tx = self.tx()?;
|
||||
let mut p = self
|
||||
.get_page(page)?
|
||||
.ok_or_else(|| Error::NotFound(page.to_string()))?;
|
||||
let mut wanted: Vec<String> = Vec::with_capacity(tags.len());
|
||||
let mut next: Vec<String> = Vec::with_capacity(tags.len());
|
||||
for raw in tags {
|
||||
let Some(t) = normalize_tag(&raw) else {
|
||||
continue;
|
||||
@@ -70,22 +49,16 @@ impl Graph {
|
||||
"tag {t:?} is longer than {MAX_TAG_CHARS} characters"
|
||||
)));
|
||||
}
|
||||
if !wanted.iter().any(|x| title_key(x) == title_key(&t)) {
|
||||
wanted.push(t);
|
||||
if !next.iter().any(|x| title_key(x) == title_key(&t)) {
|
||||
next.push(t);
|
||||
}
|
||||
}
|
||||
if (p.is_journal() || p.is_template() || p.is_tag()) && !wanted.is_empty() {
|
||||
next.sort_by_cached_key(|t| title_key(t));
|
||||
if (p.is_journal() || p.is_template()) && !next.is_empty() {
|
||||
return Err(Error::Invalid(
|
||||
"journals, templates and tag pages cannot have tags".into(),
|
||||
"journals and templates cannot have tags".into(),
|
||||
));
|
||||
}
|
||||
if wanted.iter().any(|t| title_key(t) == title_key(&p.title)) {
|
||||
return Err(Error::Invalid("a page cannot be its own tag".into()));
|
||||
}
|
||||
let mut next = Vec::with_capacity(wanted.len());
|
||||
for t in wanted {
|
||||
next.push(self.ensure_tag_page(&t)?.title);
|
||||
}
|
||||
if next == p.tags {
|
||||
tx.commit()?;
|
||||
return Ok(p);
|
||||
@@ -107,7 +80,8 @@ impl Graph {
|
||||
self.set_page_tags(page, tags)
|
||||
}
|
||||
|
||||
/// Removes a tag (no-op when the page does not have it). The tag page stays.
|
||||
/// Removes an explicit tag (no-op when the page does not list it; a type of the same name
|
||||
/// keeps implying it).
|
||||
pub fn remove_page_tag(&self, page: PageId, tag: &str) -> Result<Page> {
|
||||
let p = self
|
||||
.get_page(page)?
|
||||
@@ -117,7 +91,7 @@ impl Graph {
|
||||
self.set_page_tags(page, tags)
|
||||
}
|
||||
|
||||
/// Pages carrying `tag`, most recently updated first.
|
||||
/// Pages carrying `tag` (listed or implied by a type), most recently updated first.
|
||||
pub fn pages_with_tag(&self, tag: &str) -> Result<Vec<PageId>> {
|
||||
let mut st = self.conn().prepare_cached(
|
||||
"SELECT p.id FROM page_tags t JOIN pages p ON p.id = t.page_id
|
||||
@@ -127,39 +101,63 @@ impl Graph {
|
||||
rows.map(|r| parse_id(&r?)).collect()
|
||||
}
|
||||
|
||||
/// Tags of every tagged page, as stored.
|
||||
/// Tags of every tagged page: the listed ones, then the type if not already among them
|
||||
/// (each type implies a tag), names as stored.
|
||||
pub fn all_page_tags(&self) -> Result<HashMap<PageId, Vec<String>>> {
|
||||
let mut st = self
|
||||
.conn()
|
||||
.prepare_cached("SELECT id, tags FROM pages WHERE tags != '[]'")?;
|
||||
let rows = st.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?;
|
||||
let mut st = self.conn().prepare_cached(
|
||||
"SELECT id, tags, types FROM pages WHERE tags != '[]' OR types != '[]'",
|
||||
)?;
|
||||
let rows = st.query_map([], |r| {
|
||||
Ok((
|
||||
r.get::<_, String>(0)?,
|
||||
r.get::<_, String>(1)?,
|
||||
r.get::<_, String>(2)?,
|
||||
))
|
||||
})?;
|
||||
let mut out = HashMap::new();
|
||||
for row in rows {
|
||||
let (id, tags) = row?;
|
||||
out.insert(
|
||||
parse_id(&id)?,
|
||||
serde_json::from_str(&tags).unwrap_or_default(),
|
||||
);
|
||||
let (id, tags, types) = row?;
|
||||
let mut all: Vec<String> = serde_json::from_str(&tags).unwrap_or_default();
|
||||
let types: Vec<String> = serde_json::from_str(&types).unwrap_or_default();
|
||||
for t in types.into_iter().take(1) {
|
||||
if !all.iter().any(|x| title_key(x) == title_key(&t)) {
|
||||
all.push(t);
|
||||
}
|
||||
}
|
||||
out.insert(parse_id(&id)?, all);
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Every tag page with the number of pages under it: most used first, then by name.
|
||||
/// Every tag in use, listed or implied by a type: spelling of its first page, with its page
|
||||
/// count. Most used first, then by name.
|
||||
pub fn tag_catalog(&self) -> Result<Vec<TagInfo>> {
|
||||
let mut st = self.conn().prepare_cached(
|
||||
"SELECT p.title, (SELECT COUNT(*) FROM page_tags t WHERE t.tag_key = p.title_key)
|
||||
FROM pages p WHERE p.kind = 'tag'",
|
||||
"SELECT tags, types FROM pages WHERE tags != '[]' OR types != '[]'
|
||||
ORDER BY created_at, id",
|
||||
)?;
|
||||
let rows = st.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, u32>(1)?)))?;
|
||||
let mut out = Vec::new();
|
||||
let rows = st.query_map([], |r| Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?)))?;
|
||||
let mut used: HashMap<String, (String, u32)> = HashMap::new();
|
||||
for row in rows {
|
||||
let (name, count) = row?;
|
||||
out.push(TagInfo {
|
||||
let (tags, types) = row?;
|
||||
let tags: Vec<String> = serde_json::from_str(&tags).unwrap_or_default();
|
||||
let types: Vec<String> = serde_json::from_str(&types).unwrap_or_default();
|
||||
let mut seen = HashSet::new();
|
||||
for t in tags.into_iter().chain(types.into_iter().take(1)) {
|
||||
let key = title_key(&t);
|
||||
if seen.insert(key.clone()) {
|
||||
used.entry(key).or_insert((t, 0)).1 += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut out: Vec<TagInfo> = used
|
||||
.into_values()
|
||||
.map(|(name, count)| TagInfo {
|
||||
name,
|
||||
count,
|
||||
suggested: false,
|
||||
});
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
out.sort_by(|a, b| {
|
||||
b.count
|
||||
.cmp(&a.count)
|
||||
@@ -168,59 +166,68 @@ impl Graph {
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// A tag page was renamed: its members now list the new title.
|
||||
pub(crate) fn retitle_tag(&self, old: &str, new: &str) -> Result<()> {
|
||||
for id in self.pages_with_tag(old)? {
|
||||
if let Some(mut p) = self.get_page(id)? {
|
||||
let key = title_key(old);
|
||||
for t in &mut p.tags {
|
||||
if title_key(t) == key {
|
||||
*t = new.to_string();
|
||||
}
|
||||
}
|
||||
self.write_page(&p, &self.tick())?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A tag page was deleted: its members stop listing it.
|
||||
pub(crate) fn drop_tag_from_members(&self, title: &str) -> Result<()> {
|
||||
let key = title_key(title);
|
||||
for id in self.pages_with_tag(title)? {
|
||||
if let Some(mut p) = self.get_page(id)? {
|
||||
p.tags.retain(|t| title_key(t) != key);
|
||||
self.write_page(&p, &self.tick())?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Databases from before tag pages (or snapshots of them) list tags that have no page:
|
||||
/// create the missing pages, keeping the spelling first used.
|
||||
pub(crate) fn repair_tag_pages(&self) -> Result<()> {
|
||||
let missing: Vec<String> = {
|
||||
let mut st = self.conn().prepare_cached(
|
||||
"SELECT DISTINCT tag_key FROM page_tags
|
||||
WHERE tag_key NOT IN (SELECT title_key FROM pages)",
|
||||
)?;
|
||||
st.query_map([], |r| r.get(0))?
|
||||
.collect::<std::result::Result<_, _>>()?
|
||||
};
|
||||
if missing.is_empty() {
|
||||
return Ok(());
|
||||
/// Renames a tag everywhere: in the `tags` and `types` of every page carrying it (one
|
||||
/// transaction; each page is rewritten and logged, so it syncs). When a page already lists
|
||||
/// `new`, the two merge. Returns the number of pages changed.
|
||||
pub fn rename_tag(&self, old: &str, new: &str) -> Result<usize> {
|
||||
let new = normalize_tag(new).ok_or_else(|| Error::Invalid("empty tag name".into()))?;
|
||||
if new.chars().count() > MAX_TAG_CHARS {
|
||||
return Err(Error::Invalid(format!(
|
||||
"tag {new:?} is longer than {MAX_TAG_CHARS} characters"
|
||||
)));
|
||||
}
|
||||
let key = title_key(old);
|
||||
let tx = self.tx()?;
|
||||
let mut done = std::collections::HashSet::new();
|
||||
for (_, tags) in self.all_page_tags()? {
|
||||
for t in tags {
|
||||
let key = title_key(&t);
|
||||
if missing.contains(&key) && done.insert(key) {
|
||||
self.ensure_tag_page(&t)?;
|
||||
}
|
||||
let mut changed = 0;
|
||||
for id in self.pages_with_tag(old)? {
|
||||
let Some(mut p) = self.get_page(id)? else {
|
||||
continue;
|
||||
};
|
||||
let (tags, types) = (p.tags.clone(), p.types.clone());
|
||||
p.tags = rename_in(&p.tags, &key, &new);
|
||||
p.tags.sort_by_cached_key(|t| title_key(t));
|
||||
p.types = rename_in(&p.types, &key, &new);
|
||||
if p.tags != tags || p.types != types {
|
||||
p.updated_at = self.now_ms();
|
||||
self.write_page(&p, &self.tick())?;
|
||||
changed += 1;
|
||||
}
|
||||
}
|
||||
tx.commit()?;
|
||||
Ok(())
|
||||
Ok(changed)
|
||||
}
|
||||
|
||||
/// Removes the explicit tag `name` from every page (types of that name stay, and keep
|
||||
/// implying it). Returns the number of pages changed.
|
||||
pub fn remove_tag(&self, name: &str) -> Result<usize> {
|
||||
let key = title_key(name);
|
||||
let tx = self.tx()?;
|
||||
let mut changed = 0;
|
||||
for id in self.pages_with_tag(name)? {
|
||||
let Some(mut p) = self.get_page(id)? else {
|
||||
continue;
|
||||
};
|
||||
let before = p.tags.len();
|
||||
p.tags.retain(|t| title_key(t) != key);
|
||||
if p.tags.len() != before {
|
||||
p.updated_at = self.now_ms();
|
||||
self.write_page(&p, &self.tick())?;
|
||||
changed += 1;
|
||||
}
|
||||
}
|
||||
tx.commit()?;
|
||||
Ok(changed)
|
||||
}
|
||||
}
|
||||
|
||||
/// `names` with the entries matching `key` replaced by `new` (later duplicates dropped).
|
||||
fn rename_in(names: &[String], key: &str, new: &str) -> Vec<String> {
|
||||
let mut out: Vec<String> = Vec::with_capacity(names.len());
|
||||
for n in names {
|
||||
let n = if title_key(n) == key { new } else { n };
|
||||
if !out.iter().any(|x| title_key(x) == title_key(n)) {
|
||||
out.push(n.to_string());
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
//! Types: what kind of thing a page is ("persona", "lugar", "empresa").
|
||||
//! Types: what kind of thing a page is, one per page ("persona", "lugar", "empresa").
|
||||
//!
|
||||
//! Types are plain names from a small vocabulary, not pages: a page carries its own list
|
||||
//! ([`Page::types`], synced with the page, last writer wins). Topics are a different
|
||||
//! Types are plain names from a small vocabulary, not pages: a page carries its own
|
||||
//! ([`Page::types`], at most one name, kept as a list for compatibility; synced with the page, last writer wins). Topics are a different
|
||||
//! mechanism: see `tags`.
|
||||
|
||||
use crate::error::{Error, Result};
|
||||
@@ -12,47 +12,36 @@ use std::collections::HashMap;
|
||||
|
||||
/// Types offered in pickers before they are used anywhere (the seed of the vocabulary).
|
||||
pub const SUGGESTED_TYPES: [&str; 10] = [
|
||||
"persona",
|
||||
"lugar",
|
||||
"empresa",
|
||||
"software",
|
||||
"sueño",
|
||||
"concepto",
|
||||
"idea",
|
||||
"evento",
|
||||
"recurso",
|
||||
"persona", "lugar", "empresa", "software", "sueño", "concepto", "idea", "evento", "recurso",
|
||||
"receta",
|
||||
];
|
||||
|
||||
impl Graph {
|
||||
/// Replaces the types of `page` (names cleaned, duplicates dropped case-insensitively,
|
||||
/// order kept). Journals and templates cannot be typed.
|
||||
pub fn set_page_types(&self, page: PageId, types: Vec<String>) -> Result<Page> {
|
||||
/// Sets the single type of `page` (name cleaned), or clears it with `None`. Journals and
|
||||
/// templates cannot be typed. The type is stored as a list of zero or one name.
|
||||
pub fn set_page_type(&self, page: PageId, ty: Option<&str>) -> Result<Page> {
|
||||
let tx = self.tx()?;
|
||||
let mut p = self
|
||||
.get_page(page)?
|
||||
.ok_or_else(|| Error::NotFound(page.to_string()))?;
|
||||
let mut next: Vec<String> = Vec::with_capacity(types.len());
|
||||
for raw in types {
|
||||
let Some(t) = normalize_tag(&raw) else {
|
||||
continue;
|
||||
};
|
||||
if t.chars().count() > MAX_TAG_CHARS {
|
||||
return Err(Error::Invalid(format!(
|
||||
"type {t:?} is longer than {MAX_TAG_CHARS} characters"
|
||||
)));
|
||||
let next: Vec<String> = match ty.and_then(normalize_tag) {
|
||||
Some(t) => {
|
||||
if t.chars().count() > MAX_TAG_CHARS {
|
||||
return Err(Error::Invalid(format!(
|
||||
"type {t:?} is longer than {MAX_TAG_CHARS} characters"
|
||||
)));
|
||||
}
|
||||
vec![t]
|
||||
}
|
||||
if !next.iter().any(|x| title_key(x) == title_key(&t)) {
|
||||
next.push(t);
|
||||
}
|
||||
}
|
||||
None => Vec::new(),
|
||||
};
|
||||
if next == p.types {
|
||||
tx.commit()?;
|
||||
return Ok(p);
|
||||
}
|
||||
if (p.is_journal() || p.is_template()) && !next.is_empty() {
|
||||
return Err(Error::Invalid(
|
||||
"journals and templates cannot have types".into(),
|
||||
"journals and templates cannot have a type".into(),
|
||||
));
|
||||
}
|
||||
p.types = next;
|
||||
@@ -62,30 +51,6 @@ impl Graph {
|
||||
Ok(p)
|
||||
}
|
||||
|
||||
/// Adds a type (no-op when the page already has it).
|
||||
pub fn add_page_type(&self, page: PageId, ty: &str) -> Result<Page> {
|
||||
let p = self
|
||||
.get_page(page)?
|
||||
.ok_or_else(|| Error::NotFound(page.to_string()))?;
|
||||
let mut types = p.types;
|
||||
types.push(ty.to_string());
|
||||
self.set_page_types(page, types)
|
||||
}
|
||||
|
||||
/// Removes a type (no-op when the page does not have it).
|
||||
pub fn remove_page_type(&self, page: PageId, ty: &str) -> Result<Page> {
|
||||
let p = self
|
||||
.get_page(page)?
|
||||
.ok_or_else(|| Error::NotFound(page.to_string()))?;
|
||||
let key = title_key(ty);
|
||||
let types = p
|
||||
.types
|
||||
.into_iter()
|
||||
.filter(|t| title_key(t) != key)
|
||||
.collect();
|
||||
self.set_page_types(page, types)
|
||||
}
|
||||
|
||||
/// The vocabulary: every type in use (spelling of its first page) plus the suggestions not
|
||||
/// used yet. Used types first by count then name, then unused suggestions in seed order.
|
||||
pub fn type_catalog(&self) -> Result<Vec<TagInfo>> {
|
||||
@@ -96,7 +61,7 @@ impl Graph {
|
||||
let mut used: HashMap<String, (String, u32)> = HashMap::new();
|
||||
for row in rows {
|
||||
let types: Vec<String> = serde_json::from_str(&row?).unwrap_or_default();
|
||||
for t in types {
|
||||
for t in types.into_iter().take(1) {
|
||||
used.entry(title_key(&t)).or_insert((t, 0)).1 += 1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user