blog.gno
5.69 Kb · 223 lines
1// Package blog is Jae Kwon's personal blog on gno.land, built on
2// gno.land/p/gnoland/blog/v0, the package behind gno.land's own blog.
3//
4// Who can do what:
5// - The owner (Jae's Ledger key) decides who may write, and can hand the
6// blog to a new owner in two steps, so a typo can't lose it.
7// - Writers and the owner add, edit and remove posts.
8// - Everyone else can only read.
9//
10// What this realm adds on top of the package:
11// - Render answers only the blog's own routes, with slugs and tags limited
12// to a-z, 0-9, '-' and '_', so nothing typed into a URL is echoed into
13// the page as markdown (the package's tag page echoes it).
14// - Edit removes the old post and adds it back, so the home page and the
15// title index never keep a stale copy (the package's in-place edit does).
16// - No comments, and nothing hands out a pointer to the blog or a post.
17package blog
18
19import (
20 "strings"
21 "time"
22
23 gnoblog "gno.land/p/gnoland/blog/v0"
24 "gno.land/p/nt/avl/v0"
25)
26
27var (
28 // owner is Jae's cold (Ledger) key.
29 owner = address("g1ecsuj0q572jr0dhu29q9njtnmw03hyu7tyyvv6")
30
31 // pendingOwner, when set, may accept ownership.
32 pendingOwner address
33
34 // writers may add, edit and remove posts: address string -> true.
35 writers avl.Tree
36
37 b = &gnoblog.Blog{
38 Title: "Jae Kwon",
39 Prefix: "/r/nym-nobody777/blog:",
40 }
41)
42
43func init() {
44 // The hot key that deploys this realm and holds the nym-nobody777 name.
45 writers.Set("g1e7ct57zwke7z4l96gl25w5l0f34ljnn37gtp3w", true)
46}
47
48// Post publishes a new post. pubDate is RFC3339, or empty for the block time.
49// authors and tags are comma-separated.
50func Post(cur realm, slug, title, body, pubDate, authors, tags string) {
51 caller := mustBeWriter(cur)
52 mustSlug(slug)
53 tagList := mustTags(tags)
54 checkErr(b.NewPost(caller, slug, title, body, pubDate, splitList(authors), tagList))
55}
56
57// Edit replaces the post at slug. An empty pubDate keeps the old date.
58func Edit(cur realm, slug, title, body, pubDate, authors, tags string) {
59 caller := mustBeWriter(cur)
60 tagList := mustTags(tags)
61 old := b.GetPost(slug)
62 if old == nil {
63 panic(gnoblog.ErrNoSuchPost)
64 }
65 if pubDate == "" {
66 pubDate = old.CreatedAt.Format(time.RFC3339)
67 }
68 // Remove, then add back: if the new post is refused, the whole call
69 // fails and the old post stays.
70 b.RemovePost(slug)
71 checkErr(b.NewPost(caller, slug, title, body, pubDate, splitList(authors), tagList))
72}
73
74// Remove deletes the post at slug.
75func Remove(cur realm, slug string) {
76 mustBeWriter(cur)
77 if b.GetPost(slug) == nil {
78 panic(gnoblog.ErrNoSuchPost)
79 }
80 b.RemovePost(slug)
81}
82
83// AddWriter lets addr add, edit and remove posts. Owner only.
84func AddWriter(cur realm, addr address) {
85 mustBeOwner(cur)
86 if !addr.IsValid() {
87 panic("invalid address")
88 }
89 writers.Set(addr.String(), true)
90}
91
92// RemoveWriter takes writing rights away from addr. Owner only.
93func RemoveWriter(cur realm, addr address) {
94 mustBeOwner(cur)
95 if _, removed := writers.Remove(addr.String()); !removed {
96 panic("not a writer")
97 }
98}
99
100// ProposeOwner names a new owner, who must then call AcceptOwnership.
101// Owner only. Proposing the empty address cancels a pending proposal.
102func ProposeOwner(cur realm, addr address) {
103 mustBeOwner(cur)
104 if addr != "" && !addr.IsValid() {
105 panic("invalid address")
106 }
107 pendingOwner = addr
108}
109
110// AcceptOwnership completes a handover started with ProposeOwner.
111func AcceptOwnership(cur realm) {
112 caller := mustUserCall(cur)
113 if pendingOwner == "" || caller != pendingOwner {
114 panic("not the proposed owner")
115 }
116 owner = caller
117 pendingOwner = ""
118}
119
120// Owner returns the blog's owner.
121func Owner() address { return owner }
122
123// PendingOwner returns the proposed next owner, if any.
124func PendingOwner() address { return pendingOwner }
125
126// IsWriter reports whether addr may add, edit and remove posts.
127func IsWriter(addr address) bool {
128 return addr == owner || writers.Has(addr.String())
129}
130
131// Render shows the blog. Only "", "p/<slug>" and "t/<tag>" are answered;
132// anything else gets "404", so nothing from the URL reaches the page.
133func Render(path string) string {
134 if !isRoute(path) {
135 return "404"
136 }
137 return b.Render(path)
138}
139
140func isRoute(path string) bool {
141 if path == "" {
142 return true
143 }
144 if strings.HasPrefix(path, "p/") || strings.HasPrefix(path, "t/") {
145 return isSlug(path[2:])
146 }
147 return false
148}
149
150// isSlug reports whether s is 1 to 100 characters of a-z, 0-9, '-' or '_'.
151func isSlug(s string) bool {
152 if len(s) == 0 || len(s) > 100 {
153 return false
154 }
155 for i := 0; i < len(s); i++ {
156 c := s[i]
157 if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-' || c == '_') {
158 return false
159 }
160 }
161 return true
162}
163
164func mustSlug(s string) {
165 if !isSlug(s) {
166 panic("slug must be 1 to 100 characters of a-z, 0-9, - or _")
167 }
168}
169
170func mustTags(tags string) []string {
171 list := splitList(tags)
172 for _, t := range list {
173 if !isSlug(t) {
174 panic("tags must be 1 to 100 characters of a-z, 0-9, - or _")
175 }
176 }
177 return list
178}
179
180// splitList splits a comma-separated list, trimming spaces and dropping
181// empty entries.
182func splitList(s string) []string {
183 var out []string
184 for _, part := range strings.Split(s, ",") {
185 if part = strings.TrimSpace(part); part != "" {
186 out = append(out, part)
187 }
188 }
189 return out
190}
191
192func mustUserCall(cur realm) address {
193 if !cur.IsCurrent() {
194 panic("spoofed realm")
195 }
196 prev := cur.Previous()
197 if !prev.IsUserCall() {
198 panic("must be called directly by a user")
199 }
200 return prev.Address()
201}
202
203func mustBeOwner(cur realm) address {
204 caller := mustUserCall(cur)
205 if caller != owner {
206 panic("unauthorized: owner only")
207 }
208 return caller
209}
210
211func mustBeWriter(cur realm) address {
212 caller := mustUserCall(cur)
213 if !IsWriter(caller) {
214 panic("unauthorized: writers only")
215 }
216 return caller
217}
218
219func checkErr(err error) {
220 if err != nil {
221 panic(err)
222 }
223}