| 1 | package mapa |
| 2 | |
| 3 | import "context" |
| 4 | |
| 5 | // Which images a look may name (columns and icons spec §4.2), checked |
| 6 | // before the write: the setter's own pending image, which the commit |
| 7 | // attaches, or an image already attached to this page. Anything else is |
| 8 | // the app's 404, as an id that does not exist, and nothing is written. |
| 9 | |
| 10 | // checkLookImages refuses a look naming an image it may not. |
| 11 | func (tx *Tx) checkLookImages(page Page, before, after Record, |
| 12 | actor int64) error { |
| 13 | if after.Icon != nil && after.Icon.Image != "" && |
| 14 | !sameImage(before.Icon, after.Icon.Image) { |
| 15 | if err := tx.lookImage(page, after.Icon.Image, actor, |
| 16 | true); err != nil { |
| 17 | return err |
| 18 | } |
| 19 | } |
| 20 | if after.Banner != nil && (before.Banner == nil || |
| 21 | before.Banner.Image != after.Banner.Image) { |
| 22 | return tx.lookImage(page, after.Banner.Image, actor, false) |
| 23 | } |
| 24 | return nil |
| 25 | } |
| 26 | |
| 27 | func sameImage(icon *Icon, image string) bool { |
| 28 | return icon != nil && icon.Image == image |
| 29 | } |
| 30 | |
| 31 | // lookImage checks one image: its row is the actor's pending image or |
| 32 | // on this page, and, for an icon, within the icon's limits. |
| 33 | func (tx *Tx) lookImage(page Page, public string, actor int64, |
| 34 | icon bool) error { |
| 35 | var row ImageRow |
| 36 | err := tx.db.Where("public_id = ? AND ((page_id IS NULL AND "+ |
| 37 | "owner_id = ?) OR page_id = ?)", public, actor, page.ID). |
| 38 | Limit(1).Find(&row).Error |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | if row.ID == 0 { |
| 43 | return ErrNotFound |
| 44 | } |
| 45 | if !icon { |
| 46 | return nil |
| 47 | } |
| 48 | record, err := tx.store.openImageRecord(row) |
| 49 | if err != nil { |
| 50 | return err |
| 51 | } |
| 52 | if record.Size > MaxIconBytes || record.Width > MaxIconSide || |
| 53 | record.Height > MaxIconSide { |
| 54 | return ErrIconTooLarge |
| 55 | } |
| 56 | return nil |
| 57 | } |
| 58 | |
| 59 | // dropFromLook takes a removed image out of its page's look, in the |
| 60 | // removal's own write: a styled revision by the person removing it, so |
| 61 | // a page's current record never names an image that is gone, and the |
| 62 | // icon draws as the page glyph and the band is not drawn (columns and |
| 63 | // icons spec §4.3). |
| 64 | func (tx *Tx) dropFromLook(row ImageRow, actor int64) error { |
| 65 | if row.PageID == nil { |
| 66 | return nil |
| 67 | } |
| 68 | var page Page |
| 69 | if err := tx.db.First(&page, *row.PageID).Error; err != nil { |
| 70 | return err |
| 71 | } |
| 72 | if page.Kind != KindPage { |
| 73 | return nil |
| 74 | } |
| 75 | record, err := tx.store.OpenPage(page) |
| 76 | if err != nil { |
| 77 | return err |
| 78 | } |
| 79 | next := record |
| 80 | if sameImage(record.Icon, row.PublicID) { |
| 81 | next.Icon = nil |
| 82 | } |
| 83 | if record.Banner != nil && record.Banner.Image == row.PublicID { |
| 84 | next.Banner = nil |
| 85 | } |
| 86 | if sameLook(record, next) { |
| 87 | return nil |
| 88 | } |
| 89 | from := page.Revision |
| 90 | if err := tx.commit(&page, next, actor, VerbStyled, |
| 91 | attachNothing); err != nil { |
| 92 | return err |
| 93 | } |
| 94 | return tx.carryDrafts(page.ID, from, page.Revision) |
| 95 | } |
| 96 | |
| 97 | // DropOwnPending removes a picture the look refused, when it is still |
| 98 | // the person's own pending image, so it holds no allowance for seven |
| 99 | // days. An image already attached anywhere is never touched here. |
| 100 | func (store *Store) DropOwnPending(ctx context.Context, who, |
| 101 | public string) error { |
| 102 | var keys []string |
| 103 | err := quiet(store.write(ctx, func(tx *Tx) error { |
| 104 | actor, err := tx.member(who) |
| 105 | if err != nil { |
| 106 | return err |
| 107 | } |
| 108 | var row ImageRow |
| 109 | err = tx.db.Where("public_id = ? AND page_id IS NULL AND "+ |
| 110 | "owner_id = ?", public, actor).Limit(1).Find(&row).Error |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | if row.ID == 0 { |
| 115 | return errNoChange |
| 116 | } |
| 117 | keys, err = tx.removeImages(actor, []ImageRow{row}) |
| 118 | return err |
| 119 | })) |
| 120 | if err == nil { |
| 121 | store.emptyTrash(ctx, keys) |
| 122 | } |
| 123 | return err |
| 124 | } |
| 125 | |