| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/mapa/internal/mapa/locale" |
| 5 | |
| 6 | "context" |
| 7 | "html/template" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 11 | ) |
| 12 | |
| 13 | // PageView is one page as its reader sees it. |
| 14 | type PageView struct { |
| 15 | ID, Revision int64 |
| 16 | // Public is the page's id in every URL (publicid.go). |
| 17 | Public string |
| 18 | Kind, Rung string |
| 19 | Title, Date string |
| 20 | Label, Href string |
| 21 | Body string |
| 22 | HTML template.HTML |
| 23 | Aliases []AliasView |
| 24 | Backlinks []Linked |
| 25 | Mentions []Linked |
| 26 | // Around is the Links panel's links out and map (level-up §4.3). |
| 27 | Around *Around |
| 28 | Created, Edited time.Time |
| 29 | // From is the kicker's note on a page imported from Notion: "From |
| 30 | // Notion, created there 3 March 2024" (Notion import spec §14.2). |
| 31 | From string |
| 32 | // Look is a team page's icon, and Banner its band, when it has one |
| 33 | // (columns and icons spec §2, §3). |
| 34 | Look *Icon |
| 35 | Banner *Banner |
| 36 | } |
| 37 | |
| 38 | // Linked is a page that points at the one being read, with the blocks |
| 39 | // that do it. |
| 40 | type Linked struct { |
| 41 | ID int64 |
| 42 | Public string |
| 43 | Label, Href string |
| 44 | Kind string |
| 45 | Look *Icon |
| 46 | Contexts []Context |
| 47 | } |
| 48 | |
| 49 | // Context is one block of a linking page, drawn, and its place. |
| 50 | type Context struct { |
| 51 | Block int |
| 52 | HTML template.HTML |
| 53 | // Layout is a whole column layout, as one row of a block list. |
| 54 | Layout bool |
| 55 | } |
| 56 | |
| 57 | // maxLinked caps a backlink or mention list. The cap is applied after |
| 58 | // scoping, so it can never hint at what the viewer cannot see. |
| 59 | const maxLinked = 100 |
| 60 | |
| 61 | // View is the page as scope sees it, or ErrNotFound. |
| 62 | func (index *Index) View(ctx context.Context, scope Scope, |
| 63 | pageID int64) (PageView, error) { |
| 64 | if err := index.Sync(ctx); err != nil { |
| 65 | return PageView{}, err |
| 66 | } |
| 67 | index.mutex.RLock() |
| 68 | defer index.mutex.RUnlock() |
| 69 | page := index.pages[pageID] |
| 70 | if page == nil || !scope.sees(page) { |
| 71 | return PageView{}, ErrNotFound |
| 72 | } |
| 73 | view := page.summary(scope.Locale) |
| 74 | view.HTML = markup.RenderPage(page.blocks, |
| 75 | index.drawer(ctx, scope, page.id, page.record.Body)) |
| 76 | view.Backlinks = index.backlinks(scope, page) |
| 77 | if page.team() { |
| 78 | view.Mentions = index.mentions(scope, page, view.Backlinks) |
| 79 | } |
| 80 | view.Around = index.in(scope).around(page, view.Backlinks, |
| 81 | view.Mentions) |
| 82 | return view, nil |
| 83 | } |
| 84 | |
| 85 | // summary is the parts of a view that need no scope. |
| 86 | func (page *entry) summary(dates *locale.Locale) PageView { |
| 87 | return PageView{ID: page.id, Public: publicID(page.nonce), |
| 88 | Revision: page.revision, Kind: page.kind, |
| 89 | Rung: page.rung, Title: page.record.Title, Date: page.record.Date, |
| 90 | Label: page.label(dates), Href: page.href(), Body: page.record.Body, |
| 91 | Aliases: page.aliases, Created: page.created, Edited: page.updated, |
| 92 | From: fromNotion(dates, page.record.Origin), Look: page.look(), |
| 93 | Banner: page.banner()} |
| 94 | } |
| 95 | |
| 96 | // label is what a page is called in a list: a daily note's date is |
| 97 | // written in the reader's locale. |
| 98 | func (page *entry) label(dates *locale.Locale) string { |
| 99 | switch page.kind { |
| 100 | case KindPage: |
| 101 | return page.record.Title |
| 102 | case KindDaily: |
| 103 | return dates.Long(page.record.Date) |
| 104 | } |
| 105 | if line := firstLine(page.blocks); line != "" { |
| 106 | return line |
| 107 | } |
| 108 | return "Untitled scratch page" |
| 109 | } |
| 110 | |
| 111 | func (page *entry) href() string { |
| 112 | if page.kind == KindDaily { |
| 113 | return "/d/" + page.record.Date |
| 114 | } |
| 115 | return "/p/" + publicID(page.nonce) |
| 116 | } |
| 117 | |
| 118 | // targetFolds is the link texts that reach a page: a team page's title |
| 119 | // and aliases, or a daily note's date (§3.4). |
| 120 | func (page *entry) targetFolds() map[string]bool { |
| 121 | out := map[string]bool{} |
| 122 | for _, fold := range page.titleFolds() { |
| 123 | out[fold] = true |
| 124 | } |
| 125 | if page.kind == KindDaily { |
| 126 | out[page.record.Date] = true |
| 127 | } |
| 128 | return out |
| 129 | } |
| 130 | |
| 131 | // backlinks is every page in scope that links here, with each linking |
| 132 | // block drawn as its context (§6). |
| 133 | func (index *Index) backlinks(scope Scope, page *entry) []Linked { |
| 134 | targets := page.targetFolds() |
| 135 | sources := map[int64]string{} |
| 136 | for fold := range targets { |
| 137 | if found := index.linkers[fold]; found != nil { |
| 138 | for pageID, text := range found.inScope(scope) { |
| 139 | sources[pageID] = text |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | delete(sources, page.id) |
| 144 | images := index.imageLookup(context.Background()) |
| 145 | var out []Linked |
| 146 | for _, source := range index.ordered(sources) { |
| 147 | draw := resolver{index: index, scope: scope, page: source.id, |
| 148 | images: images} |
| 149 | linked := Linked{ID: source.id, Public: publicID(source.nonce), |
| 150 | Label: source.label(scope.Locale), |
| 151 | Href: source.href(), Kind: source.kind, Look: source.look()} |
| 152 | for _, number := range source.blocksLinking(targets) { |
| 153 | linked.Contexts = append(linked.Contexts, Context{Block: number, |
| 154 | HTML: markup.Render(source.blocks[number:number+1], draw)}) |
| 155 | } |
| 156 | out = append(out, linked) |
| 157 | if len(out) == maxLinked { |
| 158 | break |
| 159 | } |
| 160 | } |
| 161 | return out |
| 162 | } |
| 163 | |
| 164 | // blocksLinking is the blocks holding a link to any of targets. |
| 165 | func (page *entry) blocksLinking(targets map[string]bool) []int { |
| 166 | var out []int |
| 167 | for _, link := range page.links { |
| 168 | if targets[Fold(link.Target)] && |
| 169 | (len(out) == 0 || out[len(out)-1] != link.Block) { |
| 170 | out = append(out, link.Block) |
| 171 | } |
| 172 | } |
| 173 | return out |
| 174 | } |
| 175 | |
| 176 | // shownText is one spelling of a link text: the lowest page id's, so the |
| 177 | // choice is stable. |
| 178 | func shownText(texts map[int64]string) string { |
| 179 | chosen, lowest := "", int64(0) |
| 180 | for pageID, text := range texts { |
| 181 | if lowest == 0 || pageID < lowest { |
| 182 | chosen, lowest = text, pageID |
| 183 | } |
| 184 | } |
| 185 | return chosen |
| 186 | } |
| 187 | |
| 188 | // TeamPages is every team page, by title. |
| 189 | func (index *Index) TeamPages(ctx context.Context) ([]PageView, error) { |
| 190 | return index.list(ctx, func() map[int64]bool { return index.team }, |
| 191 | "", nil) |
| 192 | } |
| 193 | |
| 194 | // OwnPages is one person's private pages of one kind, newest first, read |
| 195 | // from their own partition only. |
| 196 | func (index *Index) OwnPages(ctx context.Context, scope Scope, |
| 197 | kind string) ([]PageView, error) { |
| 198 | return index.list(ctx, func() map[int64]bool { |
| 199 | return index.private[scope.Member] |
| 200 | }, kind, scope.Locale) |
| 201 | } |
| 202 | |
| 203 | // list summarises one partition, optionally of one kind. partition is |
| 204 | // called under the read lock. |
| 205 | func (index *Index) list(ctx context.Context, |
| 206 | partition func() map[int64]bool, kind string, |
| 207 | dates *locale.Locale) ([]PageView, error) { |
| 208 | if err := index.Sync(ctx); err != nil { |
| 209 | return nil, err |
| 210 | } |
| 211 | index.mutex.RLock() |
| 212 | defer index.mutex.RUnlock() |
| 213 | ids := map[int64]string{} |
| 214 | for pageID := range partition() { |
| 215 | if kind == "" || index.pages[pageID].kind == kind { |
| 216 | ids[pageID] = "" |
| 217 | } |
| 218 | } |
| 219 | var out []PageView |
| 220 | for _, page := range index.ordered(ids) { |
| 221 | out = append(out, page.summary(dates)) |
| 222 | } |
| 223 | return out, nil |
| 224 | } |
| 225 | |
| 226 | // TitleHref is the page holding title, for "a page with that title |
| 227 | // already exists": a link to it. Titles are team pages only, so this |
| 228 | // reveals nothing the asker could not open. |
| 229 | func (index *Index) TitleHref(ctx context.Context, title string) string { |
| 230 | if index.Sync(ctx) != nil { |
| 231 | return "" |
| 232 | } |
| 233 | index.mutex.RLock() |
| 234 | defer index.mutex.RUnlock() |
| 235 | if pageID, ok := index.titles[Fold(title)]; ok { |
| 236 | return index.pages[pageID].href() |
| 237 | } |
| 238 | return "" |
| 239 | } |
| 240 | |
| 241 | // Day is scope's daily note for date, and whether it exists. An |
| 242 | // unwritten day still has its backlinks: the team pages, and the |
| 243 | // viewer's own, that link [[date]] (§3.4). |
| 244 | func (index *Index) Day(ctx context.Context, scope Scope, |
| 245 | date string) (PageView, bool, error) { |
| 246 | if err := index.Sync(ctx); err != nil { |
| 247 | return PageView{}, false, err |
| 248 | } |
| 249 | index.mutex.RLock() |
| 250 | defer index.mutex.RUnlock() |
| 251 | if pageID, ok := index.days[scope.Member][date]; ok && scope.Member != 0 { |
| 252 | page := index.pages[pageID] |
| 253 | view := page.summary(scope.Locale) |
| 254 | view.HTML = markup.RenderPage(page.blocks, |
| 255 | index.drawer(ctx, scope, page.id, page.record.Body)) |
| 256 | view.Backlinks = index.backlinks(scope, page) |
| 257 | view.Around = index.in(scope).around(page, view.Backlinks, nil) |
| 258 | return view, true, nil |
| 259 | } |
| 260 | unwritten := &entry{kind: KindDaily, rung: RungPrivate, |
| 261 | owner: scope.Member, record: Record{Date: date}} |
| 262 | view := unwritten.summary(scope.Locale) |
| 263 | view.HTML = markup.EmptyParagraph |
| 264 | view.Backlinks = index.backlinks(scope, unwritten) |
| 265 | view.Around = index.in(scope).around(unwritten, view.Backlinks, nil) |
| 266 | return view, false, nil |
| 267 | } |
| 268 | |
| 269 | // Blocks is a page's blocks, drawn one by one, for choosing which to |
| 270 | // share ("Make team page"). |
| 271 | func (index *Index) Blocks(ctx context.Context, scope Scope, |
| 272 | pageID int64) ([]Context, error) { |
| 273 | if err := index.Sync(ctx); err != nil { |
| 274 | return nil, err |
| 275 | } |
| 276 | index.mutex.RLock() |
| 277 | defer index.mutex.RUnlock() |
| 278 | page := index.pages[pageID] |
| 279 | if page == nil || !scope.sees(page) { |
| 280 | return nil, ErrNotFound |
| 281 | } |
| 282 | draw := index.drawer(ctx, scope, page.id, page.record.Body) |
| 283 | out := make([]Context, 0, len(page.blocks)) |
| 284 | for number := 0; number < len(page.blocks); number++ { |
| 285 | block := page.blocks[number] |
| 286 | if block.Kind == markup.Columns { |
| 287 | // A layout is one row, drawn whole; choosing it chooses |
| 288 | // every block of it (columns and icons spec §1.10). |
| 289 | end := number |
| 290 | for end < len(page.blocks) && |
| 291 | page.blocks[end].Kind != markup.ColumnsEnd { |
| 292 | end++ |
| 293 | } |
| 294 | out = append(out, Context{Block: number, Layout: true, |
| 295 | HTML: markup.Render(page.blocks[number:end+1], draw)}) |
| 296 | continue |
| 297 | } |
| 298 | if markup.IsFence(block.Kind) { |
| 299 | continue |
| 300 | } |
| 301 | out = append(out, Context{Block: number, |
| 302 | HTML: markup.Render(page.blocks[number:number+1], draw)}) |
| 303 | } |
| 304 | return out, nil |
| 305 | } |
| 306 | |
| 307 | // Preview draws a body for scope as page would show it (0 for a page not |
| 308 | // written yet), without storing anything. |
| 309 | func (index *Index) Preview(ctx context.Context, scope Scope, page int64, |
| 310 | body string) (template.HTML, error) { |
| 311 | if err := index.Sync(ctx); err != nil { |
| 312 | return "", err |
| 313 | } |
| 314 | index.mutex.RLock() |
| 315 | defer index.mutex.RUnlock() |
| 316 | return markup.RenderPage(markup.Parse(body), |
| 317 | index.drawer(ctx, scope, page, body)), nil |
| 318 | } |
| 319 | |