oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "context"
5 "database/sql"
6 "net/http"
7)
8
9// The sidebar's two lists (level-up spec §3.2): Pinned (pins.go) and
10// Recently edited. Both are the viewer's own and both are read through
11// the scoped accessor, so a page they may no longer open drops out.
12// Recently edited is team pages only (build note 80).
13
14// recentCount is how many pages Recently edited shows.
15const recentCount = 6
16
17// recentVerbs are the writes that count as editing a page. Bulk link
18// edits on team pages log their own verbs (linkVerbs) and do not count
19// (§8); they are read only to recognise their transaction.
20var (
21 recentVerbs = []string{VerbCreated, VerbEdited, VerbReverted}
22 linkVerbs = []string{VerbRelinked, VerbMarked}
23)
24
25// recentQuery is Recently edited in one query (build note 80, review
26// round 6 L1): each live team page the person wrote in (the rung and
27// gone are plaintext, §4), at its newest write, newest first, six.
28//
29// A GROUP BY over the person's events reads every one of them: 13ms at
30// 20,000 and 150ms at 200,000, 36ms and 400ms with the bulk test. So
31// the pages are walked instead, one index step each (touched, a loose
32// scan of idx_events_actor_page), and each page's newest write is read
33// back from its end of that index, which stops at the first that
34// counts. The cost follows how many pages the person wrote in, not how
35// many times: 0.3ms at 20,000 or 200,000 events.
36//
37// An "edited" does not count when its transaction (one actor, one at)
38// also logged another "edited" or a link verb on a live team page:
39// bulk, as §3.2 and N1 define it. That test reads one transaction by
40// idx_events_at, named because the planner otherwise takes the person's
41// whole index for it. Link verbs never count themselves.
42const recentQuery = `
43WITH RECURSIVE touched(id) AS (
44 SELECT (SELECT MIN(object_id) FROM events
45 WHERE actor_id = @actor AND object_kind = @page)
46 UNION ALL
47 SELECT (SELECT MIN(object_id) FROM events
48 WHERE actor_id = @actor AND object_kind = @page
49 AND object_id > touched.id)
50 FROM touched WHERE touched.id IS NOT NULL
51),
52newest(page, last) AS (
53 SELECT touched.id, (SELECT e.id FROM events e
54 WHERE e.actor_id = @actor AND e.object_kind = @page
55 AND e.object_id = touched.id AND e.verb IN @verbs
56 AND NOT (e.verb = @edited AND EXISTS (
57 SELECT 1 FROM events other INDEXED BY idx_events_at
58 JOIN pages them ON them.id = other.object_id
59 WHERE other.at = e.at AND other.actor_id = e.actor_id
60 AND other.object_kind = @page AND other.id <> e.id
61 AND them.rung = @team AND NOT them.gone
62 AND (other.verb = @edited OR other.verb IN @links)))
63 ORDER BY e.id DESC LIMIT 1)
64 FROM touched JOIN pages ON pages.id = touched.id
65 WHERE pages.rung = @team AND NOT pages.gone
66)
67SELECT page FROM newest WHERE last IS NOT NULL
68ORDER BY last DESC LIMIT @limit`
69
70// recentEdits is the team page ids the person last wrote in, newest
71// first, at most recentCount, leaving out bulk edits: a transaction
72// that logged more than one "edited" (Update links on private pages,
73// which keep "edited" so the plaintext log says nothing of a private
74// link graph; §3.2, N1), or a link verb. No number of private writes,
75// and no number of writes to one page, can push a team page out.
76func (store *Store) recentEdits(ctx context.Context,
77 memberID int64) ([]int64, error) {
78 var ids []int64
79 err := store.database.WithContext(ctx).Raw(recentQuery,
80 recentArgs(memberID)...).Scan(&ids).Error
81 return ids, err
82}
83
84// recentArgs are recentQuery's named arguments for one person.
85func recentArgs(memberID int64) []any {
86 return []any{sql.Named("actor", memberID),
87 sql.Named("page", ObjectPage), sql.Named("verbs", recentVerbs),
88 sql.Named("links", linkVerbs), sql.Named("edited", VerbEdited),
89 sql.Named("team", RungTeam), sql.Named("limit", recentCount)}
90}
91
92// sideLists are Pinned and Recently edited for the viewer. A failure to
93// read them leaves them out; the page itself still draws.
94func (app *app) sideLists(request *http.Request,
95 page *view) ([]sideLink, []sideLink) {
96 viewer := page.Viewer
97 if viewer.MemberID == 0 {
98 return nil, nil
99 }
100 ctx := request.Context()
101 pins, pinErr := app.store.Pins(ctx, viewer)
102 recent, recentErr := app.store.recentEdits(ctx, viewer.MemberID)
103 if pinErr != nil || recentErr != nil {
104 app.logger.Error("sidebar lists", "pins", pinErr,
105 "recent", recentErr)
106 return nil, nil
107 }
108 current := ""
109 if page.Chrome != nil && page.Chrome.Page != nil {
110 current = page.Chrome.Page.Href
111 }
112 scope := ScopeOf(viewer)
113 return app.index.SideLinks(ctx, scope, pins, 0, current),
114 app.index.sideLinks(ctx, scope, recent, recentCount, current,
115 true)
116}
117
118// SideLinks is pages by id as sidebar rows, those the scope may open
119// only, at most limit of them (0 for no limit).
120func (index *Index) SideLinks(ctx context.Context, scope Scope,
121 ids []int64, limit int, current string) []sideLink {
122 return index.sideLinks(ctx, scope, ids, limit, current, false)
123}
124
125// sideLinks is SideLinks, of team pages only when teamOnly: Recently
126// edited lists no daily note and no scratch page, so no private first
127// line is on screen while it is shared (Paul, 2026-11-16; build note
128// 80). A pinned note stays, as the person chose it, with a lock.
129func (index *Index) sideLinks(ctx context.Context, scope Scope,
130 ids []int64, limit int, current string, teamOnly bool) []sideLink {
131 if err := index.Sync(ctx); err != nil {
132 return nil
133 }
134 index.mutex.RLock()
135 defer index.mutex.RUnlock()
136 view := index.in(scope)
137 var out []sideLink
138 for _, pageID := range ids {
139 found := view.page(pageID)
140 if found == nil || (teamOnly && !found.team()) {
141 continue
142 }
143 out = append(out, sideLinkOf(found, scope, current))
144 if limit > 0 && len(out) == limit {
145 break
146 }
147 }
148 return out
149}
150
151func sideIcon(kind string) string {
152 switch kind {
153 case KindDaily:
154 return "calendar"
155 case KindScratch:
156 return "file"
157 }
158 return "file-text"
159}
160