admin.nomadro.com gets sidebar L1/L2 social ops (paginated lists + entity detail); API adds soft-delete archives/stats and shared nomadro_pb for social/coffee/notify/work. Co-authored-by: Cursor <cursoragent@cursor.com>
507 lines
13 KiB
Python
507 lines
13 KiB
Python
"""Work boards/pads on PocketBase — board stored as JSON doc."""
|
|
from __future__ import annotations
|
|
|
|
import time
|
|
import uuid
|
|
from typing import Any
|
|
|
|
from nomadro_pb import ensure_once, get_pb, pb_quote
|
|
|
|
C_PROJ = "work_projects"
|
|
C_BOARD = "work_boards"
|
|
C_PAD = "work_pads"
|
|
|
|
|
|
def _now() -> str:
|
|
return time.strftime("%Y-%m-%dT%H:%M:%S.000Z", time.gmtime())
|
|
|
|
|
|
def _id(prefix: str) -> str:
|
|
return f"{prefix}_{uuid.uuid4().hex[:10]}"
|
|
|
|
|
|
def init_db() -> None:
|
|
ensure_once()
|
|
|
|
|
|
def list_projects() -> list[dict[str, Any]]:
|
|
init_db()
|
|
rows = get_pb().list_all(C_PROJ, sort="-updated_at")
|
|
return [
|
|
{
|
|
"id": r.get("id"),
|
|
"name": r.get("name"),
|
|
"description": r.get("description") or "",
|
|
"background_type": r.get("background_type") or "gradient",
|
|
"background_gradient": r.get("background_gradient") or "ocean-dive",
|
|
"board_id": r.get("board_id"),
|
|
"created_at": r.get("created_at"),
|
|
"updated_at": r.get("updated_at"),
|
|
}
|
|
for r in rows
|
|
]
|
|
|
|
|
|
def create_project(name: str, description: str = "") -> dict[str, Any]:
|
|
init_db()
|
|
now = _now()
|
|
proj = get_pb().create(
|
|
C_PROJ,
|
|
{
|
|
"name": (name or "Untitled").strip()[:120],
|
|
"description": (description or "")[:2000],
|
|
"background_type": "gradient",
|
|
"background_gradient": "ocean-dive",
|
|
"board_id": "",
|
|
"created_at": now,
|
|
"updated_at": now,
|
|
},
|
|
)
|
|
board = create_board(proj["id"], name.strip() or "Board")
|
|
get_pb().update(C_PROJ, proj["id"], {"board_id": board["id"], "updated_at": _now()})
|
|
proj["board_id"] = board["id"]
|
|
return proj
|
|
|
|
|
|
def list_boards(project_id: str | None = None) -> list[dict[str, Any]]:
|
|
init_db()
|
|
rows = get_pb().list_all(C_BOARD, sort="position")
|
|
out = []
|
|
for r in rows:
|
|
if project_id and r.get("project_id") != project_id:
|
|
continue
|
|
out.append(_board_summary(r))
|
|
return out
|
|
|
|
|
|
def _board_summary(r: dict[str, Any]) -> dict[str, Any]:
|
|
return {
|
|
"id": r.get("id"),
|
|
"project_id": r.get("project_id"),
|
|
"name": r.get("name"),
|
|
"position": r.get("position") or 0,
|
|
"default_view": r.get("default_view") or "kanban",
|
|
"background_gradient": r.get("background_gradient") or "ocean-dive",
|
|
"show_card_counter": bool(r.get("show_card_counter", True)),
|
|
"created_at": r.get("created_at"),
|
|
"updated_at": r.get("updated_at"),
|
|
}
|
|
|
|
|
|
def _empty_doc() -> dict[str, Any]:
|
|
lid = _id("list")
|
|
return {
|
|
"lists": [
|
|
{"id": lid, "name": "Todo", "type": "active", "position": 0, "color": None}
|
|
],
|
|
"cards": [],
|
|
"labels": [],
|
|
"activities": [],
|
|
}
|
|
|
|
|
|
def create_board(project_id: str, name: str) -> dict[str, Any]:
|
|
init_db()
|
|
now = _now()
|
|
rec = get_pb().create(
|
|
C_BOARD,
|
|
{
|
|
"project_id": project_id,
|
|
"name": (name or "Board").strip()[:120],
|
|
"position": 0,
|
|
"default_view": "kanban",
|
|
"background_gradient": "ocean-dive",
|
|
"show_card_counter": True,
|
|
"doc": _empty_doc(),
|
|
"created_at": now,
|
|
"updated_at": now,
|
|
},
|
|
)
|
|
return get_board(rec["id"]) # type: ignore[return-value]
|
|
|
|
|
|
def get_board(board_id: str) -> dict[str, Any] | None:
|
|
init_db()
|
|
try:
|
|
r = get_pb().request(
|
|
"GET", f"/api/collections/{C_BOARD}/records/{board_id}", admin=True
|
|
)
|
|
except Exception:
|
|
return None
|
|
doc = r.get("doc") if isinstance(r.get("doc"), dict) else _empty_doc()
|
|
return {
|
|
**_board_summary(r),
|
|
"lists": doc.get("lists") or [],
|
|
"cards": doc.get("cards") or [],
|
|
"labels": doc.get("labels") or [],
|
|
"activities": (doc.get("activities") or [])[-40:],
|
|
"card_members": doc.get("card_members") or [],
|
|
"attachments": doc.get("attachments") or [],
|
|
"comments": doc.get("comments") or [],
|
|
"task_lists": doc.get("task_lists") or [],
|
|
"tasks": doc.get("tasks") or [],
|
|
}
|
|
|
|
|
|
def _save_doc(board_id: str, doc: dict[str, Any]) -> dict[str, Any] | None:
|
|
get_pb().update(C_BOARD, board_id, {"doc": doc, "updated_at": _now()})
|
|
return get_board(board_id)
|
|
|
|
|
|
def _load_doc(board_id: str) -> tuple[dict[str, Any], dict[str, Any]] | None:
|
|
b = get_board(board_id)
|
|
if not b:
|
|
return None
|
|
doc = {
|
|
"lists": b.get("lists") or [],
|
|
"cards": b.get("cards") or [],
|
|
"labels": b.get("labels") or [],
|
|
"activities": b.get("activities") or [],
|
|
"card_members": b.get("card_members") or [],
|
|
"attachments": b.get("attachments") or [],
|
|
"comments": b.get("comments") or [],
|
|
"task_lists": b.get("task_lists") or [],
|
|
"tasks": b.get("tasks") or [],
|
|
}
|
|
return b, doc
|
|
|
|
|
|
def update_board(board_id: str, **patch: Any) -> dict[str, Any] | None:
|
|
body = {k: v for k, v in patch.items() if v is not None}
|
|
body["updated_at"] = _now()
|
|
try:
|
|
get_pb().update(C_BOARD, board_id, body)
|
|
except Exception:
|
|
return None
|
|
return get_board(board_id)
|
|
|
|
|
|
def add_list(board_id: str, name: str, typ: str = "active") -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
doc["lists"].append(
|
|
{
|
|
"id": _id("list"),
|
|
"name": name,
|
|
"type": typ,
|
|
"position": len(doc["lists"]),
|
|
"color": None,
|
|
}
|
|
)
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def add_card(
|
|
board_id: str, list_id: str, name: str, description: str = ""
|
|
) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
doc["cards"].append(
|
|
{
|
|
"id": _id("card"),
|
|
"list_id": list_id,
|
|
"board_id": board_id,
|
|
"name": name,
|
|
"description": description,
|
|
"position": len([c for c in doc["cards"] if c.get("list_id") == list_id]),
|
|
"cover": None,
|
|
"due": None,
|
|
"completed": False,
|
|
"stopwatch": None,
|
|
"label_ids": [],
|
|
"created_at": _now(),
|
|
}
|
|
)
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def update_card(board_id: str, card_id: str, **patch: Any) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
for c in doc["cards"]:
|
|
if c.get("id") == card_id:
|
|
c.update({k: v for k, v in patch.items() if v is not None})
|
|
break
|
|
else:
|
|
return None
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def move_card(
|
|
board_id: str, card_id: str, list_id: str, position: int
|
|
) -> dict[str, Any] | None:
|
|
return update_card(board_id, card_id, list_id=list_id, position=position)
|
|
|
|
|
|
def delete_card(board_id: str, card_id: str) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
doc["cards"] = [c for c in doc["cards"] if c.get("id") != card_id]
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def archive_card(board_id: str, card_id: str) -> dict[str, Any] | None:
|
|
return update_card(board_id, card_id, archived=True)
|
|
|
|
|
|
def update_list(
|
|
board_id: str, list_id: str, *, name: str | None = None, color: str | None = None
|
|
) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
for lst in doc["lists"]:
|
|
if lst.get("id") == list_id:
|
|
if name is not None:
|
|
lst["name"] = name
|
|
if color is not None:
|
|
lst["color"] = color
|
|
break
|
|
else:
|
|
return None
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def delete_list(board_id: str, list_id: str) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
doc["lists"] = [l for l in doc["lists"] if l.get("id") != list_id]
|
|
doc["cards"] = [c for c in doc["cards"] if c.get("list_id") != list_id]
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def move_list(board_id: str, list_id: str, position: int) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
for lst in doc["lists"]:
|
|
if lst.get("id") == list_id:
|
|
lst["position"] = position
|
|
break
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def toggle_card_label(board_id: str, card_id: str, label_id: str) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
for c in doc["cards"]:
|
|
if c.get("id") == card_id:
|
|
ids = list(c.get("label_ids") or [])
|
|
if label_id in ids:
|
|
ids.remove(label_id)
|
|
else:
|
|
ids.append(label_id)
|
|
c["label_ids"] = ids
|
|
break
|
|
else:
|
|
return None
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def toggle_card_member(board_id: str, card_id: str, name: str) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
members = [m for m in (doc.get("card_members") or []) if not (m.get("card_id") == card_id and m.get("name") == name)]
|
|
existed = len(members) != len(doc.get("card_members") or [])
|
|
if not existed:
|
|
members.append({"card_id": card_id, "name": name})
|
|
doc["card_members"] = members
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def add_comment(
|
|
board_id: str, card_id: str, author: str, text: str
|
|
) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
now = _now()
|
|
doc.setdefault("comments", []).append(
|
|
{
|
|
"id": _id("cmt"),
|
|
"card_id": card_id,
|
|
"author": author,
|
|
"text": text,
|
|
"created_at": now,
|
|
"updated_at": now,
|
|
}
|
|
)
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def add_attachment(
|
|
board_id: str, card_id: str, name: str, url: str
|
|
) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
doc.setdefault("attachments", []).append(
|
|
{"id": _id("att"), "card_id": card_id, "name": name, "url": url}
|
|
)
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def delete_attachment(board_id: str, attachment_id: str) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
doc["attachments"] = [
|
|
a for a in (doc.get("attachments") or []) if a.get("id") != attachment_id
|
|
]
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def ensure_task_list(card_id: str) -> str:
|
|
return _id("tl")
|
|
|
|
|
|
def add_task(board_id: str, card_id: str, name: str) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
tls = doc.setdefault("task_lists", [])
|
|
tl = next((t for t in tls if t.get("card_id") == card_id), None)
|
|
if not tl:
|
|
tl = {"id": _id("tl"), "card_id": card_id}
|
|
tls.append(tl)
|
|
doc.setdefault("tasks", []).append(
|
|
{
|
|
"id": _id("task"),
|
|
"task_list_id": tl["id"],
|
|
"card_id": card_id,
|
|
"name": name,
|
|
"completed": False,
|
|
}
|
|
)
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def toggle_task(board_id: str, task_id: str) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
for t in doc.get("tasks") or []:
|
|
if t.get("id") == task_id:
|
|
t["completed"] = not bool(t.get("completed"))
|
|
break
|
|
else:
|
|
return None
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def toggle_stopwatch(board_id: str, card_id: str) -> dict[str, Any] | None:
|
|
loaded = _load_doc(board_id)
|
|
if not loaded:
|
|
return None
|
|
_, doc = loaded
|
|
for c in doc["cards"]:
|
|
if c.get("id") == card_id:
|
|
sw = c.get("stopwatch") or {"total": 0, "startedAt": None}
|
|
if sw.get("startedAt"):
|
|
sw["startedAt"] = None
|
|
else:
|
|
sw["startedAt"] = _now()
|
|
c["stopwatch"] = sw
|
|
break
|
|
else:
|
|
return None
|
|
return _save_doc(board_id, doc)
|
|
|
|
|
|
def list_pads() -> list[dict[str, Any]]:
|
|
init_db()
|
|
rows = get_pb().list_all(C_PAD, sort="-updated_at")
|
|
return [
|
|
{
|
|
"id": r.get("id"),
|
|
"title": r.get("title"),
|
|
"authors": r.get("authors") or [],
|
|
"created_at": r.get("created_at"),
|
|
"updated_at": r.get("updated_at"),
|
|
}
|
|
for r in rows
|
|
]
|
|
|
|
|
|
def get_pad(pad_id: str) -> dict[str, Any] | None:
|
|
init_db()
|
|
try:
|
|
r = get_pb().request(
|
|
"GET", f"/api/collections/{C_PAD}/records/{pad_id}", admin=True
|
|
)
|
|
except Exception:
|
|
return None
|
|
return {
|
|
"id": r.get("id"),
|
|
"title": r.get("title"),
|
|
"text": r.get("text") or "",
|
|
"authors": r.get("authors") or [],
|
|
"revisions": r.get("revisions") or [],
|
|
"created_at": r.get("created_at"),
|
|
"updated_at": r.get("updated_at"),
|
|
}
|
|
|
|
|
|
def create_pad(title: str, author: str = "guest") -> dict[str, Any]:
|
|
init_db()
|
|
now = _now()
|
|
r = get_pb().create(
|
|
C_PAD,
|
|
{
|
|
"title": (title or "Untitled").strip()[:200],
|
|
"text": "",
|
|
"authors": [author],
|
|
"revisions": [{"id": _id("rev"), "author": author, "text": "", "at": now}],
|
|
"created_at": now,
|
|
"updated_at": now,
|
|
},
|
|
)
|
|
return get_pad(r["id"]) # type: ignore[return-value]
|
|
|
|
|
|
def update_pad(
|
|
pad_id: str, text: str, author: str = "guest", title: str | None = None
|
|
) -> dict[str, Any] | None:
|
|
p = get_pad(pad_id)
|
|
if not p:
|
|
return None
|
|
authors = list(p.get("authors") or [])
|
|
if author not in authors:
|
|
authors.append(author)
|
|
now = _now()
|
|
revs = list(p.get("revisions") or [])
|
|
revs.insert(0, {"id": _id("rev"), "author": author, "text": text, "at": now})
|
|
get_pb().update(
|
|
C_PAD,
|
|
pad_id,
|
|
{
|
|
"text": text,
|
|
"title": title if title is not None else p.get("title"),
|
|
"authors": authors,
|
|
"revisions": revs[:40],
|
|
"updated_at": now,
|
|
},
|
|
)
|
|
return get_pad(pad_id)
|