129 lines · 5.1 KB
import { all, chunks, placeholders } from "./db";
import { accountDb, repoDb } from "./scoped-db";
import { forEachScope } from "./account-scope";
import type { NotifyMessage } from "./events/emit";

export const WATCH_LEVELS = ["all", "participating", "releases", "ignore"] as const;
export type WatchLevel = (typeof WATCH_LEVELS)[number];

export type NotificationSettings = {
  emailParticipating: boolean;
  emailWatching: boolean;
  webParticipating: boolean;
  webWatching: boolean;
};

export type Delivery = { userId: string; reason: string; web: boolean; email: string | null };

const DEFAULT_SETTINGS: NotificationSettings = {
  emailParticipating: true,
  emailWatching: false,
  webParticipating: true,
  webWatching: true,
};

type SettingsRow = {
  user_id: string;
  email_participating: number;
  email_watching: number;
  web_participating: number;
  web_watching: number;
};

type JoinedRow = { user_id: string; email: string; email_verified_at: number | null } & {
  [Key in Exclude<keyof SettingsRow, "user_id">]: number | null;
};

function joinedSettings(row: JoinedRow): NotificationSettings {
  const { email_participating, email_watching, web_participating, web_watching } = row;
  if (email_participating === null || email_watching === null || web_participating === null || web_watching === null) {
    return DEFAULT_SETTINGS;