@@ -9,7 +9,6 @@ import * as Pusher from '../Pusher/pusher';
9
9
import LocalNotification from '../Notification/LocalNotification' ;
10
10
import PushNotification from '../Notification/PushNotification' ;
11
11
import * as PersonalDetails from './PersonalDetails' ;
12
- import * as User from './User' ;
13
12
import Navigation from '../Navigation/Navigation' ;
14
13
import * as ActiveClientManager from '../ActiveClientManager' ;
15
14
import Visibility from '../Visibility' ;
@@ -27,6 +26,7 @@ import * as ReportActions from './ReportActions';
27
26
import Growl from '../Growl' ;
28
27
import * as Localize from '../Localize' ;
29
28
import PusherUtils from '../PusherUtils' ;
29
+ import DateUtils from '../DateUtils' ;
30
30
31
31
let currentUserEmail ;
32
32
let currentUserAccountID ;
@@ -532,54 +532,6 @@ function updateReportActionMessage(reportID, sequenceNumber, message) {
532
532
} ) ;
533
533
}
534
534
535
- /**
536
- * Updates a report in the store with a new report action
537
- *
538
- * @param {Number } reportID
539
- * @param {Object } reportAction
540
- * @param {String } [notificationPreference] On what cadence the user would like to be notified
541
- */
542
- function updateReportWithNewAction (
543
- reportID ,
544
- reportAction ,
545
- notificationPreference = CONST . REPORT . NOTIFICATION_PREFERENCE . ALWAYS ,
546
- ) {
547
- const messageHtml = reportAction . actionName === CONST . REPORT . ACTIONS . TYPE . RENAMED
548
- ? lodashGet ( reportAction , 'originalMessage.html' , '' )
549
- : lodashGet ( reportAction , [ 'message' , 0 , 'html' ] , '' ) ;
550
-
551
- const parser = new ExpensiMark ( ) ;
552
- const messageText = parser . htmlToText ( messageHtml ) ;
553
-
554
- const updatedReportObject = {
555
- // Always merge the reportID into Onyx. If the report doesn't exist in Onyx yet, then all the rest of the data will be filled out by handleReportChanged
556
- reportID,
557
- maxSequenceNumber : reportAction . sequenceNumber ,
558
- notificationPreference,
559
- lastMessageTimestamp : reportAction . timestamp ,
560
- lastMessageText : ReportUtils . formatReportLastMessageText ( messageText ) ,
561
- lastActorEmail : reportAction . actorEmail ,
562
- } ;
563
-
564
- Onyx . merge ( `${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` , updatedReportObject ) ;
565
-
566
- const reportActionsToMerge = { } ;
567
- if ( reportAction . clientID ) {
568
- // Remove the optimistic action from the report since we are about to replace it
569
- // with the real one (which has the true sequenceNumber)
570
- reportActionsToMerge [ reportAction . clientID ] = null ;
571
- }
572
-
573
- // Add the action into Onyx
574
- reportActionsToMerge [ reportAction . sequenceNumber ] = {
575
- ...reportAction ,
576
- isAttachment : ReportUtils . isReportMessageAttachment ( lodashGet ( reportAction , [ 'message' , 0 ] , { } ) ) ,
577
- loading : false ,
578
- } ;
579
-
580
- Onyx . merge ( `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ reportID } ` , reportActionsToMerge ) ;
581
- }
582
-
583
535
/**
584
536
* Get the private pusher channel name for a Report.
585
537
*
@@ -604,13 +556,6 @@ function subscribeToUserEvents() {
604
556
return ;
605
557
}
606
558
607
- // Live-update a report's actions when a 'report comment' event is received.
608
- PusherUtils . subscribeToPrivateUserChannelEvent (
609
- Pusher . TYPE . REPORT_COMMENT ,
610
- currentUserAccountID ,
611
- pushJSON => updateReportWithNewAction ( pushJSON . reportID , pushJSON . reportAction , pushJSON . notificationPreference ) ,
612
- ) ;
613
-
614
559
// Live-update a report's actions when an 'edit comment' event is received.
615
560
PusherUtils . subscribeToPrivateUserChannelEvent ( Pusher . TYPE . REPORT_COMMENT_EDIT ,
616
561
currentUserAccountID ,
@@ -621,9 +566,9 @@ function subscribeToUserEvents() {
621
566
* Setup reportComment push notification callbacks.
622
567
*/
623
568
function subscribeToReportCommentPushNotifications ( ) {
624
- PushNotification . onReceived ( PushNotification . TYPE . REPORT_COMMENT , ( { reportID, reportAction } ) => {
569
+ PushNotification . onReceived ( PushNotification . TYPE . REPORT_COMMENT , ( { reportID, onyxData } ) => {
625
570
Log . info ( '[Report] Handled event sent by Airship' , false , { reportID} ) ;
626
- updateReportWithNewAction ( reportID , reportAction ) ;
571
+ Onyx . update ( onyxData ) ;
627
572
} ) ;
628
573
629
574
// Open correct report when push notification is clicked
@@ -904,7 +849,7 @@ function fetchAllReports(
904
849
* @param {String } text
905
850
* @param {File } [file]
906
851
*/
907
- function addAction ( reportID , text , file ) {
852
+ function addComment ( reportID , text , file ) {
908
853
// For comments shorter than 10k chars, convert the comment from MD into HTML because that's how it is stored in the database
909
854
// For longer comments, skip parsing and display plaintext for performance reasons. It takes over 40s to parse a 100k long string!!
910
855
const parser = new ExpensiMark ( ) ;
@@ -922,12 +867,12 @@ function addAction(reportID, text, file) {
922
867
: parser . htmlToText ( htmlForNewComment ) ;
923
868
924
869
// Update the report in Onyx to have the new sequence number
925
- Onyx . merge ( ` ${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` , {
870
+ const optimisticReport = {
926
871
maxSequenceNumber : newSequenceNumber ,
927
872
lastMessageTimestamp : moment ( ) . unix ( ) ,
928
873
lastMessageText : ReportUtils . formatReportLastMessageText ( textForNewComment ) ,
929
874
lastActorEmail : currentUserEmail ,
930
- } ) ;
875
+ } ;
931
876
932
877
// Generate a clientID so we can save the optimistic action to storage with the clientID as key. Later, we will
933
878
// remove the optimistic action when we add the real action created in the server. We do this because it's not
@@ -943,12 +888,10 @@ function addAction(reportID, text, file) {
943
888
// Store the optimistic action ID on the report the comment was added to. It will be removed later when refetching
944
889
// report actions in order to clear out any stuck actions (i.e. actions where the client never received a Pusher
945
890
// event, for whatever reason, from the server with the new action data
946
- Onyx . merge ( `${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` , {
947
- optimisticReportActionIDs : [ ...( optimisticReportActionIDs [ reportID ] || [ ] ) , optimisticReportActionID ] ,
948
- } ) ;
891
+ optimisticReport . optimisticReportActionIDs = [ ...( optimisticReportActionIDs [ reportID ] || [ ] ) , optimisticReportActionID ] ;
949
892
950
893
// Optimistically add the new comment to the store before waiting to save it to the server
951
- Onyx . merge ( ` ${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ reportID } ` , {
894
+ const optimisticReportActions = {
952
895
[ optimisticReportActionID ] : {
953
896
actionName : CONST . REPORT . ACTIONS . TYPE . ADDCOMMENT ,
954
897
actorEmail : currentUserEmail ,
@@ -977,41 +920,70 @@ function addAction(reportID, text, file) {
977
920
isFirstItem : false ,
978
921
isAttachment,
979
922
attachmentInfo,
980
- loading : true ,
923
+ isLoading : true ,
981
924
shouldShow : true ,
982
925
} ,
983
- } ) ;
926
+ } ;
984
927
985
- DeprecatedAPI . Report_AddComment ( {
928
+ const parameters = {
986
929
reportID,
987
930
file,
988
931
reportComment : commentText ,
989
932
clientID : optimisticReportActionID ,
990
- persist : true ,
991
- } )
992
- . then ( ( response ) => {
993
- if ( response . jsonCode === 408 ) {
994
- Growl . error ( Localize . translateLocal ( 'reportActionCompose.fileUploadFailed' ) ) ;
995
- Onyx . merge ( `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ reportID } ` , {
996
- [ optimisticReportActionID ] : null ,
997
- } ) ;
998
- console . error ( response . message ) ;
999
- return ;
1000
- }
1001
-
1002
- if ( response . jsonCode === 666 && reportID === conciergeChatReportID ) {
1003
- Growl . error ( Localize . translateLocal ( 'reportActionCompose.blockedFromConcierge' ) ) ;
1004
- Onyx . merge ( `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ reportID } ` , {
1005
- [ optimisticReportActionID ] : null ,
1006
- } ) ;
933
+ } ;
1007
934
1008
- // The fact that the API is returning this error means the BLOCKED_FROM_CONCIERGE nvp in the user details has changed since the last time we checked, so let's update
1009
- User . getUserDetails ( ) ;
1010
- return ;
1011
- }
935
+ const optimisticData = [
936
+ {
937
+ onyxMethod : CONST . ONYX . METHOD . MERGE ,
938
+ key : `${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` ,
939
+ value : optimisticReport ,
940
+ } ,
941
+ {
942
+ onyxMethod : CONST . ONYX . METHOD . MERGE ,
943
+ key : `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ reportID } ` ,
944
+ value : optimisticReportActions ,
945
+ } ,
946
+ ] ;
1012
947
1013
- updateReportWithNewAction ( reportID , response . reportAction ) ;
948
+ // Update the timezone if it's been 5 minutes from the last time the user added a comment
949
+ if ( DateUtils . canUpdateTimezone ( ) ) {
950
+ const timezone = DateUtils . getCurrentTimezone ( ) ;
951
+ parameters . timezone = JSON . stringify ( timezone ) ;
952
+ optimisticData . push ( {
953
+ onyxMethod : CONST . ONYX . METHOD . MERGE ,
954
+ key : ONYXKEYS . MY_PERSONAL_DETAILS ,
955
+ value : { timezone} ,
1014
956
} ) ;
957
+ optimisticData . push ( {
958
+ onyxMethod : CONST . ONYX . METHOD . MERGE ,
959
+ key : ONYXKEYS . PERSONAL_DETAILS ,
960
+ value : { [ currentUserEmail ] : timezone } ,
961
+ } ) ;
962
+ DateUtils . setTimezoneUpdated ( ) ;
963
+ }
964
+
965
+ API . write ( isAttachment ? 'AddAttachment' : 'AddComment' , parameters , {
966
+ optimisticData,
967
+ failureData : [
968
+ {
969
+ onyxMethod : CONST . ONYX . METHOD . MERGE ,
970
+ key : `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ reportID } ` ,
971
+ value : {
972
+ [ optimisticReportActionID ] : {
973
+ isLoading : false ,
974
+ } ,
975
+ } ,
976
+ } ,
977
+ ] ,
978
+ } ) ;
979
+ }
980
+
981
+ /**
982
+ * @param {Number } reportID
983
+ * @param {Object } file
984
+ */
985
+ function addAttachment ( reportID , file ) {
986
+ addComment ( reportID , '' , file ) ;
1015
987
}
1016
988
1017
989
/**
@@ -1525,7 +1497,8 @@ export {
1525
1497
fetchChatReportsByIDs ,
1526
1498
fetchIOUReportByID ,
1527
1499
fetchIOUReportByIDAndUpdateChatReport ,
1528
- addAction ,
1500
+ addComment ,
1501
+ addAttachment ,
1529
1502
updateLastReadActionID ,
1530
1503
updateNotificationPreference ,
1531
1504
setNewMarkerPosition ,
0 commit comments