| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/mapa/internal/mapa/locale" |
| 5 | |
| 6 | "net/url" |
| 7 | "sort" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 11 | ) |
| 12 | |
| 13 | // Scope is whose index this read is: team pages plus one person's own |
| 14 | // private pages (§6.3). Member 0 is a person with no private pages yet. |
| 15 | type Scope struct { |
| 16 | Member int64 |
| 17 | // Locale writes the dates a read gives back, such as a daily |
| 18 | // note's title or a date chip (build note 85). It scopes nothing. |
| 19 | Locale *locale.Locale |
| 20 | } |
| 21 | |
| 22 | // ScopeOf is the viewer's scope. |
| 23 | func ScopeOf(viewer Viewer) Scope { |
| 24 | return Scope{Member: viewer.MemberID, Locale: viewer.Locale} |
| 25 | } |
| 26 | |
| 27 | // sees reports whether the scope may open a page. |
| 28 | func (scope Scope) sees(page *entry) bool { |
| 29 | return page.team() || (scope.Member != 0 && page.owner == scope.Member) |
| 30 | } |
| 31 | |
| 32 | // add puts an opened page into every derivation. The caller holds the |
| 33 | // write lock. |
| 34 | func (index *Index) add(page *entry) { |
| 35 | index.pages[page.id] = page |
| 36 | if page.team() { |
| 37 | index.team[page.id] = true |
| 38 | for _, fold := range page.titleFolds() { |
| 39 | index.titles[fold] = page.id |
| 40 | } |
| 41 | } else { |
| 42 | if index.private[page.owner] == nil { |
| 43 | index.private[page.owner] = map[int64]bool{} |
| 44 | } |
| 45 | index.private[page.owner][page.id] = true |
| 46 | } |
| 47 | if page.kind == KindDaily { |
| 48 | if index.days[page.owner] == nil { |
| 49 | index.days[page.owner] = map[string]int64{} |
| 50 | } |
| 51 | index.days[page.owner][page.record.Date] = page.id |
| 52 | } |
| 53 | for _, link := range page.links { |
| 54 | index.linkersOf(Fold(link.Target)).add(page, link.Target) |
| 55 | } |
| 56 | if page.in != "" { |
| 57 | if index.within[page.in] == nil { |
| 58 | index.within[page.in] = map[int64]bool{} |
| 59 | } |
| 60 | index.within[page.in][page.id] = true |
| 61 | } |
| 62 | index.termsOf(page).add(page.id, page.counts, page.length) |
| 63 | index.titlesDirty = index.titlesDirty || page.team() |
| 64 | index.pending.dirty = true |
| 65 | } |
| 66 | |
| 67 | // termsOf is the search partition a page belongs in. |
| 68 | func (index *Index) termsOf(page *entry) *partition { |
| 69 | if page.team() { |
| 70 | return index.teamTerms |
| 71 | } |
| 72 | part := index.ownTerms[page.owner] |
| 73 | if part == nil { |
| 74 | part = newPartition() |
| 75 | index.ownTerms[page.owner] = part |
| 76 | } |
| 77 | return part |
| 78 | } |
| 79 | |
| 80 | // remove takes a page out of every derivation, if it is there. |
| 81 | func (index *Index) remove(pageID int64) { |
| 82 | page, ok := index.pages[pageID] |
| 83 | if !ok { |
| 84 | return |
| 85 | } |
| 86 | delete(index.pages, pageID) |
| 87 | delete(index.team, pageID) |
| 88 | delete(index.private[page.owner], pageID) |
| 89 | if index.days[page.owner][page.record.Date] == pageID { |
| 90 | delete(index.days[page.owner], page.record.Date) |
| 91 | } |
| 92 | for _, fold := range page.titleFolds() { |
| 93 | if index.titles[fold] == pageID { |
| 94 | delete(index.titles, fold) |
| 95 | } |
| 96 | } |
| 97 | for _, link := range page.links { |
| 98 | fold := Fold(link.Target) |
| 99 | if found := index.linkers[fold]; found != nil && found.drop(page) { |
| 100 | delete(index.linkers, fold) |
| 101 | } |
| 102 | } |
| 103 | if page.in != "" { |
| 104 | delete(index.within[page.in], page.id) |
| 105 | if len(index.within[page.in]) == 0 { |
| 106 | delete(index.within, page.in) |
| 107 | } |
| 108 | } |
| 109 | index.termsOf(page).remove(page.id, page.counts) |
| 110 | index.titlesDirty = index.titlesDirty || page.team() |
| 111 | index.pending.dirty = true |
| 112 | } |
| 113 | |
| 114 | // titleFolds is a team page's title and aliases, folded. |
| 115 | func (page *entry) titleFolds() []string { |
| 116 | if !page.team() { |
| 117 | return nil |
| 118 | } |
| 119 | out := []string{Fold(page.record.Title)} |
| 120 | for _, alias := range page.aliases { |
| 121 | out = append(out, Fold(alias.Title)) |
| 122 | } |
| 123 | return out |
| 124 | } |
| 125 | |
| 126 | func (index *Index) linkersOf(fold string) *linkers { |
| 127 | found := index.linkers[fold] |
| 128 | if found == nil { |
| 129 | found = &linkers{team: map[int64]string{}, |
| 130 | private: map[int64]map[int64]string{}} |
| 131 | index.linkers[fold] = found |
| 132 | } |
| 133 | return found |
| 134 | } |
| 135 | |
| 136 | func (found *linkers) add(page *entry, text string) { |
| 137 | if page.team() { |
| 138 | found.team[page.id] = text |
| 139 | return |
| 140 | } |
| 141 | if found.private[page.owner] == nil { |
| 142 | found.private[page.owner] = map[int64]string{} |
| 143 | } |
| 144 | found.private[page.owner][page.id] = text |
| 145 | } |
| 146 | |
| 147 | // drop removes a page, and reports whether nothing links with the text. |
| 148 | func (found *linkers) drop(page *entry) bool { |
| 149 | delete(found.team, page.id) |
| 150 | delete(found.private[page.owner], page.id) |
| 151 | if len(found.private[page.owner]) == 0 { |
| 152 | delete(found.private, page.owner) |
| 153 | } |
| 154 | return len(found.team) == 0 && len(found.private) == 0 |
| 155 | } |
| 156 | |
| 157 | // inScope is the linking pages the scope may see: the team partition |
| 158 | // and the scope's own, read directly, never filtered from a whole. |
| 159 | func (found *linkers) inScope(scope Scope) map[int64]string { |
| 160 | out := make(map[int64]string, len(found.team)) |
| 161 | for pageID, text := range found.team { |
| 162 | out[pageID] = text |
| 163 | } |
| 164 | if scope.Member != 0 { |
| 165 | for pageID, text := range found.private[scope.Member] { |
| 166 | out[pageID] = text |
| 167 | } |
| 168 | } |
| 169 | return out |
| 170 | } |
| 171 | |
| 172 | // resolver answers links for one scope. The caller holds a read lock. |
| 173 | type resolver struct { |
| 174 | index *Index |
| 175 | scope Scope |
| 176 | // page is the page being drawn, and images its images' rows |
| 177 | // (images_render.go): nil draws every image as not available. |
| 178 | page int64 |
| 179 | images *imageLookup |
| 180 | } |
| 181 | |
| 182 | // Resolve is §3.2 and §3.4: a date opens the reader's own daily note, a |
| 183 | // title or alias opens its team page, and anything else is pending. |
| 184 | // Titles are team pages only, so a resolution reveals nothing private: |
| 185 | // the current title it carries for a chip (§4.2) is a team page's. |
| 186 | func (answer resolver) Resolve(target, |
| 187 | anchor string) markup.Resolution { |
| 188 | if markup.IsDateShaped(target) { |
| 189 | if !markup.IsDate(target) { |
| 190 | return markup.Resolution{Broken: true} |
| 191 | } |
| 192 | return markup.Resolution{Href: "/d/" + target, |
| 193 | Day: answer.scope.Locale.Day(target, thisYear())} |
| 194 | } |
| 195 | pageID, ok := answer.index.titles[Fold(target)] |
| 196 | if !ok { |
| 197 | return markup.Resolution{Href: "/new?title=" + |
| 198 | url.QueryEscape(target), Pending: true} |
| 199 | } |
| 200 | page := answer.index.pages[pageID] |
| 201 | found := markup.Resolution{Href: "/p/" + publicID(page.nonce), |
| 202 | Title: page.record.Title, |
| 203 | Alias: Fold(target) != Fold(page.record.Title), |
| 204 | Icon: page.chipIcon()} |
| 205 | switch { |
| 206 | case anchor == "": |
| 207 | case page.hasMarker(anchor): |
| 208 | found.Href += "#b-" + anchor |
| 209 | default: |
| 210 | found.BlockGone = true |
| 211 | } |
| 212 | return found |
| 213 | } |
| 214 | |
| 215 | // thisYear is the year a date chip leaves out (§4.2). |
| 216 | func thisYear() int { return time.Now().Year() } |
| 217 | |
| 218 | func (page *entry) hasMarker(marker string) bool { |
| 219 | for _, block := range page.blocks { |
| 220 | if block.Marker == marker { |
| 221 | return true |
| 222 | } |
| 223 | } |
| 224 | return false |
| 225 | } |
| 226 | |
| 227 | // ordered sorts pages for a list: team pages by title, then the scope's |
| 228 | // own private pages, newest first. Nothing outside the scope is in the |
| 229 | // list, so nothing outside it can move anything in it. Each title is |
| 230 | // folded once, not once per comparison. |
| 231 | func (index *Index) ordered(ids map[int64]string) []*entry { |
| 232 | type keyed struct { |
| 233 | page *entry |
| 234 | fold string |
| 235 | } |
| 236 | pages := make([]keyed, 0, len(ids)) |
| 237 | for pageID := range ids { |
| 238 | page := index.pages[pageID] |
| 239 | pages = append(pages, keyed{page: page, fold: Fold(page.record.Title)}) |
| 240 | } |
| 241 | sort.Slice(pages, func(left, right int) bool { |
| 242 | one, other := pages[left], pages[right] |
| 243 | if one.page.team() != other.page.team() { |
| 244 | return one.page.team() |
| 245 | } |
| 246 | if one.page.team() { |
| 247 | return lessText(one.fold, other.fold, one.page.id, other.page.id) |
| 248 | } |
| 249 | if !one.page.updated.Equal(other.page.updated) { |
| 250 | return one.page.updated.After(other.page.updated) |
| 251 | } |
| 252 | return one.page.id < other.page.id |
| 253 | }) |
| 254 | out := make([]*entry, len(pages)) |
| 255 | for at, item := range pages { |
| 256 | out[at] = item.page |
| 257 | } |
| 258 | return out |
| 259 | } |
| 260 | |
| 261 | func lessText(one, other string, oneID, otherID int64) bool { |
| 262 | if one != other { |
| 263 | return one < other |
| 264 | } |
| 265 | return oneID < otherID |
| 266 | } |
| 267 | |