mirror of
https://github.com/juherr/kill-the-news.git
synced 2026-06-21 06:13:48 +00:00
feat(websub): wire real-time push notifications on email ingest
Pass ExecutionContext through the email processing chain so notifySubscribers is called via ctx.waitUntil after a new email is stored. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,7 +5,7 @@ import { processEmail, RawAttachment } from "./email-processor";
|
|||||||
export async function handleCloudflareEmail(
|
export async function handleCloudflareEmail(
|
||||||
message: ForwardableEmailMessage,
|
message: ForwardableEmailMessage,
|
||||||
env: Env,
|
env: Env,
|
||||||
_ctx: ExecutionContext,
|
ctx: ExecutionContext,
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const email = await PostalMime.parse(message.raw);
|
const email = await PostalMime.parse(message.raw);
|
||||||
@@ -41,6 +41,7 @@ export async function handleCloudflareEmail(
|
|||||||
attachments: rawAttachments,
|
attachments: rawAttachments,
|
||||||
},
|
},
|
||||||
env,
|
env,
|
||||||
|
ctx,
|
||||||
);
|
);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error processing Cloudflare email:", error);
|
console.error("Error processing Cloudflare email:", error);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
FeedConfig,
|
FeedConfig,
|
||||||
FeedMetadata,
|
FeedMetadata,
|
||||||
} from "../types";
|
} from "../types";
|
||||||
|
import { notifySubscribers } from "../utils/websub";
|
||||||
|
|
||||||
export interface RawAttachment {
|
export interface RawAttachment {
|
||||||
filename: string;
|
filename: string;
|
||||||
@@ -73,6 +74,7 @@ async function uploadAttachments(
|
|||||||
export async function processEmail(
|
export async function processEmail(
|
||||||
input: ProcessEmailInput,
|
input: ProcessEmailInput,
|
||||||
env: Env,
|
env: Env,
|
||||||
|
ctx?: ExecutionContext,
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
const feedId = EmailParser.extractFeedId(input.toAddress);
|
const feedId = EmailParser.extractFeedId(input.toAddress);
|
||||||
if (!feedId) {
|
if (!feedId) {
|
||||||
@@ -182,5 +184,8 @@ export async function processEmail(
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
console.log(`Successfully processed email for feed ${feedId}`);
|
console.log(`Successfully processed email for feed ${feedId}`);
|
||||||
|
if (ctx) {
|
||||||
|
ctx.waitUntil(notifySubscribers(feedId, env));
|
||||||
|
}
|
||||||
return new Response("Email processed successfully", { status: 200 });
|
return new Response("Email processed successfully", { status: 200 });
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -68,6 +68,7 @@ function toArrayBuffer(
|
|||||||
export async function handleForwardEmail(
|
export async function handleForwardEmail(
|
||||||
payload: ForwardEmailPayload,
|
payload: ForwardEmailPayload,
|
||||||
env: Env,
|
env: Env,
|
||||||
|
ctx?: ExecutionContext,
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
const emailData = EmailParser.parseForwardEmailPayload(payload);
|
const emailData = EmailParser.parseForwardEmailPayload(payload);
|
||||||
|
|
||||||
@@ -95,5 +96,6 @@ export async function handleForwardEmail(
|
|||||||
attachments: rawAttachments,
|
attachments: rawAttachments,
|
||||||
},
|
},
|
||||||
env,
|
env,
|
||||||
|
ctx,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export async function handle(c: Context): Promise<Response> {
|
|||||||
contentType: payload.html ? "HTML" : "Text",
|
contentType: payload.html ? "HTML" : "Text",
|
||||||
});
|
});
|
||||||
|
|
||||||
return handleForwardEmail(payload, env);
|
return handleForwardEmail(payload, env, c.executionCtx);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Error processing email:", error);
|
console.error("Error processing email:", error);
|
||||||
return new Response("Error processing email", { status: 500 });
|
return new Response("Error processing email", { status: 500 });
|
||||||
|
|||||||
Reference in New Issue
Block a user