forked from GitHubMirrors/silverbullet-icalendar
Chore: Add detailed logging to diagnose zero events issue (v0.3.20)
All checks were successful
Build SilverBullet Plug / build (push) Successful in 25s
All checks were successful
Build SilverBullet Plug / build (push) Successful in 25s
This commit is contained in:
@@ -2,4 +2,4 @@ FROM denoland/deno:latest
|
|||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN deno run -A https://github.com/silverbulletmd/silverbullet/releases/download/2.4.1/plug-compile.js -c deno.json icalendar.plug.yaml
|
RUN deno run -A https://github.com/silverbulletmd/silverbullet/releases/download/2.4.1/plug-compile.js -c deno.json icalendar.plug.yaml
|
||||||
CMD ["cat", "Library/sstent/icalendar.plug.js"]
|
CMD ["cat", "icalendar.plug.js"]
|
||||||
|
|||||||
128
icalendar.ts
128
icalendar.ts
@@ -1,7 +1,7 @@
|
|||||||
import { clientStore, config, datastore, editor, index } from "@silverbulletmd/silverbullet/syscalls";
|
import { clientStore, config, datastore, editor, index } from "@silverbulletmd/silverbullet/syscalls";
|
||||||
import { convertIcsCalendar } from "https://esm.sh/ts-ics@2.4.0";
|
import { convertIcsCalendar } from "https://esm.sh/ts-ics@2.4.0";
|
||||||
|
|
||||||
const VERSION = "0.3.19";
|
const VERSION = "0.3.20";
|
||||||
const CACHE_KEY = "icalendar:lastSync";
|
const CACHE_KEY = "icalendar:lastSync";
|
||||||
|
|
||||||
console.log(`[iCalendar] Plug script executing at top level (Version ${VERSION})`);
|
console.log(`[iCalendar] Plug script executing at top level (Version ${VERSION})`);
|
||||||
@@ -23,70 +23,118 @@ const TIMEZONE_OFFSETS: Record<string, number> = {
|
|||||||
async function getSources(): Promise<{ sources: any[], tzShift: number }> {
|
async function getSources(): Promise<{ sources: any[], tzShift: number }> {
|
||||||
try {
|
try {
|
||||||
const rawConfig = await config.get("icalendar", { sources: [] });
|
const rawConfig = await config.get("icalendar", { sources: [] });
|
||||||
const sources = rawConfig.sources || [];
|
console.log("[iCalendar] Raw config retrieved:", JSON.stringify(rawConfig));
|
||||||
const tzShift = rawConfig.tzShift || 0;
|
|
||||||
|
let sources = rawConfig.sources || [];
|
||||||
|
let tzShift = rawConfig.tzShift || 0;
|
||||||
|
|
||||||
|
// Handle common Space Lua structure where tzShift might be inside the sources table
|
||||||
|
if (sources && typeof sources === "object" && !Array.isArray(sources)) {
|
||||||
|
if (sources.tzShift !== undefined && tzShift === 0) {
|
||||||
|
tzShift = sources.tzShift;
|
||||||
|
console.log("[iCalendar] Found tzShift inside sources object:", tzShift);
|
||||||
|
}
|
||||||
|
// Convert map-like sources to array
|
||||||
|
const sourceArray = [];
|
||||||
|
for (const key in sources) {
|
||||||
|
if (sources[key] && typeof sources[key].url === "string") {
|
||||||
|
sourceArray.push(sources[key]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sources = sourceArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`[iCalendar] Final Sources: ${sources.length}, tzShift: ${tzShift}`);
|
||||||
return { sources, tzShift };
|
return { sources, tzShift };
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.error("[iCalendar] Error in getSources:", e);
|
||||||
return { sources: [], tzShift: 0 };
|
return { sources: [], tzShift: 0 };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchAndParseCalendar(source: any, hourShift = 0): Promise<any[]> {
|
async function fetchAndParseCalendar(source: any, hourShift = 0): Promise<any[]> {
|
||||||
const response = await fetch(source.url);
|
console.log(`[iCalendar] Fetching from: ${source.url}`);
|
||||||
if (!response.ok) return [];
|
try {
|
||||||
const text = await response.text();
|
const response = await fetch(source.url);
|
||||||
const calendar = convertIcsCalendar(undefined, text);
|
if (!response.ok) {
|
||||||
if (!calendar.events) return [];
|
console.error(`[iCalendar] Fetch failed for ${source.name}: ${response.status} ${response.statusText}`);
|
||||||
|
return [];
|
||||||
const events: any[] = [];
|
}
|
||||||
for (const icsEvent of calendar.events) {
|
const text = await response.text();
|
||||||
const obj = icsEvent.start;
|
console.log(`[iCalendar] Received ICS data (${text.length} chars) from ${source.name}`);
|
||||||
let wallTimeStr = "";
|
|
||||||
if (obj.local && typeof obj.local.date === "string") wallTimeStr = obj.local.date;
|
|
||||||
else if (typeof obj.date === "string") wallTimeStr = obj.date;
|
|
||||||
if (!wallTimeStr) continue;
|
|
||||||
|
|
||||||
const baseDate = new Date(wallTimeStr.replace("Z", "") + "Z");
|
|
||||||
const tzName = obj.local?.timezone || obj.timezone || "UTC";
|
|
||||||
const sourceOffset = TIMEZONE_OFFSETS[tzName] ?? 0;
|
|
||||||
const utcMillis = baseDate.getTime() - (sourceOffset * 3600000);
|
|
||||||
const finalDate = new Date(utcMillis + (hourShift * 3600000));
|
|
||||||
|
|
||||||
const pad = (n: number) => String(n).padStart(2, "0");
|
// Log a snippet of the ICS to verify content
|
||||||
const localIso = finalDate.getFullYear() + "-" + pad(finalDate.getMonth() + 1) + "-" + pad(finalDate.getDate()) + "T" + pad(finalDate.getHours()) + ":" + pad(finalDate.getMinutes()) + ":" + pad(finalDate.getSeconds());
|
console.log(`[iCalendar] ICS snippet: ${text.substring(0, 200).replace(/\n/g, "\\n")}`);
|
||||||
|
|
||||||
events.push({
|
const calendar = convertIcsCalendar(undefined, text);
|
||||||
...icsEvent,
|
if (!calendar || !calendar.events) {
|
||||||
start: localIso,
|
console.warn(`[iCalendar] No events found in ICS from ${source.name} using ts-ics`);
|
||||||
tag: "ical-event",
|
return [];
|
||||||
sourceName: source.name
|
}
|
||||||
});
|
|
||||||
|
console.log(`[iCalendar] ts-ics found ${calendar.events.length} events in ${source.name}`);
|
||||||
|
|
||||||
|
const events: any[] = [];
|
||||||
|
for (const icsEvent of calendar.events) {
|
||||||
|
const obj = icsEvent.start;
|
||||||
|
if (!obj) continue;
|
||||||
|
|
||||||
|
let wallTimeStr = "";
|
||||||
|
if (obj.local && typeof obj.local.date === "string") wallTimeStr = obj.local.date;
|
||||||
|
else if (typeof obj.date === "string") wallTimeStr = obj.date;
|
||||||
|
|
||||||
|
if (!wallTimeStr) {
|
||||||
|
console.warn("[iCalendar] Skipping event with no start date string:", icsEvent.summary);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const baseDate = new Date(wallTimeStr.replace("Z", "") + "Z");
|
||||||
|
const tzName = obj.local?.timezone || obj.timezone || "UTC";
|
||||||
|
const sourceOffset = TIMEZONE_OFFSETS[tzName] ?? 0;
|
||||||
|
const utcMillis = baseDate.getTime() - (sourceOffset * 3600000);
|
||||||
|
const finalDate = new Date(utcMillis + (hourShift * 3600000));
|
||||||
|
|
||||||
|
const pad = (n: number) => String(n).padStart(2, "0");
|
||||||
|
const localIso = finalDate.getFullYear() + "-" + pad(finalDate.getMonth() + 1) + "-" + pad(finalDate.getDate()) + "T" + pad(finalDate.getHours()) + ":" + pad(finalDate.getMinutes()) + ":" + pad(finalDate.getSeconds());
|
||||||
|
|
||||||
|
events.push({
|
||||||
|
...icsEvent,
|
||||||
|
start: localIso,
|
||||||
|
tag: "ical-event",
|
||||||
|
sourceName: source.name
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return events;
|
||||||
|
} catch (err) {
|
||||||
|
console.error(`[iCalendar] Error fetching/parsing ${source.name}:`, err);
|
||||||
|
return [];
|
||||||
}
|
}
|
||||||
return events;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function syncCalendars() {
|
export async function syncCalendars() {
|
||||||
|
console.log("[iCalendar] syncCalendars() started");
|
||||||
try {
|
try {
|
||||||
const { sources, tzShift } = await getSources();
|
const { sources, tzShift } = await getSources();
|
||||||
if (sources.length === 0) return;
|
if (sources.length === 0) {
|
||||||
|
console.log("[iCalendar] No sources to sync");
|
||||||
|
return;
|
||||||
|
}
|
||||||
await editor.flashNotification("Syncing calendars...", "info");
|
await editor.flashNotification("Syncing calendars...", "info");
|
||||||
const allEvents: any[] = [];
|
const allEvents: any[] = [];
|
||||||
for (const source of sources) {
|
for (const source of sources) {
|
||||||
try {
|
const events = await fetchAndParseCalendar(source, tzShift);
|
||||||
const events = await fetchAndParseCalendar(source, tzShift);
|
allEvents.push(...events);
|
||||||
allEvents.push(...events);
|
|
||||||
} catch (err) {
|
|
||||||
console.error(`Failed to sync ${source.name}:`, err);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
console.log(`[iCalendar] Total events to index: ${allEvents.length}`);
|
||||||
await index.indexObjects("$icalendar", allEvents);
|
await index.indexObjects("$icalendar", allEvents);
|
||||||
await editor.flashNotification(`Synced ${allEvents.length} events`, "info");
|
await editor.flashNotification(`Synced ${allEvents.length} events`, "info");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Sync failed:", err);
|
console.error("[iCalendar] syncCalendars failed:", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function forceSync() {
|
export async function forceSync() {
|
||||||
|
console.log("[iCalendar] forceSync() triggered");
|
||||||
await clientStore.del(CACHE_KEY);
|
await clientStore.del(CACHE_KEY);
|
||||||
await syncCalendars();
|
await syncCalendars();
|
||||||
}
|
}
|
||||||
@@ -106,4 +154,4 @@ export async function clearCache() {
|
|||||||
|
|
||||||
export async function showVersion() {
|
export async function showVersion() {
|
||||||
await editor.flashNotification(`iCalendar Plug ${VERSION}`, "info");
|
await editor.flashNotification(`iCalendar Plug ${VERSION}`, "info");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user