Skip to content

Commit

Permalink
Merge pull request #38 from wireapp/kj/wir-21-make-add-in-work-for-ou…
Browse files Browse the repository at this point in the history
…tlook-mobile

Kj/wir 21 make add in work for outlook mobile
  • Loading branch information
LennardZieglerCONPORT authored Jul 10, 2024
2 parents ca6280b + 033edc6 commit 73f4113
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 70 deletions.
53 changes: 32 additions & 21 deletions src/calendarIntegration/addMeetingLink.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/* global Office, console */

import { appendToBody, getBody, getLocation, getMailboxItemSubject, getMeetingTime, getOrganizer, setLocation, setSubject } from "../utils/mailbox";
import { appendToBody, getBody, getLocation, getMailboxItemSubject, getMeetingTime, getOrganizer, getOrganizerOnMobile, isMobileDevice, setLocation } from "../utils/mailbox";
import { createMeetingSummary } from "./createMeetingSummary";
import { setCustomPropertyAsync, getCustomPropertyAsync } from "../utils/customProperties";
import { showNotification, removeNotification } from "../utils/notifications";
import { isOutlookCalIntegrationEnabled } from "./isOutlookCalIntegrationEnabled";
import { createEvent } from "./createEvent";
import { mailboxItem } from "../commands/commands";
import { EventResult } from "../types/EventResult";
import { PlatformType } from "../types/PlatformTypes";

const defaultSubjectValue = "New Appointment";
let createdMeeting: EventResult;
Expand Down Expand Up @@ -93,18 +93,31 @@ async function createNewMeeting(): Promise<void> {
}


/**
* Retrieves the platform-specific organizer for the current mailbox item.
*
* @return {Promise<any>} A promise that resolves to the platform-specific organizer.
*/
async function getPlatformSpecificOrganizer(): Promise<any> {
if (isMobileDevice()) {
return await getOrganizerOnMobile(mailboxItem);
} else {
return await getOrganizer(mailboxItem);
}
}
/**
* Updates the meeting details by setting the location and appending the meeting summary to the body of the mailbox item.
*
* @param {EventResult} eventResult - The event result containing the link for the meeting.
* @return {Promise<void>} A promise that resolves when the meeting details are updated.
*/
async function updateMeetingDetails(eventResult: EventResult): Promise<void> {
getOrganizer(mailboxItem, async (organizer) => {
await setLocation(mailboxItem, eventResult.link);
const meetingSummary = createMeetingSummary(eventResult.link, organizer);
await appendToBody(mailboxItem, meetingSummary);
});
await setLocation(mailboxItem, eventResult.link);

const organizer = await getPlatformSpecificOrganizer();

const meetingSummary = createMeetingSummary(eventResult.link, organizer);
await appendToBody(mailboxItem, meetingSummary);
}

/**
Expand All @@ -122,22 +135,17 @@ async function handleExistingMeeting(): Promise<void> {
const currentLocation = await getLocation(mailboxItem);
const normalizedCurrentBody = currentBody.replace(/&amp;/g, "&");
const normalizedMeetingLink = createdMeeting.link?.replace(/&amp;/g, "&");
const organizer = await getPlatformSpecificOrganizer();
const meetingSummary = createMeetingSummary(createdMeeting.link, organizer);

getOrganizer(mailboxItem, async (organizer) => {

//Check if location is empty
if (!currentLocation) {
await setLocation(mailboxItem, createdMeeting.link);
}

const meetingSummary = createMeetingSummary(createdMeeting.link, organizer);

//Check if body is empty
if (!normalizedCurrentBody.includes(normalizedMeetingLink)) {
await appendToBody(mailboxItem, meetingSummary);
}
if (!currentLocation) {
await setLocation(mailboxItem, createdMeeting.link);
}

});

if (!normalizedCurrentBody.includes(normalizedMeetingLink)) {
await appendToBody(mailboxItem, meetingSummary);
}

await setCustomPropertyAsync(mailboxItem, "wireId", createdMeeting.id);
await setCustomPropertyAsync(mailboxItem, "wireLink", createdMeeting.link);
Expand All @@ -164,6 +172,9 @@ async function addMeetingLink(event: Office.AddinCommands.Event): Promise<void>
console.error("Error during adding Wire meeting link", error);
handleAddMeetingLinkError(error);
} finally {
if (isMobileDevice()) {
await appendToBody(mailboxItem, ""); //Workaround for mobile devices - Without body gets removed
}
event.completed();
}
}
Expand Down
36 changes: 18 additions & 18 deletions src/commands/commands.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/

/* global Office */

import { addMeetingLink } from "../calendarIntegration/addMeetingLink";

export let mailboxItem;

