-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ReportActionsUtils.js
466 lines (410 loc) · 17.1 KB
/
ReportActionsUtils.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
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
import lodashGet from 'lodash/get';
import _ from 'underscore';
import lodashMerge from 'lodash/merge';
import lodashFindLast from 'lodash/findLast';
import Onyx from 'react-native-onyx';
import moment from 'moment';
import * as CollectionUtils from './CollectionUtils';
import CONST from '../CONST';
import ONYXKEYS from '../ONYXKEYS';
import Log from './Log';
import * as CurrencyUtils from './CurrencyUtils';
import isReportMessageAttachment from './isReportMessageAttachment';
const allReportActions = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
callback: (actions, key) => {
if (!key || !actions) {
return;
}
const reportID = CollectionUtils.extractCollectionItemID(key);
allReportActions[reportID] = actions;
},
});
let isNetworkOffline = false;
Onyx.connect({
key: ONYXKEYS.NETWORK,
callback: (val) => (isNetworkOffline = lodashGet(val, 'isOffline', false)),
});
/**
* @param {Object} reportAction
* @returns {Boolean}
*/
function isCreatedAction(reportAction) {
return lodashGet(reportAction, 'actionName') === CONST.REPORT.ACTIONS.TYPE.CREATED;
}
/**
* @param {Object} reportAction
* @returns {Boolean}
*/
function isDeletedAction(reportAction) {
// A deleted comment has either an empty array or an object with html field with empty string as value
const message = lodashGet(reportAction, 'message', []);
return message.length === 0 || lodashGet(message, [0, 'html']) === '';
}
/**
* @param {Object} reportAction
* @returns {Boolean}
*/
function isMoneyRequestAction(reportAction) {
return lodashGet(reportAction, 'actionName', '') === CONST.REPORT.ACTIONS.TYPE.IOU;
}
/**
* @param {Object} reportAction
* @returns {Boolean}
*/
function hasCommentThread(reportAction) {
return lodashGet(reportAction, 'childType', '') === CONST.REPORT.TYPE.CHAT;
}
/**
* Returns the parentReportAction if the given report is a thread/task.
*
* @param {Object} report
* @returns {Object}
*/
function getParentReportAction(report) {
if (!report || !report.parentReportID || !report.parentReportActionID) {
return {};
}
return lodashGet(allReportActions, [report.parentReportID, report.parentReportActionID], {});
}
/**
* Determines if the given report action is sent money report action by checking for 'pay' type and presence of IOUDetails object.
*
* @param {Object} reportAction
* @returns {Boolean}
*/
function isSentMoneyReportAction(reportAction) {
return (
reportAction &&
reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU &&
lodashGet(reportAction, 'originalMessage.type') === CONST.IOU.REPORT_ACTION_TYPE.PAY &&
_.has(reportAction.originalMessage, 'IOUDetails')
);
}
/**
* Returns the formatted amount of a money request. The request and money sent (from send money flow) have
* currency and amount in IOUDetails object.
*
* @param {Object} reportAction
* @returns {Number}
*/
function getFormattedAmount(reportAction) {
return lodashGet(reportAction, 'originalMessage.type', '') === CONST.IOU.REPORT_ACTION_TYPE.PAY && lodashGet(reportAction, 'originalMessage.IOUDetails', false)
? CurrencyUtils.convertToDisplayString(lodashGet(reportAction, 'originalMessage.IOUDetails.amount', 0), lodashGet(reportAction, 'originalMessage.IOUDetails.currency', ''))
: CurrencyUtils.convertToDisplayString(lodashGet(reportAction, 'originalMessage.amount', 0), lodashGet(reportAction, 'originalMessage.currency', ''));
}
/**
* Returns whether the thread is a transaction thread, which is any thread with IOU parent
* report action from requesting money (type - create) or from sending money (type - pay with IOUDetails field)
*
* @param {Object} parentReportAction
* @returns {Boolean}
*/
function isTransactionThread(parentReportAction) {
const originalMessage = lodashGet(parentReportAction, 'originalMessage', {});
return (
parentReportAction &&
parentReportAction.actionName === CONST.REPORT.ACTIONS.TYPE.IOU &&
(originalMessage.type === CONST.IOU.REPORT_ACTION_TYPE.CREATE || (originalMessage.type === CONST.IOU.REPORT_ACTION_TYPE.PAY && _.has(originalMessage, 'IOUDetails')))
);
}
/**
* Sort an array of reportActions by their created timestamp first, and reportActionID second
* This gives us a stable order even in the case of multiple reportActions created on the same millisecond
*
* @param {Array} reportActions
* @param {Boolean} shouldSortInDescendingOrder
* @returns {Array}
*/
function getSortedReportActions(reportActions, shouldSortInDescendingOrder = false) {
if (!_.isArray(reportActions)) {
throw new Error(`ReportActionsUtils.getSortedReportActions requires an array, received ${typeof reportActions}`);
}
const invertedMultiplier = shouldSortInDescendingOrder ? -1 : 1;
return _.chain(reportActions)
.compact()
.sort((first, second) => {
// First sort by timestamp
if (first.created !== second.created) {
return (first.created < second.created ? -1 : 1) * invertedMultiplier;
}
// Then by action type, ensuring that `CREATED` actions always come first if they have the same timestamp as another action type
if ((first.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED || second.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) && first.actionName !== second.actionName) {
return (first.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED ? -1 : 1) * invertedMultiplier;
}
// Ensure that `REPORTPREVIEW` actions always come after if they have the same timestamp as another action type
if ((first.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW || second.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW) && first.actionName !== second.actionName) {
return (first.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW ? 1 : -1) * invertedMultiplier;
}
// Then fallback on reportActionID as the final sorting criteria. It is a random number,
// but using this will ensure that the order of reportActions with the same created time and action type
// will be consistent across all users and devices
return (first.reportActionID < second.reportActionID ? -1 : 1) * invertedMultiplier;
})
.value();
}
/**
* Finds most recent IOU request action ID.
*
* @param {Array} reportActions
* @returns {String}
*/
function getMostRecentIOURequestActionID(reportActions) {
const iouRequestTypes = [CONST.IOU.REPORT_ACTION_TYPE.CREATE, CONST.IOU.REPORT_ACTION_TYPE.SPLIT];
const iouRequestActions = _.filter(reportActions, (action) => iouRequestTypes.includes(lodashGet(action, 'originalMessage.type')));
if (_.isEmpty(iouRequestActions)) {
return null;
}
const sortedReportActions = getSortedReportActions(iouRequestActions);
return _.last(sortedReportActions).reportActionID;
}
/**
* Returns array of links inside a given report action
*
* @param {Object} reportAction
* @returns {Boolean}
*/
function extractLinksFromMessageHtml(reportAction) {
const htmlContent = lodashGet(reportAction, ['message', 0, 'html']);
// Regex to get link in href prop inside of <a/> component
const regex = /<a\s+(?:[^>]*?\s+)?href="([^"]*)"/gi;
if (!htmlContent) {
return;
}
return _.map([...htmlContent.matchAll(regex)], (match) => match[1]);
}
/**
* Returns true when the report action immediately before the specified index is a comment made by the same actor who who is leaving a comment in the action at the specified index.
* Also checks to ensure that the comment is not too old to be shown as a grouped comment.
*
* @param {Array} reportActions
* @param {Number} actionIndex - index of the comment item in state to check
* @returns {Boolean}
*/
function isConsecutiveActionMadeByPreviousActor(reportActions, actionIndex) {
// Find the next non-pending deletion report action, as the pending delete action means that it is not displayed in the UI, but still is in the report actions list.
// If we are offline, all actions are pending but shown in the UI, so we take the previous action, even if it is a delete.
const previousAction = _.find(_.drop(reportActions, actionIndex + 1), (action) => isNetworkOffline || action.pendingAction !== CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE);
const currentAction = reportActions[actionIndex];
// It's OK for there to be no previous action, and in that case, false will be returned
// so that the comment isn't grouped
if (!currentAction || !previousAction) {
return false;
}
// Comments are only grouped if they happen within 5 minutes of each other
if (new Date(currentAction.created).getTime() - new Date(previousAction.created).getTime() > 300000) {
return false;
}
// Do not group if previous action was a created action
if (previousAction.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED) {
return false;
}
// Do not group if previous or current action was a renamed action
if (previousAction.actionName === CONST.REPORT.ACTIONS.TYPE.RENAMED || currentAction.actionName === CONST.REPORT.ACTIONS.TYPE.RENAMED) {
return false;
}
return currentAction.actorEmail === previousAction.actorEmail;
}
/**
* @param {String} reportID
* @param {Object} [actionsToMerge]
* @return {Object}
*/
function getLastVisibleAction(reportID, actionsToMerge = {}) {
const actions = _.toArray(lodashMerge({}, allReportActions[reportID], actionsToMerge));
const visibleActions = _.filter(actions, (action) => !isDeletedAction(action));
if (_.isEmpty(visibleActions)) {
return {};
}
return _.max(visibleActions, (action) => moment.utc(action.created).valueOf());
}
/**
* @param {String} reportID
* @param {Object} [actionsToMerge]
* @return {String}
*/
function getLastVisibleMessageText(reportID, actionsToMerge = {}) {
const lastVisibleAction = getLastVisibleAction(reportID, actionsToMerge);
const message = lodashGet(lastVisibleAction, ['message', 0], {});
if (isReportMessageAttachment(message)) {
return CONST.ATTACHMENT_MESSAGE_TEXT;
}
if (isCreatedAction(lastVisibleAction)) {
return '';
}
const messageText = lodashGet(message, 'text', '');
return String(messageText).replace(CONST.REGEX.AFTER_FIRST_LINE_BREAK, '').substring(0, CONST.REPORT.LAST_MESSAGE_TEXT_MAX_LENGTH).trim();
}
/**
* Checks if a reportAction is deprecated.
*
* @param {Object} reportAction
* @param {String} key
* @returns {Boolean}
*/
function isReportActionDeprecated(reportAction, key) {
if (!reportAction) {
return true;
}
// HACK ALERT: We're temporarily filtering out any reportActions keyed by sequenceNumber
// to prevent bugs during the migration from sequenceNumber -> reportActionID
if (String(reportAction.sequenceNumber) === key) {
Log.info('Front-end filtered out reportAction keyed by sequenceNumber!', false, reportAction);
return true;
}
return false;
}
/**
* Checks if a reportAction is fit for display, meaning that it's not deprecated, is of a valid
* and supported type, it's not deleted and also not closed.
*
* @param {Object} reportAction
* @param {String} key
* @returns {Boolean}
*/
function shouldReportActionBeVisible(reportAction, key) {
if (isReportActionDeprecated(reportAction, key)) {
return false;
}
if (reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.TASKEDITED) {
return false;
}
// Filter out any unsupported reportAction types
if (
!_.has(CONST.REPORT.ACTIONS.TYPE, reportAction.actionName) &&
!_.contains(_.values(CONST.REPORT.ACTIONS.TYPE.POLICYCHANGELOG), reportAction.actionName) &&
!_.contains(_.values(CONST.REPORT.ACTIONS.TYPE.TASK), reportAction.actionName)
) {
return false;
}
// Ignore closed action here since we're already displaying a footer that explains why the report was closed
if (reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.CLOSED) {
return false;
}
if (lodashGet(reportAction, 'message[0].moderationDecisions[0].decision') === CONST.MODERATION.MODERATOR_DECISION_PENDING_REMOVE) {
return false;
}
// All other actions are displayed except thread parents, deleted, or non-pending actions
const isDeleted = isDeletedAction(reportAction);
const isPending = !_.isEmpty(reportAction.pendingAction);
const isDeletedParentAction = lodashGet(reportAction, ['message', 0, 'isDeletedParentAction'], false);
return !isDeleted || isPending || isDeletedParentAction;
}
/**
* A helper method to filter out report actions keyed by sequenceNumbers.
*
* @param {Object} reportActions
* @returns {Array}
*/
function filterOutDeprecatedReportActions(reportActions) {
return _.filter(reportActions, (reportAction, key) => !isReportActionDeprecated(reportAction, key));
}
/**
* This method returns the report actions that are ready for display in the ReportActionsView.
* The report actions need to be sorted by created timestamp first, and reportActionID second
* to ensure they will always be displayed in the same order (in case multiple actions have the same timestamp).
* This is all handled with getSortedReportActions() which is used by several other methods to keep the code DRY.
*
* @param {Object} reportActions
* @returns {Array}
*/
function getSortedReportActionsForDisplay(reportActions) {
const filteredReportActions = _.filter(reportActions, (reportAction, key) => shouldReportActionBeVisible(reportAction, key));
return getSortedReportActions(filteredReportActions, true);
}
/**
* In some cases, there can be multiple closed report actions in a chat report.
* This method returns the last closed report action so we can always show the correct archived report reason.
* Additionally, archived #admins and #announce do not have the closed report action so we will return null if none is found.
*
* @param {Object} reportActions
* @returns {Object|null}
*/
function getLastClosedReportAction(reportActions) {
// If closed report action is not present, return early
if (!_.some(reportActions, (action) => action.actionName === CONST.REPORT.ACTIONS.TYPE.CLOSED)) {
return null;
}
const filteredReportActions = filterOutDeprecatedReportActions(reportActions);
const sortedReportActions = getSortedReportActions(filteredReportActions);
return lodashFindLast(sortedReportActions, (action) => action.actionName === CONST.REPORT.ACTIONS.TYPE.CLOSED);
}
/**
* @param {Array} onyxData
* @returns {Object} The latest report action in the `onyxData` or `null` if one couldn't be found
*/
function getLatestReportActionFromOnyxData(onyxData) {
const reportActionUpdate = _.find(onyxData, (onyxUpdate) => onyxUpdate.key.startsWith(ONYXKEYS.COLLECTION.REPORT_ACTIONS));
if (!reportActionUpdate) {
return null;
}
const reportActions = _.values(reportActionUpdate.value);
const sortedReportActions = getSortedReportActions(reportActions);
return _.last(sortedReportActions);
}
/**
* Find the transaction associated with this reportAction, if one exists.
*
* @param {String} reportID
* @param {String} reportActionID
* @returns {String|null}
*/
function getLinkedTransactionID(reportID, reportActionID) {
const reportAction = lodashGet(allReportActions, [reportID, reportActionID]);
if (!reportAction || reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.IOU) {
return null;
}
return reportAction.originalMessage.IOUTransactionID;
}
/**
* @param {*} chatReportID
* @param {*} iouReportID
* @returns {Object} The report preview action or `null` if one couldn't be found
*/
function getReportPreviewAction(chatReportID, iouReportID) {
return _.find(
allReportActions[chatReportID],
(reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.REPORTPREVIEW && lodashGet(reportAction, 'originalMessage.linkedReportID') === iouReportID,
);
}
function isCreatedTaskReportAction(reportAction) {
return reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.ADDCOMMENT && _.has(reportAction.originalMessage, 'taskReportID');
}
/**
* A helper method to identify if the message is deleted or not.
*
* @param {Object} reportAction
* @returns {Boolean}
*/
function isMessageDeleted(reportAction) {
return lodashGet(reportAction, 'originalMessage.isDeletedParentAction', false);
}
function isWhisperAction(action) {
return (action.whisperedTo || []).length > 0;
}
export {
getSortedReportActions,
getLastVisibleAction,
getLastVisibleMessageText,
getMostRecentIOURequestActionID,
extractLinksFromMessageHtml,
isDeletedAction,
shouldReportActionBeVisible,
isReportActionDeprecated,
isConsecutiveActionMadeByPreviousActor,
getSortedReportActionsForDisplay,
getLastClosedReportAction,
getLatestReportActionFromOnyxData,
isMoneyRequestAction,
hasCommentThread,
getLinkedTransactionID,
getReportPreviewAction,
isCreatedTaskReportAction,
getParentReportAction,
isTransactionThread,
getFormattedAmount,
isSentMoneyReportAction,
isMessageDeleted,
isWhisperAction,
};