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

Closed opened this in answer to
Closed without merging

scout/forge:scout/618-tell-from-a-phone-s-share-sheet-the-forg → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
3 files changed +54 −1
src/web/standards.ts +7
@@ -149,6 +149,13 @@ export function webManifest(ctx: Ctx): Response {
149149 scope: "/",
150150 id: "/",
151151 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 },
152159 background_color: "#fafafa",
153160 theme_color: "#fafafa",
154161 icons: [
src/web/studio/feed.ts +22 −1
@@ -92,6 +92,24 @@ export function feedCard(
9292 </article>`;
9393 }
9494
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
95113 /**
96114 * The composer: say something to an app's crew, which opens a tell — the
97115 * home's one primary action. Without a script it posts to the chosen app's
@@ -100,6 +118,7 @@ export function feedCard(
100118 * it to, and it is the way to make an app instead.
101119 */
102120 export function composer(ctx: Ctx, user: User): Html {
121 const shared = sharedText(ctx.url.searchParams);
103122 const { repos } = writableAppsOf(ctx, user);
104123 if (repos.length === 0)
105124 return empty({
@@ -112,7 +131,7 @@ export function composer(ctx: Ctx, user: User): Html {
112131 const slug = (r: { ownerName: string; name: string }) => `${r.ownerName}/${r.name}`;
113132 const first = slug(repos[0]!);
114133 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>
116135 <div class="composer-bar">
117136 ${repos.length === 1
118137 ? html`<input type="hidden" name="app" value="${first}" /><span class="muted small">${first}</span>`
@@ -263,6 +282,8 @@ ${outboxJs}
263282 e.preventDefault();
264283 box.value = '';
265284 box.focus();
285 // A shared link's words are sent: the address forgets them.
286 if (location.search && history.replaceState) history.replaceState(null, '', '/');
266287 });
267288 document.addEventListener('keydown', function (e) {
268289 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";
2020 import { createToken } from "../src/core/tokens.ts";
2121 import { createUser, type User } from "../src/core/users.ts";
2222 import { readFeed } from "../src/feed/feed.ts";
23 import { sharedText } from "../src/web/studio/feed.ts";
2324 import { serve, type Servers } from "../src/http/server.ts";
2425
2526 describe("home is the feed", () => {
@@ -179,6 +180,30 @@ describe("home is the feed", () => {
179180 expect(owners.map((i) => i.kind).sort()).toEqual(["look", "run"]);
180181 });
181182
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 &lt;/textarea&gt;");
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
182207 test("the composer posts a tell without a script, to an app the person may read, and only then", async () => {
183208 const before = listIssues(app.db, garden.id, { state: "open" }).length;
184209 const posted = await fetch(`${servers.url}/tell`, {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs