Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/releases/unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ When a change has user-facing documentation, include a canonical tasknotes.dev l
```

-->

## Fixed

- (#1451) Calendar views no longer render the Google Calendar copy of an event that
was already exported from a TaskNotes task. The task-side event is shown instead, so
exported tasks no longer appear twice on the calendar and mini-calendar. Unrelated
Google Calendar events remain visible. Detached recurring exceptions and expanded
recurring instances of an exported series are also suppressed.
10 changes: 9 additions & 1 deletion src/bases/CalendarView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ import {
getCalendarConfigValue as getCalendarConfigValueFromSnapshot,
} from "./calendarConfigSnapshot";
import { buildCalendarPropertyEvent } from "./calendarPropertyEvents";
import { buildExternalCalendarEvents } from "./calendarExternalEvents";
import {
buildExternalCalendarEvents,
buildLinkedExternalCalendarEventPredicate,
} from "./calendarExternalEvents";
import {
decorateCalendarIcsEventElement,
getCalendarRelatedNoteTooltip,
Expand Down Expand Up @@ -1872,6 +1875,11 @@ export class CalendarView extends BasesViewBase {
plugin: this.plugin,
toggles: this.googleCalendarToggles,
relatedNoteCountsByEventId,
isLinkedToTask: buildLinkedExternalCalendarEventPredicate(
this.currentTasks,
"google",
this.plugin
),
})
);
}
Expand Down
40 changes: 33 additions & 7 deletions src/bases/MiniCalendarView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import {
import type { BasesEntry, BasesPropertyId, BasesView, BasesViewFactory } from "obsidian";
import TaskNotesPlugin from "../main";
import { BasesViewBase } from "./BasesViewBase";
import {
buildLinkedExternalCalendarEventPredicate,
type LinkedExternalCalendarEventPredicate,
} from "./calendarExternalEvents";
import { identifyTaskNotesFromBasesData } from "./helpers";
import { ICSEvent, TaskInfo } from "../types";
import { format } from "date-fns";
import {
Expand Down Expand Up @@ -248,8 +253,19 @@ export class MiniCalendarView extends BasesViewBase {
// Use raw Bases data (has getValue() method)
const basesEntries = this.data.data;

// Build a linked-event filter from the TaskNotes tasks in this view so
// provider-side mirrors of exported tasks are suppressed (issue #1451).
const linkedGoogleEventFilter = buildLinkedExternalCalendarEventPredicate(
await identifyTaskNotesFromBasesData(
this.dataAdapter.extractDataItems(),
this.plugin
),
"google",
this.plugin
);

// Index notes by date
this.indexNotesByDate(basesEntries);
this.indexNotesByDate(basesEntries, linkedGoogleEventFilter);

// Render calendar grid
this.renderCalendarControls();
Expand Down Expand Up @@ -287,11 +303,14 @@ export class MiniCalendarView extends BasesViewBase {
}
}

private indexNotesByDate(dataItems: BasesEntry[]): void {
private indexNotesByDate(
dataItems: BasesEntry[],
linkedGoogleEventFilter?: LinkedExternalCalendarEventPredicate
): void {
this.notesByDate.clear();

if (!this.dateProperty) {
this.indexExternalCalendarEvents();
this.indexExternalCalendarEvents(linkedGoogleEventFilter);
return;
}

Expand Down Expand Up @@ -400,7 +419,7 @@ export class MiniCalendarView extends BasesViewBase {
}
}

this.indexExternalCalendarEvents();
this.indexExternalCalendarEvents(linkedGoogleEventFilter);
}

private addEntryToDate(dateKey: string, entry: NoteEntry): void {
Expand All @@ -411,9 +430,11 @@ export class MiniCalendarView extends BasesViewBase {
this.notesByDate.get(dateKey)?.push(entry);
}

private indexExternalCalendarEvents(): void {
private indexExternalCalendarEvents(
linkedGoogleEventFilter?: LinkedExternalCalendarEventPredicate
): void {
this.indexICSEvents();
this.indexGoogleCalendarEvents();
this.indexGoogleCalendarEvents(linkedGoogleEventFilter);
this.indexMicrosoftCalendarEvents();
}

Expand Down Expand Up @@ -442,7 +463,9 @@ export class MiniCalendarView extends BasesViewBase {
}
}

private indexGoogleCalendarEvents(): void {
private indexGoogleCalendarEvents(
linkedGoogleEventFilter?: LinkedExternalCalendarEventPredicate
): void {
if (!this.plugin.googleCalendarService) {
return;
}
Expand All @@ -457,6 +480,9 @@ export class MiniCalendarView extends BasesViewBase {
const calendarId = icsEvent.subscriptionId.replace("google-", "");
if (this.googleCalendarToggles.get(calendarId) === false) continue;

// Suppress provider-side mirrors of tasks exported from TaskNotes (issue #1451).
if (linkedGoogleEventFilter?.(icsEvent)) continue;

const calendar = calendars.get(calendarId);
this.indexExternalEvent(
icsEvent,
Expand Down
128 changes: 127 additions & 1 deletion src/bases/calendarExternalEvents.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { EventInput } from "@fullcalendar/core";
import type TaskNotesPlugin from "../main";
import type { ICSEvent } from "../types";
import type { ICSEvent, TaskInfo } from "../types";
import { createICSEvent } from "./calendar-core";

export type ExternalCalendarProvider = "ics" | "google" | "microsoft";
Expand All @@ -13,13 +13,27 @@ export type ExternalCalendarEventFactory = (

type Nullable<T> = T | null;

/**
* Predicate that, given an external provider event, reports whether it corresponds
* to a TaskNotes task that was exported to that provider's calendar. Calendar views
* use this to suppress the duplicate provider-side rendering of an exported task
* (upstream issue #1451). Unrelated events must remain visible.
*/
export type LinkedExternalCalendarEventPredicate = (event: ICSEvent) => boolean;

export interface BuildExternalCalendarEventsInput {
events: readonly ICSEvent[];
provider: ExternalCalendarProvider;
plugin: TaskNotesPlugin;
toggles?: ReadonlyMap<string, boolean>;
relatedNoteCountsByEventId?: ReadonlyMap<string, number>;
createEvent?: ExternalCalendarEventFactory;
/**
* Optional predicate that flags provider events linked to an exported
* TaskNotes task. Linked events are filtered out so the calendar only
* renders the task-side representation.
*/
isLinkedToTask?: LinkedExternalCalendarEventPredicate;
}

export function getExternalCalendarToggleId(
Expand All @@ -43,13 +57,121 @@ export function shouldIncludeExternalCalendarEvent(
return toggles?.get(getExternalCalendarToggleId(event, provider)) !== false;
}

/**
* Strip the provider-prefixed event id back down to the raw provider event id.
*
* Google/Microsoft ICSEvent ids are normalized as `{provider}-{calendarId}-{rawEventId}`
* (see GoogleCalendarService/MicrosoftCalendarService). Calendar ids may themselves
* contain hyphens, so only the known `{provider}-` segment is stripped from the
* subscription id before removing that calendar-id prefix from the event id.
*/
function getRawProviderEventId(event: ICSEvent, provider: ExternalCalendarProvider): string {
if (provider !== "google" && provider !== "microsoft") {
return event.id;
}
const calendarId =
provider === "google"
? event.subscriptionId.replace("google-", "")
: event.subscriptionId.replace("microsoft-", "");
const prefix = `${provider}-${calendarId}-`;
return event.id.startsWith(prefix) ? event.id.slice(prefix.length) : event.id;
}

function getRawRecurringEventId(
event: ICSEvent,
provider: ExternalCalendarProvider
): string | undefined {
if (!event.recurringEventId) return undefined;
if (provider !== "google" && provider !== "microsoft") {
return event.recurringEventId;
}
const calendarId =
provider === "google"
? event.subscriptionId.replace("google-", "")
: event.subscriptionId.replace("microsoft-", "");
const prefix = `${provider}-${calendarId}-`;
return event.recurringEventId.startsWith(prefix)
? event.recurringEventId.slice(prefix.length)
: event.recurringEventId;
}

/**
* Build a predicate that identifies Google Calendar provider events which are the
* provider-side mirror of a TaskNotes task exported to Google Calendar.
*
* A task is linked to a Google event when, for the export target calendar:
* - the task's stored `googleCalendarEventId` equals the event's raw id (single
* event or recurring series master), or
* - the task's stored `googleCalendarExceptionEventId` equals the event's raw
* id (a detached/moved recurring occurrence), or
* - the event is an expanded instance of a recurring series whose master id
* equals the task's `googleCalendarEventId` (matched via the event's
* `recurringEventId`).
*
* Only events from the configured export target calendar are considered, so events
* from other Google calendars are always retained. Microsoft Calendar has no
* task-export mapping field today, so this helper returns a never-match predicate
* for the "microsoft" provider; the structure is in place to add Microsoft
* filtering if a mapping field is introduced.
*/
export function buildLinkedExternalCalendarEventPredicate(
tasks: readonly TaskInfo[],
provider: ExternalCalendarProvider,
plugin: TaskNotesPlugin
): LinkedExternalCalendarEventPredicate {
if (provider !== "google") {
return () => false;
}

const targetCalendarId = plugin.settings?.googleCalendarExport?.targetCalendarId ?? "";

const masterEventIds = new Set<string>();
const exceptionEventIds = new Set<string>();
for (const task of tasks) {
if (task.googleCalendarEventId) {
masterEventIds.add(task.googleCalendarEventId);
}
if (task.googleCalendarExceptionEventId) {
exceptionEventIds.add(task.googleCalendarExceptionEventId);
}
}

if (masterEventIds.size === 0 && exceptionEventIds.size === 0) {
return () => false;
}

return (event: ICSEvent) => {
if (targetCalendarId === "") {
return false;
}
const calendarId = event.subscriptionId.replace("google-", "");
if (calendarId !== targetCalendarId) {
return false;
}

const rawEventId = getRawProviderEventId(event, "google");
if (exceptionEventIds.has(rawEventId)) {
return true;
}
if (masterEventIds.has(rawEventId)) {
return true;
}
const rawRecurringEventId = getRawRecurringEventId(event, "google");
if (rawRecurringEventId && masterEventIds.has(rawRecurringEventId)) {
return true;
}
return false;
};
}

export function buildExternalCalendarEvents({
events,
provider,
plugin,
toggles,
relatedNoteCountsByEventId,
createEvent = createICSEvent,
isLinkedToTask,
}: BuildExternalCalendarEventsInput): EventInput[] {
const calendarEvents: EventInput[] = [];

Expand All @@ -58,6 +180,10 @@ export function buildExternalCalendarEvents({
continue;
}

if (isLinkedToTask?.(event)) {
continue;
}

const calendarEvent = createEvent(event, plugin, {
relatedNoteCount: relatedNoteCountsByEventId?.get(event.id),
});
Expand Down
Loading