Skip to content

Open-source status page, incident response and uptime monitoring

14.4 KB
Raw Blame
/** @jsxRuntime automatic @jsxImportSource react */

import { statusLabel } from "@openstatus/utils";
import { type Duration, Effect, Schedule } from "effect";
import { render } from "react-email";
import { Resend } from "resend";

import DashboardMagicLinkEmail from "../emails/dashboard-magic-link";
import type { DashboardMagicLinkProps } from "../emails/dashboard-magic-link";
import FollowUpEmail from "../emails/followup";
import MonitorAlertEmail, {
  monitorAlertSubject,
} from "../emails/monitor-alert";
import type { MonitorAlertProps } from "../emails/monitor-alert";
import PageSubscriptionEmail from "../emails/page-subscription";
import type { PageSubscriptionProps } from "../emails/page-subscription";
import PageUpdateEmail from "../emails/page-update";
import type { PageUpdateProps } from "../emails/page-update";
import PrivateLocationAlertEmail, {
  privateLocationAlertSubject,
} from "../emails/private-location-alert";
import type { PrivateLocationAlertProps } from "../emails/private-location-alert";
import SlackFeedbackEmail from "../emails/slack-feedback";
import StatusPageMagicLinkEmail from "../emails/status-page-magic-link";
import type { StatusPageMagicLinkProps } from "../emails/status-page-magic-link";
import TeamInvitationEmail from "../emails/team-invitation";
import type { TeamInvitationProps } from "../emails/team-invitation";
import { env } from "./env";
import { statusPageFrom } from "./sender";

export function pageUpdateSubject(req: {
  status: PageUpdateProps["status"];
  reportTitle: string;
}): string {
  if (req.status === "resolved") return `RESOLVED: ${req.reportTitle}`;
  if (req.status === "maintenance")
    return `${statusLabel("maintenance")}: ${req.reportTitle}`;
  return req.reportTitle;
}

const SYSTEM_FROM = "openstatus <[email protected]>";
const ACCOUNT_FROM = "openstatus <[email protected]>";
const SUPPORT_EMAIL = "[email protected]";

// Deterministic Resend rejections: retrying the identical request can never
// succeed (e.g. 409 invalid_idempotent_request when a key is reused with a
// different body), it only burns the backoff and re-logs the error.
const NON_RETRYABLE_RESEND_ERRORS = new Set<string>([
  "invalid_idempotent_request",
  "invalid_idempotency_key",
  "validation_error",
]);

function isRetryableSendError(error: { name: string }): boolean {
  return !NON_RETRYABLE_RESEND_ERRORS.has(error.name);
}

// split an array into chunks of a given size.
function chunk<T>(array: T[], size: number): T[][] {
  const result: T[][] = [];
  for (let i = 0; i < array.length; i += size) {
    result.push(array.slice(i, i + size));
  }
  return result;
}

export class EmailClient {
  public readonly client: Resend;
  // Base delay for the per-batch send retry. Overridable so tests can run the
  // retry path without the real ~1s exponential sleep.
  private readonly retryBackoff: Duration.Input;

  constructor(opts: { apiKey: string; retryBackoff?: Duration.Input }) {
    this.client = new Resend(opts.apiKey);
    this.retryBackoff = opts.retryBackoff ?? "1000 millis";
  }

  public async sendFollowUp(req: { to: string }) {
    if (env.NODE_ENV === "development") {
      console.log(`Sending follow up email to ${req.to}`);
      return;
    }

    try {
      const html = await render(<FollowUpEmail />);
      const result = await this.client.emails.send({
        from: "Thibault Le Ouay Ducasse <[email protected]>",
        replyTo: "Thibault Le Ouay Ducasse <[email protected]>",
        subject: "How's it going with openstatus?",
        to: req.to,
        html,
      });

      if (!result.error) {
        console.log(`Sent follow up email to ${req.to}`);
        return;
      }

      throw result.error;
    } catch (err) {
      console.error(`Error sending follow up email to ${req.to}: ${err}`);
    }
  }

  public async sendFollowUpBatched(req: { to: string[] }) {
    if (env.NODE_ENV === "development") {
      console.log(`Sending follow up emails to ${req.to.join(", ")}`);
      return;
    }

    const html = await render(<FollowUpEmail />);
    const result = await this.client.batch.send(
      req.to.map((subscriber) => ({
        from: "Thibault Le Ouay Ducasse <[email protected]>",
        subject: "How's it going with openstatus?",
        to: subscriber,
        html,
      })),
    );

    if (result.error) {
      //  We only throw the error if we are rate limited
      if (result.error?.name === "rate_limit_exceeded") {
        throw result.error;
      }
      //  Otherwise let's log the error and continue
      console.error(
        `Error sending follow up email to ${req.to}: ${result.error}`,
      );
      return;
    }

    console.log(`Sent follow up emails to ${req.to}`);
  }

