astro-atmosphere-comments 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +19 -0
- package/README.md +17 -6
- package/components/AtmosphereComments.astro +199 -39
- package/components/AtmosphereReactions.astro +68 -56
- package/components/Composer.astro +5 -2
- package/components/SignInForm.astro +55 -8
- package/components/atmosphere.css +246 -2
- package/index.d.ts +11 -0
- package/index.mjs +11 -0
- package/lib/client/account.ts +124 -16
- package/lib/client/composer.ts +16 -0
- package/lib/client/subscribe.ts +106 -0
- package/lib/permissions.ts +32 -8
- package/lib/public-settings.ts +2 -0
- package/lib/quoted.ts +4 -0
- package/lib/settings.ts +2 -0
- package/lib/uris.ts +8 -0
- package/locales/en.json +29 -10
- package/package.json +1 -1
- package/routes/post.ts +1 -0
package/lib/client/account.ts
CHANGED
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
import type { BrowserOAuthClient, OAuthSession } from "@atproto/oauth-client-browser";
|
|
24
24
|
import { safeReturnPath, withStatus } from "../http";
|
|
25
25
|
import {
|
|
26
|
-
|
|
26
|
+
allScopes,
|
|
27
27
|
callbackPath,
|
|
28
28
|
clientMetadata,
|
|
29
29
|
DEFAULT_PERMISSIONS,
|
|
@@ -34,12 +34,16 @@ import {
|
|
|
34
34
|
scopesAllow,
|
|
35
35
|
} from "../permissions";
|
|
36
36
|
import settings from "../public-settings";
|
|
37
|
-
import { authorOf, inSiteThread, isPostUri, isSitePost, rkeyOf, type PostView } from "../uris";
|
|
37
|
+
import { authorOf, inSiteThread, isPostUri, isSiteDocument, isSitePost, rkeyOf, type PostView } from "../uris";
|
|
38
38
|
|
|
39
39
|
type IdentityResolver = NonNullable<ConstructorParameters<typeof BrowserOAuthClient>[0]["identityResolver"]>;
|
|
40
40
|
|
|
41
41
|
const { base } = settings;
|
|
42
42
|
const REMEMBERED = "atmosphere:did";
|
|
43
|
+
// That the remembered sign-in asked for Standard.site's permission too.
|
|
44
|
+
const ASKED_SOCIAL = "atmosphere:social";
|
|
45
|
+
const RECOMMEND = "site.standard.graph.recommend";
|
|
46
|
+
const SUBSCRIPTION = "site.standard.graph.subscription";
|
|
43
47
|
const APPVIEW_PROXY = "did:web:api.bsky.app#bsky_appview";
|
|
44
48
|
const MAX_GRAPHEMES = 300; // Bluesky's limit for a post
|
|
45
49
|
const graphemes = new Intl.Segmenter();
|
|
@@ -55,13 +59,26 @@ const remembered = () => {
|
|
|
55
59
|
};
|
|
56
60
|
const remember = (did: string) => {
|
|
57
61
|
try {
|
|
58
|
-
if (did)
|
|
59
|
-
|
|
62
|
+
if (did) {
|
|
63
|
+
localStorage.setItem(REMEMBERED, did);
|
|
64
|
+
if (settings.standardSite) localStorage.setItem(ASKED_SOCIAL, "1");
|
|
65
|
+
} else {
|
|
66
|
+
localStorage.removeItem(REMEMBERED);
|
|
67
|
+
localStorage.removeItem(ASKED_SOCIAL);
|
|
68
|
+
}
|
|
60
69
|
} catch {
|
|
61
70
|
// See above.
|
|
62
71
|
}
|
|
63
72
|
};
|
|
64
73
|
|
|
74
|
+
const askedSocial = () => {
|
|
75
|
+
try {
|
|
76
|
+
return !!localStorage.getItem(ASKED_SOCIAL);
|
|
77
|
+
} catch {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
65
82
|
/** Whether this browser may have a sign-in (so the library is worth loading). */
|
|
66
83
|
export const maybeSignedIn = () => !!remembered();
|
|
67
84
|
|
|
@@ -77,7 +94,7 @@ function client(): Promise<BrowserOAuthClient> {
|
|
|
77
94
|
);
|
|
78
95
|
const oauth = new BrowserOAuthClient({
|
|
79
96
|
clientMetadata: import.meta.env.DEV
|
|
80
|
-
? atprotoLoopbackClientMetadata(buildAtprotoLoopbackClientId({ scope:
|
|
97
|
+
? atprotoLoopbackClientMetadata(buildAtprotoLoopbackClientId({ scope: allScopes(settings), redirect_uris: [callbackUrl()] }))
|
|
81
98
|
: clientMetadata(settings),
|
|
82
99
|
// Accounts are looked up through the site (routes/identity.ts), so the
|
|
83
100
|
// browser contacts only the reader's own account host.
|
|
@@ -104,6 +121,20 @@ function session(): Promise<OAuthSession | null> {
|
|
|
104
121
|
sessionPromise ??= (async () => {
|
|
105
122
|
const did = remembered();
|
|
106
123
|
if (!did) return null;
|
|
124
|
+
// A sign-in from before the site asked for Standard.site's permission:
|
|
125
|
+
// cancelled, so the reader signs in once and approves everything together
|
|
126
|
+
// instead of making a second trip to their account host later. (This looks
|
|
127
|
+
// at what was asked for, not what was granted, so a host that grants less
|
|
128
|
+
// can't send a reader round in circles.)
|
|
129
|
+
if (settings.standardSite && !askedSocial()) {
|
|
130
|
+
try {
|
|
131
|
+
await (await (await client()).restore(did)).signOut();
|
|
132
|
+
} catch {
|
|
133
|
+
// Already gone.
|
|
134
|
+
}
|
|
135
|
+
remember("");
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
107
138
|
try {
|
|
108
139
|
return await (await client()).restore(did);
|
|
109
140
|
} catch {
|
|
@@ -177,6 +208,33 @@ async function deleteRecord(reader: Reader, uri: string) {
|
|
|
177
208
|
await call(reader, "com.atproto.repo.deleteRecord", { body: { repo: reader.did, collection, rkey } });
|
|
178
209
|
}
|
|
179
210
|
|
|
211
|
+
/**
|
|
212
|
+
* The AT URIs of the reader's own records of `collection` whose `field` is
|
|
213
|
+
* `value`. Read from their account host, so nothing else learns who's asking.
|
|
214
|
+
*/
|
|
215
|
+
async function ownRecords(reader: Reader, collection: string, field: string, value: string): Promise<string[]> {
|
|
216
|
+
const found: string[] = [];
|
|
217
|
+
let cursor = "";
|
|
218
|
+
for (let page = 0; page < 10; page++) {
|
|
219
|
+
const query = new URLSearchParams({ repo: reader.did, collection, limit: "100", ...(cursor && { cursor }) }).toString();
|
|
220
|
+
const data = await call<{ cursor?: string; records?: { uri: string; value?: Record<string, unknown> }[] }>(reader, "com.atproto.repo.listRecords", { query });
|
|
221
|
+
found.push(...(data.records ?? []).filter((record) => record.value?.[field] === value).map((record) => record.uri));
|
|
222
|
+
if (!data.cursor) break;
|
|
223
|
+
cursor = data.cursor;
|
|
224
|
+
}
|
|
225
|
+
return found;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Makes the reader's records say `on`: creates one if there's none, or deletes them all. */
|
|
229
|
+
async function setRecord(reader: Reader, collection: string, field: string, value: string, on: boolean) {
|
|
230
|
+
const existing = await ownRecords(reader, collection, field, value);
|
|
231
|
+
if (on && !existing.length) {
|
|
232
|
+
await createRecord(reader, collection, { $type: collection, [field]: value, createdAt: new Date().toISOString() });
|
|
233
|
+
} else if (!on) {
|
|
234
|
+
for (const uri of existing) await deleteRecord(reader, uri);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
180
238
|
const post = (path: string, body: unknown) =>
|
|
181
239
|
fetch(`${base}/${path}`, {
|
|
182
240
|
method: "POST",
|
|
@@ -286,8 +344,10 @@ async function comment(body: { text?: unknown; root?: unknown; parent?: unknown;
|
|
|
286
344
|
|
|
287
345
|
const COLLECTIONS = { like: "app.bsky.feed.like", repost: "app.bsky.feed.repost" } as const;
|
|
288
346
|
|
|
289
|
-
// Likes or reposts a post (an announcement, or a comment under one), or takes
|
|
290
|
-
|
|
347
|
+
// Likes or reposts a post (an announcement, or a comment under one), or takes
|
|
348
|
+
// that back. A like of the post a page is about also recommends that page's
|
|
349
|
+
// Standard.site record (`document`), and un-liking removes the recommend.
|
|
350
|
+
async function react(body: { post?: unknown; kind?: unknown; on?: unknown; document?: unknown }) {
|
|
291
351
|
const signedIn = await reader();
|
|
292
352
|
if (signedIn instanceof Response) return signedIn;
|
|
293
353
|
const { post: uri, kind, on } = body;
|
|
@@ -307,6 +367,12 @@ async function react(body: { post?: unknown; kind?: unknown; on?: unknown }) {
|
|
|
307
367
|
} else if (!on && existing) {
|
|
308
368
|
await deleteRecord(signedIn, existing);
|
|
309
369
|
}
|
|
370
|
+
if (kind === "like" && isSitePost(uri) && isSiteDocument(body.document) && allows(signedIn, RECOMMEND, on ? "create" : "delete")) {
|
|
371
|
+
// The like stands even if this fails: it's what the reader sees here.
|
|
372
|
+
await setRecord(signedIn, RECOMMEND, "document", body.document, on).catch((error) =>
|
|
373
|
+
console.warn("[atmosphere] recommend failed:", error instanceof Error ? error.message : error),
|
|
374
|
+
);
|
|
375
|
+
}
|
|
310
376
|
refreshThread(isSitePost(view.uri) ? view.uri : view.record?.reply?.root?.uri);
|
|
311
377
|
return answer({ kind, on });
|
|
312
378
|
} catch (error) {
|
|
@@ -426,6 +492,36 @@ async function follow(body: { subject?: unknown; on?: unknown }) {
|
|
|
426
492
|
}
|
|
427
493
|
}
|
|
428
494
|
|
|
495
|
+
/** Whether the site has a Standard.site publication to subscribe to. */
|
|
496
|
+
export const canSubscribe = () => !!settings.standardSite;
|
|
497
|
+
|
|
498
|
+
/** Whether the signed-in reader subscribes to the site's publication; null if that can't be told. */
|
|
499
|
+
export async function subscribed(): Promise<boolean | null> {
|
|
500
|
+
const publication = settings.standardSite?.publication;
|
|
501
|
+
const signedIn = await reader();
|
|
502
|
+
if (!publication || signedIn instanceof Response) return null;
|
|
503
|
+
try {
|
|
504
|
+
return !!(await ownRecords(signedIn, SUBSCRIPTION, "publication", publication)).length;
|
|
505
|
+
} catch {
|
|
506
|
+
return null;
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
// Subscribes to (or unsubscribes from) the site's own publication, and no other.
|
|
511
|
+
async function subscribe(body: { on?: unknown }) {
|
|
512
|
+
const signedIn = await reader();
|
|
513
|
+
if (signedIn instanceof Response) return signedIn;
|
|
514
|
+
const publication = settings.standardSite?.publication;
|
|
515
|
+
if (!publication || typeof body.on !== "boolean") return answer({ error: "not-allowed" }, 400);
|
|
516
|
+
if (!allows(signedIn, SUBSCRIPTION, body.on ? "create" : "delete")) return needs("social");
|
|
517
|
+
try {
|
|
518
|
+
await setRecord(signedIn, SUBSCRIPTION, "publication", publication, body.on);
|
|
519
|
+
return answer({ subscribed: body.on });
|
|
520
|
+
} catch (error) {
|
|
521
|
+
return failed(error, "subscribe");
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
429
525
|
/** Whether the reader has liked or reposted these posts (those in the site's threads). */
|
|
430
526
|
export async function state(uris: string[]) {
|
|
431
527
|
const signedIn = await reader();
|
|
@@ -458,7 +554,7 @@ export async function signOut() {
|
|
|
458
554
|
|
|
459
555
|
/** What the comments and reactions send, answered as the old routes did. */
|
|
460
556
|
export function send(path: string, body: unknown): Promise<Response> {
|
|
461
|
-
const actions: Record<string, (body: never) => Promise<Response>> = { comment, react, delete: remove, hide, follow };
|
|
557
|
+
const actions: Record<string, (body: never) => Promise<Response>> = { comment, react, delete: remove, hide, follow, subscribe };
|
|
462
558
|
const action = actions[path];
|
|
463
559
|
return action ? action(body as never) : Promise.resolve(answer({ error: "bad-request" }, 400));
|
|
464
560
|
}
|
|
@@ -467,7 +563,8 @@ const HANDLE = /^(did:[a-z0-9:._%-]+|[a-z0-9.-]+\.[a-z0-9-]+)$/;
|
|
|
467
563
|
|
|
468
564
|
/**
|
|
469
565
|
* Sends the reader to their account host to approve everything they can do
|
|
470
|
-
* here (DEFAULT_PERMISSIONS, plus "
|
|
566
|
+
* here (DEFAULT_PERMISSIONS, plus "social" with the `standardSite` option and
|
|
567
|
+
* "moderate" for the site's own accounts)
|
|
471
568
|
* and any `permissions` named, coming back to `returnTo`. A new approval
|
|
472
569
|
* replaces the old one, so whatever the same account already allowed is
|
|
473
570
|
* asked for again too. If the account can't be found, the reader is sent
|
|
@@ -478,6 +575,7 @@ export async function signIn(input: string, options: { permissions?: Permission[
|
|
|
478
575
|
const handle = input.trim().replace(/^@/, "").toLowerCase();
|
|
479
576
|
if (!handle || handle.length > 253 || !HANDLE.test(handle)) return location.assign(withStatus(returnTo, "invalid-handle"));
|
|
480
577
|
const permissions = new Set<Permission>([...DEFAULT_PERMISSIONS, ...(options.permissions ?? [])]);
|
|
578
|
+
if (settings.standardSite) permissions.add("social");
|
|
481
579
|
// The site's own accounts can also hide comments under their announcements.
|
|
482
580
|
try {
|
|
483
581
|
const did = handle.startsWith("did:")
|
|
@@ -510,22 +608,32 @@ export async function signIn(input: string, options: { permissions?: Permission[
|
|
|
510
608
|
|
|
511
609
|
/** Signs in with a sign-in form (SignInForm.astro): its handle box, return address and any permission fields. */
|
|
512
610
|
export function handleSignInForm(form: HTMLFormElement, lang?: string) {
|
|
611
|
+
const button = form.querySelector<HTMLButtonElement>('button[type="submit"]');
|
|
612
|
+
let safetyNet: number | undefined;
|
|
613
|
+
// Disabled with a spinner from the moment signing in starts (a press, Enter
|
|
614
|
+
// or picking a suggestion), until the browser leaves for the account host.
|
|
615
|
+
const setBusy = (busy: boolean) => {
|
|
616
|
+
window.clearTimeout(safetyNet);
|
|
617
|
+
form.toggleAttribute("data-signing-in", busy);
|
|
618
|
+
if (button) button.disabled = busy;
|
|
619
|
+
};
|
|
513
620
|
form.addEventListener("submit", (event) => {
|
|
514
621
|
event.preventDefault();
|
|
622
|
+
if (form.hasAttribute("data-signing-in")) return;
|
|
515
623
|
const data = new FormData(form);
|
|
516
|
-
|
|
517
|
-
|
|
624
|
+
setBusy(true);
|
|
625
|
+
// Every way signIn() ends leaves the page. If that somehow doesn't happen,
|
|
626
|
+
// the reader can try again rather than face a spinner for ever.
|
|
627
|
+
safetyNet = window.setTimeout(() => setBusy(false), 30000);
|
|
518
628
|
void signIn(String(data.get("handle") ?? ""), {
|
|
519
629
|
permissions: data.getAll("permission").map(String) as Permission[],
|
|
520
630
|
returnTo: String(data.get("returnTo") ?? ""),
|
|
521
631
|
lang,
|
|
522
|
-
}).finally(() => {
|
|
523
|
-
// Back from the account host with the browser's back button.
|
|
524
|
-
window.setTimeout(() => button && (button.disabled = false), 1000);
|
|
525
632
|
});
|
|
526
633
|
});
|
|
527
|
-
//
|
|
528
|
-
|
|
634
|
+
// Back from the account host with the browser's back button: the page can
|
|
635
|
+
// come back from its cache just as it was left.
|
|
636
|
+
window.addEventListener("pageshow", () => setBusy(false));
|
|
529
637
|
}
|
|
530
638
|
|
|
531
639
|
/** On the callback page: finishes signing in, then back to the page the reader was on. */
|
package/lib/client/composer.ts
CHANGED
|
@@ -176,6 +176,20 @@ export function setUpComposer(form: HTMLFormElement, ctx: ComposerContext, behav
|
|
|
176
176
|
return { textarea, updateCount };
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
+
/** A post's age as Bluesky shows it: 10m, 4h, 6d, then the date. */
|
|
180
|
+
function postAge(when: string): string {
|
|
181
|
+
const date = new Date(when);
|
|
182
|
+
const lang = document.documentElement.lang || undefined;
|
|
183
|
+
const minutes = Math.floor((Date.now() - date.getTime()) / 60000);
|
|
184
|
+
const unit = (value: number, name: string) => new Intl.NumberFormat(lang, { style: "unit", unit: name, unitDisplay: "narrow" }).format(value);
|
|
185
|
+
if (minutes < 1) return unit(1, "minute");
|
|
186
|
+
if (minutes < 60) return unit(minutes, "minute");
|
|
187
|
+
if (minutes < 1440) return unit(Math.floor(minutes / 60), "hour");
|
|
188
|
+
if (minutes < 43200) return unit(Math.floor(minutes / 1440), "day");
|
|
189
|
+
const thisYear = date.getFullYear() === new Date().getFullYear();
|
|
190
|
+
return date.toLocaleDateString(lang, { month: "short", day: "numeric", ...(!thisYear && { year: "numeric" }) });
|
|
191
|
+
}
|
|
192
|
+
|
|
179
193
|
/**
|
|
180
194
|
* The quote preview inside a box: the reader's words as they type, with the
|
|
181
195
|
* post being quoted beneath, as Bluesky will show it. Call update() as they
|
|
@@ -199,6 +213,7 @@ export function quotePreview(form: HTMLFormElement, textarea: HTMLTextAreaElemen
|
|
|
199
213
|
const quoted = (await response.json()) as {
|
|
200
214
|
author: { handle: string; displayName?: string; avatar?: string };
|
|
201
215
|
text: string;
|
|
216
|
+
indexedAt?: string;
|
|
202
217
|
external?: { uri: string; title: string; description: string; thumb?: string };
|
|
203
218
|
};
|
|
204
219
|
loaded = uri;
|
|
@@ -206,6 +221,7 @@ export function quotePreview(form: HTMLFormElement, textarea: HTMLTextAreaElemen
|
|
|
206
221
|
$<HTMLElement>("[data-quoted-name]").textContent = quotedName;
|
|
207
222
|
ctx.setAvatar($<HTMLElement>("[data-quoted-avatar]"), quotedName, quoted.author.avatar);
|
|
208
223
|
$<HTMLElement>("[data-quoted-handle]").textContent = `@${quoted.author.handle}`;
|
|
224
|
+
$<HTMLElement>("[data-quoted-age]").textContent = quoted.indexedAt ? `· ${postAge(quoted.indexedAt)}` : "";
|
|
209
225
|
$<HTMLElement>("[data-quoted-text]").textContent = quoted.text;
|
|
210
226
|
const card = $<HTMLElement>("[data-card]");
|
|
211
227
|
card.hidden = !quoted.external;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// Subscribe / Subscribed buttons (marked data-atmosphere-subscribe): the panel
|
|
2
|
+
// under a post and any others a site places. Each component looks after its
|
|
3
|
+
// own buttons and how it offers signing in; what the reader's subscription is
|
|
4
|
+
// lives here, so every button on the page agrees.
|
|
5
|
+
//
|
|
6
|
+
// Signing in asks for this along with everything else, so there's never a
|
|
7
|
+
// second trip to the reader's account host for it.
|
|
8
|
+
import { canSubscribe, send, subscribed } from "./account";
|
|
9
|
+
|
|
10
|
+
type Translate = (key: string, vars?: Record<string, string | number>) => string;
|
|
11
|
+
/** Offers the sign-in form, coming back to #atmo-subscribe; `problem` says why it's needed again. */
|
|
12
|
+
type AskToSignIn = (problem?: string) => void;
|
|
13
|
+
|
|
14
|
+
const SIGN_IN_PROBLEMS: Record<string, string> = {
|
|
15
|
+
"sign-in-failed": "messages.signInFailed",
|
|
16
|
+
"account-not-found": "messages.accountNotFound",
|
|
17
|
+
"invalid-handle": "messages.invalidHandle",
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const buttons = new Map<HTMLButtonElement, Translate>();
|
|
21
|
+
let isSubscribed = false;
|
|
22
|
+
let loaded: Promise<boolean | null> | undefined;
|
|
23
|
+
|
|
24
|
+
const render = () => {
|
|
25
|
+
for (const [button, t] of buttons) {
|
|
26
|
+
button.title = t(isSubscribed ? "subscribe.subscribedLabel" : "subscribe.subscribeLabel");
|
|
27
|
+
// A label to fill in; without one, the button keeps what the site put in
|
|
28
|
+
// it (and shows the state with [aria-pressed]), so it needs its full name.
|
|
29
|
+
const label = button.querySelector("[data-atmosphere-subscribe-label]");
|
|
30
|
+
if (label) label.textContent = t(isSubscribed ? "subscribe.subscribed" : "subscribe.subscribe");
|
|
31
|
+
else button.setAttribute("aria-label", button.title);
|
|
32
|
+
button.setAttribute("aria-pressed", String(isSubscribed));
|
|
33
|
+
button.hidden = false;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/** Whether the reader subscribes, asked once a page; null if they aren't signed in. */
|
|
38
|
+
export function loadSubscribed(): Promise<boolean | null> {
|
|
39
|
+
loaded ??= subscribed().then((found) => {
|
|
40
|
+
isSubscribed = !!found;
|
|
41
|
+
render();
|
|
42
|
+
return found;
|
|
43
|
+
});
|
|
44
|
+
return loaded;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** After signing out here. */
|
|
48
|
+
export function forgetSubscribed() {
|
|
49
|
+
loaded = undefined;
|
|
50
|
+
isSubscribed = false;
|
|
51
|
+
render();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function press(button: HTMLButtonElement, t: Translate, ask: AskToSignIn) {
|
|
55
|
+
for (const each of buttons.keys()) each.disabled = true;
|
|
56
|
+
let problem = "";
|
|
57
|
+
try {
|
|
58
|
+
const response = await send("subscribe", { on: !isSubscribed });
|
|
59
|
+
const data = (await response.json().catch(() => ({}))) as { subscribed?: boolean; error?: string };
|
|
60
|
+
if (response.ok) isSubscribed = !!data.subscribed;
|
|
61
|
+
else if (data.error === "signed-out") ask();
|
|
62
|
+
else problem = t("messages.error");
|
|
63
|
+
} catch {
|
|
64
|
+
problem = t("messages.error");
|
|
65
|
+
}
|
|
66
|
+
for (const each of buttons.keys()) each.disabled = false;
|
|
67
|
+
render();
|
|
68
|
+
// Said beside the button, as the page may be showing nothing else.
|
|
69
|
+
document.querySelectorAll(".atmo-subscribe-problem").forEach((part) => part.remove());
|
|
70
|
+
if (problem) {
|
|
71
|
+
const note = Object.assign(document.createElement("span"), { className: "atmo-subscribe-problem", textContent: problem });
|
|
72
|
+
note.setAttribute("role", "status");
|
|
73
|
+
button.after(note);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Shows these buttons, if the site has a publication to subscribe to. */
|
|
78
|
+
export function addSubscribeButtons(list: HTMLButtonElement[], t: Translate, ask: AskToSignIn) {
|
|
79
|
+
if (!canSubscribe()) return;
|
|
80
|
+
for (const button of list) {
|
|
81
|
+
buttons.set(button, t);
|
|
82
|
+
button.addEventListener("click", () => void press(button, t, ask));
|
|
83
|
+
}
|
|
84
|
+
render();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let finished = false;
|
|
88
|
+
|
|
89
|
+
/** Back from signing in after pressing Subscribe: subscribes, as they asked. */
|
|
90
|
+
export async function finishPendingSubscribe(t: Translate, ask: AskToSignIn) {
|
|
91
|
+
if (finished || location.hash !== "#atmo-subscribe") return;
|
|
92
|
+
finished = true;
|
|
93
|
+
const url = new URL(location.href);
|
|
94
|
+
const code = url.searchParams.get("atmosphere") ?? "";
|
|
95
|
+
if (!document.querySelector("atmosphere-comments")) url.searchParams.delete("atmosphere");
|
|
96
|
+
history.replaceState(history.state, "", `${url.pathname}${url.search}`);
|
|
97
|
+
const known = await loadSubscribed();
|
|
98
|
+
if (known === null) {
|
|
99
|
+
if (SIGN_IN_PROBLEMS[code]) ask(t(SIGN_IN_PROBLEMS[code]));
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
const [first] = buttons.keys();
|
|
103
|
+
if (!first) return;
|
|
104
|
+
first.scrollIntoView({ block: "center" });
|
|
105
|
+
if (!known) await press(first, t, ask);
|
|
106
|
+
}
|
package/lib/permissions.ts
CHANGED
|
@@ -29,6 +29,10 @@ export const PERMISSIONS = {
|
|
|
29
29
|
follow: "repo:app.bsky.graph.follow?action=create&action=delete",
|
|
30
30
|
// For the site's own accounts: hiding comments under their announcements.
|
|
31
31
|
moderate: "repo:app.bsky.feed.threadgate?action=create&action=update",
|
|
32
|
+
// With the `standardSite` option: subscribing to the site's publication and
|
|
33
|
+
// recommending its posts (Standard.site's own permission set, which account
|
|
34
|
+
// hosts show as one line).
|
|
35
|
+
social: "include:site.standard.authSocial",
|
|
32
36
|
} as const;
|
|
33
37
|
|
|
34
38
|
export type Permission = keyof typeof PERMISSIONS;
|
|
@@ -38,8 +42,9 @@ export const isPermission = (value: unknown): value is Permission =>
|
|
|
38
42
|
|
|
39
43
|
// What signing in asks for: everything readers can do here. "moderate" is
|
|
40
44
|
// added only for the site's own accounts (allowedAuthors), the only ones
|
|
41
|
-
// that can use it
|
|
42
|
-
// when it's first
|
|
45
|
+
// that can use it, and "social" only for sites with the `standardSite` option.
|
|
46
|
+
// Sign-ins from before a group was added are asked for it when it's first
|
|
47
|
+
// needed; those from before "social" sign in again instead (account.ts).
|
|
43
48
|
export const DEFAULT_PERMISSIONS: Permission[] = ["post", "react", "delete", "follow"];
|
|
44
49
|
|
|
45
50
|
/** The scope to ask for: the base, plus the groups named. */
|
|
@@ -47,18 +52,26 @@ export const scopeFor = (permissions: Iterable<Permission>) =>
|
|
|
47
52
|
[BASE_SCOPE, ...new Set([...permissions].map((name) => PERMISSIONS[name]))].join(" ");
|
|
48
53
|
|
|
49
54
|
// Everything the site may ask for, which its client metadata has to list.
|
|
50
|
-
export const
|
|
55
|
+
export const allScopes = (settings: { standardSite?: unknown }) =>
|
|
56
|
+
scopeFor((Object.keys(PERMISSIONS) as Permission[]).filter((name) => name !== "social" || !!settings.standardSite));
|
|
57
|
+
|
|
58
|
+
// What Standard.site's permission set covers (its lexicon lists both).
|
|
59
|
+
const SOCIAL_COLLECTIONS = ["site.standard.graph.subscription", "site.standard.graph.recommend"];
|
|
51
60
|
|
|
52
61
|
/**
|
|
53
62
|
* Whether `scopes` (a grant) allow `action` on records of `collection`: a
|
|
54
63
|
* repo permission for that collection (or all of them) listing the action,
|
|
55
|
-
* or listing no actions, which means all of them.
|
|
64
|
+
* or listing no actions, which means all of them. A host may keep the
|
|
65
|
+
* permission set's name in the grant, or write it out as repo permissions.
|
|
56
66
|
*/
|
|
57
67
|
export function scopesAllow(scopes: string[], collection: string, action: string): boolean {
|
|
58
68
|
return scopes.some((scope) => {
|
|
59
69
|
const [name, query = ""] = scope.split("?");
|
|
60
|
-
if (name
|
|
61
|
-
const
|
|
70
|
+
if (name === PERMISSIONS.social) return SOCIAL_COLLECTIONS.includes(collection);
|
|
71
|
+
const params = new URLSearchParams(query);
|
|
72
|
+
const listed = name === "repo" && params.getAll("collection").some((item) => item === collection || item === "*");
|
|
73
|
+
if (name !== `repo:${collection}` && name !== "repo:*" && !listed) return false;
|
|
74
|
+
const actions = params.getAll("action");
|
|
62
75
|
return !actions.length || actions.includes(action);
|
|
63
76
|
});
|
|
64
77
|
}
|
|
@@ -67,6 +80,9 @@ export function scopesAllow(scopes: string[], collection: string, action: string
|
|
|
67
80
|
export function grantedPermissions(scopes: string[]): Permission[] {
|
|
68
81
|
return (Object.keys(PERMISSIONS) as Permission[]).filter((name) =>
|
|
69
82
|
PERMISSIONS[name].split(" ").every((part) => {
|
|
83
|
+
if (part === PERMISSIONS.social) {
|
|
84
|
+
return SOCIAL_COLLECTIONS.every((collection) => ["create", "delete"].every((action) => scopesAllow(scopes, collection, action)));
|
|
85
|
+
}
|
|
70
86
|
const [scope, query = ""] = part.split("?");
|
|
71
87
|
const collection = scope.replace(/^repo:/, "");
|
|
72
88
|
return new URLSearchParams(query).getAll("action").every((action) => scopesAllow(scopes, collection, action));
|
|
@@ -80,7 +96,15 @@ export function hasBase(scopes: string[]): boolean {
|
|
|
80
96
|
return BASE_SCOPE.split(" ").every((part) => names.includes(part.split("?")[0]));
|
|
81
97
|
}
|
|
82
98
|
|
|
83
|
-
type MetadataSettings = {
|
|
99
|
+
type MetadataSettings = {
|
|
100
|
+
site: string;
|
|
101
|
+
base: string;
|
|
102
|
+
clientName: string;
|
|
103
|
+
logoUri?: string;
|
|
104
|
+
policyUri?: string;
|
|
105
|
+
tosUri?: string;
|
|
106
|
+
standardSite?: { publication: string; feed?: string };
|
|
107
|
+
};
|
|
84
108
|
|
|
85
109
|
export const callbackPath = (base: string) => `${base}/callback`;
|
|
86
110
|
|
|
@@ -96,7 +120,7 @@ export function clientMetadata(settings: MetadataSettings) {
|
|
|
96
120
|
...(settings.policyUri && { policy_uri: settings.policyUri }),
|
|
97
121
|
...(settings.tosUri && { tos_uri: settings.tosUri }),
|
|
98
122
|
redirect_uris: [`${settings.site}${callbackPath(settings.base)}`] as [`https://${string}`],
|
|
99
|
-
scope:
|
|
123
|
+
scope: allScopes(settings),
|
|
100
124
|
grant_types: ["authorization_code", "refresh_token"] as ["authorization_code", "refresh_token"],
|
|
101
125
|
response_types: ["code"] as ["code"],
|
|
102
126
|
application_type: "web" as const,
|
package/lib/public-settings.ts
CHANGED
|
@@ -13,6 +13,8 @@ export type PublicSettings = {
|
|
|
13
13
|
logoUri?: string;
|
|
14
14
|
policyUri?: string;
|
|
15
15
|
tosUri?: string;
|
|
16
|
+
/** The site's Standard.site publication: turns on Subscribe, and recommending with Like. */
|
|
17
|
+
standardSite?: { publication: string; feed?: string };
|
|
16
18
|
};
|
|
17
19
|
|
|
18
20
|
export default settings as PublicSettings;
|
package/lib/quoted.ts
CHANGED
|
@@ -5,6 +5,8 @@ import { profileFor, type Profile } from "./profile";
|
|
|
5
5
|
export type Quoted = {
|
|
6
6
|
author: Profile;
|
|
7
7
|
text: string;
|
|
8
|
+
/** When Bluesky received the post (for a backdated announcement, not its stated date). */
|
|
9
|
+
indexedAt?: string;
|
|
8
10
|
external?: { uri: string; title: string; description: string; thumbSource?: string };
|
|
9
11
|
};
|
|
10
12
|
|
|
@@ -22,6 +24,7 @@ export async function quotedPost(uri: string): Promise<Quoted | null> {
|
|
|
22
24
|
const [post] = ((await response.json()) as {
|
|
23
25
|
posts?: {
|
|
24
26
|
author: { did: string };
|
|
27
|
+
indexedAt?: string;
|
|
25
28
|
record: { text?: string };
|
|
26
29
|
embed?: { external?: { uri: string; title: string; description: string; thumb?: string } };
|
|
27
30
|
}[];
|
|
@@ -31,6 +34,7 @@ export async function quotedPost(uri: string): Promise<Quoted | null> {
|
|
|
31
34
|
const quoted: Quoted = {
|
|
32
35
|
author: await profileFor(post.author.did),
|
|
33
36
|
text: post.record.text ?? "",
|
|
37
|
+
indexedAt: post.indexedAt,
|
|
34
38
|
...(external && {
|
|
35
39
|
external: {
|
|
36
40
|
uri: external.uri,
|
package/lib/settings.ts
CHANGED
|
@@ -12,6 +12,8 @@ export type AtmosphereSettings = {
|
|
|
12
12
|
logoUri?: string;
|
|
13
13
|
policyUri?: string;
|
|
14
14
|
tosUri?: string;
|
|
15
|
+
/** The site's Standard.site publication: turns on Subscribe, and recommending with Like. */
|
|
16
|
+
standardSite?: { publication: string; feed?: string };
|
|
15
17
|
hidden?: string[];
|
|
16
18
|
locale?: string;
|
|
17
19
|
messages?: Record<string, string>;
|
package/lib/uris.ts
CHANGED
|
@@ -18,6 +18,14 @@ export function isSitePost(uri: unknown): uri is string {
|
|
|
18
18
|
return isPostUri(uri) && settings.allowedAuthors.includes(authorOf(uri));
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
const DOCUMENT_URI = /^at:\/\/(did:[a-z0-9]+:[a-zA-Z0-9._:%-]+)\/site\.standard\.document\/[a-zA-Z0-9._~-]+$/;
|
|
22
|
+
|
|
23
|
+
/** True for a Standard.site document in the same account as the site's publication. */
|
|
24
|
+
export function isSiteDocument(uri: unknown): uri is string {
|
|
25
|
+
const publisher = settings.standardSite?.publication.split("/")[2];
|
|
26
|
+
return typeof uri === "string" && !!publisher && DOCUMENT_URI.exec(uri)?.[1] === publisher;
|
|
27
|
+
}
|
|
28
|
+
|
|
21
29
|
export type PostView = {
|
|
22
30
|
uri: string;
|
|
23
31
|
cid: string;
|
package/locales/en.json
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
{
|
|
2
|
-
"signIn.heading": "Sign in to leave a comment
|
|
3
|
-
"signIn.anyAccount": "
|
|
2
|
+
"signIn.heading": "Sign in to leave a comment",
|
|
3
|
+
"signIn.anyAccount": "Use {{bluesky}} or any other {{atmosphereAccount}}. {{createAccount}}",
|
|
4
4
|
"signIn.atmosphereAccount": "Atmosphere account",
|
|
5
|
+
"signIn.more": "How does signing in work?",
|
|
5
6
|
"signIn.button": "Sign in with Atmosphere",
|
|
6
7
|
"signIn.matchingAccounts": "Matching accounts",
|
|
7
|
-
"signIn.privacy": "You
|
|
8
|
-
"signIn.createAccount": "Create
|
|
9
|
-
"elsewhere.text": "Rather not sign in
|
|
8
|
+
"signIn.privacy": "You sign in on your account's own site (Bluesky's, for most people) and approve what this site can do. Your password is never shared here.",
|
|
9
|
+
"signIn.createAccount": "Create one for free",
|
|
10
|
+
"elsewhere.text": "Rather not sign in? {{link}}",
|
|
10
11
|
"elsewhere.link": "Reply on Bluesky instead →",
|
|
11
12
|
"actions.label": "Respond to this post",
|
|
13
|
+
"actions.likeAndRecommend": "Like on Bluesky, and recommend in apps such as Leaflet",
|
|
12
14
|
"actions.like": "Like",
|
|
13
15
|
"actions.liked": "Liked",
|
|
14
16
|
"actions.repost": "Repost",
|
|
@@ -52,15 +54,20 @@
|
|
|
52
54
|
"comment.deletePermission": "To delete comments here, this site needs one more permission from your Atmosphere account. Alternatively, you can delete it {{link}}.",
|
|
53
55
|
"comment.continue": "Continue to approve",
|
|
54
56
|
"comment.cancel": "Cancel",
|
|
55
|
-
"signInFirst.reply": "Sign in to reply
|
|
56
|
-
"signInFirst.like": "Sign in to like
|
|
57
|
-
"signInFirst.repost": "Sign in to repost
|
|
57
|
+
"signInFirst.reply": "Sign in to reply",
|
|
58
|
+
"signInFirst.like": "Sign in to like this comment",
|
|
59
|
+
"signInFirst.repost": "Sign in to repost this comment",
|
|
60
|
+
"signInFirst.likePost": "Sign in to like this post",
|
|
61
|
+
"signInFirst.repostPost": "Sign in to repost this post",
|
|
62
|
+
"signInFirst.quotePost": "Sign in to quote this post",
|
|
63
|
+
"comment.prompt": "Write a comment…",
|
|
64
|
+
"comment.signIn": "Sign in",
|
|
58
65
|
"status.liked": "Liked.",
|
|
59
66
|
"status.likeRemoved": "Like removed.",
|
|
60
67
|
"status.reposted": "Reposted.",
|
|
61
68
|
"status.repostRemoved": "Repost removed.",
|
|
62
69
|
"status.signedOut": "You're signed out.",
|
|
63
|
-
"messages.signedIn": "You're signed in.
|
|
70
|
+
"messages.signedIn": "You're signed in.",
|
|
64
71
|
"messages.signInFailed": "Signing in didn't finish. Please try again.",
|
|
65
72
|
"messages.accountNotFound": "We couldn't find that account. Check the handle and try again.",
|
|
66
73
|
"messages.invalidHandle": "That doesn't look like a handle. It's usually something like alice.bsky.social.",
|
|
@@ -90,9 +97,21 @@
|
|
|
90
97
|
"follow.following": "Following",
|
|
91
98
|
"follow.followAs": "Follow {{handle}} as {{me}}",
|
|
92
99
|
"follow.followingLabel": "You follow {{handle}}",
|
|
100
|
+
"subscribe.subscribe": "Subscribe",
|
|
101
|
+
"subscribe.subscribed": "✓ Subscribed",
|
|
102
|
+
"subscribe.subscribeLabel": "Subscribe with your Atmosphere account, to read new posts in apps such as Leaflet or Standard Reader",
|
|
103
|
+
"subscribe.subscribedLabel": "You're subscribed. Press to unsubscribe",
|
|
104
|
+
"subscribe.signIn": "Sign in to subscribe",
|
|
105
|
+
"subscribe.more": "More ways to follow",
|
|
106
|
+
"subscribe.copyLink": "Copy link",
|
|
107
|
+
"subscribe.copied": "Copied!",
|
|
108
|
+
"subscribe.feed": "RSS feed",
|
|
109
|
+
"subscribe.viewBluesky": "View on Bluesky",
|
|
110
|
+
"subscribe.standardReader": "View on Standard Reader",
|
|
111
|
+
"subscribe.panelText": "Read new posts in apps such as {{leaflet}} or {{standardReader}}.",
|
|
93
112
|
"credit": "Powered by {{link}}",
|
|
94
113
|
"callback.signingIn": "Signing you in…",
|
|
95
|
-
"signIn.access": "Your sign-in
|
|
114
|
+
"signIn.access": "Your sign-in is kept only in this browser. This site's server never sees it, so it can't post for you. Signing out here cancels it.",
|
|
96
115
|
"signIn.policy": "How this site handles your data",
|
|
97
116
|
"composer.needsPermission": "To comment here as {{handle}}, this site needs one more permission from your Atmosphere account.",
|
|
98
117
|
"comment.hide": "Hide",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "astro-atmosphere-comments",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Atmosphere Comments for Astro: Bluesky comments, likes, reposts and quote posts for Astro sites; readers sign in with their Atmosphere (AT Protocol) account and respond without leaving the page.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|