diff options
author | Raphael <mail@raphaelkabo.com> | 2025-05-28 18:58:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-28 18:58:46 +0100 |
commit | 3d84891118f8a81af3ddb978af9b3f8b02fd5d65 (patch) | |
tree | 0a8d344e331a0551b73435bbbb3919107737f69f /src/helpers.ts | |
parent | 6f0b7a44b995b6b66baf42a9369182fc05a90b34 (diff) | |
parent | 4664b6968fdcaca54268d60f400da02364213f05 (diff) |
Merge branch 'main' into main
Diffstat (limited to 'src/helpers.ts')
-rw-r--r-- | src/helpers.ts | 93 |
1 files changed, 58 insertions, 35 deletions
diff --git a/src/helpers.ts b/src/helpers.ts index 47b380f..5590912 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -1,5 +1,8 @@ -import moment from "moment-timezone"; -import icalGenerator from "ical-generator"; +import mongoose from 'mongoose'; +import moment from 'moment-timezone'; +import icalGenerator from 'ical-generator'; +import i18next from 'i18next'; +import handlebars from 'handlebars'; import Log from "./models/Log.js"; import { getConfig } from "./lib/config.js"; import { IEvent } from "./models/Event.js"; @@ -10,41 +13,61 @@ const siteName = config.general.site_name; // LOGGING export function addToLog(process: string, status: string, message: string) { - const logEntry = { - status, - process, - message, - timestamp: new Date(), - }; - new Log(logEntry).save().catch(() => { - console.log("Error saving log entry!"); - }); + const logEntry = { + status, + process, + message, + timestamp: new Date(), + }; + new Log(logEntry).save().catch(() => { + console.log("Error saving log entry!"); + }); } -export function exportICal(events: IEvent[], calendarName: string) { - if (!events || events.length < 1) return; +export function exportIcal(events: IEvent | IEvent[], calendarName?: string) { // Ical -> ICal + // Create a new icalGenerator... generator + const cal = icalGenerator({ + name: calendarName || siteName, + timezone: 'UTC' + }); - // Create a new icalGenerator... generator - const cal = icalGenerator({ - name: calendarName || siteName, - }); - events.forEach((event) => { - // Add the event to the generator - cal.createEvent({ - start: moment.tz(event.start, event.timezone), - end: moment.tz(event.end, event.timezone), - timezone: event.timezone, - summary: event.name, - description: event.description, - organizer: { - name: event.hostName || "Anonymous", - email: event.creatorEmail || "anonymous@anonymous.com", - }, - location: event.location, - url: "https://" + domain + "/" + event.id, - }); + const eventArray = Array.isArray(events) ? events : [events]; + eventArray.forEach(event => { + cal.createEvent({ + start: moment.tz(event.start, event.timezone), + end: moment.tz(event.end, event.timezone), + timezone: event.timezone, + summary: event.name, + description: event.description, + organizer: { + name: event.hostName || "Anonymous", + email: event.creatorEmail || 'anonymous@anonymous.com', + }, + location: event.location, + url: 'https://' + domain + '/' + event.id }); - // Stringify it! - const string = cal.toString(); - return string; + }); + + return cal.toString(); +} + +interface I18nHelpers { + t: (key: string, options?: object) => string; + tn: (key: string, options?: object) => string; + count?: number; +} + +export function getI18nHelpers(): I18nHelpers { + return { + t: function(key: string, options?: object) { + const translation = i18next.t(key, { ...this, ...options }); + const template = handlebars.compile(translation); + return template(this); + }, + tn: function(key: string, options?: object) { + const translation = i18next.t(key, { count: this.count, ...options }); + const template = handlebars.compile(translation); + return template(this); + } + }; } |