| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "sort" |
| 6 | |
| 7 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 8 | ) |
| 9 | |
| 10 | // Suggestion is one [[ autocomplete answer: a team title or alias, or |
| 11 | // a pending link text in the viewer's scope. |
| 12 | type Suggestion struct { |
| 13 | Text string `json:"text"` |
| 14 | Pending bool `json:"pending"` |
| 15 | } |
| 16 | |
| 17 | // pendingRow is one pending link text: typed, and named by no page. |
| 18 | type pendingRow struct { |
| 19 | fold, text string |
| 20 | words []string |
| 21 | } |
| 22 | |
| 23 | // pendingSet is the pending texts, partitioned as everything else is: |
| 24 | // the team pages', and each person's own. Rebuilt after a change. |
| 25 | type pendingSet struct { |
| 26 | team []pendingRow |
| 27 | own map[int64][]pendingRow |
| 28 | dirty bool |
| 29 | } |
| 30 | |
| 31 | // Suggest is [[ autocomplete (§6.3): team titles and aliases, then the |
| 32 | // pending link texts of team pages and the viewer's own, never anyone |
| 33 | // else's: their rows are never read. |
| 34 | func (index *Index) Suggest(ctx context.Context, scope Scope, query string, |
| 35 | limit int) ([]Suggestion, error) { |
| 36 | if err := index.Sync(ctx); err != nil { |
| 37 | return nil, err |
| 38 | } |
| 39 | index.mutex.RLock() |
| 40 | defer index.mutex.RUnlock() |
| 41 | tokens := queryWords(query) |
| 42 | var out []Suggestion |
| 43 | seen := map[string]bool{} |
| 44 | for _, row := range index.titleRows { |
| 45 | if len(out) == limit { |
| 46 | return out, nil |
| 47 | } |
| 48 | if startsWords(row.words, tokens) && !seen[row.fold] { |
| 49 | seen[row.fold] = true |
| 50 | out = append(out, Suggestion{Text: row.text}) |
| 51 | } |
| 52 | } |
| 53 | for _, row := range index.pendingRows(scope) { |
| 54 | if len(out) == limit { |
| 55 | break |
| 56 | } |
| 57 | if startsWords(row.words, tokens) && !seen[row.fold] { |
| 58 | seen[row.fold] = true |
| 59 | out = append(out, Suggestion{Text: row.text, Pending: true}) |
| 60 | } |
| 61 | } |
| 62 | return out, nil |
| 63 | } |
| 64 | |
| 65 | // pendingRows is the team's pending texts, then the scope's own, sorted |
| 66 | // within each. The caller holds the read lock. |
| 67 | func (index *Index) pendingRows(scope Scope) []pendingRow { |
| 68 | rows := index.pending.team |
| 69 | if scope.Member != 0 { |
| 70 | rows = append(rows[:len(rows):len(rows)], |
| 71 | index.pending.own[scope.Member]...) |
| 72 | } |
| 73 | return rows |
| 74 | } |
| 75 | |
| 76 | // rebuildPending recomputes the pending rows from the link texts. The |
| 77 | // caller holds the write lock. |
| 78 | func (index *Index) rebuildPending() { |
| 79 | team := []pendingRow{} |
| 80 | own := map[int64][]pendingRow{} |
| 81 | for fold, found := range index.linkers { |
| 82 | if _, titled := index.titles[fold]; titled || |
| 83 | markup.IsDateShaped(fold) { |
| 84 | continue |
| 85 | } |
| 86 | words := folds(fold) |
| 87 | if text := shownText(found.team); text != "" { |
| 88 | team = append(team, pendingRow{fold, text, words}) |
| 89 | continue |
| 90 | } |
| 91 | for owner, texts := range found.private { |
| 92 | own[owner] = append(own[owner], |
| 93 | pendingRow{fold, shownText(texts), words}) |
| 94 | } |
| 95 | } |
| 96 | sortRows(team) |
| 97 | for _, rows := range own { |
| 98 | sortRows(rows) |
| 99 | } |
| 100 | index.pending = pendingSet{team: team, own: own} |
| 101 | } |
| 102 | |
| 103 | func sortRows(rows []pendingRow) { |
| 104 | sort.Slice(rows, func(left, right int) bool { |
| 105 | return rows[left].fold < rows[right].fold |
| 106 | }) |
| 107 | } |
| 108 | |
| 109 | // Pending is the link texts in scope that name no page: team pages' and |
| 110 | // the viewer's own, never anyone else's (§6.3). |
| 111 | func (index *Index) Pending(ctx context.Context, |
| 112 | scope Scope) ([]string, error) { |
| 113 | if err := index.Sync(ctx); err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | index.mutex.RLock() |
| 117 | defer index.mutex.RUnlock() |
| 118 | var out []string |
| 119 | for _, row := range index.pendingRows(scope) { |
| 120 | out = append(out, row.text) |
| 121 | } |
| 122 | return out, nil |
| 123 | } |
| 124 | |