From 545c3ffb713e967a00ce20cc696a32a2be713318 Mon Sep 17 00:00:00 2001 From: Neil Rashbrook Date: Wed, 4 Nov 2020 12:19:08 +0000 Subject: [PATCH 1/2] Support email calendar property --- .../experiments/calendar/ext-calendar-utils.jsm | 15 +++++++++++++++ .../calendar/parent/ext-calendar-calendars.js | 16 +++++++++++++++- .../calendar/schema/calendar-calendars.json | 4 ++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/calendar/experiments/calendar/ext-calendar-utils.jsm b/calendar/experiments/calendar/ext-calendar-utils.jsm index 139a866..9ad293a 100644 --- a/calendar/experiments/calendar/ext-calendar-utils.jsm +++ b/calendar/experiments/calendar/ext-calendar-utils.jsm @@ -12,6 +12,7 @@ var EXPORTED_SYMBOLS = [ "propsToItem", "convertItem", "convertAlarm", + "findIdentityKey", ]; var { XPCOMUtils } = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm"); @@ -21,6 +22,7 @@ XPCOMUtils.defineLazyModuleGetters(this, { ICAL: "resource:///modules/calendar/Ical.jsm", CalEvent: "resource:///modules/CalEvent.jsm", CalTodo: "resource:///modules/CalTodo.jsm", + MailServices: "resource:///modules/MailServices.jsm", }); var { ExtensionError } = ChromeUtils.import( @@ -79,6 +81,7 @@ function convertCalendar(extension, calendar) { readOnly: calendar.readOnly, enabled: !calendar.getProperty("disabled"), color: calendar.getProperty("color") || "#A8C2E1", + email: calendar.getProperty("imip.identity")?.email, }; if (isOwnCalendar(calendar, extension)) { @@ -218,3 +221,15 @@ function convertAlarm(item, alarm) { related: ALARM_RELATED_MAP[alarm.related], }; } + +function findIdentityKey(email) { + if (!email) { + return null; + } + for (let identity of MailServices.accounts.allIdentities) { + if (identity.email == email) { + return identity.key; + } + } + throw new ExtensionError(`Email address ${email} is unknown`); +} diff --git a/calendar/experiments/calendar/parent/ext-calendar-calendars.js b/calendar/experiments/calendar/parent/ext-calendar-calendars.js index 79e432e..293e6fe 100644 --- a/calendar/experiments/calendar/parent/ext-calendar-calendars.js +++ b/calendar/experiments/calendar/parent/ext-calendar-calendars.js @@ -18,6 +18,7 @@ this.calendar_calendars = class extends ExtensionAPI { getResolvedCalendarById, isOwnCalendar, convertCalendar, + findIdentityKey, } = ChromeUtils.import(this.extension.rootURI.resolve("experiments/calendar/ext-calendar-utils.jsm")); return { @@ -80,6 +81,8 @@ this.calendar_calendars = class extends ExtensionAPI { } }, create: async function(createProperties) { + let key = findIdentityKey(createProperties.email); + let calendar = calmgr.createCalendar( createProperties.type, Services.io.newURI(createProperties.url) @@ -89,9 +92,12 @@ this.calendar_calendars = class extends ExtensionAPI { } calendar.name = createProperties.name; - if (typeof createProperties.color != "undefined") { + if (createProperties.color != null) { calendar.setProperty("color", createProperties.color); } + if (key) { + calendar.setProperty("imip.identity.key", key); + } calmgr.registerCalendar(calendar); @@ -125,6 +131,10 @@ this.calendar_calendars = class extends ExtensionAPI { } } + if (updateProperties.email != null) { + calendar.setProperty("imip.identity.key", findIdentityKey(updateProperties.email)); + } + // TODO capabilities merging }, remove: async function(id) { @@ -211,6 +221,10 @@ this.calendar_calendars = class extends ExtensionAPI { case "disabled": fire.sync(converted, { enabled: !value }); break; + case "imip.identity.key": + let identity = calendar.getProperty("imip.identity"); + fire.sync(converted, { email: identity?.email }); + break; } }, }); diff --git a/calendar/experiments/calendar/schema/calendar-calendars.json b/calendar/experiments/calendar/schema/calendar-calendars.json index 0967eff..b56105d 100644 --- a/calendar/experiments/calendar/schema/calendar-calendars.json +++ b/calendar/experiments/calendar/schema/calendar-calendars.json @@ -13,6 +13,7 @@ "url": { "type": "string" }, "readOnly": { "type": "boolean" }, "enabled": { "type": "boolean" }, + "email": { "type": "string", "optional": true }, "color": { "type": "string", "optional": true }, "capabilities": { "$ref": "CalendarCapabilities", "optional": true } } @@ -25,6 +26,7 @@ "url": { "type": "string" }, "readOnly": { "type": "boolean" }, "enabled": { "type": "boolean" }, + "email": { "type": "string", "optional": true }, "color": { "type": "string", "optional": true } } }, @@ -128,6 +130,7 @@ "url": { "type": "string" }, "readOnly": { "type": "boolean", "optional": true }, "enabled": { "type": "boolean", "optional": true }, + "email": { "type": "string", "optional": true }, "color": { "type": "string", "optional": true }, "capabilities": { "$ref": "CalendarCapabilities", "optional": true } } @@ -148,6 +151,7 @@ "url": { "type": "string", "optional": true }, "readOnly": { "type": "boolean", "optional": true }, "enabled": { "type": "boolean", "optional": true }, + "email": { "type": "string", "optional": true }, "color": { "type": "string", "optional": true }, "capabilities": { "$ref": "CalendarCapabilities", "optional": true } } From cdb717eb85fc072083963d29a964d7b8f224df5b Mon Sep 17 00:00:00 2001 From: Neil Rashbrook Date: Wed, 9 Dec 2020 23:44:27 +0000 Subject: [PATCH 2/2] Support identity calendar property --- .../calendar/ext-calendar-utils.jsm | 16 +--------- .../calendar/parent/ext-calendar-calendars.js | 32 ++++++++++++++----- .../calendar/schema/calendar-calendars.json | 8 ++--- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/calendar/experiments/calendar/ext-calendar-utils.jsm b/calendar/experiments/calendar/ext-calendar-utils.jsm index 9ad293a..61245f1 100644 --- a/calendar/experiments/calendar/ext-calendar-utils.jsm +++ b/calendar/experiments/calendar/ext-calendar-utils.jsm @@ -12,7 +12,6 @@ var EXPORTED_SYMBOLS = [ "propsToItem", "convertItem", "convertAlarm", - "findIdentityKey", ]; var { XPCOMUtils } = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm"); @@ -22,7 +21,6 @@ XPCOMUtils.defineLazyModuleGetters(this, { ICAL: "resource:///modules/calendar/Ical.jsm", CalEvent: "resource:///modules/CalEvent.jsm", CalTodo: "resource:///modules/CalTodo.jsm", - MailServices: "resource:///modules/MailServices.jsm", }); var { ExtensionError } = ChromeUtils.import( @@ -81,7 +79,7 @@ function convertCalendar(extension, calendar) { readOnly: calendar.readOnly, enabled: !calendar.getProperty("disabled"), color: calendar.getProperty("color") || "#A8C2E1", - email: calendar.getProperty("imip.identity")?.email, + identity: calendar.getProperty("imip.identity.key"), }; if (isOwnCalendar(calendar, extension)) { @@ -221,15 +219,3 @@ function convertAlarm(item, alarm) { related: ALARM_RELATED_MAP[alarm.related], }; } - -function findIdentityKey(email) { - if (!email) { - return null; - } - for (let identity of MailServices.accounts.allIdentities) { - if (identity.email == email) { - return identity.key; - } - } - throw new ExtensionError(`Email address ${email} is unknown`); -} diff --git a/calendar/experiments/calendar/parent/ext-calendar-calendars.js b/calendar/experiments/calendar/parent/ext-calendar-calendars.js index 293e6fe..5bb3817 100644 --- a/calendar/experiments/calendar/parent/ext-calendar-calendars.js +++ b/calendar/experiments/calendar/parent/ext-calendar-calendars.js @@ -18,7 +18,6 @@ this.calendar_calendars = class extends ExtensionAPI { getResolvedCalendarById, isOwnCalendar, convertCalendar, - findIdentityKey, } = ChromeUtils.import(this.extension.rootURI.resolve("experiments/calendar/ext-calendar-utils.jsm")); return { @@ -81,7 +80,15 @@ this.calendar_calendars = class extends ExtensionAPI { } }, create: async function(createProperties) { - let key = findIdentityKey(createProperties.email); + if (createProperties.identity) { + if (!context.extension.hasPermission("accountsRead")) { + throw new ExtensionError('Using identities requires the "accountsRead" permission'); + } + + if (!MailServices.accounts.allIdentities.some(i => i.key == createProperties.identity)) { + throw new ExtensionError(`Identity not found: ${createProperties.identity}`); + } + } let calendar = calmgr.createCalendar( createProperties.type, @@ -95,8 +102,8 @@ this.calendar_calendars = class extends ExtensionAPI { if (createProperties.color != null) { calendar.setProperty("color", createProperties.color); } - if (key) { - calendar.setProperty("imip.identity.key", key); + if (createProperties.identity != null) { + calendar.setProperty("imip.identity.key", createProperties.identity); } calmgr.registerCalendar(calendar); @@ -116,6 +123,16 @@ this.calendar_calendars = class extends ExtensionAPI { if (updateProperties.url && !isOwnCalendar(calendar, context.extension)) { throw new ExtensionError("Cannot update url for foreign calendars"); } + if (updateProperties.identity) { + if (!context.extension.hasPermission("accountsRead")) { + throw new ExtensionError('Using identities requires the "accountsRead" permission'); + } + + if (!MailServices.accounts.allIdentities.some(i => i.key == updateProperties.identity)) { + throw new ExtensionError(`Identity not found: ${updateProperties.identity}`); + } + } + if (updateProperties.url) { calendar.uri = Services.io.newURI(updateProperties.url); @@ -131,8 +148,8 @@ this.calendar_calendars = class extends ExtensionAPI { } } - if (updateProperties.email != null) { - calendar.setProperty("imip.identity.key", findIdentityKey(updateProperties.email)); + if (updateProperties.identity != null) { + calendar.setProperty("imip.identity.key", updateProperties.identity); } // TODO capabilities merging @@ -222,8 +239,7 @@ this.calendar_calendars = class extends ExtensionAPI { fire.sync(converted, { enabled: !value }); break; case "imip.identity.key": - let identity = calendar.getProperty("imip.identity"); - fire.sync(converted, { email: identity?.email }); + fire.sync(converted, { identity: value }); break; } }, diff --git a/calendar/experiments/calendar/schema/calendar-calendars.json b/calendar/experiments/calendar/schema/calendar-calendars.json index b56105d..2f68788 100644 --- a/calendar/experiments/calendar/schema/calendar-calendars.json +++ b/calendar/experiments/calendar/schema/calendar-calendars.json @@ -13,7 +13,7 @@ "url": { "type": "string" }, "readOnly": { "type": "boolean" }, "enabled": { "type": "boolean" }, - "email": { "type": "string", "optional": true }, + "identity": { "type": "string", "optional": true }, "color": { "type": "string", "optional": true }, "capabilities": { "$ref": "CalendarCapabilities", "optional": true } } @@ -26,7 +26,7 @@ "url": { "type": "string" }, "readOnly": { "type": "boolean" }, "enabled": { "type": "boolean" }, - "email": { "type": "string", "optional": true }, + "identity": { "type": "string", "optional": true }, "color": { "type": "string", "optional": true } } }, @@ -130,7 +130,7 @@ "url": { "type": "string" }, "readOnly": { "type": "boolean", "optional": true }, "enabled": { "type": "boolean", "optional": true }, - "email": { "type": "string", "optional": true }, + "identity": { "type": "string", "optional": true }, "color": { "type": "string", "optional": true }, "capabilities": { "$ref": "CalendarCapabilities", "optional": true } } @@ -151,7 +151,7 @@ "url": { "type": "string", "optional": true }, "readOnly": { "type": "boolean", "optional": true }, "enabled": { "type": "boolean", "optional": true }, - "email": { "type": "string", "optional": true }, + "identity": { "type": "string", "optional": true }, "color": { "type": "string", "optional": true }, "capabilities": { "$ref": "CalendarCapabilities", "optional": true } }