-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathReportActionsUtils.js
287 lines (251 loc) · 10.9 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
import lodashGet from 'lodash/get';
import _ from 'underscore';
import lodashMerge from 'lodash/merge';
import lodashFindLast from 'lodash/findLast';
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
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 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 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']) === '';
}
/**
* 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;
}
// 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 iouRequestActions = _.filter(reportActions, action => lodashGet(action, 'originalMessage.type') === CONST.IOU.REPORT_ACTION_TYPE.CREATE);
if (_.isEmpty(iouRequestActions)) {
return null;
}
const sortedReportActions = getSortedReportActions(iouRequestActions);
return _.last(sortedReportActions).reportActionID;
}
/**
* 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 (moment(currentAction.created).unix() - moment(previousAction.created).unix() > 300) {
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;
}
const htmlText = lodashGet(lastVisibleAction, 'message[0].html', '');
const parser = new ExpensiMark();
const messageText = parser.htmlToText(htmlText);
return String(messageText)
.replace(CONST.REGEX.AFTER_FIRST_LINE_BREAK, '')
.substring(0, CONST.REPORT.LAST_MESSAGE_TEXT_MAX_LENGTH);
}
/**
* 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;
}
// Filter out any unsupported reportAction types
if (!_.has(CONST.REPORT.ACTIONS.TYPE, reportAction.actionName) && !_.contains(_.values(CONST.REPORT.ACTIONS.TYPE.POLICYCHANGELOG), 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;
}
// All other actions are displayed except deleted, non-pending actions
const isDeleted = isDeletedAction(reportAction);
const isPending = !_.isEmpty(reportAction.pendingAction);
return !isDeleted || isPending;
}
/**
* 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);
}
export {
getSortedReportActions,
getLastVisibleAction,
getLastVisibleMessageText,
getMostRecentIOURequestActionID,
isDeletedAction,
shouldReportActionBeVisible,
isReportActionDeprecated,
isConsecutiveActionMadeByPreviousActor,
getSortedReportActionsForDisplay,
getLastClosedReportAction,
getLatestReportActionFromOnyxData,
};