  public async sendSlackFeedback(req: { to: string }) {
    if (env.NODE_ENV === "development") {
      console.log(`Sending slack feedback email to ${req.to}`);
      return;
    }

    try {
      const html = await render(<SlackFeedbackEmail />);
      const result = await this.client.emails.send({
        from: "Thibault Le Ouay Ducasse <[email protected]>",
        replyTo: "Thibault Le Ouay Ducasse <[email protected]>",
        subject: "How's the Slack app working for you?",
        to: req.to,
        html,
      });

      if (!result.error) {
        console.log(`Sent slack feedback email to ${req.to}`);
        return;
      }

      throw result.error;
    } catch (err) {
      console.error(`Error sending slack feedback email to ${req.to}: ${err}`);
    }
  }

  public async sendSlackFeedbackBatched(req: { to: string[] }) {
    if (env.NODE_ENV === "development") {
      console.log(`Sending slack feedback emails to ${req.to.join(", ")}`);
      return;
    }

    const html = await render(<SlackFeedbackEmail />);
    const result = await this.client.batch.send(
      req.to.map((subscriber) => ({
        from: "Thibault Le Ouay Ducasse <[email protected]>",
        subject: "How's the Slack app working for you?",
        to: subscriber,
        html,
      })),
    );

    if (result.error) {
      if (result.error?.name === "rate_limit_exceeded") {
        throw result.error;
      }
      console.error(
        `Error sending slack feedback email to ${req.to}: ${result.error}`,
      );
      return;
    }

    console.log(`Sent slack feedback emails to ${req.to}`);
  }

  public async sendPageUpdate(
    req: Omit<
      PageUpdateProps,
      "unsubscribeUrl" | "manageUrl" | "statusPageUrl"
    > & {
      subscribers: Array<{ email: string; token: string }>;
      pageSlug: string;
      customDomain?: string | null;
      // Base key for Resend idempotency. The per-batch retry below would
      // otherwise re-send the whole chunk if a request succeeds server-side
      // but the response is lost. Must be stable across retries.
      idempotencyKey?: string;
    },
  ) {
    const statusPageBaseUrl = req.customDomain
      ? `https://${req.customDomain}`
      : `https://${req.pageSlug}.openstatus.dev`;

    if (env.NODE_ENV === "development") {
      console.log(
        `Sending page update emails to ${req.subscribers
          .map((s) => s.email)
          .join(", ")}`,
      );
      return;
    }

    const chunks = chunk(req.subscribers, 100);
    for (let i = 0; i < chunks.length; i++) {
      const recipients = chunks[i];
      // suffix the chunk index so a multi-batch send doesn't collide its
      // own chunks on a single shared key
      const batchKey = req.idempotencyKey
        ? `${req.idempotencyKey}:${i}`
        : undefined;
      const sendEmail = Effect.tryPromise({
        try: () =>
          this.client.batch.send(
            recipients.map((subscriber) => {
              const unsubscribeUrl = `${statusPageBaseUrl}/unsubscribe/${subscriber.token}`;
              const manageUrl = `${statusPageBaseUrl}/manage/${subscriber.token}`;
              return {
                from: statusPageFrom(req.pageTitle),
                subject: pageUpdateSubject(req),
                to: subscriber.email,
                react: (
                  <PageUpdateEmail
                    {...req}
                    statusPageUrl={statusPageBaseUrl}
                    unsubscribeUrl={unsubscribeUrl}
                    manageUrl={manageUrl}
                  />
                ),
              };
            }),
            batchKey ? { idempotencyKey: batchKey } : undefined,
          ),
        catch: (_unknown) =>
          new Error(
            `Error sending page update batch to ${recipients.map(
              (r) => r.email,
            )}`,
          ),
      }).pipe(
        Effect.andThen((result) =>
          result.error ? Effect.fail(result.error) : Effect.succeed(result),
        ),
        Effect.retry({
          times: 3,
          schedule: Schedule.exponential(this.retryBackoff),
          while: isRetryableSendError,
        }),
      );
      await Effect.runPromise(sendEmail).catch(console.error);
    }

    console.log(
      `Sent page update email to ${req.subscribers.length} subscribers`,
    );
  }

  public async sendTeamInvitation(req: TeamInvitationProps & { to: string }) {
    if (env.NODE_ENV === "development") {
      const inviteUrl = `${req.baseUrl ?? "http://localhost:3000/invite"}?token=${req.token}`;
      console.log(`Sending team invitation email to ${req.to}`);
      console.log(`>>> Team Invitation Link: ${inviteUrl}`);
      return;
    }

    try {
      const html = await render(<TeamInvitationEmail {...req} />);
      const result = await this.client.emails.send({
        from: ACCOUNT_FROM,
        replyTo: SUPPORT_EMAIL,
        subject: req.workspaceName
          ? `${req.invitedBy} invited you to join ${req.workspaceName} on openstatus`
          : `${req.invitedBy} invited you to join openstatus`,
        to: req.to,
        html,
      });

      if (!result.error) {
        console.log(`Sent team invitation email to ${req.to}`);
        return;
      }

      throw result.error;
    } catch (err) {
      console.error(`Error sending team invitation email to ${req.to}`, err);
    }
  }

