// Package blog is Jae Kwon's personal blog on gno.land, built on // gno.land/p/gnoland/blog/v0, the package behind gno.land's own blog. // // Who can do what: // - The owner (Jae's Ledger key) decides who may write, and can hand the // blog to a new owner in two steps, so a typo can't lose it. // - Writers and the owner add, edit and remove posts. // - Everyone else can only read. // // What this realm adds on top of the package: // - Render answers only the blog's own routes, with slugs and tags limited // to a-z, 0-9, '-' and '_', so nothing typed into a URL is echoed into // the page as markdown (the package's tag page echoes it). // - Edit removes the old post and adds it back, so the home page and the // title index never keep a stale copy (the package's in-place edit does). // - No comments, and nothing hands out a pointer to the blog or a post. package blog import ( "strings" "time" gnoblog "gno.land/p/gnoland/blog/v0" "gno.land/p/nt/avl/v0" ) var ( // owner is Jae's cold (Ledger) key. owner = address("g1ecsuj0q572jr0dhu29q9njtnmw03hyu7tyyvv6") // pendingOwner, when set, may accept ownership. pendingOwner address // writers may add, edit and remove posts: address string -> true. writers avl.Tree b = &gnoblog.Blog{ Title: "Jae Kwon", Prefix: "/r/nym-nobody777/blog:", } ) func init() { // The hot key that deploys this realm and holds the nym-nobody777 name. writers.Set("g1e7ct57zwke7z4l96gl25w5l0f34ljnn37gtp3w", true) } // Post publishes a new post. pubDate is RFC3339, or empty for the block time. // authors and tags are comma-separated. func Post(cur realm, slug, title, body, pubDate, authors, tags string) { caller := mustBeWriter(cur) mustSlug(slug) tagList := mustTags(tags) checkErr(b.NewPost(caller, slug, title, body, pubDate, splitList(authors), tagList)) } // Edit replaces the post at slug. An empty pubDate keeps the old date. func Edit(cur realm, slug, title, body, pubDate, authors, tags string) { caller := mustBeWriter(cur) tagList := mustTags(tags) old := b.GetPost(slug) if old == nil { panic(gnoblog.ErrNoSuchPost) } if pubDate == "" { pubDate = old.CreatedAt.Format(time.RFC3339) } // Remove, then add back: if the new post is refused, the whole call // fails and the old post stays. b.RemovePost(slug) checkErr(b.NewPost(caller, slug, title, body, pubDate, splitList(authors), tagList)) } // Remove deletes the post at slug. func Remove(cur realm, slug string) { mustBeWriter(cur) if b.GetPost(slug) == nil { panic(gnoblog.ErrNoSuchPost) } b.RemovePost(slug) } // AddWriter lets addr add, edit and remove posts. Owner only. func AddWriter(cur realm, addr address) { mustBeOwner(cur) if !addr.IsValid() { panic("invalid address") } writers.Set(addr.String(), true) } // RemoveWriter takes writing rights away from addr. Owner only. func RemoveWriter(cur realm, addr address) { mustBeOwner(cur) if _, removed := writers.Remove(addr.String()); !removed { panic("not a writer") } } // ProposeOwner names a new owner, who must then call AcceptOwnership. // Owner only. Proposing the empty address cancels a pending proposal. func ProposeOwner(cur realm, addr address) { mustBeOwner(cur) if addr != "" && !addr.IsValid() { panic("invalid address") } pendingOwner = addr } // AcceptOwnership completes a handover started with ProposeOwner. func AcceptOwnership(cur realm) { caller := mustUserCall(cur) if pendingOwner == "" || caller != pendingOwner { panic("not the proposed owner") } owner = caller pendingOwner = "" } // Owner returns the blog's owner. func Owner() address { return owner } // PendingOwner returns the proposed next owner, if any. func PendingOwner() address { return pendingOwner } // IsWriter reports whether addr may add, edit and remove posts. func IsWriter(addr address) bool { return addr == owner || writers.Has(addr.String()) } // Render shows the blog. Only "", "p/" and "t/" are answered; // anything else gets "404", so nothing from the URL reaches the page. func Render(path string) string { if !isRoute(path) { return "404" } return b.Render(path) } func isRoute(path string) bool { if path == "" { return true } if strings.HasPrefix(path, "p/") || strings.HasPrefix(path, "t/") { return isSlug(path[2:]) } return false } // isSlug reports whether s is 1 to 100 characters of a-z, 0-9, '-' or '_'. func isSlug(s string) bool { if len(s) == 0 || len(s) > 100 { return false } for i := 0; i < len(s); i++ { c := s[i] if !(c >= 'a' && c <= 'z' || c >= '0' && c <= '9' || c == '-' || c == '_') { return false } } return true } func mustSlug(s string) { if !isSlug(s) { panic("slug must be 1 to 100 characters of a-z, 0-9, - or _") } } func mustTags(tags string) []string { list := splitList(tags) for _, t := range list { if !isSlug(t) { panic("tags must be 1 to 100 characters of a-z, 0-9, - or _") } } return list } // splitList splits a comma-separated list, trimming spaces and dropping // empty entries. func splitList(s string) []string { var out []string for _, part := range strings.Split(s, ",") { if part = strings.TrimSpace(part); part != "" { out = append(out, part) } } return out } func mustUserCall(cur realm) address { if !cur.IsCurrent() { panic("spoofed realm") } prev := cur.Previous() if !prev.IsUserCall() { panic("must be called directly by a user") } return prev.Address() } func mustBeOwner(cur realm) address { caller := mustUserCall(cur) if caller != owner { panic("unauthorized: owner only") } return caller } func mustBeWriter(cur realm) address { caller := mustUserCall(cur) if !IsWriter(caller) { panic("unauthorized: writers only") } return caller } func checkErr(err error) { if err != nil { panic(err) } }