diff --git a/src/addons/outlook/src/commands/commands.js b/src/addons/outlook/src/commands/commands.js index ae4457dc..ef94f603 100644 --- a/src/addons/outlook/src/commands/commands.js +++ b/src/addons/outlook/src/commands/commands.js @@ -1,4 +1,5 @@ /* global Office */ +const { APP_NAME } = require("../common/index"); const { createRoom, initSession } = require("../common/api"); const { startPolling } = require("../common/polling"); const { saveSession, loadSession } = require("../common/session"); @@ -6,6 +7,7 @@ const { openTransitDialog } = require("../common/transitDialog"); const { buildMeetingMessage } = require("../common/messageBuilder"); const { applyAppName } = require("../common/helpers"); const { initI18n, t } = require("../common/i18n"); +const { isMeetingAlreadyAdded } = require("../common/meetingDetector"); Office.onReady(async function (info) { @@ -26,6 +28,24 @@ function notify(message) { } function insertMeetingLink(event, session) { + const item = Office.context.mailbox.item; + + isMeetingAlreadyAdded(item) + .then((alreadyAdded) => { + if (alreadyAdded) { + notify(t("meeting.already_added", { app_name: APP_NAME })); + event.completed(); + return; + } + return _doInsertMeetingLink(event, session); + }) + .catch((err) => { + notify(t("meeting.error.details", { message: err.message })); + event.completed(); + }); +} + +function _doInsertMeetingLink(event, session) { createRoom(session) .then((data) => { const isWeb = Office.context.diagnostics.platform === "OfficeOnline"; @@ -36,7 +56,7 @@ function insertMeetingLink(event, session) { return new Promise((resolve, reject) => { item.body.setSelectedDataAsync(text, { coercionType }, (setResult) => { if (setResult.status !== Office.AsyncResultStatus.Succeeded) { - notify(t('meeting.error.details', { message: setResult.error.message })); + notify(t("meeting.error.details", { message: setResult.error.message })); resolve(); return; } diff --git a/src/addons/outlook/src/common/meetingDetector.js b/src/addons/outlook/src/common/meetingDetector.js new file mode 100644 index 00000000..0f3edb60 --- /dev/null +++ b/src/addons/outlook/src/common/meetingDetector.js @@ -0,0 +1,45 @@ +const { BASE_URL } = require("./index"); + +/** + * Returns a promise that resolves to true if a meeting link is already present + */ +function isMeetingAlreadyAdded(item) { + return Promise.all([_checkBody(item), _checkLocation(item)]).then( + ([inBody, inLocation]) => inBody || inLocation + ); +} + +function _checkBody(item) { + return new Promise((resolve) => { + item.body.getAsync(Office.CoercionType.Text, (result) => { + if (result.status !== Office.AsyncResultStatus.Succeeded) { + resolve(false); + return; + } + resolve(_containsMeetingUrl(result.value)); + }); + }); +} + +function _checkLocation(item) { + // Location only exists on appointments + if (item.itemType !== Office.MailboxEnums.ItemType.Appointment) { + return Promise.resolve(false); + } + return new Promise((resolve) => { + item.location.getAsync((result) => { + if (result.status !== Office.AsyncResultStatus.Succeeded) { + resolve(false); + return; + } + resolve(_containsMeetingUrl(result.value)); + }); + }); +} + +function _containsMeetingUrl(text) { + if (!text) return false; + return text.includes(BASE_URL); +} + +module.exports = { isMeetingAlreadyAdded }; diff --git a/src/addons/outlook/src/locales/de/translation.json b/src/addons/outlook/src/locales/de/translation.json index a86aa9b6..519bcabc 100644 --- a/src/addons/outlook/src/locales/de/translation.json +++ b/src/addons/outlook/src/locales/de/translation.json @@ -16,6 +16,7 @@ "disconnect": "Abmelden" }, "meeting": { + "already_added": "Es wurde bereits ein {{app_name}}-Meeting hinzugefügt.", "link_inserted": "Besprechungslink erfolgreich eingefügt", "generating": "Wird erstellt...", "add_meeting": "{{app_name}}-Besprechung hinzufügen", diff --git a/src/addons/outlook/src/locales/en/translation.json b/src/addons/outlook/src/locales/en/translation.json index ad49e8d6..60082f54 100644 --- a/src/addons/outlook/src/locales/en/translation.json +++ b/src/addons/outlook/src/locales/en/translation.json @@ -16,6 +16,7 @@ "disconnect": "Sign out" }, "meeting": { + "already_added": "A {{app_name}} meeting has already been added.", "link_inserted": "Meeting link inserted successfully", "generating": "Generating...", "add_meeting": "Add a {{app_name}} meeting", diff --git a/src/addons/outlook/src/locales/fr/translation.json b/src/addons/outlook/src/locales/fr/translation.json index b636f922..b1df41f1 100644 --- a/src/addons/outlook/src/locales/fr/translation.json +++ b/src/addons/outlook/src/locales/fr/translation.json @@ -16,6 +16,7 @@ "disconnect": "Se déconnecter" }, "meeting": { + "already_added": "Une réunion {{app_name}} a déjà été ajoutée.", "link_inserted": "Lien de réunion inséré avec succès", "generating": "Génération...", "add_meeting": "Ajouter une réunion {{app_name}}",