Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion apps/server/src/j5/a2a/Migrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ it.effect("tracks J5 A2A migrations independently from upstream migrations", ()
{ migration_id: 35, name: "GuestLinks" },
{ migration_id: 36, name: "OpenLinks" },
{ migration_id: 37, name: "PersonNames" },
{ migration_id: 38, name: "OpenLinkJoins" },
]);
assert.deepStrictEqual(
migrationEntries.map(([id, name]) => [id, name]),
Expand Down Expand Up @@ -132,6 +133,7 @@ it.effect("tracks J5 A2A migrations independently from upstream migrations", ()
[35, "GuestLinks"],
[36, "OpenLinks"],
[37, "PersonNames"],
[38, "OpenLinkJoins"],
],
);
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
Expand Down Expand Up @@ -1380,7 +1382,7 @@ it.effect("recreates earlier-shaped crews tables when 14 runs over them", () =>
applied.map((row) => row.migration_id),
[
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,
36, 37,
36, 37, 38,
],
);
const memberColumns = yield* sql<{ readonly name: string }>`
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/j5/a2a/Migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Migration0034 from "./migrations/034_PeopleAndGrants.ts";
import Migration0035 from "./migrations/035_GuestLinks.ts";
import Migration0036 from "./migrations/036_OpenLinks.ts";
import Migration0037 from "./migrations/037_PersonNames.ts";
import Migration0038 from "./migrations/038_OpenLinkJoins.ts";

export const J5_A2A_MIGRATIONS_TABLE = "j5_a2a_migrations";

Expand Down Expand Up @@ -86,6 +87,7 @@ export const migrationEntries = [
[36, "OpenLinks", Migration0036],
// 37 sits in the layer below 36 in the stack that introduced both; ids are never renumbered.
[37, "PersonNames", Migration0037],
[38, "OpenLinkJoins", Migration0038],
] as const;

const makeMigrationLoader = (throughId?: number) =>
Expand Down
27 changes: 27 additions & 0 deletions apps/server/src/j5/a2a/migrations/038_OpenLinkJoins.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { assert, it } from "@effect/vitest";
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";

import { runJ5A2AMigrations } from "../Migrations.ts";

it.effect("records each person who joined by an open link as one join of that link", () =>
Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;
yield* runJ5A2AMigrations({ toMigrationInclusive: 37 });
const person = (id: string, link: string | null, at: string) => sql`
INSERT INTO j5_a2a_human_person (
person_id, is_local_operator, created_at, kind, display_name, joined_open_link_id
) VALUES (${id}, 0, ${at}, 'guest', ${id}, ${link})
`;
yield* person("human:named", null, "2026-10-01T00:00:00.000Z");
yield* person("human:joined", "link-a", "2026-10-02T00:00:00.000Z");

yield* runJ5A2AMigrations();

assert.deepStrictEqual(
yield* sql`SELECT link_id, person_id, joined_at FROM j5_a2a_open_link_join`,
[{ link_id: "link-a", person_id: "human:joined", joined_at: "2026-10-02T00:00:00.000Z" }],
);
}).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))),
);
31 changes: 31 additions & 0 deletions apps/server/src/j5/a2a/migrations/038_OpenLinkJoins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as Effect from "effect/Effect";
import * as SqlClient from "effect/sql/SqlClient";

// Each time a person joins a thread through an open link (#614). A guest already signed in who
// opens a second link joins that link's thread as themselves, so one person can come through
// several links, and each link's list of who came through it, and its cap, count them.
//
// `j5_a2a_human_person.joined_open_link_id` stays: it is the link a person first came to the
// server through, which is what made their name one they gave themselves. The joins recorded
// before this are that link's.
export default Effect.gen(function* () {
const sql = yield* SqlClient.SqlClient;

// No foreign key to the link: a link's record may go while the people who joined with it stay.
yield* sql`
CREATE TABLE j5_a2a_open_link_join (
link_id TEXT NOT NULL,
person_id TEXT NOT NULL,
joined_at TEXT NOT NULL,
PRIMARY KEY (link_id, person_id)
)
`;
yield* sql`
CREATE INDEX j5_a2a_open_link_join_person_idx ON j5_a2a_open_link_join(person_id)
`;
yield* sql`
INSERT INTO j5_a2a_open_link_join (link_id, person_id, joined_at)
SELECT joined_open_link_id, person_id, created_at FROM j5_a2a_human_person
WHERE joined_open_link_id IS NOT NULL
`;
});
4 changes: 4 additions & 0 deletions apps/server/src/j5/sharing/GuestProjectIcons.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ it.layer(testLayer)("a guest and a project's favicon", (it) => {
joined_open_link_id
) VALUES (${`human:icon-full-${index}`}, 0, ${AT}, 'guest', NULL, 1, ${full.linkId})
`;
yield* sql`
INSERT INTO j5_a2a_open_link_join (link_id, person_id, joined_at)
VALUES (${full.linkId}, ${`human:icon-full-${index}`}, ${AT})
`;
}
yield* openLinks.revoke(link.linkId);
const refusals = [];
Expand Down
54 changes: 34 additions & 20 deletions apps/server/src/j5/sharing/OpenLinkHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ import { GUEST_SESSION_SUBJECT } from "./PersonAccess.ts";
export const MEMBER_OPENED_OPEN_LINK =
"You are already signed in to this server as a member. This link is for guests: send it to them rather than opening it.";

