| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "sort" |
| 7 | "strings" |
| 8 | |
| 9 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 10 | ) |
| 11 | |
| 12 | // ErrNothingChosen is "Make team page" with no blocks selected. |
| 13 | var ErrNothingChosen = errors.New("Choose at least one block to share.") |
| 14 | |
| 15 | // ErrCutsLayout is a choice that would cut a column layout: blocks must |
| 16 | // be whole layouts or lie inside one column (columns and icons §1.10). |
| 17 | var ErrCutsLayout = errors.New("Select whole columns or blocks inside " + |
| 18 | "one column.") |
| 19 | |
| 20 | // ErrNoteChanged is "Make team page" over a revision that is no longer |
| 21 | // the note's: the chosen block numbers named that revision's blocks, so |
| 22 | // they are not used (level-up §6.7). |
| 23 | var ErrNoteChanged = errors.New("Your note changed since these blocks " + |
| 24 | "were shown. Choose them again.") |
| 25 | |
| 26 | // MakeTeamPage shares private writing (§3.4, decision 8): the chosen |
| 27 | // blocks of one of who's daily or scratch notes become a NEW team page, |
| 28 | // at its first revision, and the note gets a link to it in their place. |
| 29 | // The note itself is never re-ruled, so no private-era revision ever |
| 30 | // becomes team-readable. A taken or date-shaped title asks for another |
| 31 | // (gate 2). revision is the note's revision the block numbers index; |
| 32 | // a stale one is refused with ErrNoteChanged and nothing is written. |
| 33 | func (store *Store) MakeTeamPage(ctx context.Context, who string, |
| 34 | sourceID, revision int64, chosen []int, rawTitle string) (Page, |
| 35 | error) { |
| 36 | title, err := CleanTitle(rawTitle) |
| 37 | if err != nil { |
| 38 | return Page{}, err |
| 39 | } |
| 40 | // The images the moved blocks hold, attached to the note, are copied |
| 41 | // before the transaction: no S3 call holds SQLite's writer (§2.4). |
| 42 | made, err := store.teamCopies(ctx, who, sourceID, revision, chosen, |
| 43 | title) |
| 44 | if err != nil { |
| 45 | return Page{}, err |
| 46 | } |
| 47 | var shared Page |
| 48 | err = store.write(ctx, func(tx *Tx) error { |
| 49 | var err error |
| 50 | shared, err = tx.makeTeamPage(who, sourceID, revision, chosen, title, |
| 51 | made) |
| 52 | return err |
| 53 | }) |
| 54 | if err != nil { |
| 55 | store.dropCopies(ctx, byAddress(who), made) |
| 56 | } |
| 57 | return shared, err |
| 58 | } |
| 59 | |
| 60 | // makeTeamPage is MakeTeamPage's write: the source re-checked at its |
| 61 | // revision, the new page holding the moved blocks with the copies' |
| 62 | // addresses, and the note with a link in their place. |
| 63 | func (tx *Tx) makeTeamPage(who string, sourceID, revision int64, |
| 64 | chosen []int, title string, made copied) (Page, error) { |
| 65 | actor, err := tx.member(who) |
| 66 | if err != nil { |
| 67 | return Page{}, err |
| 68 | } |
| 69 | source, err := tx.visiblePage(actor, sourceID) |
| 70 | if err != nil || source.Rung != RungPrivate { |
| 71 | return Page{}, errors.Join(ErrNotFound, err) |
| 72 | } |
| 73 | if source.Revision != revision { |
| 74 | return Page{}, ErrNoteChanged |
| 75 | } |
| 76 | record, err := tx.store.OpenPage(source) |
| 77 | if err != nil { |
| 78 | return Page{}, err |
| 79 | } |
| 80 | kept, moved, err := splitBlocks(record.Body, chosen, title) |
| 81 | if err != nil { |
| 82 | return Page{}, err |
| 83 | } |
| 84 | shared, err := tx.create(KindPage, RungTeam, actor, |
| 85 | Record{Title: title, Body: made.rewrite(moved)}, actor, VerbCreated, |
| 86 | attachOwn(0)) |
| 87 | if err != nil { |
| 88 | return Page{}, err |
| 89 | } |
| 90 | if err := tx.claimTitle(shared.ID, title); err != nil { |
| 91 | return Page{}, err |
| 92 | } |
| 93 | record.Body = kept |
| 94 | return shared, tx.commit(&source, record, actor, VerbEdited, |
| 95 | attachOwn(0)) |
| 96 | } |
| 97 | |
| 98 | // splitBlocks takes the chosen blocks out of body: what the new page |
| 99 | // holds, and the body left behind with a link where the first chosen |
| 100 | // block was. |
| 101 | func splitBlocks(body string, chosen []int, |
| 102 | title string) (string, string, error) { |
| 103 | blocks := markup.Parse(body) |
| 104 | picked := map[int]bool{} |
| 105 | for _, number := range chosen { |
| 106 | if number >= 0 && number < len(blocks) { |
| 107 | picked[number] = true |
| 108 | } |
| 109 | } |
| 110 | widenLayouts(blocks, picked) |
| 111 | if len(picked) == 0 { |
| 112 | return "", "", ErrNothingChosen |
| 113 | } |
| 114 | if !markup.WholeOrInside(blocks, picked) { |
| 115 | return "", "", ErrCutsLayout |
| 116 | } |
| 117 | order := make([]int, 0, len(picked)) |
| 118 | for number := range picked { |
| 119 | order = append(order, number) |
| 120 | } |
| 121 | sort.Ints(order) |
| 122 | var moving []markup.Block |
| 123 | for _, number := range order { |
| 124 | moving = append(moving, blocks[number]) |
| 125 | } |
| 126 | kept := withoutBlocks(markup.Normalise(body), blocks, picked, |
| 127 | "[["+title+"]]") |
| 128 | return cleanBody(kept), cleanBody(markup.Join(moving)), nil |
| 129 | } |
| 130 | |
| 131 | // withoutBlocks is body with the picked blocks taken out, each with the |
| 132 | // blank lines after it, and link in the place of the first. Every other |
| 133 | // byte stays as it was. |
| 134 | func withoutBlocks(body string, blocks []markup.Block, |
| 135 | picked map[int]bool, link string) string { |
| 136 | var out strings.Builder |
| 137 | cursor, linked := 0, false |
| 138 | for number, block := range blocks { |
| 139 | if !picked[number] { |
| 140 | continue |
| 141 | } |
| 142 | out.WriteString(body[cursor:block.Start]) |
| 143 | if !linked { |
| 144 | out.WriteString(link + "\n\n") |
| 145 | linked = true |
| 146 | } |
| 147 | cursor = len(body) |
| 148 | if number+1 < len(blocks) { |
| 149 | cursor = blocks[number+1].Start |
| 150 | } |
| 151 | } |
| 152 | out.WriteString(body[cursor:]) |
| 153 | return out.String() |
| 154 | } |
| 155 | |
| 156 | // widenLayouts picks the whole of every layout whose opener is picked: |
| 157 | // the scriptless list offers a layout as one row (columns and icons |
| 158 | // spec §1.10). |
| 159 | func widenLayouts(blocks []markup.Block, picked map[int]bool) { |
| 160 | for number := range blocks { |
| 161 | if !picked[number] || blocks[number].Kind != markup.Columns { |
| 162 | continue |
| 163 | } |
| 164 | for end := number; end < len(blocks); end++ { |
| 165 | picked[end] = true |
| 166 | if blocks[end].Kind == markup.ColumnsEnd { |
| 167 | break |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |