Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+50
-16
@@ -10,9 +10,14 @@
|
||||
// cron runs the per-folder delta sync.
|
||||
|
||||
import { type ActionFunctionArgs } from "react-router";
|
||||
import { findMailboxByWebhookSubscription, recordEmailWebhookEvent } from "~/modules/emailInbox/db/emailInbox.db.server";
|
||||
import { resolveMicrosoftMailboxRlsIdentity } from "@hivegrid/db/rls-resolvers.server";
|
||||
import { runWithRlsContext } from "@hivegrid/db/rls-context.server";
|
||||
import { getMicrosoftWebhookMailbox, recordEmailWebhookEvent } from "~/modules/emailInbox/db/emailInbox.db.server";
|
||||
|
||||
const ACCEPTED = new Response(null, { status: 202 });
|
||||
const MAX_GRAPH_NOTIFICATIONS_PER_REQUEST = 100;
|
||||
const MAX_GRAPH_SUBSCRIPTION_ID_LENGTH = 256;
|
||||
const MAX_GRAPH_CLIENT_STATE_LENGTH = 1_024;
|
||||
|
||||
interface GraphNotification {
|
||||
subscriptionId?: string;
|
||||
@@ -39,25 +44,54 @@ export const action = async ({ request }: ActionFunctionArgs) => {
|
||||
return ACCEPTED;
|
||||
}
|
||||
|
||||
for (const notification of body.value ?? []) {
|
||||
if (!notification.subscriptionId || !notification.clientState) {
|
||||
const notifications = body.value;
|
||||
if (!Array.isArray(notifications) || notifications.length > MAX_GRAPH_NOTIFICATIONS_PER_REQUEST) {
|
||||
return ACCEPTED;
|
||||
}
|
||||
|
||||
for (const notification of notifications) {
|
||||
if (
|
||||
!notification ||
|
||||
typeof notification !== "object" ||
|
||||
Array.isArray(notification) ||
|
||||
typeof notification.subscriptionId !== "string" ||
|
||||
notification.subscriptionId.length === 0 ||
|
||||
notification.subscriptionId.length > MAX_GRAPH_SUBSCRIPTION_ID_LENGTH ||
|
||||
typeof notification.clientState !== "string" ||
|
||||
notification.clientState.length === 0 ||
|
||||
notification.clientState.length > MAX_GRAPH_CLIENT_STATE_LENGTH
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
const mailbox = await findMailboxByWebhookSubscription(notification.subscriptionId);
|
||||
// clientState is the only auth: constant per mailbox, generated at subscribe.
|
||||
if (!mailbox || !mailbox.webhookClientState || mailbox.webhookClientState !== notification.clientState) {
|
||||
let identity: Awaited<ReturnType<typeof resolveMicrosoftMailboxRlsIdentity>>;
|
||||
try {
|
||||
identity = await resolveMicrosoftMailboxRlsIdentity(notification.subscriptionId, notification.clientState);
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
await recordEmailWebhookEvent({
|
||||
tenantId: mailbox.tenantId,
|
||||
mailboxId: mailbox.id,
|
||||
provider: "microsoft",
|
||||
notificationPayload: {
|
||||
subscriptionId: notification.subscriptionId,
|
||||
changeType: notification.changeType ?? null,
|
||||
resource: notification.resource ?? null,
|
||||
resourceId: notification.resourceData?.id ?? null,
|
||||
},
|
||||
if (!identity) {
|
||||
continue;
|
||||
}
|
||||
await runWithRlsContext({ tenantId: identity.tenantId, userId: identity.userId }, async () => {
|
||||
const mailbox = await getMicrosoftWebhookMailbox({
|
||||
tenantId: identity.tenantId,
|
||||
ownerUserId: identity.userId,
|
||||
mailboxId: identity.resourceId,
|
||||
subscriptionId: notification.subscriptionId!,
|
||||
clientState: notification.clientState!,
|
||||
});
|
||||
if (!mailbox) return;
|
||||
await recordEmailWebhookEvent({
|
||||
tenantId: mailbox.tenantId,
|
||||
mailboxId: mailbox.id,
|
||||
provider: "microsoft",
|
||||
notificationPayload: {
|
||||
subscriptionId: notification.subscriptionId,
|
||||
changeType: notification.changeType ?? null,
|
||||
resource: notification.resource ?? null,
|
||||
resourceId: notification.resourceData?.id ?? null,
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
return ACCEPTED;
|
||||
|
||||
Reference in new issue
Block a user