-
Notifications
You must be signed in to change notification settings - Fork 770
/
commands.js
347 lines (313 loc) · 11.8 KB
/
commands.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
Office.onReady();
/**
* The words in the subject or body that require corresponding color categories to be applied to a new
* message or appointment.
* @constant
* @type {string[]}
*/
const KEYWORDS = [
"sales",
"expense reports",
"legal",
"marketing",
"performance reviews",
];
/**
* Handle the OnNewMessageCompose or OnNewAppointmentOrganizer event by verifying that keywords have corresponding
* color categories when a new message or appointment is created. If no corresponding categories exist, they will be
* created.
* @param {Office.AddinCommands.Event} event The OnNewMessageCompose or OnNewAppointmentOrganizer event object.
*/
function onItemComposeHandler(event) {
Office.context.mailbox.masterCategories.getAsync(
{ asyncContext: event },
(asyncResult) => {
let event = asyncResult.asyncContext;
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log(asyncResult.error.message);
event.completed({
allowEvent: false,
errorMessage: "Failed to configure categories.",
});
return;
}
let categories = asyncResult.value;
let categoriesToBeCreated = [];
if (categories) {
let categoryNamesInUse = getCategoryProperty(categories, "displayName");
let categoryColorsInUse = getCategoryProperty(categories, "color");
categoriesToBeCreated = getCategoriesToBeCreated(
KEYWORDS,
categoryNamesInUse
);
if (categoriesToBeCreated.length > 0) {
categoriesToBeCreated = assignCategoryColors(
categoriesToBeCreated,
categoryColorsInUse
);
}
} else {
categoriesToBeCreated = assignCategoryColors(
getCategoriesToBeCreated(KEYWORDS)
);
}
createCategories(event, categoriesToBeCreated);
event.completed({ allowEvent: true });
}
);
}
/**
* Handle the OnMessageSend or OnAppointmentSend event by verifying that applicable color categories are
* applied to a new message or appointment before it's sent.
* @param {Office.AddinCommands.Event} event The OnMessageSend or OnAppointmentSend event object.
*/
function onItemSendHandler(event) {
Office.context.mailbox.item.subject.getAsync(
{ asyncContext: event },
(asyncResult) => {
let event = asyncResult.asyncContext;
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log(asyncResult.error.message);
event.completed({
allowEvent: false,
errorMessage: "Failed to check the subject for keywords.",
});
return;
}
let subject = asyncResult.value;
let detectedWords = [];
if (subject) {
detectedWords = checkForKeywords(KEYWORDS, subject);
}
let options = {
asyncContext: { callingEvent: event, keywordArray: detectedWords },
};
Office.context.mailbox.item.body.getAsync(
"text",
options,
(asyncResult) => {
let event = asyncResult.asyncContext.callingEvent;
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log(asyncResult.error.message);
event.completed({
allowEvent: false,
errorMessage: "Failed to check the body for keywords.",
});
return;
}
let body = asyncResult.value;
let detectedWords = asyncResult.asyncContext.keywordArray;
if (body) {
detectedWords = checkForKeywords(KEYWORDS, body, detectedWords);
}
if (detectedWords.length > 0) {
checkAppliedCategories(event, detectedWords);
} else {
event.completed({ allowEvent: true });
}
}
);
}
);
}
/**
* Get the property values of existing categories.
* @param {Office.CategoryDetails[]} categories Existing categories in Outlook.
* @param {string} property The property to extract from existing categories. Categories have a display name and a color.
* @returns {string[]} The property's value.
*/
function getCategoryProperty(categories, property) {
let values = [];
categories.forEach((category) => {
values.push(category[property]);
});
return values;
}
/**
* Determine the categories to be created based on existing categories.
* @param {string[]} keywords The keywords that require corresponding categories.
* @param {string[]} existingCategories The display names currently in use by existing categories.
* @returns {string[]} The names of the new categories.
*/
function getCategoriesToBeCreated(keywords, existingCategories = []) {
let categoriesToBeCreated = [];
if (existingCategories.length === 0) {
keywords.forEach((word) => {
categoriesToBeCreated.push(`Office Add-ins Sample: ${word}`);
});
} else {
keywords.forEach((word) => {
if (!existingCategories.includes(`Office Add-ins Sample: ${word}`)) {
categoriesToBeCreated.push(`Office Add-ins Sample: ${word}`);
}
});
}
return categoriesToBeCreated;
}
/**
* Assign a color to a new category based on available colors. If all 25 colors are in use,
* duplicate colors are assigned starting from Preset0.
* @param {string[]} categoriesToBeCreated The names of the new categories.
* @param {string[]} categoryColorsInUse The colors currently in use by existing categories.
* @returns {Office.CategoryDetails[]} The new category objects to be created.
*/
function assignCategoryColors(
categoriesToBeCreated = [],
categoryColorsInUse = []
) {
const totalColors = 25;
if (categoryColorsInUse.length >= totalColors) {
for (let i = 0; i < categoriesToBeCreated.length; i++) {
categoriesToBeCreated[i] = {
displayName: categoriesToBeCreated[i],
color: `Preset${i}`,
};
}
} else {
for (let i = 0; i < categoriesToBeCreated.length; i++) {
for (let j = 0; j < totalColors; j++) {
if (!categoryColorsInUse.includes(`Preset${j}`)) {
categoriesToBeCreated[i] = {
displayName: categoriesToBeCreated[i],
color: `Preset${j}`,
};
categoryColorsInUse.push(`Preset${j}`);
break;
}
}
}
}
return categoriesToBeCreated;
}
/**
* Create categories.
* @param {Office.AddinCommands.Event} event The OnNewMessageCompose or OnNewAppointmentOrganizer event object.
* @param {Office.CategoryDetails[]} categoriesToBeCreated The new category objects to create.
*/
function createCategories(event, categoriesToBeCreated) {
Office.context.mailbox.masterCategories.addAsync(
categoriesToBeCreated,
{ asyncContext: event },
(asyncResult) => {
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log(asyncResult.error.message);
asyncResult.asyncContext.completed({
allowEvent: false,
errorMessage: "Failed to set new categories.",
});
return;
}
}
);
}
/**
* Determine if keywords are present in the message or appointment's subject or body that require corresponding categories.
* @param {string[]} keywords The keywords that require corresponding categories.
* @param {string} text The contents of the subject or body of the message or appointment.
* @param {string[]} detectedWords The keywords found in the message or appointment's subject or body.
* @returns {string[]} Keywords detected in the message or appointment's subject or body that require corresponding categories.
*/
function checkForKeywords(keywords, text, detectedWords = []) {
keywords = new RegExp(keywords.join("|"), "gi");
text = text.toLowerCase();
let keywordsFound = text.match(keywords);
if (keywordsFound) {
checkForDuplicates(keywordsFound, detectedWords);
}
return detectedWords;
}
/**
* Check for duplicate keywords in the message or appointment's subject or body.
* @param {string[]} wordsToCompare The keywords found in the message or appointment's subject or body to compare to the existing
* list of detected keywords.
* @param {string[]} wordList The existing list of detected keywords.
*/
function checkForDuplicates(wordsToCompare = [], wordList = []) {
wordsToCompare.forEach((word) => {
if (!wordList.includes(word)) {
wordList.push(word);
}
});
}
/**
* Determine the categories to be added based on the detected keywords in the message or appointment's subject or body.
* @param {string[]} detectedWords The keywords detected in the message or appointment's subject or body.
* @returns {string[]} The names of the categories to be added to the message or appointment.
*/
function getCategoryName(detectedWords) {
let categories = [];
detectedWords.forEach((word) => {
categories.push(`Office Add-ins Sample: ${word}`);
});
return categories;
}
/**
* Check that the appropriate categories, based on detected keywords in the subject or body, are applied to the
* message or appointment before it's sent.
* @param {Office.AddinCommands.Event} event The OnMessageSend or OnAppointmentSend event object.
* @param {string[]} detectedWords The keywords found in the message or appointment's subject or body.
*/
function checkAppliedCategories(event, detectedWords) {
let options = {
asyncContext: { callingEvent: event, keywordArray: detectedWords },
};
Office.context.mailbox.item.categories.getAsync(options, (asyncResult) => {
let sendEvent = asyncResult.asyncContext.callingEvent;
if (asyncResult.status === Office.AsyncResultStatus.Failed) {
console.log(asyncResult.error.message);
sendEvent.completed({
allowEvent: false,
errorMessage: "Failed to check categories applied to the item.",
});
return;
}
let requiredCategories = getCategoryName(
asyncResult.asyncContext.keywordArray
);
let detectedCategories = asyncResult.value;
if (detectedCategories) {
let detectedCategoryNames = getCategoryProperty(
detectedCategories,
"displayName"
);
let missingCategories = getMissingCategories(
requiredCategories,
detectedCategoryNames
);
if (missingCategories.length > 0) {
let message = `Don't forget to also add the following categories: ${missingCategories.join(", ")}`;
console.log(message);
sendEvent.completed({ allowEvent: false, errorMessage: message });
return;
}
sendEvent.completed({ allowEvent: true });
} else {
let message = `You must assign the following categories before your ${
Office.context.mailbox.item.itemType
} can be sent: ${requiredCategories.join(", ")}`;
console.log(message);
sendEvent.completed({ allowEvent: false, errorMessage: message });
return;
}
});
}
/**
* Get the names of the required categories still missing from the message or appointment.
* @param {string[]} requiredCategories The names of the categories required on the message or appointment before it can be sent.
* @param {string[]} appliedCategories The names of the categories that are currently applied to the message or appointment.
* @returns {string[]} The names of the categories that need to be applied to the message or appointment.
*/
function getMissingCategories(requiredCategories, appliedCategories) {
let missingCategories = requiredCategories.filter(
(category) => !appliedCategories.includes(category)
);
return missingCategories;
}
Office.actions.associate("onMessageComposeHandler", onItemComposeHandler);
Office.actions.associate("onAppointmentComposeHandler", onItemComposeHandler);
Office.actions.associate("onMessageSendHandler", onItemSendHandler);
Office.actions.associate("onAppointmentSendHandler", onItemSendHandler);