@protobase/server 0.126.1 → 0.126.3
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/better-auth/auth-settings.d.ts +64 -0
- package/dist/better-auth/create-auth.d.ts +67 -2
- package/dist/better-auth/create-auth.js +10 -3
- package/dist/better-auth/names/modules/index.d.ts +9 -2
- package/dist/better-auth/names/modules/mail-senders.js +7 -2
- package/dist/better-auth/staff-checks.js +2 -0
- package/dist/better-auth/store/steps/index.js +3 -1
- package/dist/better-auth/store/steps/legacy-versions.js +4 -0
- package/dist/better-auth/store/steps/mail-senders/005-sender-sharing.js +24 -0
- package/dist/package.js +1 -1
- package/dist/personal-mail/listed-mail-senders.d.ts +3 -1
- package/dist/personal-mail/listed-mail-senders.js +1 -0
- package/dist/personal-mail/mail-sender.d.ts +5 -3
- package/dist/personal-mail/mail-sender.js +22 -5
- package/dist/personal-mail/mail-senders-plugin.js +39 -3
- package/dist/personal-mail/personal-mailer.d.ts +8 -6
- package/dist/personal-mail/personal-mailer.js +25 -4
- package/dist/personal-mail/sender-address-error.d.ts +5 -4
- package/dist/personal-mail/sender-address-error.js +4 -3
- package/dist/personal-mail/sender-store.js +13 -6
- package/dist/personal-mail/shared-sender-store.d.ts +2 -0
- package/dist/personal-mail/shared-sender-store.js +39 -0
- package/dist/personal-mail/shared-senders.d.ts +11 -0
- package/dist/personal-mail/shared-senders.js +22 -0
- package/package.json +6 -6
|
@@ -11,6 +11,7 @@ import { Mailer } from "../mail/mail-message.js";
|
|
|
11
11
|
import { PersonalMailProvider } from "../personal-mail/personal-mail-provider.js";
|
|
12
12
|
import { AddVerifier } from "../personal-mail/verifiers/sender-address-verifier.js";
|
|
13
13
|
import { ListedConnection, ListedMailSender, ListedVerification } from "../personal-mail/listed-mail-senders.js";
|
|
14
|
+
import { SharedSender } from "../personal-mail/shared-senders.js";
|
|
14
15
|
import { RoleDefinitions } from "./organizations/role-definitions.js";
|
|
15
16
|
import { ConnectedApp } from "./mcp/connected-apps.js";
|
|
16
17
|
import { OperatorClient } from "./openid-provider/operator-clients.js";
|
|
@@ -6414,6 +6415,7 @@ export declare const buildAppAuth: (settings: AuthSettings, input: AppAuthInput)
|
|
|
6414
6415
|
};
|
|
6415
6416
|
}>>[];
|
|
6416
6417
|
}, {
|
|
6418
|
+
shared: SharedSender[];
|
|
6417
6419
|
verifiers: boolean;
|
|
6418
6420
|
senders: ListedMailSender[];
|
|
6419
6421
|
connections: ListedConnection[];
|
|
@@ -6546,6 +6548,68 @@ export declare const buildAppAuth: (settings: AuthSettings, input: AppAuthInput)
|
|
|
6546
6548
|
}, {
|
|
6547
6549
|
connections: ConnectionListing[];
|
|
6548
6550
|
}>;
|
|
6551
|
+
shareMailSender: import("better-call").StrictEndpoint<string, {
|
|
6552
|
+
method: "POST";
|
|
6553
|
+
use: import("better-call").Middleware<import("better-call").MiddlewareOptions, (inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{
|
|
6554
|
+
session: {
|
|
6555
|
+
session: Record<string, any> & {
|
|
6556
|
+
id: string;
|
|
6557
|
+
createdAt: Date;
|
|
6558
|
+
updatedAt: Date;
|
|
6559
|
+
userId: string;
|
|
6560
|
+
expiresAt: Date;
|
|
6561
|
+
token: string;
|
|
6562
|
+
ipAddress?: string | null | undefined;
|
|
6563
|
+
userAgent?: string | null | undefined;
|
|
6564
|
+
};
|
|
6565
|
+
user: Record<string, any> & {
|
|
6566
|
+
id: string;
|
|
6567
|
+
createdAt: Date;
|
|
6568
|
+
updatedAt: Date;
|
|
6569
|
+
email: string;
|
|
6570
|
+
emailVerified: boolean;
|
|
6571
|
+
name: string;
|
|
6572
|
+
image?: string | null | undefined;
|
|
6573
|
+
};
|
|
6574
|
+
};
|
|
6575
|
+
}>>[];
|
|
6576
|
+
body: import("zod").ZodObject<{
|
|
6577
|
+
id: import("zod").ZodString;
|
|
6578
|
+
}, import("zod/v4/core").$strip>;
|
|
6579
|
+
}, {
|
|
6580
|
+
sender: ListedMailSender;
|
|
6581
|
+
}>;
|
|
6582
|
+
unshareMailSender: import("better-call").StrictEndpoint<string, {
|
|
6583
|
+
method: "POST";
|
|
6584
|
+
use: import("better-call").Middleware<import("better-call").MiddlewareOptions, (inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{
|
|
6585
|
+
session: {
|
|
6586
|
+
session: Record<string, any> & {
|
|
6587
|
+
id: string;
|
|
6588
|
+
createdAt: Date;
|
|
6589
|
+
updatedAt: Date;
|
|
6590
|
+
userId: string;
|
|
6591
|
+
expiresAt: Date;
|
|
6592
|
+
token: string;
|
|
6593
|
+
ipAddress?: string | null | undefined;
|
|
6594
|
+
userAgent?: string | null | undefined;
|
|
6595
|
+
};
|
|
6596
|
+
user: Record<string, any> & {
|
|
6597
|
+
id: string;
|
|
6598
|
+
createdAt: Date;
|
|
6599
|
+
updatedAt: Date;
|
|
6600
|
+
email: string;
|
|
6601
|
+
emailVerified: boolean;
|
|
6602
|
+
name: string;
|
|
6603
|
+
image?: string | null | undefined;
|
|
6604
|
+
};
|
|
6605
|
+
};
|
|
6606
|
+
}>>[];
|
|
6607
|
+
body: import("zod").ZodObject<{
|
|
6608
|
+
id: import("zod").ZodString;
|
|
6609
|
+
}, import("zod/v4/core").$strip>;
|
|
6610
|
+
}, {
|
|
6611
|
+
sender: ListedMailSender;
|
|
6612
|
+
}>;
|
|
6549
6613
|
};
|
|
6550
6614
|
} | {
|
|
6551
6615
|
id: "protobase-mcp-signed-query-guard";
|
|
@@ -11,6 +11,8 @@ import { Mailer } from "../mail/mail-message.js";
|
|
|
11
11
|
import { PersonalMailProvider } from "../personal-mail/personal-mail-provider.js";
|
|
12
12
|
import { SenderAddressVerifier } from "../personal-mail/verifiers/sender-address-verifier.js";
|
|
13
13
|
import { ListedConnection, ListedMailSender, ListedVerification } from "../personal-mail/listed-mail-senders.js";
|
|
14
|
+
import "../personal-mail/shared-sender-store.js";
|
|
15
|
+
import { SharedSender } from "../personal-mail/shared-senders.js";
|
|
14
16
|
import { RoleDefinitions, RolesInput } from "./organizations/role-definitions.js";
|
|
15
17
|
import { ConnectedApp } from "./mcp/connected-apps.js";
|
|
16
18
|
import { OperatorClient } from "./openid-provider/operator-clients.js";
|
|
@@ -6601,6 +6603,7 @@ export declare const createAuth: (options: CreateAuthOptions, plugins?: AuthPlug
|
|
|
6601
6603
|
};
|
|
6602
6604
|
}>>[];
|
|
6603
6605
|
}, {
|
|
6606
|
+
shared: SharedSender[];
|
|
6604
6607
|
verifiers: boolean;
|
|
6605
6608
|
senders: ListedMailSender[];
|
|
6606
6609
|
connections: ListedConnection[];
|
|
@@ -6733,6 +6736,68 @@ export declare const createAuth: (options: CreateAuthOptions, plugins?: AuthPlug
|
|
|
6733
6736
|
}, {
|
|
6734
6737
|
connections: ConnectionListing[];
|
|
6735
6738
|
}>;
|
|
6739
|
+
shareMailSender: import("better-call").StrictEndpoint<string, {
|
|
6740
|
+
method: "POST";
|
|
6741
|
+
use: import("better-call").Middleware<import("better-call").MiddlewareOptions, (inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{
|
|
6742
|
+
session: {
|
|
6743
|
+
session: Record<string, any> & {
|
|
6744
|
+
id: string;
|
|
6745
|
+
createdAt: Date;
|
|
6746
|
+
updatedAt: Date;
|
|
6747
|
+
userId: string;
|
|
6748
|
+
expiresAt: Date;
|
|
6749
|
+
token: string;
|
|
6750
|
+
ipAddress?: string | null | undefined;
|
|
6751
|
+
userAgent?: string | null | undefined;
|
|
6752
|
+
};
|
|
6753
|
+
user: Record<string, any> & {
|
|
6754
|
+
id: string;
|
|
6755
|
+
createdAt: Date;
|
|
6756
|
+
updatedAt: Date;
|
|
6757
|
+
email: string;
|
|
6758
|
+
emailVerified: boolean;
|
|
6759
|
+
name: string;
|
|
6760
|
+
image?: string | null | undefined;
|
|
6761
|
+
};
|
|
6762
|
+
};
|
|
6763
|
+
}>>[];
|
|
6764
|
+
body: import("zod").ZodObject<{
|
|
6765
|
+
id: import("zod").ZodString;
|
|
6766
|
+
}, import("zod/v4/core").$strip>;
|
|
6767
|
+
}, {
|
|
6768
|
+
sender: ListedMailSender;
|
|
6769
|
+
}>;
|
|
6770
|
+
unshareMailSender: import("better-call").StrictEndpoint<string, {
|
|
6771
|
+
method: "POST";
|
|
6772
|
+
use: import("better-call").Middleware<import("better-call").MiddlewareOptions, (inputContext: import("better-call").MiddlewareInputContext<import("better-call").MiddlewareOptions>) => Promise<{
|
|
6773
|
+
session: {
|
|
6774
|
+
session: Record<string, any> & {
|
|
6775
|
+
id: string;
|
|
6776
|
+
createdAt: Date;
|
|
6777
|
+
updatedAt: Date;
|
|
6778
|
+
userId: string;
|
|
6779
|
+
expiresAt: Date;
|
|
6780
|
+
token: string;
|
|
6781
|
+
ipAddress?: string | null | undefined;
|
|
6782
|
+
userAgent?: string | null | undefined;
|
|
6783
|
+
};
|
|
6784
|
+
user: Record<string, any> & {
|
|
6785
|
+
id: string;
|
|
6786
|
+
createdAt: Date;
|
|
6787
|
+
updatedAt: Date;
|
|
6788
|
+
email: string;
|
|
6789
|
+
emailVerified: boolean;
|
|
6790
|
+
name: string;
|
|
6791
|
+
image?: string | null | undefined;
|
|
6792
|
+
};
|
|
6793
|
+
};
|
|
6794
|
+
}>>[];
|
|
6795
|
+
body: import("zod").ZodObject<{
|
|
6796
|
+
id: import("zod").ZodString;
|
|
6797
|
+
}, import("zod/v4/core").$strip>;
|
|
6798
|
+
}, {
|
|
6799
|
+
sender: ListedMailSender;
|
|
6800
|
+
}>;
|
|
6736
6801
|
};
|
|
6737
6802
|
} | {
|
|
6738
6803
|
id: "protobase-mcp-signed-query-guard";
|
|
@@ -6840,7 +6905,7 @@ export declare const createAuth: (options: CreateAuthOptions, plugins?: AuthPlug
|
|
|
6840
6905
|
SERVER_ONLY: true;
|
|
6841
6906
|
};
|
|
6842
6907
|
}, {
|
|
6843
|
-
jwks_uri
|
|
6908
|
+
jwks_uri? /** The roles with their labels, for the role switcher. */ : string | undefined;
|
|
6844
6909
|
userinfo_endpoint: string;
|
|
6845
6910
|
acr_values_supported?: string[] | undefined;
|
|
6846
6911
|
subject_types_supported: ("public" | "pairwise")[];
|
|
@@ -9533,7 +9598,7 @@ export declare const createAuth: (options: CreateAuthOptions, plugins?: AuthPlug
|
|
|
9533
9598
|
SERVER_ONLY: true;
|
|
9534
9599
|
};
|
|
9535
9600
|
}, {
|
|
9536
|
-
jwks_uri
|
|
9601
|
+
jwks_uri? /** The roles with their labels, for the role switcher. */ : string | undefined;
|
|
9537
9602
|
userinfo_endpoint: string;
|
|
9538
9603
|
acr_values_supported?: string[] | undefined;
|
|
9539
9604
|
subject_types_supported: ("public" | "pairwise")[];
|
|
@@ -17,6 +17,7 @@ import { mailSendersPlugin } from "../personal-mail/mail-senders-plugin.js";
|
|
|
17
17
|
import { senderVerifications } from "../personal-mail/verifiers/sender-verifications.js";
|
|
18
18
|
import { verificationStore } from "../personal-mail/verifiers/verification-store.js";
|
|
19
19
|
import { mailSenderUseCheck } from "../personal-mail/mail-sender-use-check.js";
|
|
20
|
+
import { sharedSenderReader } from "../personal-mail/shared-sender-store.js";
|
|
20
21
|
import { bothAccountHooks, connectionAccountHooks } from "./connections/connection-account-hooks.js";
|
|
21
22
|
import { connectionEvents } from "./connections/connection-events.js";
|
|
22
23
|
import { listConnections } from "./connections/list-connections.js";
|
|
@@ -160,7 +161,7 @@ const pageLoadReads = [
|
|
|
160
161
|
* The options `createAuth` gives Better Auth, with the roles, default role, mailer, operator provider and platform
|
|
161
162
|
* sign-in provider already resolved.
|
|
162
163
|
*/
|
|
163
|
-
const betterAuthOptions = (options, { origins = [], passkeyDomain, roles, definitions, defaultRole, appName = defaultAppName, mailer, signUpRole, signUpDomains, signUpFields = resolveSignUpFields(void 0), operator, platformSignIn = [], saml = [], organizations, mcp, api, openId, personalAccessTokens, context = noContext, signInPolicy = defaultSignInPolicy, mobile, deletion, mailSenderUse, senderVerifications: verifications, personalMailer: mailOf = noMailer, lockedAccounts = [], connections, names = resolveNames({
|
|
164
|
+
const betterAuthOptions = (options, { origins = [], passkeyDomain, roles, definitions, defaultRole, appName = defaultAppName, mailer, signUpRole, signUpDomains, signUpFields = resolveSignUpFields(void 0), operator, platformSignIn = [], saml = [], organizations, mcp, api, openId, personalAccessTokens, context = noContext, signInPolicy = defaultSignInPolicy, mobile, deletion, mailSenderUse, senderVerifications: verifications, personalMailer: mailOf = noMailer, sharedSenders, lockedAccounts = [], connections, names = resolveNames({
|
|
164
165
|
schema: storeSchema({ options }),
|
|
165
166
|
tables: options.tables
|
|
166
167
|
}), ...plugins }) => {
|
|
@@ -396,7 +397,7 @@ const betterAuthOptions = (options, { origins = [], passkeyDomain, roles, defini
|
|
|
396
397
|
definitions,
|
|
397
398
|
names: named
|
|
398
399
|
})] : [],
|
|
399
|
-
...mailProviders.length > 0 ? [mailSendersPlugin(mailProviders, named, mailOf, mailSenderUse, verifications)] : [],
|
|
400
|
+
...mailProviders.length > 0 ? [mailSendersPlugin(mailProviders, named, mailOf, mailSenderUse, verifications, sharedSenders)] : [],
|
|
400
401
|
...mcp ? loaded(plugins.mcpAuthPlugins, "mcp")({
|
|
401
402
|
mcp,
|
|
402
403
|
...api && { api },
|
|
@@ -571,6 +572,10 @@ const createAuth = (options, plugins = {}) => {
|
|
|
571
572
|
const verifiers = options.senderVerifiers ?? [];
|
|
572
573
|
if (verifiers.length > 0 && personalMail.length === 0) throw new Error("auth.senderVerifiers: verifiers prove From addresses of linked mailboxes; turn on auth.personalMail");
|
|
573
574
|
const verifications = verifiers.length > 0 ? senderVerifications(verifiers, verificationStore(async () => storeSqlOf(await context())), mailboxOf) : void 0;
|
|
575
|
+
const sharedSenders = sharedSenderReader(async () => storeSqlOf(await context()), {
|
|
576
|
+
organizations: Boolean(organizations),
|
|
577
|
+
verifiers: verifications?.ids ?? []
|
|
578
|
+
});
|
|
574
579
|
if (resolvedConnections.length > 0) deletion.set("connections", releaseUserConnections({
|
|
575
580
|
connections: resolvedConnections,
|
|
576
581
|
events,
|
|
@@ -590,6 +595,7 @@ const createAuth = (options, plugins = {}) => {
|
|
|
590
595
|
mailSenderUse: senderUse,
|
|
591
596
|
...verifications && { senderVerifications: verifications },
|
|
592
597
|
personalMailer: mailboxOf,
|
|
598
|
+
sharedSenders,
|
|
593
599
|
lockedAccounts,
|
|
594
600
|
origins,
|
|
595
601
|
passkeyDomain,
|
|
@@ -622,7 +628,8 @@ const createAuth = (options, plugins = {}) => {
|
|
|
622
628
|
instance = auth;
|
|
623
629
|
const senderStore = {
|
|
624
630
|
...sqlSenderStore(async () => storeSqlOf(await context())),
|
|
625
|
-
...verifications && { proofsOf: verifications.proofsOf }
|
|
631
|
+
...verifications && { proofsOf: verifications.proofsOf },
|
|
632
|
+
sharedWith: sharedSenders
|
|
626
633
|
};
|
|
627
634
|
return Object.assign(auth, {
|
|
628
635
|
/**
|
|
@@ -749,7 +749,14 @@ export declare const builtInModules: readonly [{
|
|
|
749
749
|
field: "shared";
|
|
750
750
|
attribute: {
|
|
751
751
|
readonly type: "boolean";
|
|
752
|
-
readonly required:
|
|
752
|
+
readonly required: false;
|
|
753
|
+
};
|
|
754
|
+
};
|
|
755
|
+
readonly shared_at: {
|
|
756
|
+
field: "sharedAt";
|
|
757
|
+
attribute: {
|
|
758
|
+
readonly type: "date";
|
|
759
|
+
readonly required: false;
|
|
753
760
|
};
|
|
754
761
|
};
|
|
755
762
|
readonly signature: {
|
|
@@ -824,7 +831,7 @@ export declare const builtInModules: readonly [{
|
|
|
824
831
|
};
|
|
825
832
|
};
|
|
826
833
|
};
|
|
827
|
-
readonly indexes: ["mail_sender_default_idx", "mail_sender_shared_idx", "mail_sender_connection_address_uidx", "mail_sender_user_address_idx"];
|
|
834
|
+
readonly indexes: ["mail_sender_default_idx", "mail_sender_shared_idx", "mail_sender_connection_address_uidx", "mail_sender_user_address_idx", "mail_sender_shared_at_idx"];
|
|
828
835
|
};
|
|
829
836
|
readonly mail_sender_verification: {
|
|
830
837
|
readonly scope: "global";
|
|
@@ -26,7 +26,11 @@ const mailSenders = {
|
|
|
26
26
|
}),
|
|
27
27
|
shared: own("shared", {
|
|
28
28
|
type: "boolean",
|
|
29
|
-
required:
|
|
29
|
+
required: false
|
|
30
|
+
}),
|
|
31
|
+
shared_at: own("sharedAt", {
|
|
32
|
+
type: "date",
|
|
33
|
+
required: false
|
|
30
34
|
}),
|
|
31
35
|
signature: own("signature", {
|
|
32
36
|
type: "string",
|
|
@@ -74,7 +78,8 @@ const mailSenders = {
|
|
|
74
78
|
"mail_sender_default_idx",
|
|
75
79
|
"mail_sender_shared_idx",
|
|
76
80
|
"mail_sender_connection_address_uidx",
|
|
77
|
-
"mail_sender_user_address_idx"
|
|
81
|
+
"mail_sender_user_address_idx",
|
|
82
|
+
"mail_sender_shared_at_idx"
|
|
78
83
|
]
|
|
79
84
|
},
|
|
80
85
|
mail_sender_verification: {
|
|
@@ -69,6 +69,8 @@ const accountChanges = [
|
|
|
69
69
|
"/account/mail-senders/add",
|
|
70
70
|
"/account/mail-senders/update",
|
|
71
71
|
"/account/mail-senders/remove",
|
|
72
|
+
"/account/mail-senders/share",
|
|
73
|
+
"/account/mail-senders/unshare",
|
|
72
74
|
"/account/mail-senders/verify",
|
|
73
75
|
"/account/mail-senders/refresh"
|
|
74
76
|
];
|
|
@@ -7,6 +7,7 @@ import { mailSenders } from "./mail-senders/001-mail-senders.js";
|
|
|
7
7
|
import { mailSenderNames } from "./mail-senders/002-mail-sender-names.js";
|
|
8
8
|
import { senderAddressEvidence } from "./mail-senders/003-sender-address-evidence.js";
|
|
9
9
|
import { senderVerifications } from "./mail-senders/004-sender-verifications.js";
|
|
10
|
+
import { senderSharing } from "./mail-senders/005-sender-sharing.js";
|
|
10
11
|
import { mcp } from "./mcp/001-mcp.js";
|
|
11
12
|
import { organizations } from "./organizations/001-organizations.js";
|
|
12
13
|
import { organizationIndex } from "./organizations/002-organization-index.js";
|
|
@@ -39,7 +40,8 @@ const moduleSteps = {
|
|
|
39
40
|
mailSenders,
|
|
40
41
|
mailSenderNames,
|
|
41
42
|
senderAddressEvidence,
|
|
42
|
-
senderVerifications
|
|
43
|
+
senderVerifications,
|
|
44
|
+
senderSharing
|
|
43
45
|
],
|
|
44
46
|
platform_devices: [platformDevices]
|
|
45
47
|
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ddl } from "../../ddl.js";
|
|
2
|
+
//#region src/better-auth/store/steps/mail-senders/005-sender-sharing.ts
|
|
3
|
+
/**
|
|
4
|
+
* When the owner of each From address shared it with the app's other senders, NULL while it is theirs alone. It takes
|
|
5
|
+
* the place of `shared`, which nothing writes from this version on, so it may be NULL; a later step drops it.
|
|
6
|
+
*/
|
|
7
|
+
const senderSharing = {
|
|
8
|
+
step: 5,
|
|
9
|
+
version: 17,
|
|
10
|
+
name: "sender_sharing",
|
|
11
|
+
title: "When the owner of each From address shared it with the app's other senders",
|
|
12
|
+
tables: [],
|
|
13
|
+
statements: ({ table }) => {
|
|
14
|
+
const sender = table("mail_sender");
|
|
15
|
+
const s = sender.columns;
|
|
16
|
+
return [
|
|
17
|
+
ddl`alter table ${sender} add column ${s.shared_at} timestamptz`,
|
|
18
|
+
ddl`alter table ${sender} alter column ${s.shared} drop not null`,
|
|
19
|
+
ddl`create index ${sender.index("mail_sender_shared_at_idx")} on ${sender} (${s.user_id}) where ${s.shared_at} is not null`
|
|
20
|
+
];
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
//#endregion
|
|
24
|
+
export { senderSharing };
|
package/dist/package.js
CHANGED
|
@@ -5,7 +5,8 @@ import "./mail-sender.js";
|
|
|
5
5
|
//#region src/personal-mail/listed-mail-senders.d.ts
|
|
6
6
|
/**
|
|
7
7
|
* An address as the account page lists it: its `state`, `detached` for one on no connection they can send through,
|
|
8
|
-
* and its `connection`, none when detached; `listedAt` when the provider's last listing held it
|
|
8
|
+
* and its `connection`, none when detached; `listedAt` when the provider's last listing held it; `sharedAt` when they
|
|
9
|
+
* shared it with the app's other senders.
|
|
9
10
|
*/
|
|
10
11
|
export type ListedMailSender = {
|
|
11
12
|
id: string;
|
|
@@ -16,6 +17,7 @@ export type ListedMailSender = {
|
|
|
16
17
|
state: Exclude<SenderRowState, 'removed'>;
|
|
17
18
|
connection?: string;
|
|
18
19
|
listedAt?: string;
|
|
20
|
+
sharedAt?: string;
|
|
19
21
|
/** The latest verification by each of the app's verifiers, when it has any. */
|
|
20
22
|
verifications?: ListedVerification[];
|
|
21
23
|
};
|
|
@@ -23,6 +23,7 @@ const listedSender = (book, row, verifications) => {
|
|
|
23
23
|
state,
|
|
24
24
|
...state !== "detached" && row.connectionId && { connection: row.connectionId },
|
|
25
25
|
...row.listedAt && { listedAt: new Date(row.listedAt).toISOString() },
|
|
26
|
+
...row.sharedAt && { sharedAt: new Date(row.sharedAt).toISOString() },
|
|
26
27
|
...verifications && { verifications: verifications.filter(({ senderId }) => senderId === row.id).map(listedVerification) }
|
|
27
28
|
};
|
|
28
29
|
};
|
|
@@ -7,14 +7,16 @@ import "./mailbox-connections.js";
|
|
|
7
7
|
* A From address someone may send mail from: `id`, the record a send names (`sender_address_id`), null for the
|
|
8
8
|
* mailbox's own address, which goes out with no From header, so the provider fills in its own; `address`, unknown
|
|
9
9
|
* when it is the mailbox's own and the link does not name it; the display `name` its owner entered, if any; `ownerId`,
|
|
10
|
-
* whose mailbox it goes out through
|
|
11
|
-
* mailbox's own address is `verified`: the provider's sign-in
|
|
10
|
+
* whose mailbox it goes out through, none for an address someone else shared (`shared`); its own `signature`, if it
|
|
11
|
+
* has one; and its `state` (`SenderAddressState`). The mailbox's own address is `verified`: the provider's sign-in
|
|
12
|
+
* vouches for it, as a shared one is only while its owner's evidence says so.
|
|
12
13
|
*/
|
|
13
14
|
export type MailSender = {
|
|
14
15
|
id: string | null;
|
|
15
16
|
address?: string;
|
|
16
17
|
name?: string;
|
|
17
|
-
ownerId
|
|
18
|
+
ownerId?: string;
|
|
19
|
+
shared?: true;
|
|
18
20
|
signature?: string;
|
|
19
21
|
state: SenderAddressState;
|
|
20
22
|
};
|
|
@@ -21,6 +21,14 @@ const senderOf = (row, state) => ({
|
|
|
21
21
|
...row.signature && { signature: row.signature },
|
|
22
22
|
state
|
|
23
23
|
});
|
|
24
|
+
const sharedSender = (row) => ({
|
|
25
|
+
id: row.id,
|
|
26
|
+
address: row.address,
|
|
27
|
+
...row.name && { name: row.name },
|
|
28
|
+
shared: true,
|
|
29
|
+
...row.signature && { signature: row.signature },
|
|
30
|
+
state: "verified"
|
|
31
|
+
});
|
|
24
32
|
const mailboxSender = (book) => ({
|
|
25
33
|
id: null,
|
|
26
34
|
...book.ownAddress && { address: book.ownAddress },
|
|
@@ -30,9 +38,10 @@ const mailboxSender = (book) => ({
|
|
|
30
38
|
const sameAddress = (a, b) => a.trim().toLowerCase() === b.trim().toLowerCase();
|
|
31
39
|
/**
|
|
32
40
|
* The sender `book`'s person sends as for `address`, matched without regard to case: one of their usable records;
|
|
33
|
-
* else their mailbox's own address
|
|
34
|
-
* `MailSenderRefusedError` any other address, one someone else
|
|
35
|
-
* one that is a record on more than one of
|
|
41
|
+
* else their mailbox's own address; else one someone else shared with them (`book.shared`). Without `address`, their
|
|
42
|
+
* default record, else their mailbox's own. Refuses with `MailSenderRefusedError` any other address, one someone else
|
|
43
|
+
* entered and did not share included, and with `sender_address_ambiguous` one that is a record on more than one of
|
|
44
|
+
* their connections, or shared by more than one person.
|
|
36
45
|
*/
|
|
37
46
|
const chooseSender = (book, address) => {
|
|
38
47
|
const usable = usableRows(book);
|
|
@@ -44,12 +53,20 @@ const chooseSender = (book, address) => {
|
|
|
44
53
|
if (matching.length > 1) throw new SenderAddressError("sender_address_ambiguous");
|
|
45
54
|
if (matching[0]) return senderOf(matching[0].row, matching[0].state);
|
|
46
55
|
if (book.ownAddress && sameAddress(book.ownAddress, address)) return mailboxSender(book);
|
|
56
|
+
const shared = (book.shared ?? []).filter((row) => sameAddress(row.address, address));
|
|
57
|
+
if (shared.length > 1) throw new SenderAddressError("sender_address_ambiguous");
|
|
58
|
+
if (shared[0]) return sharedSender(shared[0]);
|
|
47
59
|
throw new MailSenderRefusedError(address);
|
|
48
60
|
};
|
|
49
|
-
/**
|
|
61
|
+
/**
|
|
62
|
+
* Every sender the person may choose: their mailbox's own address, when the link names it and no record holds it, then
|
|
63
|
+
* their records, then the addresses others shared with them that none of theirs holds.
|
|
64
|
+
*/
|
|
50
65
|
const usableSenders = (book) => {
|
|
51
66
|
const usable = usableRows(book);
|
|
52
|
-
|
|
67
|
+
const theirs = [...book.ownAddress && !usable.some(({ row }) => sameAddress(row.address, book.ownAddress)) ? [mailboxSender(book)] : [], ...usable.map(({ row, state }) => senderOf(row, state))];
|
|
68
|
+
const shared = (book.shared ?? []).filter((row) => !theirs.some((sender) => sender.address && sameAddress(sender.address, row.address)));
|
|
69
|
+
return [...theirs, ...shared.map(sharedSender)];
|
|
53
70
|
};
|
|
54
71
|
//#endregion
|
|
55
72
|
export { chooseSender, senderOf, stateIn, usableRows, usableSenders };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { senderAddressInput } from "./sender-address-input.js";
|
|
2
2
|
import { isRemoved } from "./sender-address-state.js";
|
|
3
3
|
import { stateIn } from "./mail-sender.js";
|
|
4
|
+
import { listedShared, usableShared } from "./shared-senders.js";
|
|
4
5
|
import { linkedMailbox } from "./personal-mailer.js";
|
|
5
6
|
import { headersLocale } from "../locale/request-locale.js";
|
|
6
7
|
import { storeSqlOf } from "../better-auth/store/store-sql.js";
|
|
@@ -18,7 +19,9 @@ const mailSenderPaths = {
|
|
|
18
19
|
update: "/account/mail-senders/update",
|
|
19
20
|
remove: "/account/mail-senders/remove",
|
|
20
21
|
refresh: "/account/mail-senders/refresh",
|
|
21
|
-
verify: "/account/mail-senders/verify"
|
|
22
|
+
verify: "/account/mail-senders/verify",
|
|
23
|
+
share: "/account/mail-senders/share",
|
|
24
|
+
unshare: "/account/mail-senders/unshare"
|
|
22
25
|
};
|
|
23
26
|
/** The longest signature, in characters: a closing and a few lines of contact details. */
|
|
24
27
|
const maxSignatureLength = 2e3;
|
|
@@ -38,6 +41,10 @@ const noMailbox = {
|
|
|
38
41
|
code: "MAILBOX_NOT_FOUND",
|
|
39
42
|
message: "Connect a mailbox first."
|
|
40
43
|
};
|
|
44
|
+
const notVerified = {
|
|
45
|
+
code: "MAIL_SENDER_NOT_VERIFIED",
|
|
46
|
+
message: "Only a verified address can be shared: prove it is yours first."
|
|
47
|
+
};
|
|
41
48
|
const unlinkPath = "/unlink-account";
|
|
42
49
|
const signatureField = z$1.string().max(maxSignatureLength).transform((value) => value.trim() ? value : null);
|
|
43
50
|
const nameField = z$1.string().max(100).regex(/^[^\r\n]*$/).transform((value) => value.trim() || null);
|
|
@@ -82,8 +89,13 @@ const ownRow = (rows, id) => {
|
|
|
82
89
|
* verification by each verifier; `POST /account/mail-senders/verify { id }` starts every verifier on one of the
|
|
83
90
|
* person's addresses on a connection they can send through, and answers that address's `verifications`; and
|
|
84
91
|
* `POST /mail-sender-verifications/report` settles one from outside (`reportEndpoint`). Without, neither path exists.
|
|
92
|
+
*
|
|
93
|
+
* `POST /account/mail-senders/share { id }` shares one of the person's addresses with the app's other senders, refused
|
|
94
|
+
* with a 409 `MAIL_SENDER_NOT_VERIFIED` unless it is `verified`; `POST /account/mail-senders/unshare { id }` makes it
|
|
95
|
+
* theirs alone again. The list answers, as `shared`, the verified addresses others shared with the person that are in
|
|
96
|
+
* reach (`sharedWith`), each by id, address and display name alone.
|
|
85
97
|
*/
|
|
86
|
-
const mailSendersPlugin = (providers, names, mailOf, inUse, verifications) => ({
|
|
98
|
+
const mailSendersPlugin = (providers, names, mailOf, inUse, verifications, sharedWith) => ({
|
|
87
99
|
id: "protobase-mail-senders",
|
|
88
100
|
schema: names.tables(mailSenderModel),
|
|
89
101
|
hooks: { before: [{
|
|
@@ -102,9 +114,10 @@ const mailSendersPlugin = (providers, names, mailOf, inUse, verifications) => ({
|
|
|
102
114
|
use: [sessionMiddleware]
|
|
103
115
|
}, async (ctx) => {
|
|
104
116
|
const { userId, book } = await bookOf(ctx, providers, verifications);
|
|
105
|
-
const latest = verifications ?
|
|
117
|
+
const [latest, shared] = await Promise.all([verifications ? verifications.latestOf(userId) : void 0, sharedWith ? sharedWith(userId) : []]);
|
|
106
118
|
return ctx.json({
|
|
107
119
|
...listedMailSenders(book, latest),
|
|
120
|
+
shared: usableShared(shared, providers).map(listedShared),
|
|
108
121
|
verifiers: Boolean(verifications)
|
|
109
122
|
});
|
|
110
123
|
}),
|
|
@@ -176,6 +189,29 @@ const mailSendersPlugin = (providers, names, mailOf, inUse, verifications) => ({
|
|
|
176
189
|
const connections = await mailOf(ctx.context.session.user.id).refresh();
|
|
177
190
|
return ctx.json({ connections });
|
|
178
191
|
}),
|
|
192
|
+
shareMailSender: createAuthEndpoint(mailSenderPaths.share, {
|
|
193
|
+
method: "POST",
|
|
194
|
+
use: [sessionMiddleware],
|
|
195
|
+
body: z$1.object({ id: idField })
|
|
196
|
+
}, async (ctx) => {
|
|
197
|
+
const { userId, store, book } = await bookOf(ctx, providers, verifications);
|
|
198
|
+
const row = ownRow(book.rows, ctx.body.id);
|
|
199
|
+
if (stateIn(book, row) !== "verified") throw new APIError("CONFLICT", notVerified);
|
|
200
|
+
const shared = await store.share(userId, row.id, /* @__PURE__ */ new Date());
|
|
201
|
+
if (!shared) throw new APIError("NOT_FOUND", notFound);
|
|
202
|
+
return ctx.json({ sender: listedSender(book, shared) });
|
|
203
|
+
}),
|
|
204
|
+
unshareMailSender: createAuthEndpoint(mailSenderPaths.unshare, {
|
|
205
|
+
method: "POST",
|
|
206
|
+
use: [sessionMiddleware],
|
|
207
|
+
body: z$1.object({ id: idField })
|
|
208
|
+
}, async (ctx) => {
|
|
209
|
+
const { userId, store, book } = await bookOf(ctx, providers, verifications);
|
|
210
|
+
ownRow(book.rows, ctx.body.id);
|
|
211
|
+
const row = await store.share(userId, ctx.body.id, null);
|
|
212
|
+
if (!row) throw new APIError("NOT_FOUND", notFound);
|
|
213
|
+
return ctx.json({ sender: listedSender(book, row) });
|
|
214
|
+
}),
|
|
179
215
|
...verifications && {
|
|
180
216
|
verifyMailSender: createAuthEndpoint(mailSenderPaths.verify, {
|
|
181
217
|
method: "POST",
|
|
@@ -15,16 +15,18 @@ export type PersonalMailMessage = MailMessage & {
|
|
|
15
15
|
export type PersonalMail = {
|
|
16
16
|
/**
|
|
17
17
|
* Sends `message` through the mailbox connection of the From address it names, from that address, with its display
|
|
18
|
-
* name: the record decides both, so they cannot disagree.
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
18
|
+
* name: the record decides both, so they cannot disagree. An address someone else shared with the person goes out
|
|
19
|
+
* through its owner's connection, only while it is shared, verified and its owner in reach. Rejects, sending nothing,
|
|
20
|
+
* with a 404 problem for an id that is neither one of the person's nor shared with them, and with a
|
|
21
|
+
* `SenderAddressError` for an address the provider says the mailbox cannot send as (`failed`), one still `pending`,
|
|
22
|
+
* one on no connection they can send through (`detached`), or a shared one whose owner must connect their mailbox
|
|
23
|
+
* again (`owner_unlinked`). An address no listing read yet is listed first, when its provider can. Resolves to the `Message-ID` the recipient
|
|
22
24
|
* sees when the mail provider can tell it (see `SentMail`).
|
|
23
25
|
*/
|
|
24
26
|
send: (message: PersonalMailMessage) => Promise<SentMail>;
|
|
25
|
-
/** The sender a From address stands for, with its record's id and own signature, or refuses as `chooseSender` does. */
|
|
27
|
+
/** The sender a From address stands for, the person's own or one shared with them, with its record's id and own signature, or refuses as `chooseSender` does. */
|
|
26
28
|
sender: (address?: string) => Promise<MailSender>;
|
|
27
|
-
/** Every sender the person may choose, as a send dialog lists them, each with its state. */
|
|
29
|
+
/** Every sender the person may choose, as a send dialog lists them, each with its state, those shared with them last. */
|
|
28
30
|
senders: () => Promise<MailSender[]>;
|
|
29
31
|
/** Lists the send-as addresses of each of the person's connections whose provider can, each on its own (`listConnections`). */
|
|
30
32
|
refresh: () => Promise<ConnectionListing[]>;
|
|
@@ -4,6 +4,7 @@ import { hasListing, isRemoved, senderRowState } from "./sender-address-state.js
|
|
|
4
4
|
import { chooseSender, usableSenders } from "./mail-sender.js";
|
|
5
5
|
import { mailboxConnections } from "./mailbox-connections.js";
|
|
6
6
|
import { listConnection, listConnections } from "./sender-listing.js";
|
|
7
|
+
import { sharedConnection, sharedState, usableShared } from "./shared-senders.js";
|
|
7
8
|
//#region src/personal-mail/personal-mailer.ts
|
|
8
9
|
/**
|
|
9
10
|
* The mail provider of `linked`, the accounts linked to a person, of the app's `providers` in their order, with the
|
|
@@ -26,7 +27,8 @@ const refuseOwnFrom = (message) => {
|
|
|
26
27
|
};
|
|
27
28
|
/**
|
|
28
29
|
* Mail from `userId`'s own mailbox: each message goes out through one of the connections they linked, with an access
|
|
29
|
-
* token refreshed when it expired, from a From address record of theirs
|
|
30
|
+
* token refreshed when it expired, from a From address record of theirs, or through its owner's connection from one
|
|
31
|
+
* shared with them. Rejects with a `PersonalMailError` and sends
|
|
30
32
|
* nothing when the mailbox it goes through is not linked (`not_linked`) or the provider no longer honours the link (`link_expired`).
|
|
31
33
|
*/
|
|
32
34
|
const personalMailer = (accounts, providers, senders, userId, now = () => /* @__PURE__ */ new Date()) => {
|
|
@@ -52,7 +54,7 @@ const personalMailer = (accounts, providers, senders, userId, now = () => /* @__
|
|
|
52
54
|
};
|
|
53
55
|
};
|
|
54
56
|
const sendThrough = async ({ account, provider }, message, row) => {
|
|
55
|
-
const accessToken = await accounts.accessToken(userId, account, provider.id);
|
|
57
|
+
const accessToken = await accounts.accessToken(row?.userId ?? userId, account, provider.id);
|
|
56
58
|
return provider.send(message, accessToken, row && {
|
|
57
59
|
address: row.address,
|
|
58
60
|
...row.name && { name: row.name }
|
|
@@ -68,6 +70,24 @@ const personalMailer = (accounts, providers, senders, userId, now = () => /* @__
|
|
|
68
70
|
if ((await listConnection(lister, connection)).status !== "listed") return row;
|
|
69
71
|
return (await senders.ofUser(userId)).find((candidate) => candidate.id === row.id) ?? row;
|
|
70
72
|
};
|
|
73
|
+
const sendShared = async (id, message) => {
|
|
74
|
+
const [row] = senders.sharedWith ? await senders.sharedWith(userId, id) : [];
|
|
75
|
+
if (!row) throw new SenderAddressNotFoundError();
|
|
76
|
+
const state = sharedState(row, providers);
|
|
77
|
+
const connection = sharedConnection(row, providers);
|
|
78
|
+
if (state === "detached") throw new SenderAddressError("sender_address_owner_unlinked");
|
|
79
|
+
if (state !== "verified" || !connection) throw new SenderAddressNotFoundError();
|
|
80
|
+
try {
|
|
81
|
+
return await sendThrough(connection, message, row);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
if (error instanceof PersonalMailError && error.code === "link_expired") throw new SenderAddressError("sender_address_owner_unlinked");
|
|
84
|
+
throw error;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const withShared = async (book) => senders.sharedWith ? {
|
|
88
|
+
...book,
|
|
89
|
+
shared: usableShared(await senders.sharedWith(userId), providers)
|
|
90
|
+
} : book;
|
|
71
91
|
return {
|
|
72
92
|
send: async ({ sender_address_id, ...message }) => {
|
|
73
93
|
refuseOwnFrom(message);
|
|
@@ -75,6 +95,7 @@ const personalMailer = (accounts, providers, senders, userId, now = () => /* @__
|
|
|
75
95
|
if (sender_address_id === null) return sendAsMailbox(book, message);
|
|
76
96
|
const row = sender_address_id === void 0 ? book.rows.find((candidate) => candidate.isDefault && !isRemoved(candidate)) : book.rows.find((candidate) => candidate.id === sender_address_id);
|
|
77
97
|
if (!row && sender_address_id === void 0) return sendAsMailbox(book, message);
|
|
98
|
+
if (!row && sender_address_id !== void 0) return sendShared(sender_address_id, message);
|
|
78
99
|
if (!row || isRemoved(row)) throw new SenderAddressNotFoundError();
|
|
79
100
|
const connection = book.connections.find(({ account }) => account.id === row.connectionId);
|
|
80
101
|
if (!connection) throw new SenderAddressError("sender_address_detached");
|
|
@@ -83,8 +104,8 @@ const personalMailer = (accounts, providers, senders, userId, now = () => /* @__
|
|
|
83
104
|
if (state === "pending") throw new SenderAddressError("sender_address_pending");
|
|
84
105
|
return sendThrough(connection, message, row);
|
|
85
106
|
},
|
|
86
|
-
sender: async (address) => chooseSender(await bookOf(), address),
|
|
87
|
-
senders: async () => usableSenders(await bookOf()),
|
|
107
|
+
sender: async (address) => chooseSender(await withShared(await bookOf()), address),
|
|
108
|
+
senders: async () => usableSenders(await withShared(await bookOf())),
|
|
88
109
|
refresh: async () => listConnections(lister, (await bookOf()).connections)
|
|
89
110
|
};
|
|
90
111
|
};
|
|
@@ -3,17 +3,18 @@ import { HttpProblem } from "../problem.js";
|
|
|
3
3
|
/**
|
|
4
4
|
* Why a message names a From address it cannot go out from, sending nothing: `failed`, the mailbox's provider says it
|
|
5
5
|
* cannot send as it; `pending`, its owner has not confirmed it with the provider yet; `detached`, it is on no mailbox
|
|
6
|
-
* connection its owner can send through; `ambiguous`, an address or the mailbox's own stands for more than one of them
|
|
6
|
+
* connection its owner can send through; `ambiguous`, an address or the mailbox's own stands for more than one of them;
|
|
7
|
+
* `owner_unlinked`, an address someone else shared goes out through a mailbox of theirs that they must connect again.
|
|
7
8
|
*/
|
|
8
|
-
export type SenderAddressErrorCode = 'sender_address_failed' | 'sender_address_pending' | 'sender_address_detached' | 'sender_address_ambiguous';
|
|
9
|
+
export type SenderAddressErrorCode = 'sender_address_failed' | 'sender_address_pending' | 'sender_address_detached' | 'sender_address_ambiguous' | 'sender_address_owner_unlinked';
|
|
9
10
|
/** Thrown when a send names a From address it cannot go out from; it answers as a 409 problem whose `code` says why. */
|
|
10
11
|
export declare class SenderAddressError extends HttpProblem {
|
|
11
12
|
readonly code: SenderAddressErrorCode;
|
|
12
13
|
constructor(code: SenderAddressErrorCode);
|
|
13
14
|
}
|
|
14
15
|
/**
|
|
15
|
-
* Thrown when a send names a From address id that is not one of the sender's: unknown, removed or someone else's
|
|
16
|
-
* the same words, so the answer does not tell which. It answers as a 404 problem.
|
|
16
|
+
* Thrown when a send names a From address id that is not one of the sender's: unknown, removed, or someone else's and
|
|
17
|
+
* not shared with them, or no longer verified, in the same words, so the answer does not tell which. It answers as a 404 problem.
|
|
17
18
|
*/
|
|
18
19
|
export declare class SenderAddressNotFoundError extends HttpProblem {
|
|
19
20
|
constructor();
|
|
@@ -4,7 +4,8 @@ const details = {
|
|
|
4
4
|
sender_address_failed: "Your mailbox cannot send as this address: set it up with your mail provider, or choose another",
|
|
5
5
|
sender_address_pending: "This address still waits for you to confirm it with your mail provider",
|
|
6
6
|
sender_address_detached: "This address is on no mailbox you have connected: add it again under Account → Mailbox",
|
|
7
|
-
sender_address_ambiguous: "This address is on more than one of your mailboxes: choose the From address by its id"
|
|
7
|
+
sender_address_ambiguous: "This address is on more than one of your mailboxes: choose the From address by its id",
|
|
8
|
+
sender_address_owner_unlinked: "This address belongs to someone else, who must connect their mailbox again before mail goes out from it"
|
|
8
9
|
};
|
|
9
10
|
/** Thrown when a send names a From address it cannot go out from; it answers as a 409 problem whose `code` says why. */
|
|
10
11
|
var SenderAddressError = class extends HttpProblem {
|
|
@@ -15,8 +16,8 @@ var SenderAddressError = class extends HttpProblem {
|
|
|
15
16
|
}
|
|
16
17
|
};
|
|
17
18
|
/**
|
|
18
|
-
* Thrown when a send names a From address id that is not one of the sender's: unknown, removed or someone else's
|
|
19
|
-
* the same words, so the answer does not tell which. It answers as a 404 problem.
|
|
19
|
+
* Thrown when a send names a From address id that is not one of the sender's: unknown, removed, or someone else's and
|
|
20
|
+
* not shared with them, or no longer verified, in the same words, so the answer does not tell which. It answers as a 404 problem.
|
|
20
21
|
*/
|
|
21
22
|
var SenderAddressNotFoundError = class extends HttpProblem {
|
|
22
23
|
constructor() {
|
|
@@ -14,7 +14,7 @@ const columns = (store) => {
|
|
|
14
14
|
sql`${c.connection_id} as "connectionId"`,
|
|
15
15
|
sql`${c.address} as address`,
|
|
16
16
|
sql`${c.name} as name`,
|
|
17
|
-
sql`${c.
|
|
17
|
+
sql`${c.shared_at} as "sharedAt"`,
|
|
18
18
|
sql`${c.signature} as signature`,
|
|
19
19
|
sql`${c.is_default} as "isDefault"`,
|
|
20
20
|
sql`${c.created_at} as "createdAt"`,
|
|
@@ -41,8 +41,8 @@ const sqlSenderStore = (store) => ({
|
|
|
41
41
|
type: async (sender, at) => {
|
|
42
42
|
const s = await store();
|
|
43
43
|
const { table, c, selected } = columns(s);
|
|
44
|
-
const { rows } = await sql`insert into ${table} (id, ${c.user_id}, ${c.connection_id}, ${c.address}, ${c.name}, ${c.
|
|
45
|
-
values (${s.newId}, ${sender.userId}, ${sender.connectionId}, ${sender.address}, ${sender.name},
|
|
44
|
+
const { rows } = await sql`insert into ${table} (id, ${c.user_id}, ${c.connection_id}, ${c.address}, ${c.name}, ${c.signature}, ${c.is_default}, ${c.created_at}, ${c.typed_at})
|
|
45
|
+
values (${s.newId}, ${sender.userId}, ${sender.connectionId}, ${sender.address}, ${sender.name}, ${sender.signature}, false, ${at}, ${at})
|
|
46
46
|
on conflict (${c.connection_id}, ${c.address_key}) do update set ${c.typed_at} = greatest(excluded.${c.typed_at}, ${table}.${c.removed_at} + interval '1 millisecond'), ${c.name} = excluded.${c.name}, ${c.signature} = excluded.${c.signature}
|
|
47
47
|
returning ${selected}`.execute(s.db);
|
|
48
48
|
return rows[0];
|
|
@@ -54,8 +54,8 @@ returning ${selected}`.execute(s.db);
|
|
|
54
54
|
const states = listed.map(({ provider_state }) => provider_state ?? null);
|
|
55
55
|
await sql`with listed (address, state) as (select * from unnest(${addresses}::text[], ${states}::text[])),
|
|
56
56
|
upserted as (
|
|
57
|
-
insert into ${table} (id, ${c.user_id}, ${c.connection_id}, ${c.address}, ${c.
|
|
58
|
-
select ${s.newId}, ${userId}, ${connectionId}, listed.address, false,
|
|
57
|
+
insert into ${table} (id, ${c.user_id}, ${c.connection_id}, ${c.address}, ${c.is_default}, ${c.created_at}, ${c.listed_at}, ${c.provider_state}, ${c.provider_state_read_at})
|
|
58
|
+
select ${s.newId}, ${userId}, ${connectionId}, listed.address, false, ${readAt}, ${readAt}, listed.state, ${readAt} from listed
|
|
59
59
|
on conflict (${c.connection_id}, ${c.address_key}) do update set ${c.listed_at} = excluded.${c.listed_at}, ${c.provider_state} = excluded.${c.provider_state}, ${c.provider_state_read_at} = excluded.${c.provider_state_read_at}
|
|
60
60
|
where ${table}.${c.provider_state_read_at} is null or ${table}.${c.provider_state_read_at} <= excluded.${c.provider_state_read_at}
|
|
61
61
|
returning id
|
|
@@ -84,7 +84,14 @@ where ${c.connection_id} = ${connectionId} and ${c.user_id} = ${userId} and ${c.
|
|
|
84
84
|
remove: async (userId, id, at) => {
|
|
85
85
|
const s = await store();
|
|
86
86
|
const { table, c } = columns(s);
|
|
87
|
-
await sql`update ${table} set ${c.removed_at} = ${at}, ${c.is_default} = false where id = ${id} and ${c.user_id} = ${userId}`.execute(s.db);
|
|
87
|
+
await sql`update ${table} set ${c.removed_at} = ${at}, ${c.is_default} = false, ${c.shared_at} = null where id = ${id} and ${c.user_id} = ${userId}`.execute(s.db);
|
|
88
|
+
},
|
|
89
|
+
share: async (userId, id, at) => {
|
|
90
|
+
const s = await store();
|
|
91
|
+
const { table, c, selected } = columns(s);
|
|
92
|
+
const sharedAt = at === null ? sql`null` : sql`coalesce(${c.shared_at}, ${at})`;
|
|
93
|
+
const { rows } = await sql`update ${table} set ${c.shared_at} = ${sharedAt} where id = ${id} and ${c.user_id} = ${userId} returning ${selected}`.execute(s.db);
|
|
94
|
+
return rows[0];
|
|
88
95
|
}
|
|
89
96
|
});
|
|
90
97
|
//#endregion
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { sql } from "kysely";
|
|
2
|
+
//#region src/personal-mail/shared-sender-store.ts
|
|
3
|
+
/**
|
|
4
|
+
* The shared addresses in SQL, on the auth store: one query, which the shared addresses' partial index on their owner
|
|
5
|
+
* serves, joined with the owner's account and, with `organizations`, filtered to owners in an organization of the
|
|
6
|
+
* person's. Its rows carry no state: `sharedState` derives it, as for the owner's own addresses.
|
|
7
|
+
*/
|
|
8
|
+
const sharedSenderReader = (store, { organizations, verifiers }) => async (userId, id) => {
|
|
9
|
+
const s = await store();
|
|
10
|
+
const sender = s.table("mail_sender");
|
|
11
|
+
const c = sender.columns;
|
|
12
|
+
const account = s.table("account");
|
|
13
|
+
const a = account.columns;
|
|
14
|
+
const verification = s.table("mail_sender_verification");
|
|
15
|
+
const v = verification.columns;
|
|
16
|
+
const member = organizations ? s.table("member") : void 0;
|
|
17
|
+
const proved = verifiers.length === 0 ? sql`null::timestamptz` : sql`(select max(${v.settled_at}) from ${verification} where ${v.sender_id} = ${sender}.id and ${v.verifier} = any(${[...verifiers]}::text[]) and ${v.state} = 'verified' and ${v.valid_until} > now())`;
|
|
18
|
+
const inReach = member ? sql`and ${sender}.${c.user_id} in (select theirs.${member.columns.user_id} from ${member} as mine join ${member} as theirs on theirs.${member.columns.organization_id} = mine.${member.columns.organization_id} where mine.${member.columns.user_id} = ${userId})` : sql``;
|
|
19
|
+
const named = id === void 0 ? sql`` : sql`and ${sender}.id = ${id}`;
|
|
20
|
+
const { rows } = await sql`select ${sender}.id, ${sender}.${c.user_id} as "userId", ${sender}.${c.connection_id} as "connectionId", ${sender}.${c.address} as address,
|
|
21
|
+
${sender}.${c.name} as name, ${sender}.${c.shared_at} as "sharedAt", ${sender}.${c.signature} as signature, false as "isDefault", ${sender}.${c.created_at} as "createdAt",
|
|
22
|
+
${sender}.${c.typed_at} as "typedAt", ${sender}.${c.listed_at} as "listedAt", ${sender}.${c.provider_state} as "providerState",
|
|
23
|
+
${sender}.${c.provider_state_read_at} as "providerStateReadAt", ${sender}.${c.removed_at} as "removedAt",
|
|
24
|
+
case when ${account}.id is null then null else json_build_object('id', ${account}.id, 'providerId', ${account}.${a.provider_id}, 'scope', ${account}.${a.scope}, 'createdAt', ${account}.${a.created_at}) end as account,
|
|
25
|
+
${proved} as "provedAt"
|
|
26
|
+
from ${sender}
|
|
27
|
+
left join ${account} on ${account}.id = ${sender}.${c.connection_id} and ${account}.${a.user_id} = ${sender}.${c.user_id}
|
|
28
|
+
where ${sender}.${c.shared_at} is not null and ${sender}.${c.user_id} <> ${userId} ${named} ${inReach}
|
|
29
|
+
order by ${sender}.${c.created_at}, ${sender}.${c.address_key}, ${sender}.id`.execute(s.db);
|
|
30
|
+
return rows.map((row) => ({
|
|
31
|
+
...row,
|
|
32
|
+
account: row.account && {
|
|
33
|
+
...row.account,
|
|
34
|
+
createdAt: new Date(row.account.createdAt)
|
|
35
|
+
}
|
|
36
|
+
}));
|
|
37
|
+
};
|
|
38
|
+
//#endregion
|
|
39
|
+
export { sharedSenderReader };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import "./sender-address-state.js";
|
|
2
|
+
import "./mailbox-connections.js";
|
|
3
|
+
import "./shared-sender-store.js";
|
|
4
|
+
//#region src/personal-mail/shared-senders.d.ts
|
|
5
|
+
/** An address someone else shared, as the person sees it: never its owner, nor the mailbox it goes out through. */
|
|
6
|
+
export type SharedSender = {
|
|
7
|
+
id: string;
|
|
8
|
+
address: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
};
|
|
11
|
+
//#endregion
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { storedScopes } from "./linked-accounts.js";
|
|
2
|
+
import { senderRowState } from "./sender-address-state.js";
|
|
3
|
+
import { mailboxConnections } from "./mailbox-connections.js";
|
|
4
|
+
//#region src/personal-mail/shared-senders.ts
|
|
5
|
+
/** The connection a shared address goes out through: its owner's account, while that still holds a provider's send scopes. */
|
|
6
|
+
const sharedConnection = ({ account }, providers) => account ? mailboxConnections([{
|
|
7
|
+
id: account.id,
|
|
8
|
+
providerId: account.providerId,
|
|
9
|
+
scopes: storedScopes(account.scope),
|
|
10
|
+
createdAt: account.createdAt
|
|
11
|
+
}], providers)[0] : void 0;
|
|
12
|
+
/** The state of a shared address, derived as for its owner (`senderRowState`): only a `verified` one goes out. */
|
|
13
|
+
const sharedState = (row, providers) => senderRowState(row, sharedConnection(row, providers) !== void 0, row.provedAt ?? void 0);
|
|
14
|
+
/** The shared addresses the person may send from: those still verified. */
|
|
15
|
+
const usableShared = (rows, providers) => rows.filter((row) => sharedState(row, providers) === "verified");
|
|
16
|
+
const listedShared = ({ id, address, name }) => ({
|
|
17
|
+
id,
|
|
18
|
+
address,
|
|
19
|
+
...name && { name }
|
|
20
|
+
});
|
|
21
|
+
//#endregion
|
|
22
|
+
export { listedShared, sharedConnection, sharedState, usableShared };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/server",
|
|
3
|
-
"version": "0.126.
|
|
3
|
+
"version": "0.126.3",
|
|
4
4
|
"description": "The Protobase REST API as a Hono app, with Better Auth sign-in.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -44,11 +44,11 @@
|
|
|
44
44
|
"@better-auth/sso": "^1.7.12",
|
|
45
45
|
"@livekit/protocol": "1.53.1",
|
|
46
46
|
"@modelcontextprotocol/server": "^2.3.1",
|
|
47
|
-
"@protobase/layout": "0.126.
|
|
48
|
-
"@protobase/query": "0.126.
|
|
49
|
-
"@protobase/schema": "0.126.
|
|
50
|
-
"@protobase/workflow-plugins": "0.126.
|
|
51
|
-
"@protobase/workflows": "0.126.
|
|
47
|
+
"@protobase/layout": "0.126.3",
|
|
48
|
+
"@protobase/query": "0.126.3",
|
|
49
|
+
"@protobase/schema": "0.126.3",
|
|
50
|
+
"@protobase/workflow-plugins": "0.126.3",
|
|
51
|
+
"@protobase/workflows": "0.126.3",
|
|
52
52
|
"@react-email/render": "^2.1.0",
|
|
53
53
|
"@react-pdf/primitives": "^4.4.0",
|
|
54
54
|
"@react-pdf/renderer": "^4.9.0",
|