Skip to content

Commit

Permalink
Render iCalendar Yahrzeit reminder in Hebrew
Browse files Browse the repository at this point in the history
  • Loading branch information
mjradwin committed Jun 20, 2024
1 parent ab5b9c1 commit ebc8eb4
Showing 1 changed file with 40 additions and 29 deletions.
69 changes: 40 additions & 29 deletions src/yahrzeit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {getIpAddress} from './getIpAddress.js';
import {ulid} from 'ulid';
import {getMaxYahrzeitId, isNumKey, summarizeAnniversaryTypes,
getAnniversaryTypes,
YAHRZEIT, BIRTHDAY,
getCalendarNames, makeCalendarTitle,
getYahrzeitDetailsFromDb, getYahrzeitDetailForId} from './yahrzeitCommon.js';
import {makeLogInfo} from './logger.js';
Expand Down Expand Up @@ -519,29 +520,7 @@ async function makeYahrzeitEvents(maxId, query, reminder) {
const events0 = await getEventsForId(query, id, startYear, years);
events = events.concat(events0);
if (reminder) {
const reminders = events0
.filter((ev) => ev.type === 'Yahrzeit')
.map((ev) => {
const hd = ev.getDate().prev();
const dt = hd.greg();
const dow = hd.getDay();
const eventTimeStr = dow === 6 ? '20:00' : dow === 5 ? '14:30' : '16:30';
const uid = 'reminder-' + dayjs(dt).format('YYYYMMDD') + '-' + ev.hash + '-' + id;
return new Event(
hd,
`${ev.name} Yahrzeit reminder`,
flags.USER_EVENT,
{
eventTime: dt,
eventTimeStr,
memo: ev.memo,
emoji: ev.emoji,
alarm: 'P0DT0H0M0S',
uid,
category: 'Personal',
},
);
});
const reminders = makeReminderEvents(events0, id);
events = events.concat(reminders);
}
}
Expand All @@ -559,6 +538,36 @@ async function makeYahrzeitEvents(maxId, query, reminder) {
return events;
}

function getAlarmTime(hd) {
switch (hd.getDay()) {
case 6: return '20:00';
case 5: return '14:30';
default: return '16:30';
}
}

function makeReminderEvents(events, id) {
const yahrzeitEvents = events.filter((ev) => ev.type === YAHRZEIT);
return yahrzeitEvents.map((ev) => {
const hd = ev.getDate().prev();
const dt = hd.greg();
const uid = 'reminder-' + dayjs(dt).format('YYYYMMDD') + '-' + ev.hash + '-' + id;
const name = ev.name;
const subj = hebrewRe.test(name) ?
`${name} ${YAHRZEIT_HE} תזכורת` :
`${name} Yahrzeit reminder`;
return new Event(hd, subj, flags.USER_EVENT, {
eventTime: dt,
eventTimeStr: getAlarmTime(hd),
memo: ev.memo,
emoji: ev.emoji,
alarm: 'P0DT0H0M0S',
uid,
category: 'Personal',
});
});
}

/**
* @param {any} query
* @param {number} id
Expand Down Expand Up @@ -586,8 +595,10 @@ async function getEventsForId(query, id, startYear, numYears) {

const hebrewRe = /[\u05d0-\u05ea]/;

const YAHRZEIT_HE = 'יארצייט';

const en2he = {
Yahrzeit: 'יארצייט',
Yahrzeit: YAHRZEIT_HE,
Birthday: 'יום הולדת',
Anniversary: 'יום נישואין',
};
Expand All @@ -608,7 +619,7 @@ function makeYahrzeitSubject(info, hd, yearNumber, appendHebDate) {
const isHebrewName = hebrewRe.test(name);
const type = info.type;
if (type !== 'Other') {
const isYahrzeit = type === 'Yahrzeit';
const isYahrzeit = type === YAHRZEIT;
if (isHebrewName) {
const prefix = en2he[type];
subj = isYahrzeit ?
Expand Down Expand Up @@ -638,8 +649,8 @@ function makeYahrzeitSubject(info, hd, yearNumber, appendHebDate) {
*/
async function makeYahrzeitEvent(id, info, hyear, appendHebDate, calendarId, includeUrl) {
const type = info.type;
const isYahrzeit = (type === 'Yahrzeit');
const isBirthday = (type === 'Birthday');
const isYahrzeit = type === YAHRZEIT;
const isBirthday = type === BIRTHDAY;
const origDt = info.day.toDate();
const hd = isYahrzeit ?
HebrewCalendar.getYahrzeit(hyear, origDt) :
Expand Down Expand Up @@ -677,8 +688,8 @@ async function makeYahrzeitEvent(id, info, hyear, appendHebDate, calendarId, inc

function makeMemo(id, info, observed, nth, typeStr, hebdate, includeUrl, calendarId) {
const type = info.type;
const isYahrzeit = (type === 'Yahrzeit');
const isBirthday = (type === 'Birthday');
const isYahrzeit = type === YAHRZEIT;
const isBirthday = type === BIRTHDAY;
const isOther = (type === 'Other');
const name = info.name;
const nameAndType = isOther ? name : `${name}’s ${typeStr}`;
Expand Down

0 comments on commit ebc8eb4

Please sign in to comment.