|
|
|
@@ -12,7 +12,8 @@ import {
|
|
|
|
|
* wrote it in the App, so both show and send alike.
|
|
|
|
|
*
|
|
|
|
|
* Cross Code's reach an App set to "From Cross Code" from the Hub: `GET /api/v2/devotionals`
|
|
|
|
|
* (`hub.DEVOTIONALS_PATH`), once a night, the next 3 months (`devotionalsReplySchema`).
|
|
|
|
|
* (`hub.DEVOTIONALS_PATH`), once a night, the next 2 weeks at most (1.24.0, `CROSSCODE_DEVOTIONALS_FETCH_DAYS`;
|
|
|
|
|
* `devotionalsReplySchema`).
|
|
|
|
|
*
|
|
|
|
|
* Both write them by hand or import a CSV (`DEVOTIONAL_CSV_COLUMNS`, `checkDevotionalCsv`), with
|
|
|
|
|
* the same checks in the Hub and the App.
|
|
|
|
@@ -25,9 +26,21 @@ export const DEVOTIONAL_SOURCES = ["crosscode", "church"] as const;
|
|
|
|
|
export const devotionalSourceSchema = z.enum(DEVOTIONAL_SOURCES);
|
|
|
|
|
export type DevotionalSource = z.infer<typeof devotionalSourceSchema>;
|
|
|
|
|
|
|
|
|
|
/** How far ahead the Hub schedules Cross Code's devotionals, and how far ahead an App fetches them. */
|
|
|
|
|
/**
|
|
|
|
|
* 1.24.0: the most days one `GET /api/v2/devotionals` answer covers, `from` included (two weeks):
|
|
|
|
|
* an App fetches them each night, so it always has the days ahead even if the Hub is down for a while.
|
|
|
|
|
*/
|
|
|
|
|
export const CROSSCODE_DEVOTIONALS_FETCH_DAYS = 14;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @deprecated 1.24.0: the Hub schedules `CROSSCODE_DEVOTIONALS_SCHEDULE_MONTHS` ahead and an App
|
|
|
|
|
* fetches `CROSSCODE_DEVOTIONALS_FETCH_DAYS`. Kept so nothing that read it breaks.
|
|
|
|
|
*/
|
|
|
|
|
export const CROSSCODE_DEVOTIONALS_AHEAD_MONTHS = 3;
|
|
|
|
|
|
|
|
|
|
/** 1.24.0: how far ahead the Hub schedules Cross Code's devotionals (two years; was 3 months). */
|
|
|
|
|
export const CROSSCODE_DEVOTIONALS_SCHEDULE_MONTHS = 24;
|
|
|
|
|
|
|
|
|
|
export const DEVOTIONAL_LIMITS = { title: 200, reference: 100, passageText: 10_000, body: 20_000, prayer: 5_000, author: 200 } as const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@@ -66,14 +79,15 @@ export type Devotional = z.infer<typeof devotionalSchema>;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The Hub's answer to `GET /api/v2/devotionals?from=YYYY-MM-DD`: Cross Code's devotionals from
|
|
|
|
|
* `from` to `to` (3 months on), one per day at most, oldest first. The App replaces its copy of
|
|
|
|
|
* `from` to `to` (1.24.0: at most `CROSSCODE_DEVOTIONALS_FETCH_DAYS` days, `from` included; 3 months
|
|
|
|
|
* before), one per day at most, oldest first. The App replaces its copy of
|
|
|
|
|
* that range with them (a day missing from the list was removed in the Hub).
|
|
|
|
|
*/
|
|
|
|
|
export const devotionalsReplySchema = z.object({
|
|
|
|
|
schema: z.literal(DEVOTIONAL_SCHEMA),
|
|
|
|
|
from: dateSchema,
|
|
|
|
|
to: dateSchema,
|
|
|
|
|
devotionals: z.array(devotionalSchema.extend({ source: z.literal("crosscode") })).max(100),
|
|
|
|
|
devotionals: z.array(devotionalSchema.extend({ source: z.literal("crosscode") })).max(100), // 14 from 1.24.0; 100 kept for older Hubs
|
|
|
|
|
generatedAt: dateTimeSchema,
|
|
|
|
|
});
|
|
|
|
|
export type DevotionalsReply = z.infer<typeof devotionalsReplySchema>;
|
|
|
|
@@ -114,9 +128,12 @@ export const DEVOTIONAL_CSV_COLUMNS = ["date", "title", "passage", "translation"
|
|
|
|
|
export type DevotionalCsvColumn = (typeof DEVOTIONAL_CSV_COLUMNS)[number];
|
|
|
|
|
const REQUIRED_COLUMNS: readonly DevotionalCsvColumn[] = ["date", "title", "passage", "translation", "passage_text", "body"];
|
|
|
|
|
|
|
|
|
|
/** Rows per import (a year and a bit), and the file size the Hub and App accept. */
|
|
|
|
|
export const DEVOTIONAL_CSV_MAX_ROWS = 400;
|
|
|
|
|
export const DEVOTIONAL_CSV_MAX_BYTES = 2 * 1024 * 1024;
|
|
|
|
|
/**
|
|
|
|
|
* Rows per import and the file size the Hub and App accept. 1.24.0: two years and a bit (800 rows,
|
|
|
|
|
* 8 MB; was 400 and 2 MB), so the Hub's two years ahead fit in one file.
|
|
|
|
|
*/
|
|
|
|
|
export const DEVOTIONAL_CSV_MAX_ROWS = 800;
|
|
|
|
|
export const DEVOTIONAL_CSV_MAX_BYTES = 8 * 1024 * 1024;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reads CSV (RFC 4180: quoted fields, `""` for a quote, line breaks inside quotes), with or
|
|
|
|
@@ -180,7 +197,7 @@ export interface DevotionalCsvResult {
|
|
|
|
|
export interface DevotionalCsvOptions {
|
|
|
|
|
/** Dates that already have a devotional. */
|
|
|
|
|
taken?: Iterable<string>;
|
|
|
|
|
/** The last date allowed (the Hub: 3 months from today). Later dates are errors. */
|
|
|
|
|
/** The last date allowed (the Hub: two years from today). Later dates are errors. */
|
|
|
|
|
latestDate?: string;
|
|
|
|
|
/** The translations allowed (the Hub and App 1.0: `FREE_BIBLE_TRANSLATION_IDS`). Any by default. */
|
|
|
|
|
translations?: readonly BibleTranslationId[];
|
|
|
|
@@ -192,7 +209,7 @@ export interface DevotionalCsvOptions {
|
|
|
|
|
* twice in the file, a date already filled). Never throws.
|
|
|
|
|
*/
|
|
|
|
|
export function checkDevotionalCsv(text: string, options: DevotionalCsvOptions = {}): DevotionalCsvResult {
|
|
|
|
|
if (text.length > DEVOTIONAL_CSV_MAX_BYTES) return { fileError: "The file is larger than 2 MB", rows: [] };
|
|
|
|
|
if (text.length > DEVOTIONAL_CSV_MAX_BYTES) return { fileError: `The file is larger than ${DEVOTIONAL_CSV_MAX_BYTES / 1024 / 1024} MB`, rows: [] };
|
|
|
|
|
const all = parseCsv(text);
|
|
|
|
|
const header = all[0];
|
|
|
|
|
if (!header) return { fileError: "The file is empty", rows: [] };
|
|
|
|
@@ -228,7 +245,7 @@ export function checkDevotionalCsv(text: string, options: DevotionalCsvOptions =
|
|
|
|
|
|
|
|
|
|
const date = required("date", "The date");
|
|
|
|
|
if (date && !dateSchema.safeParse(date).success) errors.push({ column: "date", message: `"${date}" isn't a date (use YYYY-MM-DD, e.g. 2027-01-05)` });
|
|
|
|
|
else if (date && options.latestDate && date > options.latestDate) errors.push({ column: "date", message: `${date} is more than ${CROSSCODE_DEVOTIONALS_AHEAD_MONTHS} months ahead (the last day is ${options.latestDate})` });
|
|
|
|
|
else if (date && options.latestDate && date > options.latestDate) errors.push({ column: "date", message: `${date} is too far ahead (the last day is ${options.latestDate})` });
|
|
|
|
|
else if (date && seen.has(date)) errors.push({ column: "date", message: `${date} is also on line ${seen.get(date)}` });
|
|
|
|
|
if (date && !seen.has(date)) seen.set(date, line);
|
|
|
|
|
|
|
|
|
|