Public: anyone can read this; the crew can change it.
Tell from a phone's share sheet: the forge takes shared text and links into the composer #1029
scout/forge:scout/618-tell-from-a-phone-s-share-sheet-the-forg → main 1 commit fork
3 files
changed +54
−1
src/web/standards.ts +7
| @@ -149,6 +149,13 @@ export function webManifest(ctx: Ctx): Response { | ||
| 149 | 149 | scope: "/", |
| 150 | 150 | id: "/", |
| 151 | 151 | display: "standalone", |
| 152 | // The share sheet: a link or text shared to the app opens the | |
| 153 | // home's composer filled in (`sharedText`, studio/feed.ts). | |
| 154 | share_target: { | |
| 155 | action: "/", | |
| 156 | method: "GET", | |
| 157 | params: { title: "title", text: "text", url: "url" }, | |
| 158 | }, | |
| 152 | 159 | background_color: "#fafafa", |
| 153 | 160 | theme_color: "#fafafa", |
| 154 | 161 | icons: [ |
src/web/studio/feed.ts +22 −1
| @@ -92,6 +92,24 @@ export function feedCard( | ||
| 92 | 92 | </article>`; |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | /** | |
| 96 | * What the phone's share sheet handed the manifest's `share_target` (a GET | |
| 97 | * to `/` with `title`, `text` and `url`), as the words to start a tell | |
| 98 | * with. Android often repeats the link inside `text`, so a part already | |
| 99 | * said by another is left out; capped to what a tell holds. | |
| 100 | */ | |
| 101 | export function sharedText(q: URLSearchParams): string { | |
| 102 | const parts: string[] = []; | |
| 103 | for (const k of ["title", "text", "url"]) { | |
| 104 | const v = (q.get(k) ?? "").replace(/\0/g, "").trim(); | |
| 105 | if (v !== "" && !parts.some((p) => p.includes(v))) parts.push(v); | |
| 106 | } | |
| 107 | return parts | |
| 108 | .filter((p, i) => !parts.some((o, j) => j !== i && o !== p && o.includes(p))) | |
| 109 | .join("\n") | |
| 110 | .slice(0, 2000); | |
| 111 | } | |
| 112 | ||
| 95 | 113 | /** |
| 96 | 114 | * The composer: say something to an app's crew, which opens a tell — the |
| 97 | 115 | * home's one primary action. Without a script it posts to the chosen app's |
| @@ -100,6 +118,7 @@ export function feedCard( | ||
| 100 | 118 | * it to, and it is the way to make an app instead. |
| 101 | 119 | */ |
| 102 | 120 | export function composer(ctx: Ctx, user: User): Html { |
| 121 | const shared = sharedText(ctx.url.searchParams); | |
| 103 | 122 | const { repos } = writableAppsOf(ctx, user); |
| 104 | 123 | if (repos.length === 0) |
| 105 | 124 | return empty({ |
| @@ -112,7 +131,7 @@ export function composer(ctx: Ctx, user: User): Html { | ||
| 112 | 131 | const slug = (r: { ownerName: string; name: string }) => `${r.ownerName}/${r.name}`; |
| 113 | 132 | const first = slug(repos[0]!); |
| 114 | 133 | return html`<form id="composer" class="composer" method="post" action="/tell"> |
| 115 | <textarea name="text" rows="2" maxlength="2000" required placeholder="Tell the crew what should change" aria-label="Tell the crew"></textarea> | |
| 134 | <textarea name="text" rows="${shared === "" ? 2 : 4}" maxlength="2000" required placeholder="Tell the crew what should change" aria-label="Tell the crew" ${shared === "" ? "" : "autofocus"}>${shared}</textarea> | |
| 116 | 135 | <div class="composer-bar"> |
| 117 | 136 | ${repos.length === 1 |
| 118 | 137 | ? html`<input type="hidden" name="app" value="${first}" /><span class="muted small">${first}</span>` |
| @@ -263,6 +282,8 @@ ${outboxJs} | ||
| 263 | 282 | e.preventDefault(); |
| 264 | 283 | box.value = ''; |
| 265 | 284 | box.focus(); |
| 285 | // A shared link's words are sent: the address forgets them. | |
| 286 | if (location.search && history.replaceState) history.replaceState(null, '', '/'); | |
| 266 | 287 | }); |
| 267 | 288 | document.addEventListener('keydown', function (e) { |
| 268 | 289 | if (e.key !== 'x' || e.metaKey || e.ctrlKey || e.altKey) return; |
test/home-feed.test.ts +25
| @@ -20,6 +20,7 @@ import { createSession } from "../src/core/sessions.ts"; | ||
| 20 | 20 | import { createToken } from "../src/core/tokens.ts"; |
| 21 | 21 | import { createUser, type User } from "../src/core/users.ts"; |
| 22 | 22 | import { readFeed } from "../src/feed/feed.ts"; |
| 23 | import { sharedText } from "../src/web/studio/feed.ts"; | |
| 23 | 24 | import { serve, type Servers } from "../src/http/server.ts"; |
| 24 | 25 | |
| 25 | 26 | describe("home is the feed", () => { |
| @@ -179,6 +180,30 @@ describe("home is the feed", () => { | ||
| 179 | 180 | expect(owners.map((i) => i.kind).sort()).toEqual(["look", "run"]); |
| 180 | 181 | }); |
| 181 | 182 | |
| 183 | test("a link shared from a phone opens the composer filled in, escaped, and sends as a tell", async () => { | |
| 184 | const m = (await (await fetch(`${servers.url}/manifest.webmanifest`)).json()) as { | |
| 185 | share_target: { action: string; method: string; params: Record<string, string> }; | |
| 186 | }; | |
| 187 | expect(m.share_target.method).toBe("GET"); | |
| 188 | expect(m.share_target.action).toBe("/"); | |
| 189 | expect(m.share_target.params).toEqual({ title: "title", text: "text", url: "url" }); | |
| 190 | const q = new URLSearchParams({ | |
| 191 | title: "Rain gauges", | |
| 192 | text: "Look at this </textarea><script>x</script> https://example.com/g", | |
| 193 | url: "https://example.com/g", | |
| 194 | }); | |
| 195 | const page = await (await get(`/?${q}`, aliceCookie)).text(); | |
| 196 | const box = /<textarea name="text"[^>]*id?[^>]*>([^<]*)<\/textarea>/.exec(page); | |
| 197 | expect(page).not.toContain("<script>x</script>"); | |
| 198 | expect(page).toContain("Rain gauges\nLook at this </textarea>"); | |
| 199 | expect(box === null || box[1]!.includes("Rain gauges")).toBe(true); | |
| 200 | expect(sharedText(new URLSearchParams({ text: "see https://a.b/c", url: "https://a.b/c" }))).toBe( | |
| 201 | "see https://a.b/c", | |
| 202 | ); | |
| 203 | expect(sharedText(new URLSearchParams({ text: "x".repeat(3000) })).length).toBe(2000); | |
| 204 | expect(sharedText(new URLSearchParams())).toBe(""); | |
| 205 | }); | |
| 206 | ||
| 182 | 207 | test("the composer posts a tell without a script, to an app the person may read, and only then", async () => { |
| 183 | 208 | const before = listIssues(app.db, garden.id, { state: "open" }).length; |
| 184 | 209 | const posted = await fetch(`${servers.url}/tell`, { |
new above
offline