// Office is ready. Init
Office.onReady(function () {
mailboxItem = Office.context.mailbox.item;
});

// Register the functions.
Office.actions.associate("addMeetingLink", addMeetingLink);
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/

/* global Office */

import { addMeetingLink } from "../calendarIntegration/addMeetingLink";

export let mailboxItem;

// Office is ready. Init
Office.onReady(function () {
mailboxItem = Office.context.mailbox.item;
});

// Register the functions.
Office.actions.associate("addMeetingLink", addMeetingLink);
8 changes: 8 additions & 0 deletions src/types/PlatformTypes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export enum PlatformType {
IOS = "iOS",
ANDROID = "Android",
PC = "PC",
OFFICEONLINE = "OfficeOnline",
MAC = "Mac",
UNIVERSAL = "Universal"
}
92 changes: 61 additions & 31 deletions src/utils/mailbox.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
export async function getMailboxItemSubject(item) : Promise<string> {
return new Promise((resolve,reject) => {
const { subject } = item;
import { PlatformType } from "../types/PlatformTypes";

subject.getAsync(function (asyncResult) {
if (asyncResult.status == Office.AsyncResultStatus.Failed) {
console.error("Failed to get item subject");
reject(new Error("Failed to get item subject"));
} else {
resolve(asyncResult.value);
}
export async function getMailboxItemSubject(item): Promise<string> {
return new Promise((resolve, reject) => {
const { subject } = item;

subject.getAsync(function (asyncResult) {
if (asyncResult.status == Office.AsyncResultStatus.Failed) {
console.error("Failed to get item subject");
reject(new Error("Failed to get item subject"));
} else {
resolve(asyncResult.value);
}
});
});
});
}

export async function getOrganizer(item, callback) {
const { organizer } = item;
export async function getOrganizer(item): Promise<string> {
return new Promise((resolve, reject) => {
const { organizer } = item;

await organizer.getAsync(function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.error("Failed to get organizer.");
} else {
callback(asyncResult.value.displayName);
}
organizer.getAsync(function (asyncResult) {
if (asyncResult.status === Office.AsyncResultStatus.Succeeded) {
resolve(asyncResult.value.displayName);
} else {
console.error(asyncResult.error);
reject(new Error("Failed to get organizer"));
}
});
});
}

export async function getOrganizerOnMobile(item) {
return new Promise((resolve, reject) => {
item.body.getAsync("html", { asyncContext: Office.context.mailbox.userProfile.displayName }, (result) => {
if (result.status === Office.AsyncResultStatus.Succeeded) {
resolve(result.asyncContext);
} else {
reject(new Error("Failed to get body."));
}
});
});
}

export async function setSubject(item, newSubject:string){

export async function setSubject(item, newSubject: string) {
const { subject } = item;

subject.setAsync(newSubject, function (asyncResult) {
Expand All @@ -47,18 +63,17 @@ export async function getMeetingTime(item): Promise<string> {
reject(new Error(`Failed to get start time: ${asyncResult.error.message}`));
return;
}
const locale = Office.context.displayLanguage;
const formattedDate = new Date(asyncResult.value).toLocaleString(locale, {
year: 'numeric',
month: 'numeric',
day: 'numeric',
});
resolve(formattedDate);
const locale = Office.context.displayLanguage;
const formattedDate = new Date(asyncResult.value).toLocaleString(locale, {
year: "numeric",
month: "numeric",
day: "numeric",
});
resolve(formattedDate);
});
});
}


export function getBody(item): Promise<string> {
return new Promise((resolve, reject) => {
const { body } = item;
Expand All @@ -74,7 +89,6 @@ export function getBody(item): Promise<string> {
});
}


export function setBody(item, newBody) {
const { body } = item;
const type = { coercionType: Office.CoercionType.Html };
Expand Down Expand Up @@ -109,7 +123,11 @@ export async function getLocation(item) {
});
});
}

export function isMobileDevice() {
return (
Office.context.platform.toString() == PlatformType.IOS || Office.context.platform.toString() == PlatformType.ANDROID
);
}

export async function setLocation(item, meetlingLink) {
const { location } = item;
Expand All @@ -120,4 +138,16 @@ export async function setLocation(item, meetlingLink) {
return;
}
});

if (isMobileDevice()) {
/* Workaround for mobile devices - sometimes location gets removed*/
let currentLocation = await getLocation(item);

location.setAsync(currentLocation + "", function (asyncResult) {
if (asyncResult.status !== Office.AsyncResultStatus.Succeeded) {
console.error(`Action failed with message ${asyncResult.error.message}`);
return;
}
});
}
}

0 comments on commit 73f4113

Please sign in to comment.