  public async sendMonitorAlert(req: MonitorAlertProps & { to: string }) {
    if (env.NODE_ENV === "development") {
      console.log(`Sending monitor alert email to ${req.to}`);
      return;
    }

    try {
      const { to: _to, ...props } = req;
      const html = await render(<MonitorAlertEmail {...props} />);
      const result = await this.client.emails.send({
        from: SYSTEM_FROM,
        replyTo: SUPPORT_EMAIL,
        subject: monitorAlertSubject(props),
        to: req.to,
        html,
      });

      if (!result.error) {
        console.log(`Sent monitor alert email to ${req.to}`);
        return;
      }

      throw result.error;
    } catch (err) {
      console.error(`Error sending monitor alert to ${req.to}`, err);
      throw err;
    }
  }

  public async sendPageSubscription(
    req: PageSubscriptionProps & { to: string },
  ) {
    if (env.NODE_ENV === "development") {
      console.log(`Sending page subscription email to ${req.to}`);
      return;
    }

    try {
      const html = await render(<PageSubscriptionEmail {...req} />);
      const result = await this.client.emails.send({
        from: statusPageFrom(req.page),
        subject: `Confirm your subscription to ${req.page}`,
        to: req.to,
        html,
      });

      if (!result.error) {
        console.log(`Sent page subscription email to ${req.to}`);
        return;
      }

      throw result.error;
    } catch (err) {
      console.error(`Error sending page subscription to ${req.to}`, err);
    }
  }

  public async sendStatusPageMagicLink(
    req: StatusPageMagicLinkProps & { to: string },
  ) {
    if (env.NODE_ENV === "development") {
      console.log(`Sending status page magic link email to ${req.to}`);
      console.log(`>>> Magic Link: ${req.link}`);
      return;
    }

    try {
      const html = await render(<StatusPageMagicLinkEmail {...req} />);
      const result = await this.client.emails.send({
        from: statusPageFrom(req.page),
        subject: `Authenticate to ${req.page}`,
        to: req.to,
        html,
      });

      if (!result.error) {
        console.log(`Sent status page magic link email to ${req.to}`);
        return;
      }

      throw result.error;
    } catch (err) {
      console.error(`Error sending status page magic link to ${req.to}`, err);
    }
  }

  /** Throws on a Resend failure so the login form can say the email did not go out. */
  public async sendDashboardMagicLink(
    req: DashboardMagicLinkProps & { to: string },
  ) {
    if (env.NODE_ENV === "development") {
      console.log(`Sending dashboard magic link email to ${req.to}`);
      console.log(`>>> Magic Link: ${req.link}`);
      return;
    }

    const html = await render(<DashboardMagicLinkEmail link={req.link} />);
    const result = await this.client.emails.send({
      from: ACCOUNT_FROM,
      subject: "Sign in to openstatus",
      to: req.to,
      html,
    });

    if (result.error) {
      console.error(
        `Error sending dashboard magic link to ${req.to}`,
        result.error,
      );
      throw result.error;
    }
    console.log(`Sent dashboard magic link email to ${req.to}`);
  }

  public async sendPrivateLocationAlert(
    req: Omit<PrivateLocationAlertProps, "lastSeenAt"> & {
      to: string[];
      lastSeenAt: Date;
    },
  ) {
    if (req.to.length === 0) return;

    const subject = privateLocationAlertSubject(req);

    if (env.NODE_ENV === "development") {
      console.log(
        `Sending private location ${req.status} email to ${req.to.join(", ")}`,
      );
      return;
    }

    try {
      const html = await render(
        <PrivateLocationAlertEmail
          locationName={req.locationName}
          status={req.status}
          lastSeenAt={req.lastSeenAt.toISOString()}
          monitorCount={req.monitorCount}
        />,
      );
      const result = await this.client.batch.send(
        req.to.map((to) => ({
          from: SYSTEM_FROM,
          replyTo: SUPPORT_EMAIL,
          subject,
          to,
          html,
        })),
      );

      if (result.error) {
        if (result.error?.name === "rate_limit_exceeded") {
          throw result.error;
        }
        console.error(
          `Error sending private location alert to ${req.to}: ${result.error}`,
        );
        return;
      }

      console.log(`Sent private location ${req.status} email to ${req.to}`);
    } catch (err) {
      console.error(
        `Error sending private location alert to ${req.to}: ${err}`,
      );
    }
  }
}