Skip to content

Commit a4439ab

Browse files
committed
refactor
1 parent f475a1e commit a4439ab

30 files changed

Lines changed: 736 additions & 449 deletions

‎README.md‎

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ Useful verification commands on the VPS:
5858

5959
```sh
6060
systemctl status feedback-service
61-
curl --fail --silent --show-error https://feedback-api.cpp.social/
61+
curl --fail --silent --show-error --compressed \
62+
'https://feedback-api.cpp.social/v1/sites/cpp-social/reactions?keys=feedback/example'
6263
```
6364

6465
## Site mappings and metadata
@@ -127,29 +128,35 @@ addresses.
127128
## Counter cache and discussion content
128129

129130
The service database keeps discussion identifiers and aggregate counters only.
130-
Discussion/comment bodies are never persisted or
131-
reused between requests, preventing deleted content from being served from a
132-
stale cache. Only concurrent reads for the same thread share an in-flight GitHub
133-
call.
131+
Discussion and comment bodies are not persisted. Filtered thread responses are
132+
held in a bounded ten-second memory cache keyed by discussion ID, so a recently
133+
deleted comment may remain visible briefly. Identical concurrent reads share an
134+
in-flight GitHub call.
135+
136+
This revision uses schema version 6. It does not migrate existing version 5
137+
databases. Remove the service's SQLite database files before starting this build;
138+
configured `known_discussions` are seeded again and counters refill from GitHub.
134139

135140
`cache_fresh_seconds` is the age at which a requested tracked counter needs an
136-
authoritative GitHub refresh. The default is five seconds. A batched request
141+
authoritative GitHub refresh. The default is 60 seconds. A batched request
137142
refreshes only stale resources; recently refreshed resources in the same request
138-
are served from SQLite. For example, if A was last refreshed 30 seconds ago and B
139-
three seconds ago, requesting `[A, B]` sends only A's discussion ID to GitHub and
143+
are served from SQLite. For example, if A was last refreshed 90 seconds ago and B
144+
30 seconds ago, requesting `[A, B]` sends only A's discussion ID to GitHub and
140145
returns B from SQLite in the same response.
141146

142147
`refresh_cooldown_seconds` is the minimum delay before retrying a refresh attempt
143148
for the same resource. It primarily prevents repeated GitHub calls after a failed
144149
or concurrent attempt. Concurrent requests also join the same in-flight site
145-
batch. The default is five seconds.
150+
batch. The default is 60 seconds.
146151

147152
`refresh_sweep_seconds` controls the low-priority full maintenance cycle. The
148153
default is 86400 seconds (daily). The service walks only discussions whose last
149154
authoritative snapshot is that old, in batches of 50 with pacing between full
150155
batches. Targeted requests and successful votes continue independently.
151156

152-
Browser mutations do not write speculative server values. The next successful
157+
Successful browser mutations update the displayed counters immediately, without
158+
submitting a second vote or trusting an unverified client count on the server.
159+
The next successful
153160
targeted or maintenance refresh replaces counters with GitHub's absolute counts.
154161
Counter responses use `no-cache`, so
155162
browsers revalidate with this service; that does not imply a GitHub request while

‎config/sites.example.toml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ default_category = "articles"
1919
discussion_body = "Discussion for [{title}]({url})."
2020

2121
# Optional cache tuning; these are the defaults.
22-
# cache_fresh_seconds = 5
23-
# refresh_cooldown_seconds = 5
22+
# cache_fresh_seconds = 60
23+
# refresh_cooldown_seconds = 60
2424
# refresh_sweep_seconds = 86400
2525
# max_batch_size = 100
2626
intents = ["votes", "reactions", "discussion", "comments", "answers", "polls", "authors", "moderation", "comment_reactions", "labels", "github_link"]

‎deploy/nginx-feedback.conf‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ location ~ ^/v1/sites/[a-z0-9-]+/(?:oauth/(?:authorize|exchange)|discussions/ens
3434
proxy_pass http://127.0.0.1:18080;
3535
}
3636

