| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | |
| 6 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 7 | ) |
| 8 | |
| 9 | // Drawing an image on a page (§3.1, §4.5). Rendering on page P draws |
| 10 | // /i/<id> as available only when the image is attached to P, or is |
| 11 | // pending and owned by the viewer, while its row exists: what a person |
| 12 | // sees in their own draft. Everything else is the one fixed box, |
| 13 | // "Image not available": a missing id, an image attached to another |
| 14 | // page, someone else's pending image, a removed one. So Bo's rendering |
| 15 | // of a team page is byte for byte the same whether or not an id in it |
| 16 | // names one of Ana's private images (IMG1). |
| 17 | |
| 18 | // imageEntry is what drawing needs of one image row. |
| 19 | type imageEntry struct { |
| 20 | page, owner int64 |
| 21 | width, height int |
| 22 | } |
| 23 | |
| 24 | // imageLookup reads image rows for one rendering, each at most once. |
| 25 | // A GET reads it; it never writes. |
| 26 | type imageLookup struct { |
| 27 | ctx context.Context |
| 28 | store *Store |
| 29 | rows map[string]*imageEntry |
| 30 | // remove makes each available image a link to its confirm page: |
| 31 | // History's rendered old revision (§3.3). |
| 32 | remove bool |
| 33 | } |
| 34 | |
| 35 | func (index *Index) imageLookup(ctx context.Context) *imageLookup { |
| 36 | return &imageLookup{ctx: ctx, store: index.store, |
| 37 | rows: map[string]*imageEntry{}} |
| 38 | } |
| 39 | |
| 40 | // fetch reads the rows ids name that are not read yet, in one query. |
| 41 | func (lookup *imageLookup) fetch(ids []string) { |
| 42 | var wanted []string |
| 43 | for _, id := range ids { |
| 44 | if _, done := lookup.rows[id]; !done && markup.IsImageID(id) { |
| 45 | wanted = append(wanted, id) |
| 46 | lookup.rows[id] = nil |
| 47 | } |
| 48 | } |
| 49 | if len(wanted) == 0 { |
| 50 | return |
| 51 | } |
| 52 | var rows []ImageRow |
| 53 | err := lookup.store.database.WithContext(lookup.ctx). |
| 54 | Where("public_id IN ?", wanted).Find(&rows).Error |
| 55 | if err != nil { |
| 56 | lookup.store.logger().Warn("mapa: reading images", "err", err) |
| 57 | return |
| 58 | } |
| 59 | for _, row := range rows { |
| 60 | record, err := lookup.store.openImageRecord(row) |
| 61 | if err != nil { |
| 62 | continue |
| 63 | } |
| 64 | entry := &imageEntry{owner: row.OwnerID, width: record.Width, |
| 65 | height: record.Height} |
| 66 | if row.PageID != nil { |
| 67 | entry.page = *row.PageID |
| 68 | } |
| 69 | lookup.rows[row.PublicID] = entry |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // fetchBody reads every image body names. |
| 74 | func (lookup *imageLookup) fetchBody(body string) *imageLookup { |
| 75 | lookup.fetch(sortedKeys(markup.ImageIDs(body))) |
| 76 | return lookup |
| 77 | } |
| 78 | |
| 79 | // answer is one image's state on page for scope. |
| 80 | func (lookup *imageLookup) answer(scope Scope, page int64, |
| 81 | id string) markup.ImageAnswer { |
| 82 | if lookup == nil { |
| 83 | return markup.ImageAnswer{} |
| 84 | } |
| 85 | lookup.fetch([]string{id}) |
| 86 | entry := lookup.rows[id] |
| 87 | if entry == nil { |
| 88 | return markup.ImageAnswer{} |
| 89 | } |
| 90 | shown := entry.page != 0 && entry.page == page |
| 91 | if entry.page == 0 { |
| 92 | shown = scope.Member != 0 && entry.owner == scope.Member |
| 93 | } |
| 94 | if !shown { |
| 95 | return markup.ImageAnswer{} |
| 96 | } |
| 97 | answer := markup.ImageAnswer{Available: true, Width: entry.width, |
| 98 | Height: entry.height} |
| 99 | if lookup.remove { |
| 100 | answer.Link = markup.ImagePrefix + id + "/remove" |
| 101 | } |
| 102 | return answer |
| 103 | } |
| 104 | |
| 105 | // Image is the resolver's image answer: on its page, for its scope. |
| 106 | func (answer resolver) Image(id string) markup.ImageAnswer { |
| 107 | return answer.images.answer(answer.scope, answer.page, id) |
| 108 | } |
| 109 | |
| 110 | // drawer is a resolver for one page and its images. |
| 111 | func (index *Index) drawer(ctx context.Context, scope Scope, page int64, |
| 112 | body string) resolver { |
| 113 | return resolver{index: index, scope: scope, page: page, |
| 114 | images: index.imageLookup(ctx).fetchBody(body)} |
| 115 | } |
| 116 | |