CI & Build / Build now, or wait for Android? (push) Successful in 3s
CI & Build / Python lint (push) Successful in 4s
CI & Build / TypeScript typecheck (push) Successful in 7s
CI & Build / Python tests (push) Successful in 11s
CI & Build / Build & push image (push) Successful in 34s
Desktop (Tauri) / Windows installer (cross-compiled) (push) Successful in 2m21s
Desktop (Tauri) / Tauri desktop (Linux) (push) Successful in 4m18s
Desktop (Tauri) / Update manifest (push) Successful in 5s
A wiki-link was stored only as normalized TEXT, so a note's NAME was the edge. Renaming it broke every inbound link — and the fix that shipped for that (task 1848, option b) was `_rename_inbound_links`: rewrite the `[[Old Name]]` text inside the body of every note that linked to the renamed one. That works while an explicit title exists to hold still. It stops being defensible the moment a note's name is just its first body line, which is where M13 is going: fixing a typo in your opening sentence would silently edit other notes' words, with nothing to opt out to. So this lands first, before the title comes out, and that window never ships. `note_links` gains `target_id`, bound when the link is written. `target_norm` stays and is what an UNRESOLVED link carries — linking to a note that doesn't exist yet is a supported way to create one, so a link has to be able to name a target that isn't there. Resolution reads the id, falling back to the name only where nothing was bound, which is what lets a forward link connect the moment its target appears. `_claim_unresolved_links` then binds it, so the fallback is a transitional state rather than a permanent one. `_rename_inbound_links` and `rewrite_link_title` are gone. What replaced them touches link rows only: a note's text is never modified by something happening to a different note. The client can no longer resolve links for itself, and that is the point. It used to look `[[text]]` up in a client-side name index, which only held together BECAUSE renaming rewrote the text everywhere. Now the written text can name something the target is no longer called, and only the server holds the binding — so each note serializes its resolved links (`norm`, `id`, and the target's name as it stands NOW). A renamed note reads correctly everywhere it is linked from, without a single body having been edited. Unresolved links are simply absent and fall through to the create-on-click affordance that already existed; so does the offline desktop store, which derives links at query time and has no binding to send. The name-fallback join is owner-scoped everywhere it appears. Bound ids were resolved owner-scoped when written, but matching on display_title alone would have let two users who each have a note called "Groceries" see the other's id and name through an unresolved link (rule 47). The new behaviour is all SQL and this suite runs without a database, so the dead helpers' tests are removed rather than replaced. This repo has no integration lane to hold that ground — noted, not papered over.
118 lines
4.3 KiB
Python
118 lines
4.3 KiB
Python
from __future__ import annotations
|
|
|
|
from quart import Blueprint, g, jsonify
|
|
from sqlalchemy import and_, func, or_, select
|
|
from sqlalchemy.orm import aliased
|
|
|
|
from .auth import login_required
|
|
from .db import session_scope
|
|
from .models.label import Label, NoteLabel
|
|
from .models.note import Note
|
|
from .models.note_link import NoteLink
|
|
|
|
bp = Blueprint("graph", __name__, url_prefix="/api/graph")
|
|
|
|
|
|
@bp.get("")
|
|
@login_required
|
|
async def get_graph():
|
|
"""Spatial view of the owner's non-trashed notes.
|
|
|
|
Nodes are two kinds:
|
|
- notes (kind="note") — every non-trashed note; each carries its first
|
|
label's color for tinting.
|
|
- labels (kind="label", id "label:<uuid>") — every label actually attached
|
|
to a live note, acting as a clustering HUB so tagged notes gravitate
|
|
together even without wiki-links between them.
|
|
Edges are two kinds:
|
|
- wiki-links (kind="link") — resolved [[links]] (note_links.target_norm
|
|
matched to a note's normalized display_title).
|
|
- membership (kind="label") — each note → each of its label hubs.
|
|
The frontend toggles labels + unlinked notes; the graph is a light auxiliary
|
|
lens, not a focal surface.
|
|
"""
|
|
source = aliased(Note)
|
|
target = aliased(Note)
|
|
# A link joins on the note it was BOUND to, and only falls back to matching by
|
|
# name where it was never bound — a forward link written before its target
|
|
# existed. Name-matching alone is what used to make a rename break the graph.
|
|
edge_stmt = (
|
|
select(source.id, target.id)
|
|
.select_from(NoteLink)
|
|
.join(source, source.id == NoteLink.source_id)
|
|
.join(
|
|
target,
|
|
or_(
|
|
target.id == NoteLink.target_id,
|
|
and_(
|
|
NoteLink.target_id.is_(None),
|
|
func.lower(func.trim(target.display_title)) == NoteLink.target_norm,
|
|
),
|
|
),
|
|
)
|
|
.where(
|
|
source.owner_id == g.user_id,
|
|
source.deleted_at.is_(None),
|
|
target.owner_id == g.user_id,
|
|
target.deleted_at.is_(None),
|
|
source.id != target.id,
|
|
)
|
|
)
|
|
async with session_scope() as db:
|
|
rows = (await db.execute(edge_stmt)).all()
|
|
edges = []
|
|
seen: set = set()
|
|
for src_id, tgt_id in rows:
|
|
key = (src_id, tgt_id)
|
|
if key in seen:
|
|
continue
|
|
seen.add(key)
|
|
edges.append({"source": str(src_id), "target": str(tgt_id), "kind": "link"})
|
|
|
|
# Note ↔ label membership: one row per (note, label) for the owner's
|
|
# non-trashed notes. Drives both the note-color tint (first label by name)
|
|
# and the label-hub nodes + membership edges.
|
|
label_rows = (
|
|
await db.execute(
|
|
select(NoteLabel.note_id, Label.id, Label.name, Label.color)
|
|
.join(Label, Label.id == NoteLabel.label_id)
|
|
.join(Note, Note.id == NoteLabel.note_id)
|
|
.where(
|
|
Label.owner_id == g.user_id,
|
|
Note.owner_id == g.user_id,
|
|
Note.deleted_at.is_(None),
|
|
)
|
|
.order_by(Label.name)
|
|
)
|
|
).all()
|
|
|
|
first_color: dict = {}
|
|
label_nodes: dict = {}
|
|
for note_id, label_id, label_name, label_color in label_rows:
|
|
first_color.setdefault(note_id, label_color)
|
|
hub_id = f"label:{label_id}"
|
|
if hub_id not in label_nodes:
|
|
label_nodes[hub_id] = {
|
|
"id": hub_id,
|
|
"title": f"#{label_name}",
|
|
"color": label_color or "default",
|
|
"kind": "label",
|
|
"label_id": str(label_id),
|
|
}
|
|
edges.append({"source": str(note_id), "target": hub_id, "kind": "label"})
|
|
|
|
note_rows = (
|
|
await db.scalars(select(Note).where(Note.owner_id == g.user_id, Note.deleted_at.is_(None)))
|
|
).all()
|
|
nodes = [
|
|
{
|
|
"id": str(n.id),
|
|
"title": n.display_title or "Untitled",
|
|
"color": first_color.get(n.id, "default"),
|
|
"kind": "note",
|
|
}
|
|
for n in note_rows
|
|
]
|
|
nodes.extend(label_nodes.values())
|
|
return jsonify({"nodes": nodes, "edges": edges})
|