37-
location / {
37+
location ~ ^/v1/sites/[a-z0-9-]+/(?:reactions|discussion)$ {
3838
limit_req zone=feedback_general burst=30 nodelay;
3939
proxy_pass http://127.0.0.1:18080;
4040
}
41+
42+
location / {
43+
return 404;
44+
}

‎deploy/nixos-module.nix‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ in
6060

6161
services.nginx = mkIf cfg.proxy.enable {
6262
enable = mkDefault true;
63+
additionalModules = [ pkgs.nginxModules.brotli ];
6364
recommendedGzipSettings = mkDefault true;
6465
recommendedProxySettings = mkDefault true;
6566
appendHttpConfig = ''
@@ -73,6 +74,10 @@ in
7374
extraConfig = ''
7475
client_max_body_size 16k;
7576
client_body_timeout 5s;
77+
brotli on;
78+
brotli_comp_level 4;
79+
brotli_min_length 1024;
80+
brotli_types application/json application/problem+json text/plain;
7681
'';
7782
locations = {
7883
"~ ^/v1/sites/[a-z0-9-]+/(?:oauth/(?:authorize|exchange)|discussions/ensure)$" = {
@@ -84,7 +89,7 @@ in
8489
proxy_send_timeout 15s;
8590
'';
8691
};
87-
"/" = {
92+
"~ ^/v1/sites/[a-z0-9-]+/(?:reactions|discussion)$" = {
8893
proxyPass = "http://127.0.0.1:18080";
8994
extraConfig = ''
9095
limit_req zone=feedback_general burst=30 nodelay;
@@ -93,6 +98,9 @@ in
9398
proxy_send_timeout 15s;
9499
'';
95100
};
101+
"/" = {
102+
extraConfig = "return 404;";
103+
};
96104
};
97105
};
98106
};

‎docs/API.md‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,23 @@ is initiated only by user interaction. The browser then talks directly to
2020
GitHub for viewer state and mutations; the service never receives the user's
2121
GitHub token after exchange and provides no general user-token proxy.
2222

23+
## Permissions
24+
25+
| Principal | Permission used | Operation |
26+
| --- | --- | --- |
27+
| GitHub App installation | Discussions read | Find discussions, read threads, reactions, polls, labels, and author data |
28+
| GitHub App installation | Discussions write | Create a discussion after a signed creation grant |
29+
| Signed-in GitHub user | Discussions read/write on the target repository | Read viewer state; vote, react, comment, reply, edit, delete, and mark answers where GitHub permits |
30+
| Browser on an allowed site origin | Service read routes | Read public counters and threads without signing in |
31+
| Browser with OAuth state and PKCE verifier | Service OAuth routes | Exchange a code for a user token and creation grant |
32+
33+
The example requests only the target repository's Discussions permission through
34+
the GitHub App. GitHub still enforces repository roles for edits, deletion, and
35+
answer selection. A creation grant does not grant those GitHub privileges.
36+
Origin checks enforce browser CORS policy; non-browser callers can forge an
37+
`Origin` header, so public reads rely on bounded batches, short-lived caching,
38+
and GitHub request concurrency limits rather than origin as authentication.
39+
2340
## Configuration and intents
2441

2542
Every site declares `mode`, `intents`, one or more categories, and a default
@@ -37,7 +54,7 @@ in different categories should use category-qualified keys.
3754
| `reactions` | Main-post reaction groups selected by `reaction_counters` |
3855
| `discussion` | Authoritative thread read and discussion creation |
3956
| `comments` | Comments and replies |
40-
| `answers` | Accepted-answer state |
57+
| `answers` | Selected-answer state (`isAnswer`); GitHub GraphQL does not expose the verified-answer badge |
4158
| `polls` | Poll question, options, and totals |
4259
| `authors` | Author identity and `authorAssociation` |
4360
| `moderation` | Moderation reason for visible comments; minimized comments are always excluded |
@@ -62,6 +79,20 @@ All JSON responses contain `v: 1`. Errors are
6279
| `POST` | `/v1/sites/{site}/discussions/ensure` | Find or create a thread for a validated resource and grant |
6380
| `GET` | `/v1/sites/{site}/discussion?keys=a,b` | Fetch up to ten authoritative configured threads in one GitHub `nodes` query |
6481

82+
`OPTIONS` is registered explicitly for the reaction and POST routes. It checks
83+
the site origin and returns the allowed method and headers. `HEAD` is disabled
84+
because a nominally cheap metadata request could otherwise trigger a GitHub read.
85+
86+
The route map lives in `src/feedback/api/routes.py`. Handlers in
87+
`src/feedback/api/endpoints/` handle HTTP validation and responses; shared site
88+
authorization is in `src/feedback/api/context.py`. Counter shaping and GitHub
89+
operations live under `src/feedback/service/` and `src/feedback/protocol/`.
90+
91+
The service registers only these API paths. `/` and documentation/schema paths
92+
return 404. The production reverse proxy rejects all other paths before they
93+
reach the service. It offers Brotli for eligible JSON responses when the client
94+
sends `Accept-Encoding: br`; small responses are left uncompressed.
95+
6596
A minimal counter response is:
6697

6798
```json
@@ -72,6 +103,8 @@ A minimal counter response is:
72103
`reaction_counters` is non-empty. Cache age and server internals are not exposed.
73104
Unknown resources have `id: null` and zero counts. Counter responses revalidate
74105
with an ETag. Discussion and OAuth responses are `no-store`.
106+
Thread reads currently include the first 25 top-level comments and the first
107+
25 replies under each; pagination is not exposed by this API.
75108

76109
## Browser API
77110

@@ -87,6 +120,12 @@ viewer-reaction state. A discussion UI can compose these
87120
without routing user actions through the service. Authentication tokens are held
88121
in session storage; counter/viewer snapshots contain no token.
89122

123+
The example offers a simple textarea or local Markdown formatting tools. GitHub
124+
renders submitted Markdown. Successful mutations update the visible count from
125+
GitHub's response. Mutations are not aborted after dispatch because cancelling
126+
the network request cannot establish whether GitHub applied the change. After an
127+
uncertain failure, refresh viewer state before retrying.
128+
90129
## GitHub request audit and abuse boundaries
91130

92131
The server can contact GitHub only in these places:
@@ -101,16 +140,17 @@ The server can contact GitHub only in these places:
101140
4. Discussion discovery/creation. Work is serialized per site/resource; exact
102141
repository and category matches are required, and the resulting ID is stored.
103142
5. Discussion reads. Up to ten threads are grouped in one `nodes(ids:)` request.
104-
Identical simultaneous batches share only the in-flight request. Responses
105-
are never retained.
143+
Identical simultaneous batches share in-flight work; completed, filtered
144+
results are held in a bounded 10-second memory cache. Deleted or minimized
145+
content can therefore remain visible for up to 10 seconds after moderation.
106146

107147
Request sizes, key syntax, JSON fields, body sizes, origins, and batch size are
108148
bounded. Security headers are applied globally. Logs omit tokens, OAuth codes,
109149
comment bodies, URLs, origins, and client addresses. `--verbose` enables safe
110150
route timing and cache/GitHub-boundary diagnostics.
111151

112152
The database stores discussion identity and aggregate counters only. It does not
113-
store discussion or comment bodies. Consequently a later request cannot serve a
114-
cached copy of deleted content. Deleted or minimized comment nodes are excluded
115-
from discussion responses together with their replies, before the API returns
116-
the payload; no hidden body is sent to the browser.
153+
store discussion or comment bodies. Deleted or minimized comment nodes are
154+
excluded from discussion responses together with their replies before caching.
155+
The example browser also keeps a 10-second thread snapshot to avoid network
156+
requests when users change local presentation settings.

‎runtime/pages/example/index.html‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,23 @@ <h2>Full discussions</h2>
3030
<section id="thread" class="thread"></section>
3131
<form id="comment-form" class="comment-form">
3232
<div class="comment-box">
33-
<div class="comment-box-header">Write</div>
33+
<div class="comment-box-header">
34+
<span>Write</span>
35+
<label>Editor
36+
<select id="editor-mode">
37+
<option value="simple">Simple</option>
38+
<option value="markdown">Markdown tools</option>
39+
</select>
40+
</label>
41+
</div>
42+
<div id="editor-tools" class="editor-tools" hidden>
43+
<button type="button" data-markdown="bold" aria-label="Bold">B</button>
44+
<button type="button" data-markdown="italic" aria-label="Italic">I</button>
45+
<button type="button" data-markdown="code" aria-label="Code">&lt;/&gt;</button>
46+
<button type="button" data-markdown="link" aria-label="Link">Link</button>
47+
<button type="button" data-markdown="quote" aria-label="Quote">Quote</button>
48+
<button type="button" data-markdown="list" aria-label="List">List</button>
49+
</div>
3450
<textarea id="comment-body" rows="4" maxlength="16000" placeholder="Leave a comment"></textarea>
3551
<small>Markdown is supported. Content is rendered using GitHub's sanitized Markdown HTML.</small>
3652
</div>

‎runtime/pages/example/style.css‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ main {
9292
.cards {
9393
display: grid;
9494
gap: 1rem;
95-
grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr));
95+
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
9696
margin-bottom: 2rem;
9797
}
9898

@@ -101,12 +101,17 @@ main {
101101
border: 1px solid #d0d7de;
102102
border-radius: 0.375rem;
103103
box-shadow: 0 1px 0 rgba(31, 35, 40, 0.04);
104+
display: flex;
105+
flex-direction: column;
106+
gap: 0.75rem;
107+
min-width: 0;
104108
padding: 1rem;
105-
position: relative;
106109
}
107110

108-
.ranking-card h3 { margin: 0 7rem 0.75rem 0; }
109-
.card-votes { display: flex; gap: 0.25rem; position: absolute; right: 1rem; top: 1rem; }
111+
.ranking-card h3 { margin: 0; overflow-wrap: anywhere; }
112+
.card-votes { display: flex; flex-wrap: wrap; gap: 0.35rem; }
113+
.ranking-card .reactions { margin-top: 0; }
114+
.ranking-card small { color: #656d76; }
110115

111116
.discussion { border: 1px solid #d0d7de; border-radius: 0.375rem; padding: 1rem; }
112117
.own-post { border-color: #356fc0; box-shadow: inset 4px 0 #356fc0; }
@@ -225,7 +230,7 @@ main {
225230
}
226231
.poll-option:disabled { cursor: default; opacity: 1; }
227232

228-
.verified-answer {
233+
.answer {
229234
box-shadow: inset 4px 0 #1a7f37;
230235
}
231236

@@ -304,10 +309,22 @@ textarea {
304309
.comment-box-header {
305310
background: #f6f8fa;
306311
border-bottom: 1px solid #d0d7de;
312+
display: flex;
307313
font-weight: 600;
314+
justify-content: space-between;
308315
padding: 0.6rem 0.9rem;
309316
}
310317

318+
.editor-tools {
319+
background: #f6f8fa;
320+
border-bottom: 1px solid #d0d7de;
321+
display: flex;
322+
gap: 0.25rem;
323+
padding: 0.35rem 0.75rem;
324+
}
325+
326+
.editor-tools[hidden] { display: none; }
327+
311328
.comment-box textarea {
312329
border: 0;
313330
box-sizing: border-box;

0 commit comments

Comments
 (0)