Prune cached events when a CalDAV calendar is deselected

Saving the wizard with fewer selected calendars removes their cache entries, and the events view only shows calendars still selected on their instance.
This commit is contained in:
raucao committed 2026-10-28 00:28:00 +04:00
1 parent b8705a5032
commit b63d849817
2 files changed
+128 -3

No files matched your search

+43 -3
View File
@@ -186,6 +186,27 @@ export default class CalendarService extends Service {
});
await this.reload();
await this.pruneUnselectedCalendars(instanceKey);
}
// A calendar the user deselected should not keep showing cached events.
async pruneUnselectedCalendars(instanceKey) {
const instance = this.instances.find(
(candidate) => candidate.id === instanceKey
);
if (!instance || instance.type !== CALDAV_TYPE) {
return;
}
const selected = new Set(instance.selectedCalendarIds);
const stale = this.calendarCache
.forSource(CALDAV_TYPE, instanceKey)
.filter((entry) => !selected.has(entry.source.calendarId));
await Promise.all(
stale.map((entry) => this.calendarCache.remove(entry.key))
);
}
async configureIcs({ url, name }, key = null) {
@@ -332,13 +353,16 @@ export default class CalendarService extends Service {
// --- View ----------------------------------------------------------------
// Combined upcoming events from every enabled source, split into today and
// the next few days that have events.
// the next few days that have events. Only calendars still selected on their
// instance are shown.
get events() {
const enabledIds = new Set(this.instances.map((instance) => instance.id));
const instancesById = new Map(
this.instances.map((instance) => [instance.id, instance])
);
const events = this.calendarCache
.all()
.filter((entry) => enabledIds.has(entry.source.instanceKey))
.filter((entry) => this.isEntryVisible(entry, instancesById))
.flatMap((entry) =>
entry.events.map((event) => ({ ...event, source: entry.source }))
);
@@ -346,6 +370,22 @@ export default class CalendarService extends Service {
return groupEvents(events);
}
isEntryVisible(entry, instancesById) {
const instance = instancesById.get(entry.source.instanceKey);
if (!instance) {
return false;
}
// A CalDAV account can hold several calendars; an ICS instance owns one
// feed, so its single entry is visible whenever the instance is.
if (instance.type === CALDAV_TYPE) {
return instance.selectedCalendarIds.includes(entry.source.calendarId);
}
return true;
}
willDestroy() {
this.stopPolling();
this.sockethub.release(CALENDAR_CONSUMER);
+85
View File
@@ -248,6 +248,91 @@ module('Unit | Service | calendar', function (hooks) {
service.stopPolling();
});
test('deselecting a calendar prunes its cached events', async function (assert) {
const { service, cache } = await setup(this, {
handler: (message) =>
message.type === 'query' ? { items: [EVENT] } : { items: [] },
});
const key = caldavInstanceKey('https://cal.example/dav/', 'alice');
const second = {
id: 'c2',
type: 'calendar',
name: 'Personal',
components: ['event'],
};
await service.configureCaldav({
url: 'https://cal.example/dav/',
username: 'alice',
password: 'secret',
calendars: [CALENDAR, second],
selectedCalendarIds: ['c1', 'c2'],
});
await service.refreshAll();
assert.ok(cache.get(`calendar-caldav:${key}:c1`));
assert.ok(cache.get(`calendar-caldav:${key}:c2`));
await service.configureCaldav({
url: 'https://cal.example/dav/',
username: 'alice',
password: 'secret',
calendars: [CALENDAR, second],
selectedCalendarIds: ['c1'],
});
assert.ok(cache.get(`calendar-caldav:${key}:c1`));
assert.strictEqual(cache.get(`calendar-caldav:${key}:c2`), null);
service.stopPolling();
});
test('events hide cached entries for calendars no longer selected', async function (assert) {
const { service, cache } = await setup(this);
const now = new Date();
const today = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`;
service.instances = [
{
id: 'calendar-caldav-x',
type: 'calendar-caldav',
selectedCalendarIds: ['c1'],
},
];
await cache.put({
key: 'calendar-caldav:calendar-caldav-x:c1',
source: {
type: 'calendar-caldav',
instanceKey: 'calendar-caldav-x',
calendarId: 'c1',
name: 'Work',
},
fetchedAt: Date.now(),
events: [{ id: 'kept', name: 'Kept', startTime: today, allDay: true }],
});
await cache.put({
key: 'calendar-caldav:calendar-caldav-x:c2',
source: {
type: 'calendar-caldav',
instanceKey: 'calendar-caldav-x',
calendarId: 'c2',
name: 'Old',
},
fetchedAt: Date.now(),
events: [
{ id: 'hidden', name: 'Hidden', startTime: today, allDay: true },
],
});
assert.deepEqual(
service.events.today.map((event) => event.id),
['kept']
);
});
test('events groups cached events from enabled sources', async function (assert) {
const { service, cache } = await setup(this);