@capybara-hq/kernel 0.1.0-rc.59 → 0.1.0-rc.60
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/dist/admin/admin.controller.d.ts +10 -5
- package/dist/admin/admin.controller.js +16 -7
- package/dist/admin/admin.schemas.js +2 -1
- package/dist/admin/audit-log.service.d.ts +1 -0
- package/dist/admin/audit-log.service.js +4 -1
- package/dist/admin/extensions.schemas.js +2 -0
- package/dist/agents/admin-agents.d.ts +1 -0
- package/dist/agents/admin-agents.js +4 -1
- package/dist/agents/agents.controller.d.ts +1 -10
- package/dist/agents/agents.controller.js +2 -14
- package/dist/agents/agents.schemas.d.ts +10 -0
- package/dist/agents/agents.schemas.js +19 -0
- package/dist/background/tasks.service.d.ts +5 -0
- package/dist/background/tasks.service.js +10 -3
- package/dist/catalog/admin-catalog.schemas.js +2 -0
- package/dist/catalog/component-scope.schemas.js +2 -0
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/org-plugin-uploads.schemas.js +2 -0
- package/dist/catalog/requests.service.d.ts +2 -0
- package/dist/catalog/requests.service.js +4 -1
- package/dist/channels/cards.d.ts +9 -0
- package/dist/channels/cards.js +32 -1
- package/dist/channels/phrases.d.ts +5 -0
- package/dist/channels/phrases.js +5 -0
- package/dist/conversations/attachments.service.d.ts +34 -0
- package/dist/conversations/attachments.service.js +124 -21
- package/dist/conversations/conversations.controller.d.ts +5 -1
- package/dist/conversations/conversations.controller.js +11 -5
- package/dist/conversations/conversations.service.d.ts +5 -0
- package/dist/conversations/conversations.service.js +10 -0
- package/dist/conversations/event-people.d.ts +12 -0
- package/dist/conversations/event-people.js +28 -0
- package/dist/db/kernel-schema.d.ts +18 -0
- package/dist/devices/devices.service.d.ts +3 -2
- package/dist/devices/devices.service.js +17 -2
- package/dist/errors.d.ts +1 -0
- package/dist/errors.js +1 -0
- package/dist/evals/trajectory.service.d.ts +3 -0
- package/dist/evals/trajectory.service.js +2 -0
- package/dist/extensions/installs.d.ts +3 -0
- package/dist/extensions/installs.js +1 -0
- package/dist/http/api.module.js +2 -0
- package/dist/http/meta.controller.d.ts +1 -0
- package/dist/http/meta.controller.js +6 -1
- package/dist/identity/account-avatar.controller.d.ts +30 -0
- package/dist/identity/account-avatar.controller.js +117 -0
- package/dist/identity/account-avatar.d.ts +67 -0
- package/dist/identity/account-avatar.js +136 -0
- package/dist/identity/account-avatars.service.d.ts +77 -0
- package/dist/identity/account-avatars.service.js +321 -0
- package/dist/identity/accounts.d.ts +26 -0
- package/dist/identity/accounts.js +70 -2
- package/dist/identity/app-auth.controller.d.ts +2 -1
- package/dist/identity/app-auth.controller.js +2 -1
- package/dist/identity/app-auth.d.ts +24 -7
- package/dist/identity/app-auth.js +19 -9
- package/dist/identity/app-auth.schemas.d.ts +16 -3
- package/dist/identity/app-auth.schemas.js +5 -2
- package/dist/identity/external.js +7 -1
- package/dist/identity/identity.module.d.ts +2 -1
- package/dist/identity/identity.module.js +7 -1
- package/dist/identity/me.controller.d.ts +29 -5
- package/dist/identity/me.controller.js +57 -6
- package/dist/identity/me.schemas.d.ts +36 -0
- package/dist/identity/me.schemas.js +30 -0
- package/dist/identity/principal.d.ts +2 -2
- package/dist/identity/principal.js +2 -2
- package/dist/local-devices/local-device-gateway.d.ts +2 -0
- package/dist/local-devices/local-device-gateway.js +8 -1
- package/dist/local-devices/relay.d.ts +10 -9
- package/dist/local-devices/relay.js +32 -9
- package/dist/local-devices/targets.d.ts +12 -0
- package/dist/local-devices/targets.js +18 -2
- package/dist/orgs/members.service.d.ts +3 -1
- package/dist/orgs/members.service.js +9 -2
- package/dist/platform/exclusions.js +33 -57
- package/dist/platform/file-ref.d.ts +87 -0
- package/dist/platform/file-ref.js +164 -0
- package/dist/platform/operation.d.ts +26 -0
- package/dist/platform/ops/agent-skills.ops.d.ts +2 -0
- package/dist/platform/ops/agent-skills.ops.js +156 -4
- package/dist/platform/ops/agents.ops.d.ts +3 -1
- package/dist/platform/ops/agents.ops.js +75 -17
- package/dist/platform/ops/approvals.ops.js +5 -0
- package/dist/platform/ops/catalog-admin.ops.d.ts +9 -1
- package/dist/platform/ops/catalog-admin.ops.js +86 -3
- package/dist/platform/ops/components.ops.d.ts +3 -1
- package/dist/platform/ops/components.ops.js +61 -2
- package/dist/platform/ops/connectors.ops.js +15 -1
- package/dist/platform/ops/devices.ops.js +6 -1
- package/dist/platform/ops/extensions-admin.ops.d.ts +6 -4
- package/dist/platform/ops/extensions-admin.ops.js +109 -6
- package/dist/platform/ops/inbox.ops.js +10 -0
- package/dist/platform/ops/list-handoff.d.ts +10 -0
- package/dist/platform/ops/list-handoff.js +11 -0
- package/dist/platform/ops/marketplaces.ops.js +2 -1
- package/dist/platform/ops/org-plugins.ops.d.ts +12 -3
- package/dist/platform/ops/org-plugins.ops.js +97 -6
- package/dist/platform/ops/requests.ops.js +9 -0
- package/dist/platform/ops/skills.ops.d.ts +5 -1
- package/dist/platform/ops/skills.ops.js +98 -4
- package/dist/platform/ops/summary.d.ts +5 -0
- package/dist/platform/ops/summary.js +10 -0
- package/dist/platform/parity.d.ts +6 -4
- package/dist/platform/parity.js +94 -68
- package/dist/roles/worker.module.js +5 -0
- package/dist/sandbox/request-not-sent.d.ts +13 -0
- package/dist/sandbox/request-not-sent.js +18 -0
- package/dist/schedules/scheduled-tasks.service.d.ts +10 -6
- package/dist/schedules/scheduled-tasks.service.js +13 -16
- package/dist/shares/shares.service.d.ts +1 -0
- package/dist/shares/shares.service.js +9 -3
- package/dist/skills/changes.service.d.ts +1 -0
- package/dist/skills/changes.service.js +4 -7
- package/dist/skills/skills.controller.d.ts +3 -7
- package/dist/skills/skills.controller.js +3 -16
- package/dist/skills/skills.schemas.d.ts +5 -0
- package/dist/skills/skills.schemas.js +17 -0
- package/dist/skills/skills.service.d.ts +3 -1
- package/dist/skills/skills.service.js +13 -4
- package/dist/tools/admin-connectors.d.ts +1 -0
- package/dist/tools/admin-connectors.js +4 -1
- package/dist/tools/approvals.service.d.ts +16 -2
- package/dist/tools/approvals.service.js +13 -0
- package/dist/tools/builtins/builtin-tools.d.ts +3 -0
- package/dist/tools/builtins/builtin-tools.js +29 -9
- package/dist/tools/builtins/platform.d.ts +23 -0
- package/dist/tools/builtins/platform.js +95 -6
- package/dist/tools/builtins/sandbox/common.d.ts +5 -0
- package/dist/tools/builtins/sandbox/common.js +11 -0
- package/dist/tools/builtins/skill-load.js +4 -4
- package/dist/tools/pipeline.js +10 -3
- package/dist/tools/questions.controller.js +2 -2
- package/dist/tools/questions.service.d.ts +7 -1
- package/dist/tools/questions.service.js +9 -2
- package/dist/tools/registry.d.ts +6 -0
- package/dist/tools/tools.module.js +3 -0
- package/package.json +3 -3
- package/src/migrations/0171_account_avatar.sql +26 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
+
};
|
|
10
|
+
import { Controller, Get, Inject, Param, Res } from "@nestjs/common";
|
|
11
|
+
import { sql } from "kysely";
|
|
12
|
+
import { ApiError } from "../errors.js";
|
|
13
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
14
|
+
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
15
|
+
import { avatarColumns, ownAvatar, safeSsoUrl } from "./account-avatar.js";
|
|
16
|
+
import { AccountAvatarsService } from "./account-avatars.service.js";
|
|
17
|
+
import { AllowPendingPasswordChange, CurrentPrincipal, requireUser, } from "./principal.js";
|
|
18
|
+
/** How long a browser may reuse the redirect (the presigned URL itself lives 300 s). */
|
|
19
|
+
const AVATAR_CACHE_CONTROL = "private, max-age=60";
|
|
20
|
+
/** A refusal is never cached: the image may appear (or access be granted) at any moment. */
|
|
21
|
+
const NOT_FOUND_CACHE_CONTROL = "private, no-store";
|
|
22
|
+
/**
|
|
23
|
+
* The single refusal of the avatar route: a reader who may not see the person, an unknown
|
|
24
|
+
* account and a person without an image all get exactly this, so existence never leaks.
|
|
25
|
+
*/
|
|
26
|
+
const notFound = () => new ApiError("account.not_found", "Account not found");
|
|
27
|
+
/**
|
|
28
|
+
* A person's avatar image (account profile spec §5): readable by the account itself, a super
|
|
29
|
+
* admin, or anyone who shares an org with the person (any role, guests included; deleted orgs do
|
|
30
|
+
* not count). Default auth: a session or a personal token; service tokens and embed tokens never.
|
|
31
|
+
* An account that must change its password first still reads its own image (the shell draws it),
|
|
32
|
+
* and only its own.
|
|
33
|
+
*/
|
|
34
|
+
let AccountAvatarController = class AccountAvatarController {
|
|
35
|
+
db;
|
|
36
|
+
avatars;
|
|
37
|
+
log;
|
|
38
|
+
constructor(db, avatars, log) {
|
|
39
|
+
this.db = db;
|
|
40
|
+
this.avatars = avatars;
|
|
41
|
+
this.log = log;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 302 to a short-lived presigned URL of the person's own upload; else (no upload, its object
|
|
45
|
+
* gone, or no object store) 302 to their SSO `https:` avatar; else 404. The `v` query parameter
|
|
46
|
+
* only busts browser caches and is ignored. An object store failure never fails the route: it
|
|
47
|
+
* falls back like a missing object, with a warning.
|
|
48
|
+
*/
|
|
49
|
+
async avatar(principal, accountId, reply) {
|
|
50
|
+
const reader = requireUser(principal).account;
|
|
51
|
+
const refuse = () => {
|
|
52
|
+
reply.header("cache-control", NOT_FOUND_CACHE_CONTROL);
|
|
53
|
+
return notFound();
|
|
54
|
+
};
|
|
55
|
+
if (reader.mustChangePassword && accountId !== reader.id)
|
|
56
|
+
throw refuse();
|
|
57
|
+
const row = await this.db
|
|
58
|
+
.selectFrom("accounts")
|
|
59
|
+
.select((eb) => avatarColumns(eb, "accounts", "target"))
|
|
60
|
+
.where("accounts.id", "=", accountId)
|
|
61
|
+
.$if(!reader.isSuperAdmin, (q) => q.where((eb) => eb.or([
|
|
62
|
+
eb("accounts.id", "=", reader.id),
|
|
63
|
+
eb.exists(eb
|
|
64
|
+
.selectFrom("memberships as target_m")
|
|
65
|
+
.innerJoin("memberships as reader_m", "reader_m.org_id", "target_m.org_id")
|
|
66
|
+
.innerJoin("orgs as shared_org", "shared_org.id", "target_m.org_id")
|
|
67
|
+
.select(sql `1`.as("one"))
|
|
68
|
+
.where("target_m.account_id", "=", eb.ref("accounts.id"))
|
|
69
|
+
.where("reader_m.account_id", "=", reader.id)
|
|
70
|
+
.where("shared_org.deleted_at", "is", null)),
|
|
71
|
+
])))
|
|
72
|
+
.executeTakeFirst();
|
|
73
|
+
if (!row)
|
|
74
|
+
throw refuse();
|
|
75
|
+
let location = null;
|
|
76
|
+
const own = ownAvatar(row.target_avatar);
|
|
77
|
+
if (own)
|
|
78
|
+
location = await this.uploadUrl(accountId, own.uploadId);
|
|
79
|
+
location ??= safeSsoUrl(row.target_sso_avatar_url);
|
|
80
|
+
if (location === null)
|
|
81
|
+
throw refuse();
|
|
82
|
+
await reply.header("cache-control", AVATAR_CACHE_CONTROL).redirect(location, 302);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The presigned URL of the bound upload; null when it is gone, there is no object store, or the
|
|
86
|
+
* store failed (logged).
|
|
87
|
+
*/
|
|
88
|
+
async uploadUrl(accountId, uploadId) {
|
|
89
|
+
try {
|
|
90
|
+
return await this.avatars.objectUrl(accountId, uploadId);
|
|
91
|
+
}
|
|
92
|
+
catch (err) {
|
|
93
|
+
if (!(err instanceof ApiError && err.code === "storage.unavailable")) {
|
|
94
|
+
this.log.warn("account avatar: object store failed, falling back", {
|
|
95
|
+
accountId,
|
|
96
|
+
uploadId,
|
|
97
|
+
err,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
__decorate([
|
|
105
|
+
Get(":accountId/avatar"),
|
|
106
|
+
AllowPendingPasswordChange(),
|
|
107
|
+
__param(0, CurrentPrincipal()),
|
|
108
|
+
__param(1, Param("accountId")),
|
|
109
|
+
__param(2, Res())
|
|
110
|
+
], AccountAvatarController.prototype, "avatar", null);
|
|
111
|
+
AccountAvatarController = __decorate([
|
|
112
|
+
Controller("api/v1/accounts"),
|
|
113
|
+
__param(0, Inject(KernelDb)),
|
|
114
|
+
__param(1, Inject(AccountAvatarsService)),
|
|
115
|
+
__param(2, Inject(KERNEL_LOGGER))
|
|
116
|
+
], AccountAvatarController);
|
|
117
|
+
export { AccountAvatarController };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { type ExpressionBuilder, type Kysely } from "kysely";
|
|
2
|
+
import type { KernelDatabase } from "../db/kernel-schema.js";
|
|
3
|
+
import type { Account } from "./accounts.js";
|
|
4
|
+
/**
|
|
5
|
+
* The avatar of a person (account profile spec §5): their own upload (`accounts.avatar`), else the
|
|
6
|
+
* `https:` avatar of their most recently refreshed non-local identity, else none (the UI draws
|
|
7
|
+
* the initial). One helper builds every person's `avatarUrl`, so every surface agrees.
|
|
8
|
+
*/
|
|
9
|
+
/** `accounts.avatar`: the person's own avatar (null: none). */
|
|
10
|
+
export type AccountAvatar = {
|
|
11
|
+
kind: "upload";
|
|
12
|
+
uploadId: string;
|
|
13
|
+
};
|
|
14
|
+
/** A person as a response names them: id, display name and avatar URL. */
|
|
15
|
+
export interface PersonRef {
|
|
16
|
+
id: string;
|
|
17
|
+
name: string;
|
|
18
|
+
avatarUrl: string | null;
|
|
19
|
+
}
|
|
20
|
+
/** An `Account` as `/me` and the admin account routes return it. */
|
|
21
|
+
export type AccountView = Account & {
|
|
22
|
+
avatarUrl: string | null;
|
|
23
|
+
/** Where the shown image comes from: the person's upload, or the identity source (e.g. "lark:t1"). */
|
|
24
|
+
avatarSource: {
|
|
25
|
+
kind: "upload";
|
|
26
|
+
} | {
|
|
27
|
+
kind: "identity";
|
|
28
|
+
source: string;
|
|
29
|
+
} | null;
|
|
30
|
+
};
|
|
31
|
+
/** The `accounts.avatar` value when it is a well-formed upload reference, else null. */
|
|
32
|
+
export declare function ownAvatar(raw: unknown): AccountAvatar | null;
|
|
33
|
+
/**
|
|
34
|
+
* The normalized URL (`href`) when it parses with the `https:` protocol, else null. Normalizing
|
|
35
|
+
* drops what a header cannot carry (tabs and line breaks), so the redirect never fails.
|
|
36
|
+
*/
|
|
37
|
+
export declare function safeSsoUrl(raw: unknown): string | null;
|
|
38
|
+
/** Pure: own upload first, else an https SSO URL, else null. */
|
|
39
|
+
export declare function accountAvatarUrl(row: {
|
|
40
|
+
id: string;
|
|
41
|
+
avatar: AccountAvatar | null;
|
|
42
|
+
ssoAvatarUrl: string | null;
|
|
43
|
+
}): string | null;
|
|
44
|
+
type AnyAccountsEb = ExpressionBuilder<KernelDatabase, any>;
|
|
45
|
+
/** The row keys `avatarColumns(eb, alias, as)` adds for the output prefix `P`. */
|
|
46
|
+
export type AvatarColumnsRow<P extends string> = {
|
|
47
|
+
[K in `${P}_avatar`]: AccountAvatar | null;
|
|
48
|
+
} & {
|
|
49
|
+
[K in `${P}_sso_avatar_url`]: string | null;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Kysely select fragment for any query already joined to `accounts` as `alias`: `<alias>.avatar`
|
|
53
|
+
* as `<as>_avatar` and a correlated subquery as `<as>_sso_avatar_url`. The prefix keeps two people
|
|
54
|
+
* in one row (and other `avatar` columns) apart; read the result with {@link personAvatarUrl}.
|
|
55
|
+
*/
|
|
56
|
+
export declare function avatarColumns<P extends string>(eb: AnyAccountsEb, alias: string, as: P): readonly [import("kysely").AliasedRawBuilder<AccountAvatar | null, `${P}_avatar`>, import("kysely").AliasedSelectQueryBuilder<{
|
|
57
|
+
value: string | null;
|
|
58
|
+
}, `${P}_sso_avatar_url`>];
|
|
59
|
+
/** The `avatarUrl` of the person whose `avatarColumns(eb, alias, as)` the row carries. */
|
|
60
|
+
export declare function personAvatarUrl<P extends string>(row: AvatarColumnsRow<P>, as: P, id: string): string | null;
|
|
61
|
+
/** One query: ids → PersonRef (missing ids are absent from the map). */
|
|
62
|
+
export declare function loadPeople(db: Kysely<KernelDatabase>, ids: readonly string[]): Promise<Map<string, PersonRef>>;
|
|
63
|
+
/** The given accounts with `avatarUrl` and `avatarSource`, in their order (one query). */
|
|
64
|
+
export declare function withAvatarUrls(db: Kysely<KernelDatabase>, accounts: readonly Account[]): Promise<AccountView[]>;
|
|
65
|
+
/** One account with `avatarUrl` and `avatarSource` ({@link withAvatarUrls} for a single one). */
|
|
66
|
+
export declare function withAvatarUrl(db: Kysely<KernelDatabase>, account: Account): Promise<AccountView>;
|
|
67
|
+
export {};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { sql } from "kysely";
|
|
3
|
+
/** The `accounts.avatar` value when it is a well-formed upload reference, else null. */
|
|
4
|
+
export function ownAvatar(raw) {
|
|
5
|
+
if (typeof raw !== "object" || raw === null)
|
|
6
|
+
return null;
|
|
7
|
+
const v = raw;
|
|
8
|
+
return v.kind === "upload" && typeof v.uploadId === "string" && v.uploadId !== ""
|
|
9
|
+
? { kind: "upload", uploadId: v.uploadId }
|
|
10
|
+
: null;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* The normalized URL (`href`) when it parses with the `https:` protocol, else null. Normalizing
|
|
14
|
+
* drops what a header cannot carry (tabs and line breaks), so the redirect never fails.
|
|
15
|
+
*/
|
|
16
|
+
export function safeSsoUrl(raw) {
|
|
17
|
+
if (typeof raw !== "string")
|
|
18
|
+
return null;
|
|
19
|
+
try {
|
|
20
|
+
const url = new URL(raw);
|
|
21
|
+
return url.protocol === "https:" ? url.href : null;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** First 10 chars of the base64url sha256 of the avatar value: changes bust browser caches. */
|
|
28
|
+
function version(value) {
|
|
29
|
+
return createHash("sha256").update(value).digest("base64url").slice(0, 10);
|
|
30
|
+
}
|
|
31
|
+
/** Pure: own upload first, else an https SSO URL, else null. */
|
|
32
|
+
export function accountAvatarUrl(row) {
|
|
33
|
+
const own = ownAvatar(row.avatar);
|
|
34
|
+
const value = own ? `upload:${own.uploadId}` : safeSsoUrl(row.ssoAvatarUrl);
|
|
35
|
+
if (value === null)
|
|
36
|
+
return null;
|
|
37
|
+
const ver = version(own ? value : `sso:${value}`);
|
|
38
|
+
return `/api/v1/accounts/${encodeURIComponent(row.id)}/avatar?v=${ver}`;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* A correlated subquery on the identity whose avatar is shown for `<alias>.id`: the most recently
|
|
42
|
+
* refreshed non-local identity with an `https://` avatar (ties broken by key, so the url and the
|
|
43
|
+
* source subqueries pick the same row). The SQL `LIKE 'https://%'` is a prefix filter only and can
|
|
44
|
+
* disagree with the `new URL()` check of {@link safeSsoUrl} on a malformed value (rare): such a row
|
|
45
|
+
* is still picked here, the TS check wins, and the person shows no SSO avatar (null) rather than an
|
|
46
|
+
* older identity's.
|
|
47
|
+
*/
|
|
48
|
+
function ssoIdentity(eb, alias, column) {
|
|
49
|
+
return eb
|
|
50
|
+
.selectFrom("identities as avatar_identity")
|
|
51
|
+
.select(column === "url"
|
|
52
|
+
? sql `avatar_identity.profile->>'avatarUrl'`.as("value")
|
|
53
|
+
: sql `avatar_identity.source`.as("value"))
|
|
54
|
+
.where(sql.ref("avatar_identity.account_id"), "=", sql.ref(`${alias}.id`))
|
|
55
|
+
.where("avatar_identity.source", "<>", "local")
|
|
56
|
+
.where(sql `avatar_identity.profile->>'avatarUrl'`, "like", "https://%")
|
|
57
|
+
.orderBy(sql `avatar_identity.updated_at desc nulls last`)
|
|
58
|
+
.orderBy("avatar_identity.created_at", "desc")
|
|
59
|
+
.orderBy("avatar_identity.source")
|
|
60
|
+
.orderBy("avatar_identity.external_id")
|
|
61
|
+
.limit(1);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Kysely select fragment for any query already joined to `accounts` as `alias`: `<alias>.avatar`
|
|
65
|
+
* as `<as>_avatar` and a correlated subquery as `<as>_sso_avatar_url`. The prefix keeps two people
|
|
66
|
+
* in one row (and other `avatar` columns) apart; read the result with {@link personAvatarUrl}.
|
|
67
|
+
*/
|
|
68
|
+
export function avatarColumns(eb, alias, as) {
|
|
69
|
+
return [
|
|
70
|
+
sql `${sql.ref(`${alias}.avatar`)}`.as(`${as}_avatar`),
|
|
71
|
+
ssoIdentity(eb, alias, "url").as(`${as}_sso_avatar_url`),
|
|
72
|
+
];
|
|
73
|
+
}
|
|
74
|
+
/** The `avatarUrl` of the person whose `avatarColumns(eb, alias, as)` the row carries. */
|
|
75
|
+
export function personAvatarUrl(row, as, id) {
|
|
76
|
+
// a generic key does not index the mapped type; the values are re-checked by accountAvatarUrl
|
|
77
|
+
const cols = row;
|
|
78
|
+
return accountAvatarUrl({
|
|
79
|
+
id,
|
|
80
|
+
avatar: cols[`${as}_avatar`],
|
|
81
|
+
ssoAvatarUrl: cols[`${as}_sso_avatar_url`],
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/** Reads the avatar columns (plus the SSO source) of the given accounts in one query. */
|
|
85
|
+
async function avatarRows(db, ids) {
|
|
86
|
+
if (ids.length === 0)
|
|
87
|
+
return [];
|
|
88
|
+
return db
|
|
89
|
+
.selectFrom("accounts")
|
|
90
|
+
.select((eb) => [
|
|
91
|
+
"accounts.id",
|
|
92
|
+
"accounts.display_name",
|
|
93
|
+
...avatarColumns(eb, "accounts", "account"),
|
|
94
|
+
ssoIdentity(eb, "accounts", "source").as("sso_avatar_source"),
|
|
95
|
+
])
|
|
96
|
+
.where("accounts.id", "in", [...new Set(ids)])
|
|
97
|
+
.execute();
|
|
98
|
+
}
|
|
99
|
+
/** One query: ids → PersonRef (missing ids are absent from the map). */
|
|
100
|
+
export async function loadPeople(db, ids) {
|
|
101
|
+
const people = new Map();
|
|
102
|
+
for (const r of await avatarRows(db, ids)) {
|
|
103
|
+
people.set(r.id, {
|
|
104
|
+
id: r.id,
|
|
105
|
+
name: r.display_name,
|
|
106
|
+
avatarUrl: personAvatarUrl(r, "account", r.id),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return people;
|
|
110
|
+
}
|
|
111
|
+
/** The given accounts with `avatarUrl` and `avatarSource`, in their order (one query). */
|
|
112
|
+
export async function withAvatarUrls(db, accounts) {
|
|
113
|
+
const rows = new Map((await avatarRows(db, accounts.map((a) => a.id))).map((r) => [r.id, r]));
|
|
114
|
+
return accounts.map((a) => {
|
|
115
|
+
const r = rows.get(a.id);
|
|
116
|
+
const avatar = ownAvatar(r?.account_avatar);
|
|
117
|
+
const sso = safeSsoUrl(r?.account_sso_avatar_url);
|
|
118
|
+
const avatarSource = avatar
|
|
119
|
+
? { kind: "upload" }
|
|
120
|
+
: sso !== null && r?.sso_avatar_source
|
|
121
|
+
? { kind: "identity", source: r.sso_avatar_source }
|
|
122
|
+
: null;
|
|
123
|
+
return {
|
|
124
|
+
...a,
|
|
125
|
+
avatarUrl: accountAvatarUrl({ id: a.id, avatar, ssoAvatarUrl: sso }),
|
|
126
|
+
avatarSource,
|
|
127
|
+
};
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/** One account with `avatarUrl` and `avatarSource` ({@link withAvatarUrls} for a single one). */
|
|
131
|
+
export async function withAvatarUrl(db, account) {
|
|
132
|
+
const [view] = await withAvatarUrls(db, [account]);
|
|
133
|
+
if (!view)
|
|
134
|
+
throw new Error("withAvatarUrl: withAvatarUrls returned no row");
|
|
135
|
+
return view;
|
|
136
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { PresignedUpload } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
3
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
4
|
+
import { LockService } from "../infra/lock/lock.service.js";
|
|
5
|
+
import type { KernelLogger } from "../log.js";
|
|
6
|
+
import { StorageService } from "../storage/storage.service.js";
|
|
7
|
+
/** The hourly cleanup of expired, unbound account avatar uploads (`@LockedCron`). */
|
|
8
|
+
export declare const ACCOUNT_AVATAR_CLEANUP_JOB = "account-avatar-cleanup";
|
|
9
|
+
/**
|
|
10
|
+
* A person's own avatar uploads (account profile spec §5.3): register → the browser uploads to
|
|
11
|
+
* the presigned URL → `prepare` checks the object → `bind` in the profile write's transaction.
|
|
12
|
+
* Bytes live under `deployment/account-avatars/<accountId>/<uploadId>`; an unbound upload carries
|
|
13
|
+
* `expires_at` and is swept by {@link AccountAvatarCleanupJob}. No in-process state.
|
|
14
|
+
*/
|
|
15
|
+
export declare class AccountAvatarsService {
|
|
16
|
+
private readonly db;
|
|
17
|
+
readonly txHost: TransactionHost;
|
|
18
|
+
private readonly storage;
|
|
19
|
+
private readonly log;
|
|
20
|
+
constructor(db: KernelDb, txHost: TransactionHost, storage: Pick<StorageService, "available" | "assertAvailable" | "deployment">, log: Pick<KernelLogger, "warn">);
|
|
21
|
+
/**
|
|
22
|
+
* Registers a `pending` upload (intent first: the row, expiring in 24 h, is written before the
|
|
23
|
+
* presigned upload is returned, so a crash leaves only an expiring row). A type other than
|
|
24
|
+
* png / jpeg / webp → `attachment.type_not_allowed`; over 2 MB → `attachment.too_large`; already
|
|
25
|
+
* 10 pending uploads → `attachment.too_many_pending` (soft: concurrent calls may pass together);
|
|
26
|
+
* no object store → `storage.unavailable`.
|
|
27
|
+
*/
|
|
28
|
+
createUpload(accountId: string, input: {
|
|
29
|
+
contentType: string;
|
|
30
|
+
size: number;
|
|
31
|
+
}): Promise<{
|
|
32
|
+
uploadId: string;
|
|
33
|
+
upload: PresignedUpload;
|
|
34
|
+
expiresAt: string;
|
|
35
|
+
}>;
|
|
36
|
+
/**
|
|
37
|
+
* Makes the account's upload ready to be bound (outside any transaction: it reads the object
|
|
38
|
+
* store). Not the account's, or expired → `attachment.not_found`. A `pending` upload is checked
|
|
39
|
+
* against the store: no object → `attachment.not_ready`; a size other than declared, a stored
|
|
40
|
+
* type or an image signature other than declared → the object is deleted and
|
|
41
|
+
* `attachment.too_large` / `attachment.not_ready` / `attachment.type_not_allowed`. The expiry of
|
|
42
|
+
* an unbound upload is extended so the cleanup cannot remove it before {@link bind} runs.
|
|
43
|
+
*/
|
|
44
|
+
prepare(accountId: string, uploadId: string): Promise<void>;
|
|
45
|
+
/** The store-side checks of {@link prepare}; returns the stored size. */
|
|
46
|
+
private check;
|
|
47
|
+
/**
|
|
48
|
+
* In the caller's transaction: binds the account's ready, unexpired upload (row lock; its
|
|
49
|
+
* `expires_at` is cleared, so the cleanup keeps it) and expires every other upload of the
|
|
50
|
+
* account, the previous avatar among them. `null` binds none (the avatar is removed). An upload
|
|
51
|
+
* that is not the account's ready one → `attachment.not_ready` while pending, else
|
|
52
|
+
* `attachment.not_found`; binding the bound upload again is fine.
|
|
53
|
+
*/
|
|
54
|
+
bind(accountId: string, uploadId: string | null): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* A short-lived presigned GET (inline, with the checked type) of the account's bound upload;
|
|
57
|
+
* null when there is none, or its object is gone or no longer the size checked.
|
|
58
|
+
*/
|
|
59
|
+
objectUrl(accountId: string, uploadId: string): Promise<string | null>;
|
|
60
|
+
/**
|
|
61
|
+
* Deletes expired uploads (`expires_at < now()`), bound ones never, in batches of 100: the rows
|
|
62
|
+
* are read without a lock, each object is deleted outside any transaction, then its row
|
|
63
|
+
* (`DELETE … WHERE id AND expires_at < now()`). This needs no claim because an expired row can
|
|
64
|
+
* never come back: {@link prepare} extends only an unexpired expiry and {@link bind} refuses an
|
|
65
|
+
* expired row, so nothing binds a row whose object is being deleted. A failed object delete
|
|
66
|
+
* keeps the row for the next run. Does nothing without an object store. Returns the rows deleted.
|
|
67
|
+
*/
|
|
68
|
+
deleteExpired(): Promise<number>;
|
|
69
|
+
}
|
|
70
|
+
/** Deletes expired account avatar uploads once per hour across replicas; the worker role's. */
|
|
71
|
+
export declare class AccountAvatarCleanupJob {
|
|
72
|
+
private readonly avatars;
|
|
73
|
+
readonly lockService: LockService;
|
|
74
|
+
readonly log: KernelLogger;
|
|
75
|
+
constructor(avatars: Pick<AccountAvatarsService, "deleteExpired">, lockService: LockService, log: KernelLogger);
|
|
76
|
+
run(): Promise<void>;
|
|
77
|
+
}
|