oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "context"
5 "errors"
6
7 "amadan.net/oficina/mapa/internal/mapa/emoji"
8 "amadan.net/oficina/mapa/internal/mapa/markup"
9)
10
11// A team page's look: its icon and its banner (columns and icons spec
12// §2, §3, §4). Both live in the sealed record, so nothing here is
13// plaintext. Changing them mints a revision holding the same body,
14// logged styled, as a rename's does (§4.1).
15
16// VerbStyled is a change to a team page's icon or banner (§4.1).
17const VerbStyled = "styled"
18
19// Icon is an emoji or a Mapa image, never both.
20type Icon struct {
21 Emoji string `json:"emoji,omitempty"`
22 Image string `json:"image,omitempty"`
23}
24
25// Banner is a picture and its vertical focal position, 0 (top) to 100
26// (bottom) in steps of 5.
27type Banner struct {
28 Image string `json:"image"`
29 Y int `json:"y"`
30}
31
32// The icon's limits (§2.3): it is drawn at 16 to 40 CSS pixels and is
33// fetched wherever a title is, so it is small.
34const (
35 MaxIconBytes = 128 << 10
36 MaxIconSide = 512
37)
38
39// ErrIconEmoji is an icon that is not one RGI emoji (§2.2).
40var ErrIconEmoji = errors.New("Choose one emoji or upload a picture.")
41
42// ErrIconTooLarge is a picture over the icon's limits (§2.3).
43var ErrIconTooLarge = errors.New("mapa: too large for an icon")
44
45// images is every image the record references: its body's addresses,
46// its icon's and its banner's (§4.2).
47func (record Record) images() map[string]bool {
48 named := markup.ImageIDs(record.Body)
49 if record.Icon != nil && record.Icon.Image != "" {
50 named[record.Icon.Image] = true
51 }
52 if record.Banner != nil && record.Banner.Image != "" {
53 named[record.Banner.Image] = true
54 }
55 return named
56}
57
58// EmojiIcon is text as an emoji icon, or ErrIconEmoji.
59func EmojiIcon(text string) (*Icon, error) {
60 canonical, ok := emoji.Canonical(text)
61 if !ok {
62 return nil, ErrIconEmoji
63 }
64 return &Icon{Emoji: canonical}, nil
65}
66
67// BannerY is a stored position: clamped to 0–100, to the nearest 5.
68func BannerY(position int) int {
69 position = min(max(position, 0), 100)
70 return (position + 2) / 5 * 5
71}
72
73// LookChange is what one look post asks for. A field left unset is
74// kept; a set one replaces the page's, nil removing it.
75type LookChange struct {
76 SetIcon bool
77 Icon *Icon
78 SetBanner bool
79 Banner *Banner
80}
81
82// Restyled is what a look post did: the revision it was made over and
83// the one it minted, so the editor follows its base as it follows a
84// rename's (§4.1). Both are 0 when it changed nothing and no styled or
85// renamed revision is the head.
86type Restyled struct {
87 From, To int64
88}
89
90// SetLook changes a team page's look in one write (§4.1).
91func (store *Store) SetLook(ctx context.Context, who string, pageID int64,
92 change LookChange) (Restyled, error) {
93 var done Restyled
94 err := quiet(store.write(ctx, func(tx *Tx) error {
95 actor, err := tx.member(who)
96 if err != nil {
97 return err
98 }
99 page, record, err := tx.teamPage(actor, pageID)
100 if err != nil {
101 return err
102 }
103 next := record.restyled(change)
104 if sameLook(record, next) {
105 if done.From, done.To, err = tx.renamedTo(page); err != nil {
106 return err
107 }
108 return errNoChange
109 }
110 if err := tx.checkLookImages(page, record, next, actor); err != nil {
111 return err
112 }
113 done.From = page.Revision
114 err = tx.commit(&page, next, actor, VerbStyled, attachOwn(0))
115 if err != nil {
116 return err
117 }
118 done.To = page.Revision
119 return tx.carryDrafts(page.ID, done.From, done.To)
120 }))
121 return done, err
122}
123
124// restyled is record with change applied.
125func (record Record) restyled(change LookChange) Record {
126 if change.SetIcon {
127 record.Icon = change.Icon
128 if record.Icon != nil && *record.Icon == (Icon{}) {
129 record.Icon = nil
130 }
131 }
132 if change.SetBanner {
133 record.Banner = movedBanner(record.Banner, change.Banner)
134 }
135 return record
136}
137
138func sameLook(one, other Record) bool {
139 return samePointer(one.Icon, other.Icon) &&
140 samePointer(one.Banner, other.Banner)
141}
142
143func samePointer[T comparable](one, other *T) bool {
144 if one == nil || other == nil {
145 return one == other
146 }
147 return *one == *other
148}
149
150// movedBanner is the banner a change asks for: next, or, when next
151// names no picture (a Reposition), the current one at next's position.
152func movedBanner(current, next *Banner) *Banner {
153 if next == nil {
154 return nil
155 }
156 moved := *next
157 if moved.Image == "" {
158 if current == nil {
159 return nil
160 }
161 moved.Image = current.Image
162 }
163 moved.Y = BannerY(moved.Y)
164 return &moved
165}
166