-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathReport.js
More file actions
507 lines (440 loc) · 16.6 KB
/
Report.js
File metadata and controls
507 lines (440 loc) · 16.6 KB
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import moment from 'moment';
import _ from 'underscore';
import lodashGet from 'lodash.get';
import Ion from '../Ion';
import {queueRequest, onReconnect} from '../API';
import IONKEYS from '../../IONKEYS';
import CONFIG from '../../CONFIG';
import * as Pusher from '../Pusher/pusher';
import promiseAllSettled from '../promiseAllSettled';
import ExpensiMark from '../ExpensiMark';
import Notification from '../Notification';
import * as PersonalDetails from './PersonalDetails';
import {redirect} from './App';
import Visibility from '../Visibility';
let currentUserEmail;
let currentUserAccountID;
Ion.connect({
key: IONKEYS.SESSION,
callback: (val) => {
// When signed out, val is undefined
if (val) {
currentUserEmail = val.email;
currentUserAccountID = val.accountID;
}
}
});
let currentURL;
Ion.connect({
key: IONKEYS.CURRENT_URL,
callback: val => currentURL = val,
});
let personalDetails;
Ion.connect({
key: IONKEYS.PERSONAL_DETAILS,
callback: val => personalDetails = val,
});
let myPersonalDetails;
Ion.connect({
key: IONKEYS.MY_PERSONAL_DETAILS,
callback: val => myPersonalDetails = val,
});
// Keeps track of the max sequence number for each report
const reportMaxSequenceNumbers = {};
// List of reportIDs that we define in .env
const configReportIDs = CONFIG.REPORT_IDS.split(',').map(Number);
/**
* Checks the report to see if there are any unread action items
*
* @param {object} report
* @returns {boolean}
*/
function hasUnreadActions(report) {
const usersLastReadActionID = lodashGet(report, [
'reportNameValuePairs',
`lastReadActionID_${currentUserAccountID}`,
]);
if (report.reportActionList.length === 0) {
return false;
}
if (!usersLastReadActionID) {
return true;
}
// Find the most recent sequence number from the report actions
const maxSequenceNumber = reportMaxSequenceNumbers[report.reportID];
if (!maxSequenceNumber) {
return false;
}
// There are unread items if the last one the user has read is less than the highest sequence number we have
return usersLastReadActionID < maxSequenceNumber;
}
/**
* Only store the minimal amount of data in Ion that needs to be stored
* because space is limited
*
* @param {object} report
* @param {number} report.reportID
* @param {string} report.reportName
* @param {object} report.reportNameValuePairs
* @returns {object}
*/
function getSimplifiedReportObject(report) {
return {
reportID: report.reportID,
reportName: report.reportName,
reportNameValuePairs: report.reportNameValuePairs,
isUnread: hasUnreadActions(report),
pinnedReport: configReportIDs.includes(report.reportID),
};
}
/**
* Returns a generated report title based on the participants
*
* @param {array} sharedReportList
* @return {string}
*/
function getChatReportName(sharedReportList) {
return _.chain(sharedReportList)
.map(participant => participant.email)
.filter(participant => participant !== currentUserEmail)
.map(participant => lodashGet(personalDetails, [participant, 'firstName']) || participant)
.value()
.join(', ');
}
/**
* Fetches chat reports when provided a list of
* chat report IDs
*
* @param {Array} chatList
* @return {Promise} only used internally when fetchAll() is called
*/
function fetchChatReportsByIDs(chatList) {
let fetchedReports;
return queueRequest('Get', {
returnValueList: 'reportStuff',
reportIDList: chatList.join(','),
shouldLoadOptionalKeys: true,
})
.then(({reports}) => {
fetchedReports = reports;
// Build array of all participant emails so we can
// get the personal details.
const emails = _.chain(reports)
.pluck('sharedReportList')
.reduce((participants, sharedList) => {
const emailArray = _.map(sharedList, participant => participant.email);
return [...participants, ...emailArray];
}, [])
.filter(email => email !== currentUserEmail)
.unique()
.value();
// Fetch the person details if there are any
if (emails && emails.length !== 0) {
PersonalDetails.getForEmails(emails.join(','));
}
// Process the reports and store them in Ion
const ionPromises = _.map(fetchedReports, (report) => {
const newReport = getSimplifiedReportObject(report);
if (lodashGet(report, 'reportNameValuePairs.type') === 'chat') {
newReport.reportName = getChatReportName(report.sharedReportList);
}
// Merge the data into Ion
Ion.merge(`${IONKEYS.COLLECTION.REPORT}${report.reportID}`, newReport);
});
return Promise.all(ionPromises);
});
}
/**
* Updates a report in the store with a new report action
*
* @param {number} reportID
* @param {object} reportAction
*/
function updateReportWithNewAction(reportID, reportAction) {
const previousMaxSequenceNumber = reportMaxSequenceNumbers[reportID];
const newMaxSequenceNumber = reportAction.sequenceNumber;
const hasNewSequenceNumber = newMaxSequenceNumber > previousMaxSequenceNumber;
// Always merge the reportID into Ion
// If the report doesn't exist in Ion yet, then all the rest of the data will be filled out
// by handleReportChanged
Ion.merge(`${IONKEYS.COLLECTION.REPORT}${reportID}`, {
reportID,
isUnread: hasNewSequenceNumber,
maxSequenceNumber: reportAction.sequenceNumber,
});
// Add the action into Ion
Ion.merge(`${IONKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, {
[reportAction.sequenceNumber]: reportAction,
});
// If this comment is from the current user we don't want to parrot whatever they wrote back to them.
if (reportAction.actorEmail === currentUserEmail) {
console.debug('[NOTIFICATION] No notification because comment is from the currently logged in user');
return;
}
const currentReportID = Number(lodashGet(currentURL.split('/'), [1], 0));
// If we are currently viewing this report do not show a notification.
if (reportID === currentReportID && Visibility.isVisible()) {
console.debug('[NOTIFICATION] No notification because it was a comment for the current report');
return;
}
console.debug('[NOTIFICATION] Creating notification');
Notification.showCommentNotification({
reportAction,
onClick: () => {
// Navigate to this report onClick
redirect(reportID);
}
});
}
/**
* Initialize our pusher subscriptions to listen for new report comments
*/
function subscribeToReportCommentEvents() {
const pusherChannelName = `private-user-accountID-${currentUserAccountID}`;
if (Pusher.isSubscribed(pusherChannelName) || Pusher.isAlreadySubscribing(pusherChannelName)) {
return;
}
Pusher.subscribe(pusherChannelName, 'reportComment', (pushJSON) => {
updateReportWithNewAction(pushJSON.reportID, pushJSON.reportAction);
});
}
/**
* Get all chat reports and provide the proper report name
* by fetching sharedReportList and personalDetails
*
* @returns {Promise} only used internally when fetchAll() is called
*/
function fetchChatReports() {
return queueRequest('Get', {
returnValueList: 'chatList',
})
// The string cast below is necessary as Get rvl='chatList' may return an int
.then(({chatList}) => fetchChatReportsByIDs(String(chatList).split(',')));
}
/**
* Get the actions of a report
*
* @param {number} reportID
*/
function fetchActions(reportID) {
queueRequest('Report_GetHistory', {reportID})
.then((data) => {
const indexedData = _.indexBy(data.history, 'sequenceNumber');
const maxSequenceNumber = _.chain(data.history)
.pluck('sequenceNumber')
.max()
.value();
Ion.merge(`${IONKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, indexedData);
Ion.merge(`${IONKEYS.COLLECTION.REPORT}${reportID}`, {maxSequenceNumber});
});
}
/**
* Get all of our reports
*
* @param {boolean} shouldRedirectToFirstReport this is set to false when the network reconnect
* code runs
* @param {boolean} shouldFetchActions whether or not the actions of the reports should also be fetched
*/
function fetchAll(shouldRedirectToFirstReport = true, shouldFetchActions = false) {
let fetchedReports;
// Request each report one at a time to allow individual reports to fail if access to it is prevented by Auth
const reportFetchPromises = _.map(configReportIDs, reportID => queueRequest('Get', {
returnValueList: 'reportStuff',
reportIDList: reportID,
shouldLoadOptionalKeys: true,
}));
// Chat reports need to be fetched separately than the reports hard-coded in the config
// files. The promise for fetching them is added to the array of promises here so
// that both types of reports (chat reports and hard-coded reports) are fetched in
// parallel
reportFetchPromises.push(fetchChatReports());
promiseAllSettled(reportFetchPromises)
.then((data) => {
fetchedReports = _.compact(_.map(data, (promiseResult) => {
// Grab the report from the promise result which stores it in the `value` key
const report = lodashGet(promiseResult, 'value.reports', {});
// If there is no report found from the promise, return null
// Otherwise, grab the actual report object from the first index in the values array
return _.isEmpty(report) ? null : _.values(report)[0];
}));
// Set the first report ID so that the logged in person can be redirected there
// if they are on the home page
if (shouldRedirectToFirstReport && (currentURL === '/' || currentURL === '/home')) {
const firstReportID = _.first(_.pluck(fetchedReports, 'reportID'));
if (firstReportID) {
redirect(`/${firstReportID}`);
}
}
_.each(fetchedReports, (report) => {
// Merge the data into Ion. Don't use set() here or multiSet() because then that would
// overwrite any existing data (like if they have unread messages)
Ion.merge(`${IONKEYS.COLLECTION.REPORT}${report.reportID}`, getSimplifiedReportObject(report));
if (shouldFetchActions) {
fetchActions(report.reportID);
}
});
});
}
/**
* Get the report ID, and then the actions, for a chat report for a specific
* set of participants
*
* @param {string[]} participants
*/
function fetchOrCreateChatReport(participants) {
let reportID;
if (participants.length < 2) {
throw new Error('fetchOrCreateChatReport() must have at least two participants');
}
queueRequest('CreateChatReport', {
emailList: participants.join(','),
})
.then((data) => {
// Set aside the reportID in a local variable so it can be accessed in the rest of the chain
reportID = data.reportID;
// Make a request to get all the information about the report
return queueRequest('Get', {
returnValueList: 'reportStuff',
reportIDList: reportID,
shouldLoadOptionalKeys: true,
});
})
// Put the report object into Ion
.then((data) => {
const report = data.reports[reportID];
// Store only the absolute bare minimum of data in Ion because space is limited
const newReport = getSimplifiedReportObject(report);
newReport.reportName = getChatReportName(report.sharedReportList);
// Merge the data into Ion. Don't use set() here or multiSet() because then that would
// overwrite any existing data (like if they have unread messages)
Ion.merge(`${IONKEYS.COLLECTION.REPORT}${reportID}`, newReport);
// Redirect the logged in person to the new report
redirect(`/${reportID}`);
});
}
/**
* Add an action item to a report
*
* @param {number} reportID
* @param {string} text
* @param {object} file
*/
function addAction(reportID, text, file) {
const actionKey = `${IONKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`;
// Convert the comment from MD into HTML because that's how it is stored in the database
const parser = new ExpensiMark();
const htmlComment = parser.replace(text);
const isAttachment = _.isEmpty(text) && file !== undefined;
// The new sequence number will be one higher than the highest
let highestSequenceNumber = reportMaxSequenceNumbers[reportID] || 0;
const newSequenceNumber = highestSequenceNumber + 1;
// Update the report in Ion to have the new sequence number
Ion.merge(`${IONKEYS.COLLECTION.REPORT}${reportID}`, {
maxSequenceNumber: newSequenceNumber,
});
// Optimistically add the new comment to the store before waiting to save it to the server
Ion.merge(actionKey, {
[newSequenceNumber]: {
actionName: 'ADDCOMMENT',
actorEmail: currentUserEmail,
person: [
{
style: 'strong',
text: myPersonalDetails.displayName || currentUserEmail,
type: 'TEXT'
}
],
automatic: false,
sequenceNumber: ++highestSequenceNumber,
avatar: myPersonalDetails.avatarURL,
timestamp: moment().unix(),
message: [
{
type: 'COMMENT',
html: isAttachment ? 'Uploading Attachment...' : htmlComment,
// Remove HTML from text when applying optimistic offline comment
text: isAttachment ? 'Uploading Attachment...'
: htmlComment.replace(/<[^>]*>?/gm, ''),
}
],
isFirstItem: false,
isAttachment,
}
});
queueRequest('Report_AddComment', {
reportID,
reportComment: htmlComment,
file
});
}
/**
* Updates the last read action ID on the report. It optimistically makes the change to the store, and then let's the
* network layer handle the delayed write.
*
* @param {number} reportID
* @param {number} sequenceNumber
*/
function updateLastReadActionID(reportID, sequenceNumber) {
const currentMaxSequenceNumber = reportMaxSequenceNumbers[reportID];
if (sequenceNumber < currentMaxSequenceNumber) {
return;
}
// Update the lastReadActionID on the report optimistically
Ion.merge(`${IONKEYS.COLLECTION.REPORT}${reportID}`, {
isUnread: false,
reportNameValuePairs: {
[`lastReadActionID_${currentUserAccountID}`]: sequenceNumber,
}
});
// Mark the report as not having any unread items
queueRequest('Report_SetLastReadActionID', {
accountID: currentUserAccountID,
reportID,
sequenceNumber,
});
}
/**
* Saves the comment left by the user as they are typing. By saving this data the user can switch between chats, close
* tab, refresh etc without worrying about loosing what they typed out.
*
* @param {number} reportID
* @param {string} comment
*/
function saveReportComment(reportID, comment) {
Ion.merge(`${IONKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`, comment);
}
/**
* When a report changes in Ion, this fetches the report from the API if the report doesn't have a name
* and it keeps track of the max sequence number on the report actions.
*
* @param {object} report
*/
function handleReportChanged(report) {
if (!report) {
return;
}
// A report can be missing a name if a comment is received via pusher event
// and the report does not yet exist in Ion (eg. a new DM created with the logged in person)
if (report.reportName === undefined) {
fetchChatReportsByIDs([report.reportID]);
}
// Store the max sequence number for each report
reportMaxSequenceNumbers[report.reportID] = report.maxSequenceNumber;
}
Ion.connect({
key: IONKEYS.COLLECTION.REPORT,
callback: handleReportChanged
});
// When the app reconnects from being offline, fetch all of the reports and their actions
onReconnect(() => {
fetchAll(false, true);
});
export {
fetchAll,
fetchActions,
fetchOrCreateChatReport,
addAction,
updateLastReadActionID,
subscribeToReportCommentEvents,
saveReportComment,
};