/** What someone joining is told when the name they typed is someone else's, or their own. */
export const OPEN_LINK_NAME_TAKEN =
"That name is already in use on this server. If it is yours, open this link on the device you joined with, or ask for a link for this device.";

const decodeJoin = Schema.decodeUnknownEffect(SharingJoinRequest);
const decodeDescribe = Schema.decodeUnknownEffect(SharingDescribeRequest);

Expand All @@ -42,12 +46,18 @@ export const openLinkHttpRouteLayer = Layer.unwrap(
const openLinks = wired.value;
const auth = yield* Effect.serviceOption(EnvironmentAuth.EnvironmentAuth);
const environment = yield* Effect.serviceOption(ServerEnvironment.ServerEnvironment);
/** The session a browser's cookie carries, when it carries one that works. */
const sessionOf = (request: HttpServerRequest.HttpServerRequest) =>
Option.isNone(auth)
? Effect.succeed(null)
: auth.value.authenticateHttpRequest(request).pipe(Effect.orElseSucceed(() => null));

/**
* What a link opens, asked before pairing or joining so the page can say what the person is
* about to do. It spends and changes nothing. A link that would not work now gets the join
* route's one refusal, whatever is wrong with it. A member's browser gets the same answer as
* anyone: it is refused at the next step, where it would pair or join.
* anyone: it is refused at the next step, where it would pair or join. A guest's browser is
* also told whom it can join as.
*/
const describeRoute = HttpRouter.add(
"POST",
Expand All @@ -60,7 +70,8 @@ export const openLinkHttpRouteLayer = Layer.unwrap(
const serverName = Option.isNone(environment)
? ""
: (yield* environment.value.getDescriptor).label;
return yield* openLinks.describe(decoded.value.link).pipe(
const session = yield* sessionOf(request);
return yield* openLinks.describe(decoded.value.link, session).pipe(
Effect.flatMap((described) =>
// Each entry by its titles and icon; its project's id stays here. The favicon link
// is the one a guest's own client is given once paired (`GuestProjectIcons.ts`):
Expand Down Expand Up @@ -105,37 +116,40 @@ export const openLinkHttpRouteLayer = Layer.unwrap(
const request = yield* HttpServerRequest.HttpServerRequest;
// The route asks for no sign-in, but a browser sends its cookie. A member who opens the
// link would be recorded as a new guest and then paired as one, so they are told
// instead, and no one is recorded. Anyone else, signed in or not, goes on.
const member = Option.isNone(auth)
? false
: yield* auth.value.authenticateHttpRequest(request).pipe(
Effect.map((session) => session.subject !== GUEST_SESSION_SUBJECT),
Effect.orElseSucceed(() => false),
);
if (member) return refusal(409, "signed_in_as_member", MEMBER_OPENED_OPEN_LINK);
// instead, and no one is recorded. A guest may join as themselves. Anyone else, signed
// in or not, gives a name and goes on.
const session = yield* sessionOf(request);
if (session !== null && session.subject !== GUEST_SESSION_SUBJECT) {
return refusal(409, "signed_in_as_member", MEMBER_OPENED_OPEN_LINK);
}
const decoded = yield* request.json.pipe(Effect.flatMap(decodeJoin), Effect.option);
if (Option.isNone(decoded)) {
return refusal(
400,
"invalid_request",
"A link is needed, and a name: one line of ordinary characters, up to 80 of them, with no square brackets.",
"A link is needed, and any name given is one line of ordinary characters, up to 80 of them, with no square brackets.",
);
}
return yield* openLinks.join(decoded.value).pipe(
Effect.map((link) =>
HttpServerResponse.jsonUnsafe({
credential: link.credential,
expiresAt: link.expiresAt,
}),
const { token, displayName } = decoded.value;
return yield* openLinks.join({ token, displayName, session }).pipe(
Effect.map((joined) =>
joined.kind === "new"
? HttpServerResponse.jsonUnsafe({
credential: joined.link.credential,
expiresAt: joined.link.expiresAt,
})
: // Already paired: the browser goes on to the thread it was just given.
HttpServerResponse.jsonUnsafe({ threadId: joined.threadId }),
),
Effect.catchTags({
OpenLinkRefusedError: (error) =>
Effect.succeed(refusal(404, "link_refused", error.message)),
OpenLinkNameError: (error) =>
Effect.succeed(refusal(400, "invalid_request", error.message)),
// Told only to the holder of a link that works, who is about to be let in.
PersonNameTakenError: (error) =>
Effect.succeed(refusal(409, "name_taken", error.message)),
// Told only to the holder of a link that works, who is about to be let in. A name is
// no proof of who someone is, so a person on another device is not let in by theirs.
PersonNameTakenError: () =>
Effect.succeed(refusal(409, "name_taken", OPEN_LINK_NAME_TAKEN)),
}),
Effect.catch((cause) =>
Effect.logError("J5 open link join failed", { cause }).pipe(
Expand Down
Loading
Loading