@@ -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
@@ -901,7 +846,7 @@ function fetchAllReports(
901
846
* @param {String } text
902
847
* @param {File } [file]
903
848
*/
904
- function addAction ( reportID , text , file ) {
849
+ function addComment ( reportID , text , file ) {
905
850
// For comments shorter than 10k chars, convert the comment from MD into HTML because that's how it is stored in the database
906
851
// For longer comments, skip parsing and display plaintext for performance reasons. It takes over 40s to parse a 100k long string!!
907
852
const parser = new ExpensiMark ( ) ;
@@ -919,12 +864,12 @@ function addAction(reportID, text, file) {
919
864
: parser . htmlToText ( htmlForNewComment ) ;
920
865
921
866
// Update the report in Onyx to have the new sequence number
922
- Onyx . merge ( ` ${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` , {
867
+ const optimisticReport = {
923
868
maxSequenceNumber : newSequenceNumber ,
924
869
lastMessageTimestamp : moment ( ) . unix ( ) ,
925
870
lastMessageText : ReportUtils . formatReportLastMessageText ( textForNewComment ) ,
926
871
lastActorEmail : currentUserEmail ,
927
- } ) ;
872
+ } ;
928
873
929
874
// Generate a clientID so we can save the optimistic action to storage with the clientID as key. Later, we will
930
875
// remove the optimistic action when we add the real action created in the server. We do this because it's not
@@ -940,12 +885,10 @@ function addAction(reportID, text, file) {
940
885
// Store the optimistic action ID on the report the comment was added to. It will be removed later when refetching
941
886
// report actions in order to clear out any stuck actions (i.e. actions where the client never received a Pusher
942
887
// event, for whatever reason, from the server with the new action data
943
- Onyx . merge ( `${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` , {
944
- optimisticReportActionIDs : [ ...( optimisticReportActionIDs [ reportID ] || [ ] ) , optimisticReportActionID ] ,
945
- } ) ;
888
+ optimisticReport . optimisticReportActionIDs = [ ...( optimisticReportActionIDs [ reportID ] || [ ] ) , optimisticReportActionID ] ;
946
889
947
890
// Optimistically add the new comment to the store before waiting to save it to the server
948
- Onyx . merge ( ` ${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ reportID } ` , {
891
+ const optimisticReportActions = {
949
892
[ optimisticReportActionID ] : {
950
893
actionName : CONST . REPORT . ACTIONS . TYPE . ADDCOMMENT ,
951
894
actorEmail : currentUserEmail ,
@@ -974,41 +917,70 @@ function addAction(reportID, text, file) {
974
917
isFirstItem : false ,
975
918
isAttachment,
976
919
attachmentInfo,
977
- loading : true ,
920
+ isLoading : true ,
978
921
shouldShow : true ,
979
922
} ,
980
- } ) ;
923
+ } ;
981
924
982
- DeprecatedAPI . Report_AddComment ( {
925
+ const parameters = {
983
926
reportID,
984
927
file,
985
928
reportComment : commentText ,
986
929
clientID : optimisticReportActionID ,
987
- persist : true ,
988
- } )
989
- . then ( ( response ) => {
990
- if ( response . jsonCode === 408 ) {
991
- Growl . error ( Localize . translateLocal ( 'reportActionCompose.fileUploadFailed' ) ) ;
992
- Onyx . merge ( `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ reportID } ` , {
993
- [ optimisticReportActionID ] : null ,
994
- } ) ;
995
- console . error ( response . message ) ;
996
- return ;
997
- }
998
-
999
- if ( response . jsonCode === 666 && reportID === conciergeChatReportID ) {
1000
- Growl . error ( Localize . translateLocal ( 'reportActionCompose.blockedFromConcierge' ) ) ;
1001
- Onyx . merge ( `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ reportID } ` , {
1002
- [ optimisticReportActionID ] : null ,
1003
- } ) ;
930
+ } ;
1004
931
1005
- // 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
1006
- User . getUserDetails ( ) ;
1007
- return ;
1008
- }
932
+ const optimisticData = [
933
+ {
934
+ onyxMethod : CONST . ONYX . METHOD . MERGE ,
935
+ key : `${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` ,
936
+ value : optimisticReport ,
937
+ } ,
938
+ {
939
+ onyxMethod : CONST . ONYX . METHOD . MERGE ,
940
+ key : `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ reportID } ` ,
941
+ value : optimisticReportActions ,
942
+ } ,
943
+ ] ;
1009
944
1010
- updateReportWithNewAction ( reportID , response . reportAction ) ;
945
+ // Update the timezone if it's been 5 minutes from the last time the user added a comment
946
+ if ( DateUtils . canUpdateTimezone ( ) ) {
947
+ const timezone = DateUtils . getCurrentTimezone ( ) ;
948
+ parameters . timezone = JSON . stringify ( timezone ) ;
949
+ optimisticData . push ( {
950
+ onyxMethod : CONST . ONYX . METHOD . MERGE ,
951
+ key : ONYXKEYS . MY_PERSONAL_DETAILS ,
952
+ value : { timezone} ,
1011
953
} ) ;
954
+ optimisticData . push ( {
955
+ onyxMethod : CONST . ONYX . METHOD . MERGE ,
956
+ key : ONYXKEYS . PERSONAL_DETAILS ,
957
+ value : { [ currentUserEmail ] : timezone } ,
958
+ } ) ;
959
+ DateUtils . setTimezoneUpdated ( ) ;
960
+ }
961
+
962
+ API . write ( isAttachment ? 'AddAttachment' : 'AddComment' , parameters , {
963
+ optimisticData,
964
+ failureData : [
965
+ {
966
+ onyxMethod : CONST . ONYX . METHOD . MERGE ,
967
+ key : `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ reportID } ` ,
968
+ value : {
969
+ [ optimisticReportActionID ] : {
970
+ isLoading : false ,
971
+ } ,
972
+ } ,
973
+ } ,
974
+ ] ,
975
+ } ) ;
976
+ }
977
+
978
+ /**
979
+ * @param {Number } reportID
980
+ * @param {Object } file
981
+ */
982
+ function addAttachment ( reportID , file ) {
983
+ addComment ( reportID , '' , file ) ;
1012
984
}
1013
985
1014
986
/**
@@ -1522,7 +1494,8 @@ export {
1522
1494
fetchChatReportsByIDs ,
1523
1495
fetchIOUReportByID ,
1524
1496
fetchIOUReportByIDAndUpdateChatReport ,
1525
- addAction ,
1497
+ addComment ,
1498
+ addAttachment ,
1526
1499
updateLastReadActionID ,
1527
1500
updateNotificationPreference ,
1528
1501
setNewMarkerPosition ,
0 commit comments