Files
Docwell/Server/internal/convert/database.go
T
tajniak81andClaude Opus 5.5 406779b353 Open record links on public pages and find records in search
- Public pages follow "#b-…" and "#r-…" links like the page view does,
  and their block and record links copy the public address. The retry
  logic is shared as followLink (Packages/editor/src/hashlinks.ts).
- Search indexes each database record as its own document (from the
  page's content_json), returned with a "record" field and opened beside
  its page. Filter-only listings stay pages only; removed records and
  trashed pages drop out. The index mapping version is bumped, so it
  rebuilds once.
- Phone app: search lists records and opens them through a new
  "follow" bridge message.
- Guide updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 14:50:51 +01:00

259 lines
6.1 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-or-later
package convert
import (
"encoding/json"
"strings"
"time"
)
// Databases (ADR-13) reach the converters as "database" nodes whose
// attrs.data holds the records, written by yrs from the page's Y.Doc. Every
// export draws one as its title and a table of the first view's visible
// columns, in the rows' own order.
// expandDatabases replaces database nodes, at any depth, with a title and a table.
func expandDatabases(n Node) Node {
if len(n.Content) == 0 {
return n
}
out := make([]Node, 0, len(n.Content))
for _, c := range n.Content {
if c.Type == "database" {
out = append(out, databaseNodes(c)...)
} else {
out = append(out, expandDatabases(c))
}
}
n.Content = out
return n
}
type dbColumn struct {
id, name, typ string
options map[string]string
}
func databaseNodes(n Node) []Node {
data, _ := n.Attrs["data"].(map[string]any)
if data == nil {
return nil
}
var cols []dbColumn
for _, raw := range list(data["columns"]) {
c, _ := raw.(map[string]any)
id, _ := c["id"].(string)
if id == "" {
continue
}
col := dbColumn{id: id, typ: str(c["type"]), name: str(c["name"]), options: map[string]string{}}
for _, o := range list(c["options"]) {
if om, ok := o.(map[string]any); ok {
col.options[str(om["id"])] = str(om["name"])
}
}
cols = append(cols, col)
}
// The first view decides which columns show.
hidden := map[string]bool{}
if views := list(data["views"]); len(views) > 0 {
if v, ok := views[0].(map[string]any); ok {
for _, h := range list(v["hidden"]) {
hidden[str(h)] = true
}
}
}
shown := cols[:0:0]
for _, c := range cols {
if c.typ == "title" || !hidden[c.id] {
shown = append(shown, c)
}
}
if len(shown) == 0 {
return nil
}
cell := func(typ, text string) Node {
p := Node{Type: "paragraph"}
if text != "" {
p.Content = []Node{{Type: "text", Text: text}}
}
return Node{Type: typ, Content: []Node{p}}
}
head := Node{Type: "tableRow"}
for _, c := range shown {
head.Content = append(head.Content, cell("tableHeader", c.name))
}
table := Node{Type: "table", Content: []Node{head}}
for _, raw := range list(data["rows"]) {
r, _ := raw.(map[string]any)
if r == nil {
continue
}
row := Node{Type: "tableRow"}
for _, c := range shown {
row.Content = append(row.Content, cell("tableCell", cellText(c, r)))
}
table.Content = append(table.Content, row)
}
var out []Node
if title := str(data["title"]); title != "" {
out = append(out, Node{Type: "paragraph", Content: []Node{{Type: "text", Text: title, Marks: []Mark{{Type: "bold"}}}}})
}
return append(out, table)
}
// cellText is a cell as text, like cellText in Packages/editor/src/database/model.ts.
func cellText(c dbColumn, row map[string]any) string {
cells, _ := row["cells"].(map[string]any)
v := cells[c.id]
switch c.typ {
case "createdTime":
if ms, ok := row["createdAt"].(float64); ok && ms > 0 {
return time.UnixMilli(int64(ms)).UTC().Format("2006-01-02")
}
return ""
case "createdBy":
if p, ok := row["createdBy"].(map[string]any); ok {
return str(p["name"])
}
return ""
case "checkbox":
if v == true {
return "✓"
}
return ""
case "select":
return c.options[str(v)]
case "multiSelect":
var names []string
for _, id := range list(v) {
if name := c.options[str(id)]; name != "" {
names = append(names, name)
}
}
return strings.Join(names, ", ")
case "member", "attachment":
var names []string
for _, x := range list(v) {
if m, ok := x.(map[string]any); ok && str(m["name"]) != "" {
names = append(names, str(m["name"]))
}
}
return strings.Join(names, ", ")
case "progress":
if f, ok := v.(float64); ok {
return formatNumber(f) + "%"
}
return ""
}
switch x := v.(type) {
case string:
return x
case float64:
return formatNumber(x)
}
return ""
}
func list(v any) []any {
l, _ := v.([]any)
return l
}
func str(v any) string {
s, _ := v.(string)
return s
}
// Record is one database record as search sees it (ADR-13).
type Record struct {
Database, Row string
Title string // the title property
Text string // the other properties and the record's body
}
// Records lists the records of every database in a page's content_json.
func Records(raw json.RawMessage) ([]Record, error) {
doc, err := Parse(raw)
if err != nil {
return nil, err
}
var out []Record
var walk func(n Node)
walk = func(n Node) {
if n.Type == "database" {
out = append(out, databaseRecords(n)...)
return
}
for _, c := range n.Content {
walk(c)
}
}
walk(doc)
return out, nil
}
func databaseRecords(n Node) []Record {
data, _ := n.Attrs["data"].(map[string]any)
db := n.attr("id")
if data == nil || db == "" {
return nil
}
var cols []dbColumn
for _, raw := range list(data["columns"]) {
c, _ := raw.(map[string]any)
col := dbColumn{id: str(c["id"]), typ: str(c["type"]), options: map[string]string{}}
for _, o := range list(c["options"]) {
if om, ok := o.(map[string]any); ok {
col.options[str(om["id"])] = str(om["name"])
}
}
if col.id != "" {
cols = append(cols, col)
}
}
var out []Record
for _, raw := range list(data["rows"]) {
r, _ := raw.(map[string]any)
if r == nil || str(r["id"]) == "" {
continue
}
rec := Record{Database: db, Row: str(r["id"])}
var parts []string
for _, c := range cols {
// Checkmarks and dates the record was made say nothing to search for.
if c.typ == "checkbox" || c.typ == "createdTime" {
continue
}
t := cellText(c, r)
if c.typ == "title" {
rec.Title = t
} else if t != "" {
parts = append(parts, t)
}
}
parts = jsonText(r["body"], parts)
rec.Text = strings.Join(parts, " ")
out = append(out, rec)
}
return out
}
// jsonText appends the text of ProseMirror JSON (a node or a list of them).
func jsonText(v any, parts []string) []string {
switch x := v.(type) {
case []any:
for _, c := range x {
parts = jsonText(c, parts)
}
case map[string]any:
if t, ok := x["text"].(string); ok && t != "" {
parts = append(parts, t)
}
parts = jsonText(x["content"], parts)
}
return parts
}