Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

vole
Clone

Read only, no credentials:

https://rodent.software/vole.git

Write over SSH with a registered key:

git clone ssh://git@rodent.software:2222/vole.git

A free and open-source forge that lives in your Git repo.

Reorganise the web package around one implementation per rule

c717d4eb92b4257b05fa065918b0071f6302f53e · Keenan Nemetz · 14d ago · parent 4506a4d3

Reorganise the web package around one implementation per rule

server/web had grown by feature, so several rules and widgets existed twice: the
comment stack for issues and change requests, the pager, the file tree, the write
preamble, and the ACL predicates. Each pair agreed today and would drift
tomorrow. No route, stored format, or permission rule changes; the few behaviour
differences found along the way are listed at the end.

Files now say what they answer:

  web.go          the Web type, render, open, the error pages
  page.go         Page, pageSection, who, nav
  templatefuncs.go the template function map, with the escaping rules noted
  ui.go           relative time, avatars, breadcrumbs, the pager
  markdown.go     the goldmark and bluemonday boundary
  codebrowser.go  the code browser, out of web.go
  tokens.go       the label/assignee token input
  rolepicker.go   the role control, out of admin.go
  comments.go     the comment view, thread builder, action menus, snippet
  comment_writes.go  add, edit and delete a comment, for both surfaces
  write_context.go   beginWrite/beginPost/beginAdminPost, fail, refuse
  capabilities.go    the ACL rules a page and a handler share
  audit.go        the audit log
  request_view.go the change conversation page, out of requests.go
  admin_access.go the instance-admin access rules, out of settings.go

A comment is one thing, so it has one view and one thread builder: metadata.Comment
is the same type on both surfaces and only a change request can anchor one to a
diff line. Adding, editing and deleting are one implementation each, with a
commentTopic naming what differs per surface. The pager is one control for issues,
changes and the instance feed; the file tree is one widget for the code browser
and a change's Files tab.

Every write starts at beginWrite (session, body bound, CSRF, repository, rules,
form) and asks capabilities.go for the one rule it needs, so the control a page
draws and the check a handler makes cannot disagree. A rejected write answers
through refuse; a write that fails for another reason answers through fail, which
re-renders the section for HTMX and returns text otherwise.

Templates follow the same split: chrome.html, components.html, diff.html and
role-picker.html hold what more than one surface renders, and each page owns its
fragments. The route table moved to routes.go, one function per surface.

Behaviour changes found while unifying:

  - A reply cycle (two comments answering each other, possible through a pushed
    metadata branch) used to drop both comments from the page; anything the walk
    does not reach is now emitted as a root.
  - A rejected anchored comment keeps its "Commenting on path:line" note and
    hidden anchor fields, so the reviewer does not have to pick the line again.
  - A repository whose access rules cannot be read answers 500 rather than 403;
    the account was being blamed for a broken instance.
  - Pager wording is now "Showing a-b of n / page x of y" with Previous/Next
    everywhere, and the instance feed's page links keep their kind filter.
  - The activity feed, issue list and change list share one page control.

Docs: docs/ui-plan.md gains a "Where the code lives" map, the write gate, the
shared comment model, the route table, and rules for rejected writes, shared
template fragments, and the stylesheet's section order. The passkey script is now
an ordinary asset (static/auth.js) under the same CSP as the rest.

server/web/page.go +96

@@ -0,0 +1,96 @@
1 package web
2
3 // The data every page and every fragment starts from: the document title, who is
4 // looking, the CSRF token, and which navigation item is current. A handler builds
5 // it with w.page, never by hand, so the chrome cannot disagree with the ACL that
6 // admitted the request.
7
8 import (
9 "strings"
10
11 "github.com/rodent-software/vole/forge/access"
12 )
13
14 // Page is embedded by every view. Exported so html/template can promote its
15 // fields; an unexported embedded type is not reachable from a template.
16 type Page struct {
17 Title string
18 Error string
19 LoggedIn bool
20 User string
21 AccountName string
22 // CSRF is derived from the session token and must be on every state-changing
23 // form or fetch.
24 CSRF string
25 // DevLogin names the account a loopback browser is signed in as. Empty on a
26 // normal instance; the header uses it to make the development state visible.
27 DevLogin string
28 // CanAdmin reports whether the viewer may administer the repository named in
29 // the request path. The repository header uses it to show Settings.
30 CanAdmin bool
31 // InstanceAdmin reports whether the viewer may use the instance-wide admin
32 // surface. The global bar uses it to show Admin.
33 InstanceAdmin bool
34 // Query is the repository search on the index page. The global search box
35 // echoes it; other pages leave it empty.
36 Query string
37 // Section is the global destination the current page belongs to, used by the
38 // rail to mark the current item. It is presentation only.
39 Section string
40 }
41
42 // pageSection maps a request path to the global destination it belongs to. A
43 // repository page belongs to Repositories: the repository's own tabs carry the
44 // finer context.
45 func pageSection(path string) string {
46 switch {
47 case path == "/" || path == "/repos/new" || strings.HasPrefix(path, "/r/"):
48 return "repos"
49 case strings.HasPrefix(path, "/activity"):
50 return "activity"
51 case path == "/users" || strings.HasPrefix(path, "/users/"):
52 return "users"
53 case strings.HasPrefix(path, "/account"):
54 return "account"
55 }
56 return ""
57 }
58
59 // who renders an account name for a page. It is a presentation helper only:
60 // nothing is authorized by what this returns.
61 func (w *Web) who(name string) string {
62 if w.Ident == nil || name == "" {
63 return name
64 }
65 a, err := w.Ident.Find(name)
66 if err != nil {
67 return name
68 }
69 return a.Name
70 }
71
72 // repoNav is the repository header's data: which repository, which tab is
73 // current, and whether anonymous readers can see it. Visibility comes from the
74 // same ACL rule the handlers use, so the lock/globe icon cannot disagree with
75 // whether an anonymous clone works. Templates call it as
76 // {{template "repohead" (nav .Name "issues")}}.
77 func (w *Web) repoNav(name, tab string, canAdmin bool) repoNavData {
78 n := repoNavData{Name: name, Tab: tab, Private: true, CanAdmin: canAdmin}
79 m, err := w.Repos.Open(name)
80 if err != nil {
81 return n
82 }
83 n.Description = m.Description
84 if acl, err := m.ACL(); err == nil {
85 n.Private = !acl.Can("", access.Read)
86 }
87 return n
88 }
89
90 type repoNavData struct {
91 Name string
92 Description string
93 Tab string
94 Private bool
95 CanAdmin bool
96 }