copilotz 1.0.0-rc.20 → 1.0.0-rc.21
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 +417 -0
- package/README.md +295 -19
- package/hosts/orgs/mod.d.ts +4 -1
- package/hosts/orgs/mod.js +2 -2
- package/hosts/web/files.d.ts +6 -2
- package/hosts/web/files.js +36 -3
- package/hosts/web/node/mod.d.ts +1 -1
- package/hosts/web/node/mod.js +1 -1
- package/hosts/web/node/serve.d.ts +26 -0
- package/hosts/web/node/serve.js +23 -1
- package/hosts/web/routes.d.ts +28 -1
- package/hosts/web/routes.js +65 -10
- package/hosts/web/server.d.ts +5 -4
- package/hosts/web/server.js +1 -1
- package/package.json +2 -2
- package/plugins/access/accounts/collections/account.d.ts +2 -2
- package/plugins/access/contacts/actions/chat.d.ts +1 -0
- package/plugins/access/contacts/actions/chat.js +45 -0
- package/plugins/access/contacts/actions/forget.d.ts +1 -0
- package/plugins/access/contacts/actions/forget.js +56 -0
- package/plugins/access/contacts/actions/start.d.ts +1 -0
- package/plugins/access/contacts/actions/start.js +50 -0
- package/plugins/access/contacts/collections/contact.d.ts +81 -0
- package/plugins/access/contacts/collections/contact.js +58 -0
- package/plugins/access/contacts/contract.d.ts +10 -0
- package/plugins/access/contacts/contract.js +10 -0
- package/plugins/access/contacts/mod.d.ts +27 -0
- package/plugins/access/contacts/mod.js +49 -0
- package/plugins/access/contacts/views/contact.d.ts +8 -0
- package/plugins/access/contacts/views/contact.js +25 -0
- package/plugins/access/contacts/web.d.ts +7 -0
- package/plugins/access/contacts/web.js +11 -0
- package/plugins/access/directory/collections/credit-code.d.ts +1 -1
- package/plugins/access/directory/collections/invite.d.ts +1 -1
- package/plugins/access/identity/mod.js +8 -6
- package/plugins/access/mod.d.ts +7 -1
- package/plugins/access/mod.js +7 -2
- package/plugins/access/spaces/actions/agents.js +12 -6
- package/plugins/access/spaces/actions/create.js +10 -1
- package/plugins/access/spaces/actions/invitation.js +5 -2
- package/plugins/access/spaces/actions/keep.d.ts +1 -0
- package/plugins/access/spaces/actions/keep.js +55 -0
- package/plugins/access/spaces/actions/member.js +17 -4
- package/plugins/access/spaces/actions/rename.js +4 -0
- package/plugins/access/spaces/collections/member.d.ts +1 -1
- package/plugins/access/spaces/collections/request.d.ts +1 -1
- package/plugins/access/spaces/collections/space.d.ts +9 -0
- package/plugins/access/spaces/collections/space.js +2 -0
- package/plugins/access/spaces/contract.d.ts +4 -3
- package/plugins/access/spaces/contract.js +1 -1
- package/plugins/access/spaces/mod.d.ts +7 -1
- package/plugins/access/spaces/mod.js +8 -1
- package/plugins/access/spaces/resources/tools.d.ts +0 -5
- package/plugins/access/spaces/resources/tools.js +11 -1
- package/plugins/access/spaces/shared/agents.js +2 -2
- package/plugins/access/spaces/shared/can.d.ts +18 -3
- package/plugins/access/spaces/shared/can.js +32 -5
- package/plugins/access/spaces/shared/declared.d.ts +21 -0
- package/plugins/access/spaces/shared/declared.js +8 -0
- package/plugins/access/spaces/shared/labels.d.ts +7 -1
- package/plugins/access/spaces/shared/labels.js +56 -8
- package/plugins/access/spaces/shared/managers.js +2 -2
- package/plugins/access/spaces/shared/names.d.ts +9 -5
- package/plugins/access/spaces/shared/names.js +36 -19
- package/plugins/access/spaces/shared/reach.d.ts +20 -6
- package/plugins/access/spaces/shared/reach.js +62 -20
- package/plugins/access/spaces/shared/rules.d.ts +10 -5
- package/plugins/access/spaces/shared/rules.js +35 -10
- package/plugins/access/spaces/shared/seat.d.ts +3 -2
- package/plugins/access/spaces/shared/seat.js +9 -2
- package/plugins/access/spaces/views/where.d.ts +4 -2
- package/plugins/access/spaces/views/where.js +9 -0
- package/plugins/access/spaces/web.js +1 -1
- package/plugins/access/web.d.ts +1 -0
- package/plugins/access/web.js +1 -0
- package/plugins/agents/chat/actions/erase.d.ts +1 -0
- package/plugins/agents/chat/actions/erase.js +51 -0
- package/plugins/agents/chat/actions/open.d.ts +1 -0
- package/plugins/agents/chat/actions/open.js +49 -0
- package/plugins/agents/chat/actions/quiet.d.ts +1 -0
- package/plugins/agents/chat/actions/quiet.js +33 -0
- package/plugins/agents/chat/actions/say.d.ts +1 -0
- package/plugins/agents/chat/actions/say.js +82 -0
- package/plugins/agents/chat/actions/send.js +57 -19
- package/plugins/agents/chat/actions/shown.d.ts +1 -0
- package/plugins/agents/chat/actions/shown.js +35 -0
- package/plugins/agents/chat/collections/intent.d.ts +124 -0
- package/plugins/agents/chat/collections/intent.js +52 -0
- package/plugins/agents/chat/collections/message.d.ts +74 -1
- package/plugins/agents/chat/collections/message.js +23 -1
- package/plugins/agents/chat/collections/shown.d.ts +105 -0
- package/plugins/agents/chat/collections/shown.js +34 -0
- package/plugins/agents/chat/contract.d.ts +15 -7
- package/plugins/agents/chat/contract.js +7 -1
- package/plugins/agents/chat/mod.d.ts +20 -2
- package/plugins/agents/chat/mod.js +43 -4
- package/plugins/agents/chat/processors/failed.js +15 -5
- package/plugins/agents/chat/processors/turn.d.ts +1 -1
- package/plugins/agents/chat/processors/turn.js +95 -35
- package/plugins/agents/chat/shared/delivery.d.ts +16 -2
- package/plugins/agents/chat/shared/delivery.js +62 -7
- package/plugins/agents/chat/shared/send.d.ts +1 -3
- package/plugins/agents/chat/shared/send.js +2 -9
- package/plugins/agents/chat/shared/tools.d.ts +33 -0
- package/plugins/agents/chat/shared/tools.js +28 -0
- package/plugins/agents/chat/shared/types.d.ts +59 -1
- package/plugins/agents/chat/views/message.d.ts +4 -2
- package/plugins/agents/chat/views/message.js +22 -4
- package/plugins/agents/chat/views/prompt.js +38 -4
- package/plugins/agents/history/collections/outline.d.ts +1 -1
- package/plugins/agents/history/collections/summary.d.ts +1 -1
- package/plugins/agents/history/mod.js +2 -1
- package/plugins/agents/history/processors/erase.d.ts +3 -0
- package/plugins/agents/history/processors/erase.js +11 -0
- package/plugins/agents/memory/mod.js +2 -1
- package/plugins/agents/memory/processors/extract.d.ts +1 -1
- package/plugins/agents/memory/processors/extract.js +5 -0
- package/plugins/agents/memory/resources/tools.js +1 -1
- package/plugins/agents/memory/shared/settings.d.ts +7 -2
- package/plugins/agents/memory/shared/settings.js +9 -1
- package/plugins/agents/routines/collections/routine.d.ts +1 -1
- package/plugins/agents/routines/collections/run.d.ts +1 -1
- package/plugins/compute/apps/collections/app.d.ts +2 -2
- package/plugins/compute/cloud/resources/tools.js +3 -3
- package/plugins/compute/computers/collections/approval.d.ts +2 -2
- package/plugins/compute/computers/collections/grant.d.ts +1 -1
- package/plugins/compute/computers/collections/preview.d.ts +2 -2
- package/plugins/compute/computers/program/version.d.ts +1 -1
- package/plugins/compute/computers/program/version.js +1 -1
- package/plugins/compute/computers/resources/downloads.js +1 -1
- package/plugins/compute/computers/shared/wake.d.ts +1 -1
- package/plugins/compute/computers/shared/wake.js +17 -0
- package/plugins/interfaces/api/collections/key.d.ts +1 -1
- package/plugins/interfaces/api/shared/events.js +5 -4
- package/plugins/interfaces/maintenance/actions/read.d.ts +4 -0
- package/plugins/interfaces/maintenance/actions/read.js +76 -0
- package/plugins/interfaces/maintenance/actions/write.d.ts +4 -0
- package/plugins/interfaces/maintenance/actions/write.js +64 -0
- package/plugins/interfaces/maintenance/contract.d.ts +60 -0
- package/plugins/interfaces/maintenance/contract.js +2 -0
- package/plugins/interfaces/maintenance/mod.d.ts +52 -0
- package/plugins/interfaces/maintenance/mod.js +66 -0
- package/plugins/interfaces/maintenance/resources/routes.d.ts +71 -0
- package/plugins/interfaces/maintenance/resources/routes.js +457 -0
- package/plugins/interfaces/maintenance/shared/ops.d.ts +4 -0
- package/plugins/interfaces/maintenance/shared/ops.js +19 -0
- package/plugins/interfaces/mcp/actions/call.d.ts +11 -0
- package/plugins/interfaces/mcp/actions/call.js +304 -0
- package/plugins/interfaces/mcp/actions/prune.d.ts +1 -0
- package/plugins/interfaces/mcp/actions/prune.js +18 -0
- package/plugins/interfaces/mcp/collections/closed.d.ts +23 -0
- package/plugins/interfaces/mcp/collections/closed.js +17 -0
- package/plugins/interfaces/mcp/collections/rate.d.ts +27 -0
- package/plugins/interfaces/mcp/collections/rate.js +18 -0
- package/plugins/interfaces/mcp/collections/receipt.d.ts +46 -0
- package/plugins/interfaces/mcp/collections/receipt.js +46 -0
- package/plugins/interfaces/mcp/contract.d.ts +9 -0
- package/plugins/interfaces/mcp/contract.js +9 -0
- package/plugins/interfaces/mcp/mod.d.ts +84 -0
- package/plugins/interfaces/mcp/mod.js +122 -0
- package/plugins/interfaces/mcp/processors/closed.d.ts +13 -0
- package/plugins/interfaces/mcp/processors/closed.js +16 -0
- package/plugins/interfaces/mcp/resources/running.d.ts +21 -0
- package/plugins/interfaces/mcp/resources/running.js +36 -0
- package/plugins/interfaces/mcp/resources/server.d.ts +54 -0
- package/plugins/interfaces/mcp/resources/server.js +423 -0
- package/plugins/interfaces/mcp/resources/tools.d.ts +114 -0
- package/plugins/interfaces/mcp/resources/tools.js +324 -0
- package/plugins/interfaces/mcp/shared/caller.d.ts +45 -0
- package/plugins/interfaces/mcp/shared/caller.js +99 -0
- package/plugins/interfaces/mcp/shared/keys.d.ts +50 -0
- package/plugins/interfaces/mcp/shared/keys.js +53 -0
- package/plugins/interfaces/mcp/shared/settings.d.ts +187 -0
- package/plugins/interfaces/mcp/shared/settings.js +176 -0
- package/plugins/interfaces/mcp/views/offered.d.ts +20 -0
- package/plugins/interfaces/mcp/views/offered.js +20 -0
- package/plugins/interfaces/mod.d.ts +5 -1
- package/plugins/interfaces/mod.js +5 -1
- package/plugins/interfaces/terminal/actions/print.js +1 -1
- package/plugins/interfaces/terminal/actions/set.js +1 -1
- package/plugins/interfaces/terminal/collections/output.d.ts +1 -1
- package/plugins/interfaces/terminal/collections/shell.js +1 -1
- package/plugins/interfaces/voice/actions/block.d.ts +2 -0
- package/plugins/interfaces/voice/actions/block.js +74 -0
- package/plugins/interfaces/voice/actions/call.d.ts +19 -0
- package/plugins/interfaces/voice/actions/call.js +253 -0
- package/plugins/interfaces/voice/actions/hook.d.ts +5 -0
- package/plugins/interfaces/voice/actions/hook.js +225 -0
- package/plugins/interfaces/voice/actions/prune.d.ts +1 -0
- package/plugins/interfaces/voice/actions/prune.js +35 -0
- package/plugins/interfaces/voice/collections/blocked.d.ts +87 -0
- package/plugins/interfaces/voice/collections/blocked.js +34 -0
- package/plugins/interfaces/voice/collections/call.d.ts +279 -0
- package/plugins/interfaces/voice/collections/call.js +77 -0
- package/plugins/interfaces/voice/collections/phone.d.ts +74 -0
- package/plugins/interfaces/voice/collections/phone.js +26 -0
- package/plugins/interfaces/voice/contract.d.ts +14 -0
- package/plugins/interfaces/voice/contract.js +2 -0
- package/plugins/interfaces/voice/mod.d.ts +57 -0
- package/plugins/interfaces/voice/mod.js +118 -0
- package/plugins/interfaces/voice/processors/forget.d.ts +13 -0
- package/plugins/interfaces/voice/processors/forget.js +27 -0
- package/plugins/interfaces/voice/resources/relay.d.ts +77 -0
- package/plugins/interfaces/voice/resources/relay.js +428 -0
- package/plugins/interfaces/voice/resources/tools.d.ts +1 -0
- package/plugins/interfaces/voice/resources/tools.js +25 -0
- package/plugins/interfaces/voice/shared/caller.d.ts +27 -0
- package/plugins/interfaces/voice/shared/caller.js +38 -0
- package/plugins/interfaces/voice/shared/settings.d.ts +136 -0
- package/plugins/interfaces/voice/shared/settings.js +121 -0
- package/plugins/interfaces/voice/shared/signature.d.ts +11 -0
- package/plugins/interfaces/voice/shared/signature.js +34 -0
- package/plugins/interfaces/voice/views/call.d.ts +41 -0
- package/plugins/interfaces/voice/views/call.js +30 -0
- package/plugins/interfaces/voice/views/context.d.ts +9 -0
- package/plugins/interfaces/voice/views/context.js +14 -0
- package/plugins/interfaces/voice/views/message.d.ts +17 -0
- package/plugins/interfaces/voice/views/message.js +41 -0
- package/plugins/interfaces/whatsapp/actions/connect.js +3 -0
- package/plugins/interfaces/whatsapp/actions/disconnect.js +4 -1
- package/plugins/interfaces/whatsapp/actions/hook.js +27 -11
- package/plugins/interfaces/whatsapp/actions/set.d.ts +8 -0
- package/plugins/interfaces/whatsapp/actions/set.js +121 -0
- package/plugins/interfaces/whatsapp/collections/blocked.d.ts +29 -0
- package/plugins/interfaces/whatsapp/collections/blocked.js +22 -0
- package/plugins/interfaces/whatsapp/collections/code.d.ts +1 -1
- package/plugins/interfaces/whatsapp/collections/number.d.ts +90 -4
- package/plugins/interfaces/whatsapp/collections/number.js +36 -4
- package/plugins/interfaces/whatsapp/collections/phone.d.ts +13 -3
- package/plugins/interfaces/whatsapp/collections/phone.js +9 -3
- package/plugins/interfaces/whatsapp/collections/rate.d.ts +27 -0
- package/plugins/interfaces/whatsapp/collections/rate.js +13 -0
- package/plugins/interfaces/whatsapp/collections/sent.d.ts +24 -12
- package/plugins/interfaces/whatsapp/collections/sent.js +11 -7
- package/plugins/interfaces/whatsapp/collections/thread.d.ts +14 -6
- package/plugins/interfaces/whatsapp/collections/thread.js +7 -5
- package/plugins/interfaces/whatsapp/collections/typing.d.ts +70 -0
- package/plugins/interfaces/whatsapp/collections/typing.js +21 -0
- package/plugins/interfaces/whatsapp/contract.d.ts +27 -4
- package/plugins/interfaces/whatsapp/contract.js +34 -1
- package/plugins/interfaces/whatsapp/events/undelivered.d.ts +8 -0
- package/plugins/interfaces/whatsapp/events/undelivered.js +17 -0
- package/plugins/interfaces/whatsapp/mod.d.ts +28 -2
- package/plugins/interfaces/whatsapp/mod.js +65 -6
- package/plugins/interfaces/whatsapp/processors/forget.d.ts +6 -0
- package/plugins/interfaces/whatsapp/processors/forget.js +11 -0
- package/plugins/interfaces/whatsapp/processors/receive.d.ts +17 -3
- package/plugins/interfaces/whatsapp/processors/receive.js +264 -81
- package/plugins/interfaces/whatsapp/processors/reply.d.ts +25 -5
- package/plugins/interfaces/whatsapp/processors/reply.js +216 -20
- package/plugins/interfaces/whatsapp/processors/typing.d.ts +8 -0
- package/plugins/interfaces/whatsapp/processors/typing.js +25 -0
- package/plugins/interfaces/whatsapp/shared/customers.d.ts +13 -0
- package/plugins/interfaces/whatsapp/shared/customers.js +16 -0
- package/plugins/interfaces/whatsapp/shared/graph.d.ts +31 -6
- package/plugins/interfaces/whatsapp/shared/graph.js +117 -10
- package/plugins/interfaces/whatsapp/shared/media.d.ts +23 -0
- package/plugins/interfaces/whatsapp/shared/media.js +46 -0
- package/plugins/interfaces/whatsapp/shared/number.d.ts +19 -8
- package/plugins/interfaces/whatsapp/shared/number.js +64 -7
- package/plugins/interfaces/whatsapp/shared/parts.d.ts +94 -0
- package/plugins/interfaces/whatsapp/shared/parts.js +184 -0
- package/plugins/interfaces/whatsapp/shared/payload.d.ts +27 -1
- package/plugins/interfaces/whatsapp/shared/payload.js +36 -1
- package/plugins/interfaces/whatsapp/shared/settings.d.ts +49 -1
- package/plugins/interfaces/whatsapp/shared/settings.js +11 -0
- package/plugins/interfaces/whatsapp/views/message.d.ts +13 -0
- package/plugins/interfaces/whatsapp/views/message.js +13 -0
- package/plugins/interfaces/whatsapp/views/web/line.d.ts +24 -0
- package/plugins/interfaces/whatsapp/views/web/line.js +44 -0
- package/plugins/interfaces/whatsapp/views/web/space.d.ts +7 -5
- package/plugins/interfaces/whatsapp/views/web/space.js +17 -10
- package/plugins/interfaces/whatsapp/views/web/style.d.ts +1 -1
- package/plugins/interfaces/whatsapp/views/web/style.js +8 -0
- package/plugins/models/adapters/llm/anthropic.d.ts +1 -1
- package/plugins/models/adapters/llm/anthropic.js +28 -15
- package/plugins/models/adapters/llm/gemini.d.ts +1 -1
- package/plugins/models/adapters/llm/gemini.js +2 -1
- package/plugins/models/adapters/llm/openai-compatible.d.ts +1 -1
- package/plugins/models/adapters/llm/openai-compatible.js +2 -1
- package/plugins/models/adapters/llm/openai.d.ts +1 -1
- package/plugins/models/adapters/llm/openai.js +16 -4
- package/plugins/models/shared/types.d.ts +8 -0
- package/plugins/models/shared/types.js +10 -0
- package/runtime/actor.d.ts +6 -0
- package/runtime/actor.js +9 -4
- package/runtime/aggregate.js +8 -2
- package/runtime/blobs.d.ts +15 -12
- package/runtime/blobs.js +58 -11
- package/runtime/cache.d.ts +6 -0
- package/runtime/cache.js +19 -1
- package/runtime/define.d.ts +10 -0
- package/runtime/errors.d.ts +20 -2
- package/runtime/errors.js +7 -1
- package/runtime/executor.js +27 -11
- package/runtime/links.d.ts +31 -0
- package/runtime/links.js +60 -0
- package/runtime/maintain.d.ts +88 -0
- package/runtime/maintain.js +120 -0
- package/runtime/memory.js +7 -4
- package/runtime/mod.d.ts +6 -4
- package/runtime/mod.js +5 -2
- package/runtime/mongo.d.ts +13 -0
- package/runtime/mongo.js +39 -8
- package/runtime/observe.d.ts +2 -0
- package/runtime/observe.js +14 -10
- package/runtime/ops.d.ts +5 -0
- package/runtime/ops.js +10 -1
- package/runtime/owner.d.ts +18 -1
- package/runtime/owner.js +127 -61
- package/runtime/presence.d.ts +18 -0
- package/runtime/presence.js +25 -0
- package/runtime/registry.d.ts +5 -0
- package/runtime/registry.js +18 -0
- package/runtime/runtime.js +9 -3
- package/runtime/store.d.ts +48 -1
- package/runtime/wire.d.ts +1 -0
- package/runtime/wire.js +3 -1
- package/stores/blobs/files/mod.js +4 -2
- package/stores/blobs/gcs/mod.d.ts +3 -0
- package/stores/blobs/gcs/mod.js +6 -5
- package/stores/blobs/http/mod.d.ts +1 -1
- package/stores/blobs/http/mod.js +2 -1
- package/stores/latency/mod.js +5 -1
- package/stores/sql/postgres/maintenance.d.ts +24 -0
- package/stores/sql/postgres/maintenance.js +258 -0
- package/stores/sql/postgres/mod.d.ts +20 -4
- package/stores/sql/postgres/mod.js +363 -96
- package/stores/sql/postgres/update.js +3 -1
- package/stores/sql/shared/capped.d.ts +2 -0
- package/stores/sql/shared/capped.js +22 -0
- package/stores/sql/shared/rows.d.ts +14 -0
- package/stores/sql/shared/rows.js +29 -0
- package/stores/sql/shared/statement.d.ts +12 -0
- package/stores/sql/shared/statement.js +244 -0
- package/stores/sql/sqlite/apart.d.ts +11 -0
- package/stores/sql/sqlite/apart.js +219 -0
- package/stores/sql/sqlite/mod.d.ts +16 -2
- package/stores/sql/sqlite/mod.js +246 -51
- package/testing/mod.js +1 -1
|
@@ -1,16 +1,35 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Whether the caller may do something in a space (`read`, `write`, `manage`):
|
|
3
|
-
* what reach says, with every limit on them
|
|
4
|
-
*
|
|
3
|
+
* what reach says, with every limit on them; and whether they're there at all
|
|
4
|
+
* (`present`: the space, or a document in it, such as a contact's chat).
|
|
5
|
+
* Trusted code asks here before it goes on as the system, and no one else
|
|
6
|
+
* works this out.
|
|
5
7
|
*/
|
|
6
8
|
import { forbidden, personOf } from "../../../../runtime/mod.js";
|
|
7
|
-
import { reachOf } from "./reach.js";
|
|
8
|
-
export async function may(ctx, verb, space
|
|
9
|
+
import { docKey, reachOf } from "./reach.js";
|
|
10
|
+
export async function may(ctx, verb, space,
|
|
11
|
+
/** A document of this collection, its id `space`, shared on its own: asked about as itself. */
|
|
12
|
+
on) {
|
|
9
13
|
const { actor } = ctx;
|
|
10
14
|
if (!actor || actor.kind === "system")
|
|
11
15
|
return true;
|
|
12
16
|
// Read as the system: what's read to decide labels nothing the caller then writes.
|
|
13
|
-
|
|
17
|
+
const held = (await reachOf(actor, ctx.as())).get(on ? docKey(on, space) : space);
|
|
18
|
+
// A document shared on its own is never a space: only asked about as that document.
|
|
19
|
+
return !!held && !!held.on === !!on && held.can.has(verb);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Whether the caller is in `space` at all: they reach it, or a document in it
|
|
23
|
+
* on its own (a contact, its chat there). What's said to anyone there (who
|
|
24
|
+
* answers in it) is said to them too.
|
|
25
|
+
*/
|
|
26
|
+
export async function present(ctx, space) {
|
|
27
|
+
const { actor } = ctx;
|
|
28
|
+
if (!actor || actor.kind === "system")
|
|
29
|
+
return true;
|
|
30
|
+
const reach = await reachOf(actor, ctx.as());
|
|
31
|
+
return (!!reach.get(space) && !reach.get(space).on) ||
|
|
32
|
+
[...reach.values()].some((held) => held.on && held.home === space);
|
|
14
33
|
}
|
|
15
34
|
/** Throws `forbidden`, with `message`, unless the caller may. */
|
|
16
35
|
export async function must(ctx, verb, space, message = `You can't ${verb} in that space.`) {
|
|
@@ -22,3 +41,11 @@ export async function mustOwnOr(ctx, owner, verb, space, message) {
|
|
|
22
41
|
if (owner !== personOf(ctx.actor))
|
|
23
42
|
await must(ctx, verb, space, message);
|
|
24
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Who a document is shared with on its own (a chat's contacts:
|
|
46
|
+
* `contact:c1`), read as the system: none for one that isn't.
|
|
47
|
+
*/
|
|
48
|
+
export async function sharedWith(ctx, on, id) {
|
|
49
|
+
const found = await ctx.as().db.collection("member").find({ space: docKey(on, id) });
|
|
50
|
+
return found.map((m) => m.member);
|
|
51
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spaces the app declares (the `spaces` resource, by id): its own, owned by
|
|
3
|
+
* nobody but the system, made and kept as declared when something needs them
|
|
4
|
+
* (`space.keep`: a WhatsApp line the app declares keeps its space). Each is a
|
|
5
|
+
* space like any other (reach, rules, members): its name and its agents are
|
|
6
|
+
* the app's, so people neither rename it nor take its agents out.
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* resources: { spaces: { line: { name: "Mobifácil", agents: ["booking"] } } }
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
import type { Context } from "../../../../runtime/mod.js";
|
|
13
|
+
/** A declared space: its name, and the agents working there (editors; the first answers new chats). */
|
|
14
|
+
export type DeclaredSpace = {
|
|
15
|
+
name: string;
|
|
16
|
+
agents?: string[];
|
|
17
|
+
};
|
|
18
|
+
/** The space `id`, as the app declares it, if it does. */
|
|
19
|
+
export declare const declaredOf: (ctx: Pick<Context, "resources">, id: string) => DeclaredSpace | undefined;
|
|
20
|
+
/** Whether `agent` is one of the agents the app declares in space `id`. */
|
|
21
|
+
export declare const declaredAgent: (ctx: Pick<Context, "resources">, id: string, agent: string) => boolean;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** The space `id`, as the app declares it, if it does. */
|
|
2
|
+
export const declaredOf = (ctx, id) => {
|
|
3
|
+
const spaces = (ctx.resources.spaces ?? {});
|
|
4
|
+
// Its own keys only: "constructor" or "toString" is no space the app declares.
|
|
5
|
+
return Object.hasOwn(spaces, id) ? spaces[id] ?? undefined : undefined;
|
|
6
|
+
};
|
|
7
|
+
/** Whether `agent` is one of the agents the app declares in space `id`. */
|
|
8
|
+
export const declaredAgent = (ctx, id, agent) => !!declaredOf(ctx, id)?.agents?.includes(agent);
|
|
@@ -8,7 +8,13 @@
|
|
|
8
8
|
* who does shares it (`share`). An agent writing that anywhere else, or into
|
|
9
9
|
* a collection that keeps no labels, is refused (`needs_share`): a person has
|
|
10
10
|
* to choose to show it there. Documents that are members (agents people
|
|
11
|
-
* make) carry nothing: they're meant for
|
|
11
|
+
* make, contacts) carry nothing: they're meant for whoever they work with.
|
|
12
|
+
*
|
|
13
|
+
* A document that may be shared on its own (a chat: other collections'
|
|
14
|
+
* documents are its parts) labels as itself, and so do its parts (a chat's
|
|
15
|
+
* messages), so a write into one shared with someone outside its space (a
|
|
16
|
+
* contact) knows what they can't see: anything else read, even from the
|
|
17
|
+
* same space, labels it with that space, which they don't reach.
|
|
12
18
|
*/
|
|
13
19
|
import type { Labels } from "../../../../runtime/mod.js";
|
|
14
20
|
export declare const labels: Labels;
|
|
@@ -1,6 +1,24 @@
|
|
|
1
|
-
import { reachOf } from "./reach.js";
|
|
1
|
+
import { docKey, homeOf, reachOf } from "./reach.js";
|
|
2
2
|
import { nameOf, namesOf, PROFILE } from "./names.js";
|
|
3
|
+
/** The `shared` resource; an entry is a collection's own key, never what objects inherit. */
|
|
3
4
|
const sharedOf = (ctx) => (ctx.resources.shared ?? {});
|
|
5
|
+
const entryOf = (shared, collection) => Object.hasOwn(shared, collection) ? shared[collection] : undefined;
|
|
6
|
+
/** A document's own label: `#<collection>:<id>`, apart from spaces' ids. */
|
|
7
|
+
const OWN = "#";
|
|
8
|
+
/**
|
|
9
|
+
* The document that `doc` of `collection` is shared with, as a label's key:
|
|
10
|
+
* the one it's part of (a message's chat), or itself, when it's one others
|
|
11
|
+
* are parts of (a chat). Others are shared only through their space.
|
|
12
|
+
*/
|
|
13
|
+
function wholeOf(collection, doc, shared) {
|
|
14
|
+
const part = entryOf(shared, collection)?.part;
|
|
15
|
+
if (part)
|
|
16
|
+
return typeof doc[part] === "string" ? docKey(part, doc[part]) : undefined;
|
|
17
|
+
const whole = Object.values(shared).some((entry) => entry?.part === collection);
|
|
18
|
+
return whole && typeof doc._id === "string" ? docKey(collection, doc._id) : undefined;
|
|
19
|
+
}
|
|
20
|
+
/** Who's a member of a document on its own (`thread:t1`): contacts in a chat, say. */
|
|
21
|
+
const membersOn = async (key, ctx) => new Set((await ctx.db.collection("member").find({ space: key })).map((m) => m.member));
|
|
4
22
|
const needsShare = (from, named) => Object.assign(new Error(`That would show what came from ${named.join(", ")} to people who can't see it: ` +
|
|
5
23
|
"a person who can has to share it."), { code: "needs_share", sources: from });
|
|
6
24
|
/** Spaces as people say them: by name; a Personal space as its owner's (by their name). */
|
|
@@ -25,23 +43,53 @@ async function described(ids, ctx) {
|
|
|
25
43
|
const covered = async (space, ctx) => new Set([space, ...(await reachOf({ kind: "space", id: space }, ctx)).keys()]);
|
|
26
44
|
export const labels = {
|
|
27
45
|
read(collection, docs, ctx) {
|
|
28
|
-
const
|
|
46
|
+
const all = sharedOf(ctx);
|
|
47
|
+
const shared = entryOf(all, collection);
|
|
29
48
|
// A document that's a member (an agent) is meant for everywhere it works: reading it labels nothing.
|
|
30
49
|
if (!shared || shared.member)
|
|
31
50
|
return [];
|
|
32
51
|
const field = shared.field ?? "in";
|
|
33
|
-
return docs.flatMap((doc) =>
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
52
|
+
return docs.flatMap((doc) => {
|
|
53
|
+
const whole = wholeOf(collection, doc, all);
|
|
54
|
+
return [
|
|
55
|
+
...(whole
|
|
56
|
+
? [`${OWN}${whole}`]
|
|
57
|
+
: typeof doc[field] === "string"
|
|
58
|
+
? [doc[field]]
|
|
59
|
+
: []),
|
|
60
|
+
...(Array.isArray(doc.sources) ? doc.sources : []),
|
|
61
|
+
];
|
|
62
|
+
});
|
|
37
63
|
},
|
|
38
64
|
async write(actor, collection, doc, read, ctx) {
|
|
39
|
-
const
|
|
65
|
+
const all = sharedOf(ctx);
|
|
66
|
+
const shared = entryOf(all, collection);
|
|
40
67
|
const space = shared ? doc[shared.field ?? "in"] : undefined;
|
|
41
68
|
if (typeof space !== "string")
|
|
42
69
|
return doc;
|
|
43
70
|
const seen = await covered(space, ctx);
|
|
44
|
-
|
|
71
|
+
// Who else it's for: whoever the document it goes in is shared with on its own (a contact).
|
|
72
|
+
const whole = wholeOf(collection, doc, all);
|
|
73
|
+
const outside = whole ? await membersOn(whole, ctx) : new Set();
|
|
74
|
+
const found = new Set();
|
|
75
|
+
for (const label of read) {
|
|
76
|
+
if (!label.startsWith(OWN)) {
|
|
77
|
+
// A space: its members see it; someone in only this document doesn't.
|
|
78
|
+
if (!seen.has(label) || outside.size)
|
|
79
|
+
found.add(label);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const key = label.slice(OWN.length);
|
|
83
|
+
if (key === whole)
|
|
84
|
+
continue;
|
|
85
|
+
const home = await homeOf(key, ctx);
|
|
86
|
+
if (!home)
|
|
87
|
+
continue;
|
|
88
|
+
const on = outside.size ? await membersOn(key, ctx) : outside;
|
|
89
|
+
if (!seen.has(home) || [...outside].some((m) => !on.has(m)))
|
|
90
|
+
found.add(home);
|
|
91
|
+
}
|
|
92
|
+
const foreign = [...found];
|
|
45
93
|
if (!foreign.length)
|
|
46
94
|
return doc;
|
|
47
95
|
const elsewhere = typeof actor.at === "string" && actor.at !== space;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { forbidden } from "../../../../runtime/mod.js";
|
|
2
|
-
import {
|
|
2
|
+
import { rolesOf } from "./reach.js";
|
|
3
3
|
/** Throws if `key` (`person:ana`) is `space`'s last manager and `next` (their role after, if any) doesn't manage. */
|
|
4
4
|
export async function keepsAManager(ctx, space, key, next) {
|
|
5
|
-
const roles =
|
|
5
|
+
const roles = rolesOf(ctx);
|
|
6
6
|
const manages = (role) => !!role && (roles[role] ?? []).includes("manage");
|
|
7
7
|
if (manages(next))
|
|
8
8
|
return;
|
|
@@ -32,12 +32,16 @@ export declare function withNames(text: string, names: Record<string, string>):
|
|
|
32
32
|
/** The ids of people a text may name (sign-in methods' ids: `google:1139…`, `email:ana@…`). */
|
|
33
33
|
export declare const idsIn: (text: string) => string[];
|
|
34
34
|
/**
|
|
35
|
-
* The `names` resource: where
|
|
36
|
-
*
|
|
37
|
-
*
|
|
35
|
+
* The `names` resource: where names are, by source: people's (the spaces
|
|
36
|
+
* plugin's profiles, `{ collection }`), and other kinds of actors whose ids
|
|
37
|
+
* start with a prefix (contacts: `{ collection: "contact", prefix: "contact:" }`),
|
|
38
|
+
* each read through its collection's `names` query (`{ ids }` → names by id,
|
|
39
|
+
* as the asker may read them). An app without it shows ids as they are.
|
|
38
40
|
*/
|
|
39
|
-
export type NamesResource =
|
|
41
|
+
export type NamesResource = Record<string, NamesSource>;
|
|
42
|
+
export type NamesSource = {
|
|
40
43
|
collection: string;
|
|
44
|
+
prefix?: string;
|
|
41
45
|
};
|
|
42
46
|
type Db = {
|
|
43
47
|
collection(name: string): {
|
|
@@ -53,7 +57,7 @@ export declare function namesIn(ctx: {
|
|
|
53
57
|
resources: Record<string, unknown>;
|
|
54
58
|
db: Db;
|
|
55
59
|
}, ids: string[]): Record<string, string>;
|
|
56
|
-
/** Pictures by id, in a View: a face beside a name, for
|
|
60
|
+
/** Pictures by id, in a View: a face beside a name, for the people who have one. */
|
|
57
61
|
export declare function picturesIn(ctx: {
|
|
58
62
|
resources: Record<string, unknown>;
|
|
59
63
|
db: Db;
|
|
@@ -51,38 +51,55 @@ export function withNames(text, names) {
|
|
|
51
51
|
export const idsIn = (text) => [
|
|
52
52
|
...new Set(text.match(/\b[a-z][a-z0-9-]*:[A-Za-z0-9._@+-]+/g) ?? []),
|
|
53
53
|
];
|
|
54
|
-
const where = (resources) => resources.names?.collection;
|
|
55
54
|
/** The people among ids, each once, in order (the same read for the same people). */
|
|
56
55
|
export const peopleIn = (ids) => [...new Set(ids.filter(Boolean).map(bare))].sort();
|
|
56
|
+
/**
|
|
57
|
+
* Ids by the source that names them (the one whose prefix they start with,
|
|
58
|
+
* else people's), each list once and in order: the same read for the same ids.
|
|
59
|
+
*/
|
|
60
|
+
function bySource(resources, ids) {
|
|
61
|
+
const sources = Object.values((resources.names ?? {})).filter(Boolean);
|
|
62
|
+
const people = sources.find((s) => !s.prefix);
|
|
63
|
+
const found = new Map();
|
|
64
|
+
for (const id of ids) {
|
|
65
|
+
if (!id)
|
|
66
|
+
continue;
|
|
67
|
+
const source = sources.find((s) => s.prefix && id.startsWith(s.prefix)) ?? people;
|
|
68
|
+
if (source)
|
|
69
|
+
found.set(source, [...found.get(source) ?? [], id]);
|
|
70
|
+
}
|
|
71
|
+
return [...found].map(([source, wanted]) => ({
|
|
72
|
+
collection: source.collection,
|
|
73
|
+
ids: source.prefix ? [...new Set(wanted)].sort() : peopleIn(wanted),
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
57
76
|
/** Names by id, in a View (in a page too: its reads answer at once, or once they're in). */
|
|
58
77
|
export function namesIn(ctx, ids) {
|
|
59
|
-
const
|
|
60
|
-
const wanted
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
return
|
|
78
|
+
const names = {};
|
|
79
|
+
for (const { collection, ids: wanted } of bySource(ctx.resources, ids)) {
|
|
80
|
+
const found = ctx.db.collection(collection).query("names", {
|
|
81
|
+
ids: wanted,
|
|
82
|
+
});
|
|
83
|
+
Object.assign(names, found.value ?? {});
|
|
84
|
+
}
|
|
85
|
+
return names;
|
|
67
86
|
}
|
|
68
|
-
/** Pictures by id, in a View: a face beside a name, for
|
|
87
|
+
/** Pictures by id, in a View: a face beside a name, for the people who have one. */
|
|
69
88
|
export function picturesIn(ctx, ids) {
|
|
70
|
-
const
|
|
89
|
+
const people = Object.values((ctx.resources.names ?? {}))
|
|
90
|
+
.find((s) => s && !s.prefix);
|
|
71
91
|
const wanted = peopleIn(ids);
|
|
72
|
-
if (!
|
|
92
|
+
if (!people || !wanted.length)
|
|
73
93
|
return {};
|
|
74
|
-
const found = ctx.db.collection(collection).query("pictures", {
|
|
94
|
+
const found = ctx.db.collection(people.collection).query("pictures", {
|
|
75
95
|
ids: wanted,
|
|
76
96
|
});
|
|
77
97
|
return found.value ?? {};
|
|
78
98
|
}
|
|
79
99
|
/** Names by id, in an Action, a Processor or a tool. */
|
|
80
100
|
export async function namesFor(ctx, ids) {
|
|
81
|
-
const
|
|
82
|
-
const wanted = peopleIn(ids);
|
|
83
|
-
if (!collection || !wanted.length)
|
|
84
|
-
return {};
|
|
85
|
-
return await ctx.db.collection(collection).query("names", {
|
|
101
|
+
const found = await Promise.all(bySource(ctx.resources, ids).map(({ collection, ids: wanted }) => ctx.db.collection(collection).query("names", {
|
|
86
102
|
ids: wanted,
|
|
87
|
-
});
|
|
103
|
+
})));
|
|
104
|
+
return Object.assign({}, ...found);
|
|
88
105
|
}
|
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* What an actor reaches: the spaces they're a member of, and the spaces those
|
|
3
3
|
* draw on, each with what the roles along the path allow (`read`, `write`,
|
|
4
|
-
* `manage`)
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* `manage`), and the documents shared with them on their own (a contact's
|
|
5
|
+
* chat), each with the space it lives in (`home`). An agent acting for
|
|
6
|
+
* someone reaches what both reach: a space, or a document one of them reaches
|
|
7
|
+
* on its own and the other through its space. An actor limited to one space
|
|
8
|
+
* (`within`: a key's work) reaches only that. Everything that asks what
|
|
9
|
+
* someone may do asks here, so those limits hold everywhere.
|
|
7
10
|
*/
|
|
8
11
|
import type { Actor, ReadContext } from "../../../../runtime/mod.js";
|
|
9
12
|
export type Verb = "read" | "write" | "manage";
|
|
10
|
-
/**
|
|
11
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Each space reached, with what may be done there; documents shared on their
|
|
15
|
+
* own say their collection (`on`) and the space they live in (`home`).
|
|
16
|
+
*/
|
|
17
|
+
export type Reach = Map<string, Reached>;
|
|
18
|
+
type Reached = {
|
|
12
19
|
can: Set<Verb>;
|
|
13
20
|
on?: string;
|
|
14
|
-
|
|
21
|
+
home?: string;
|
|
22
|
+
};
|
|
15
23
|
/**
|
|
16
24
|
* Where a document shared on its own is kept among reach and memberships: its
|
|
17
25
|
* own name, `note:n1`, so a document never passes for a space with the same id.
|
|
@@ -41,5 +49,11 @@ export declare const rolesOf: (ctx: Pick<ReadContext, "resources">) => Record<st
|
|
|
41
49
|
export declare function orgRoleOf(actor: Actor, ctx: ReadContext): Promise<OrgRole | undefined>;
|
|
42
50
|
/** What `actor` reaches; an agent acting for someone (`for`), what both reach. */
|
|
43
51
|
export declare function reachOf(actor: Actor, ctx: ReadContext): Promise<Reach>;
|
|
52
|
+
/**
|
|
53
|
+
* The space a document shared on its own lives in, by its key (`thread:t1`):
|
|
54
|
+
* its shared collection's field says. Undefined for anything else.
|
|
55
|
+
*/
|
|
56
|
+
export declare function homeOf(key: string, ctx: ReadContext): Promise<string | undefined>;
|
|
44
57
|
/** The spaces (or documents) reached where `verb` is allowed. */
|
|
45
58
|
export declare const where: (reach: Reach, verb: Verb, on?: string) => string[];
|
|
59
|
+
export {};
|
|
@@ -24,10 +24,9 @@ export const runsOrg = (role) => !!role && RUN.has(role);
|
|
|
24
24
|
const ALL = ["read", "write", "manage"];
|
|
25
25
|
/** How an actor appears as a member: `person:ana`. */
|
|
26
26
|
export const memberKey = (actor) => `${actor.kind}:${actor.id}`;
|
|
27
|
-
export const rolesOf = (ctx) =>
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
});
|
|
27
|
+
export const rolesOf = (ctx) =>
|
|
28
|
+
// No prototype: a role is one the app names, so "toString" or "__proto__" is no role at all.
|
|
29
|
+
Object.assign(Object.create(null), ROLES, (ctx.resources.roles ?? {}));
|
|
31
30
|
/**
|
|
32
31
|
* Spaces reached from `key`, through the spaces it's in, each with the verbs
|
|
33
32
|
* along the path: one query per level (the owner keeps them cached).
|
|
@@ -82,14 +81,11 @@ export async function reachOf(actor, ctx) {
|
|
|
82
81
|
const limited = typeof within === "string"
|
|
83
82
|
? new Map([...reach].filter(([space]) => space === within))
|
|
84
83
|
: reach;
|
|
85
|
-
// A space put away is read and managed, not written in, until it's brought back.
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (held
|
|
89
|
-
limited.set(
|
|
90
|
-
...held,
|
|
91
|
-
can: new Set([...held.can].filter((v) => v !== "write")),
|
|
92
|
-
});
|
|
84
|
+
// A space put away is read and managed, not written in, until it's brought back: nor what's in it.
|
|
85
|
+
const archived = new Set((await ctx.db.collection("space").find({ archived: { $exists: true } })).map((s) => String(s._id)));
|
|
86
|
+
for (const [key, held] of limited) {
|
|
87
|
+
if (held.can.has("write") && archived.has(held.home ?? key)) {
|
|
88
|
+
limited.set(key, { ...held, can: new Set([...held.can].filter((v) => v !== "write")) });
|
|
93
89
|
}
|
|
94
90
|
}
|
|
95
91
|
return limited;
|
|
@@ -102,25 +98,71 @@ async function unlimited(actor, ctx) {
|
|
|
102
98
|
const role = await orgRoleOf(actor, ctx);
|
|
103
99
|
if (runsOrg(role))
|
|
104
100
|
return await everything(ctx);
|
|
105
|
-
const own = await walk(memberKey(actor), ctx);
|
|
101
|
+
const own = await housed(await walk(memberKey(actor), ctx), ctx);
|
|
106
102
|
// Members see who's in the org; guests only the spaces they're in.
|
|
107
103
|
if (role === "member")
|
|
108
104
|
own.set(ORG, { can: new Set(["read"]) });
|
|
109
105
|
return own;
|
|
110
106
|
}
|
|
111
|
-
const own = await walk(memberKey(actor), ctx);
|
|
107
|
+
const own = await housed(await walk(memberKey(actor), ctx), ctx);
|
|
112
108
|
const theirs = await reachOf(principal, ctx);
|
|
113
109
|
const both = new Map();
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
continue;
|
|
118
|
-
const can = new Set([...mine.can].filter((verb) => other.can.has(verb)));
|
|
110
|
+
/** `held` (one side's), with only the verbs the other side's `other` allows too. */
|
|
111
|
+
const keep = (key, held, other) => {
|
|
112
|
+
const can = new Set([...held.can].filter((verb) => other?.can.has(verb)));
|
|
119
113
|
if (can.size)
|
|
120
|
-
both.set(
|
|
114
|
+
both.set(key, { ...held, can });
|
|
115
|
+
};
|
|
116
|
+
for (const [key, mine] of own) {
|
|
117
|
+
// The same space or document; else a document it reaches on its own, in a space they reach.
|
|
118
|
+
keep(key, mine, theirs.get(key) ?? (mine.home ? theirs.get(mine.home) : undefined));
|
|
119
|
+
}
|
|
120
|
+
for (const [key, other] of theirs) {
|
|
121
|
+
// A document they reach on its own, in a space it reaches.
|
|
122
|
+
if (!own.has(key) && other.home)
|
|
123
|
+
keep(key, other, own.get(other.home));
|
|
121
124
|
}
|
|
122
125
|
return both;
|
|
123
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* The space each document reached on its own lives in (`home`): its shared
|
|
129
|
+
* collection's field says, read once per collection. Someone who reaches no
|
|
130
|
+
* document on its own reads nothing here.
|
|
131
|
+
*/
|
|
132
|
+
async function housed(reach, ctx) {
|
|
133
|
+
const shared = (ctx.resources.shared ?? {});
|
|
134
|
+
const ids = new Map();
|
|
135
|
+
for (const [key, held] of reach) {
|
|
136
|
+
if (held.on && Object.hasOwn(shared, held.on)) {
|
|
137
|
+
ids.set(held.on, [...ids.get(held.on) ?? [], key.slice(held.on.length + 1)]);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
for (const [on, wanted] of ids) {
|
|
141
|
+
const field = shared[on].field ?? "in";
|
|
142
|
+
const docs = await ctx.db.collection(on).find(wanted.length === 1 ? { _id: wanted[0] } : { _id: { $in: wanted } });
|
|
143
|
+
for (const doc of docs) {
|
|
144
|
+
const key = docKey(on, String(doc._id));
|
|
145
|
+
const home = doc[field];
|
|
146
|
+
if (typeof home === "string")
|
|
147
|
+
reach.set(key, { ...reach.get(key), home });
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return reach;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The space a document shared on its own lives in, by its key (`thread:t1`):
|
|
154
|
+
* its shared collection's field says. Undefined for anything else.
|
|
155
|
+
*/
|
|
156
|
+
export async function homeOf(key, ctx) {
|
|
157
|
+
const at = key.indexOf(":");
|
|
158
|
+
const [on, id] = [key.slice(0, at), key.slice(at + 1)];
|
|
159
|
+
const shared = (ctx.resources.shared ?? {});
|
|
160
|
+
if (at < 1 || !Object.hasOwn(shared, on))
|
|
161
|
+
return undefined;
|
|
162
|
+
const doc = await ctx.db.collection(on).findOne({ _id: id });
|
|
163
|
+
const home = doc?.[shared[on].field ?? "in"];
|
|
164
|
+
return typeof home === "string" ? home : undefined;
|
|
165
|
+
}
|
|
124
166
|
/** The spaces (or documents) reached where `verb` is allowed. */
|
|
125
167
|
export const where = (reach, verb, on) => [...reach].filter(([, r]) => r.can.has(verb) && (on === undefined ? !r.on : r.on === on))
|
|
126
168
|
.map(([space]) => on === undefined ? space : space.slice(on.length + 1));
|
|
@@ -3,8 +3,9 @@
|
|
|
3
3
|
* documents live `in` a space (the `shared` resource names them and their
|
|
4
4
|
* field): an actor reads those in spaces they may read, and writes those in
|
|
5
5
|
* spaces they may write (or manage, for some), plus documents shared with
|
|
6
|
-
* them on their own
|
|
7
|
-
*
|
|
6
|
+
* them on their own and those documents' parts (a chat's messages come with
|
|
7
|
+
* it); documents that are members (agents) are read where they work too. A
|
|
8
|
+
* query that names a space, or a document it reaches, needs nothing more:
|
|
8
9
|
* it goes to the store as it is. Spaces and memberships are readable where reached and
|
|
9
10
|
* changed only by those who manage them; invitations are their managers',
|
|
10
11
|
* and read by whom they're to (by the email they signed in with).
|
|
@@ -15,14 +16,18 @@ import { type Verb } from "./reach.js";
|
|
|
15
16
|
* The `shared` resource: collections whose documents live in spaces, the
|
|
16
17
|
* field saying where (`in`), whether they keep labels (see labels.ts), what
|
|
17
18
|
* changing one needs where it lives (`write`: "write" by default, "manage"
|
|
18
|
-
* for those only its managers change),
|
|
19
|
-
*
|
|
20
|
-
*
|
|
19
|
+
* for those only its managers change), whether its documents are members
|
|
20
|
+
* of spaces themselves (`member`: the kind, `agent` for agents people make),
|
|
21
|
+
* readable too where they're members, and whether each is part of a document
|
|
22
|
+
* of another shared collection (`part`: that collection, which its field of
|
|
23
|
+
* the same name names: a message's `thread`), reached with it when that
|
|
24
|
+
* document is shared on its own.
|
|
21
25
|
*/
|
|
22
26
|
export type Shared = Record<string, {
|
|
23
27
|
field?: string;
|
|
24
28
|
labels?: boolean;
|
|
25
29
|
write?: Verb;
|
|
26
30
|
member?: string;
|
|
31
|
+
part?: string;
|
|
27
32
|
}>;
|
|
28
33
|
export declare const rules: Guard;
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import { membersOf } from "./agents.js";
|
|
2
|
-
import { memberKey, reachOf, where } from "./reach.js";
|
|
2
|
+
import { homeOf, memberKey, reachOf, where } from "./reach.js";
|
|
3
|
+
/** The documents of `on` reached on their own with `verb`: each id, and the space it lives in. */
|
|
4
|
+
const held = (reach, verb, on) => [...reach].flatMap(([key, r]) => r.on === on && r.home && r.can.has(verb)
|
|
5
|
+
? [[key.slice(on.length + 1), r.home]]
|
|
6
|
+
: []);
|
|
3
7
|
export const rules = async (actor, { collection, access, filter }, ctx) => {
|
|
4
8
|
const shared = (ctx.resources.shared ?? {});
|
|
5
9
|
const own = ["space", "member", "request", "invitation"];
|
|
6
|
-
if (!(collection
|
|
10
|
+
if (!Object.hasOwn(shared, collection) && !own.includes(collection))
|
|
7
11
|
return undefined;
|
|
8
12
|
const reach = await reachOf(actor, ctx);
|
|
9
13
|
if (collection === "invitation") {
|
|
@@ -23,9 +27,18 @@ export const rules = async (actor, { collection, access, filter }, ctx) => {
|
|
|
23
27
|
// Who's where isn't content: an agent reads it as far as the person it works for reaches.
|
|
24
28
|
const by = access === "read" && actor.for ? await reachOf(actor.for, ctx) : reach;
|
|
25
29
|
const spaces = where(by, access === "read" ? "read" : "manage");
|
|
26
|
-
|
|
30
|
+
if (collection === "space")
|
|
31
|
+
return { _id: { $in: spaces } };
|
|
32
|
+
// Who a document of a space is shared with on its own (a contact, in a chat), asked by its key.
|
|
33
|
+
const named = filter?.space;
|
|
34
|
+
if (access === "read" && typeof named === "string" && !spaces.includes(named)) {
|
|
35
|
+
const home = await homeOf(named, ctx);
|
|
36
|
+
if (by.has(named) || (home && spaces.includes(home)))
|
|
37
|
+
return { space: named };
|
|
38
|
+
}
|
|
39
|
+
return { space: { $in: spaces } };
|
|
27
40
|
}
|
|
28
|
-
const { field = "in", labels, write = "write", member } = shared[collection];
|
|
41
|
+
const { field = "in", labels, write = "write", member, part } = shared[collection];
|
|
29
42
|
const verb = access === "read" ? "read" : write;
|
|
30
43
|
const spaces = where(reach, verb);
|
|
31
44
|
// A labelled document needs its sources reached too, to read or change it.
|
|
@@ -35,11 +48,23 @@ export const rules = async (actor, { collection, access, filter }, ctx) => {
|
|
|
35
48
|
const named = filter?.[field];
|
|
36
49
|
if (typeof named === "string" && spaces.includes(named))
|
|
37
50
|
return sourced ?? true;
|
|
38
|
-
const
|
|
51
|
+
const and = (f) => sourced ? { $and: [f, sourced] } : f;
|
|
52
|
+
// Documents shared on their own, and their parts (a chat shared with a contact, its messages):
|
|
53
|
+
// each only where it lives, so nothing written as one of them lands in another space.
|
|
54
|
+
const docs = held(reach, verb, collection);
|
|
55
|
+
const wholes = part ? held(reach, verb, part) : [];
|
|
56
|
+
const whole = part ? filter?.[part] : undefined;
|
|
57
|
+
const asked = wholes.find(([id]) => id === whole);
|
|
58
|
+
if (asked)
|
|
59
|
+
return and({ [field]: asked[1] });
|
|
39
60
|
// Documents that are members (agents) are read where they work too.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const
|
|
43
|
-
|
|
44
|
-
|
|
61
|
+
const members = member && access === "read" ? await membersOf(ctx, spaces, member) : [];
|
|
62
|
+
// Only what's reached, so a store can use its indexes (a contact reaches no space at all).
|
|
63
|
+
const anyOf = [
|
|
64
|
+
...(spaces.length ? [{ [field]: { $in: spaces } }] : []),
|
|
65
|
+
...docs.map(([id, home]) => ({ _id: id, [field]: home })),
|
|
66
|
+
...(members.length ? [{ _id: { $in: members } }] : []),
|
|
67
|
+
...wholes.map(([id, home]) => ({ [part]: id, [field]: home })),
|
|
68
|
+
];
|
|
69
|
+
return and(anyOf.length > 1 ? { $or: anyOf } : anyOf[0] ?? { [field]: { $in: [] } });
|
|
45
70
|
};
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Giving a member a role in a space, however they came to it (a manager
|
|
3
|
-
* setting it, a request granted): the role is one this app has, an agent
|
|
4
|
-
* manages,
|
|
3
|
+
* setting it, a request granted): the role is one this app has, an agent or a
|
|
4
|
+
* contact never manages, a contact is let into documents only (its chat),
|
|
5
|
+
* never a whole space, and a space keeps a manager.
|
|
5
6
|
*/
|
|
6
7
|
import { type Context } from "../../../../runtime/mod.js";
|
|
7
8
|
/** Checks `role` for `member` (`person:ana`); keeping a manager only where it's a space's own. */
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Giving a member a role in a space, however they came to it (a manager
|
|
3
|
-
* setting it, a request granted): the role is one this app has, an agent
|
|
4
|
-
* manages,
|
|
3
|
+
* setting it, a request granted): the role is one this app has, an agent or a
|
|
4
|
+
* contact never manages, a contact is let into documents only (its chat),
|
|
5
|
+
* never a whole space, and a space keeps a manager.
|
|
5
6
|
*/
|
|
6
7
|
import { invalid } from "../../../../runtime/mod.js";
|
|
7
8
|
import { keepsAManager } from "./managers.js";
|
|
@@ -14,9 +15,15 @@ export function checkRole(ctx, member, role) {
|
|
|
14
15
|
if (member.startsWith("agent:") && verbs.includes("manage")) {
|
|
15
16
|
throw invalid("Agents work here as viewers or editors.");
|
|
16
17
|
}
|
|
18
|
+
if (member.startsWith("contact:") && verbs.includes("manage")) {
|
|
19
|
+
throw invalid("A contact reads or writes in what's shared with them, never manages it.");
|
|
20
|
+
}
|
|
17
21
|
}
|
|
18
22
|
export async function seat(ctx, space, member, role) {
|
|
19
23
|
checkRole(ctx, member, role);
|
|
24
|
+
if (member.startsWith("contact:")) {
|
|
25
|
+
throw invalid("A contact is let into a document (its chat), never into a whole space.");
|
|
26
|
+
}
|
|
20
27
|
await keepsAManager(ctx, space, member, role);
|
|
21
28
|
await ctx.as().db.collection("member").replaceOne({ _id: `${space}|${member}` }, { member, space, role }, { upsert: true });
|
|
22
29
|
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Where you are, for agents (the chat plugin's `where` context): the space a
|
|
3
|
-
* conversation is in, who its members are,
|
|
4
|
-
*
|
|
3
|
+
* conversation is in, who its members are, what it draws on, and who it's
|
|
4
|
+
* shared with on its own (a contact: someone outside the org, who sees only
|
|
5
|
+
* it), so an agent knows who will see what it writes there.
|
|
5
6
|
*/
|
|
6
7
|
import type { ViewContext } from "../../../../runtime/mod.js";
|
|
7
8
|
export declare const whereView: (thread: {
|
|
9
|
+
_id?: string;
|
|
8
10
|
in?: string;
|
|
9
11
|
}, ctx: ViewContext<string>) => string | undefined;
|