@@ -763,7 +763,7 @@ const actions = {
763
763
* @param {string } replyToId Optional
764
764
* @returns {Promise }
765
765
*/
766
- async sendMessage ( { commit, rootState } , { message, recipientId, replyToId } ) {
766
+ async sendMessage ( { commit, rootState, dispatch } , { message, recipientId, replyToId } ) {
767
767
let id
768
768
try {
769
769
const type = replyToId
@@ -811,16 +811,18 @@ const actions = {
811
811
} )
812
812
} catch ( error ) {
813
813
if ( isAllNodesOfflineError ( error ) ) {
814
+ // if the error is caused by connection we keep the message in PENDING status
815
+ // and try to resend it after the connection is restored
816
+
817
+ // timeout for self deleting out of pending messages
814
818
const timeout = setTimeout ( ( ) => {
815
- commit ( 'updateMessage' , {
816
- id,
817
- status : TS . REJECTED ,
818
- partnerId : recipientId
819
+ dispatch ( 'rejectPendingMessage' , {
820
+ messageId : id ,
821
+ recipientId
819
822
} )
823
+ } , Number ( CryptosInfo . ADM . timeout . message ) )
820
824
821
- commit ( 'deletePendingMessage' , id )
822
- } , + CryptosInfo . ADM . timeout . message )
823
-
825
+ // put the message into pending messages object
824
826
commit ( 'addPendingMessage' , {
825
827
messageId : id ,
826
828
type : MessageType . BASIC_ENCRYPTED_MESSAGE ,
@@ -856,7 +858,10 @@ const actions = {
856
858
* @param {string } replyToId Optional
857
859
* @returns {Promise }
858
860
*/
859
- async sendAttachment ( { commit, rootState } , { files, message, recipientId, replyToId } ) {
861
+ async sendAttachment (
862
+ { commit, rootState, dispatch } ,
863
+ { files, message, recipientId, replyToId }
864
+ ) {
860
865
const recipientPublicKey = await getPublicKey ( recipientId )
861
866
const senderPublicKey = await getPublicKey ( rootState . address )
862
867
@@ -949,24 +954,26 @@ const actions = {
949
954
return res
950
955
} )
951
956
. catch ( ( err ) => {
952
- // update `message.status` to 'REJECTED'
957
+ // update `message.status` to 'REJECTED' if the error is not caused by connection
953
958
if ( ! isAllNodesOfflineError ( err ) ) {
954
959
commit ( 'updateMessage' , {
955
960
id : messageObject . id ,
956
961
status : TS . REJECTED ,
957
962
partnerId : recipientId
958
963
} )
959
964
} else {
965
+ // if the error is caused by connection we keep the message in PENDING status
966
+ // and try to resend it after the connection is restored
967
+
968
+ // timeout for self deleting out of pending messages
960
969
const timeout = setTimeout ( ( ) => {
961
- commit ( 'updateMessage' , {
962
- id : messageObject . id ,
963
- status : TS . REJECTED ,
964
- partnerId : recipientId
970
+ dispatch ( 'rejectPendingMessage' , {
971
+ messageId : messageObject . id ,
972
+ recipientId
965
973
} )
974
+ } , Number ( CryptosInfo . ADM . timeout . attachment ) )
966
975
967
- commit ( 'deletePendingMessage' , messageObject . id )
968
- } , + CryptosInfo . ADM . timeout . attachment )
969
-
976
+ // put the message into pending messages object
970
977
commit ( 'addPendingMessage' , {
971
978
messageId : messageObject . id ,
972
979
type : MessageType . RICH_CONTENT_MESSAGE ,
@@ -992,7 +999,7 @@ const actions = {
992
999
* @param {number } id Message Id
993
1000
* @returns {Promise }
994
1001
*/
995
- resendMessage ( { getters, commit } , { recipientId, messageId } ) {
1002
+ resendMessage ( { getters, commit, dispatch } , { recipientId, messageId } ) {
996
1003
const message = getters . partnerMessageById ( recipientId , messageId )
997
1004
998
1005
// update message status from `rejected` to `sent`
@@ -1020,24 +1027,20 @@ const actions = {
1020
1027
throw new Error ( i18n . global . t ( 'chats.message_rejected' ) )
1021
1028
}
1022
1029
1023
- commit ( 'updateMessage' , {
1024
- id : messageId ,
1025
- realId : res . transactionId ,
1026
- status : TS . REGISTERED ,
1027
- partnerId : recipientId
1030
+ dispatch ( 'registerPendingMessage' , {
1031
+ transactionId : res . transactionId ,
1032
+ messageId,
1033
+ recipientId
1028
1034
} )
1029
1035
1030
1036
return res
1031
1037
} )
1032
1038
. catch ( ( error ) => {
1033
1039
if ( ! isAllNodesOfflineError ( error ) ) {
1034
- commit ( 'updateMessage ', {
1040
+ dispatch ( 'rejectPendingMessage ', {
1035
1041
messageId,
1036
- status : TS . REJECTED ,
1037
- partnerId : recipientId
1042
+ recipientId
1038
1043
} )
1039
-
1040
- commit ( 'deletePendingMessage' , messageId )
1041
1044
}
1042
1045
1043
1046
throw error
@@ -1054,12 +1057,14 @@ const actions = {
1054
1057
* @param {FileData[] } files
1055
1058
* @returns {Promise }
1056
1059
*/
1057
- async resendAttachment ( { getters, commit } , { recipientId, messageId, files } ) {
1060
+ async resendAttachment ( { getters, commit, dispatch } , { recipientId, messageId, files } ) {
1058
1061
const message = getters . partnerMessageById ( recipientId , messageId )
1059
1062
if ( ! message ) {
1060
1063
return Promise . reject ( new Error ( 'Message not found in history' ) )
1061
1064
}
1062
1065
1066
+ // in case the files were not fully uploaded or uploaded incorrectly we
1067
+ // reupload them from scratch to resend a message with attachments
1063
1068
const cidsOld = files . map ( ( file ) => [ file . cid , file . preview ?. cid ] ) . filter ( Boolean )
1064
1069
for ( const [ cid ] of cidsOld ) {
1065
1070
commit ( 'attachment/resetUploadProgress' , { cid } , { root : true } )
@@ -1083,21 +1088,18 @@ const actions = {
1083
1088
}
1084
1089
1085
1090
if ( ! isAllNodesOfflineError ( err ) ) {
1086
- commit ( 'updateMessage' , {
1087
- id : messageId ,
1088
- status : TS . REJECTED ,
1089
- partnerId : recipientId
1091
+ dispatch ( 'rejectPendingMessage' , {
1092
+ messageId,
1093
+ recipientId
1090
1094
} )
1091
1095
1092
- commit ( 'deletePendingMessage' , messageId )
1093
-
1094
1096
throw err
1095
1097
}
1096
1098
}
1097
1099
1098
- const newAsset = message . replyToId
1100
+ const newAsset = message . isReply
1099
1101
? {
1100
- replyto_id : message . replyToId ,
1102
+ replyto_id : message . asset . replyto_id ,
1101
1103
reply_message : attachmentAsset ( files , message . message , uploadData . cids )
1102
1104
}
1103
1105
: attachmentAsset ( files , message . message , uploadData . cids )
@@ -1117,29 +1119,46 @@ const actions = {
1117
1119
if ( ! res . success ) {
1118
1120
throw new Error ( i18n . global . t ( 'chats.message_rejected' ) )
1119
1121
}
1120
- commit ( 'updateMessage' , {
1121
- id : messageId ,
1122
- realId : res . transactionId ,
1123
- status : TS . REGISTERED ,
1124
- partnerId : recipientId
1122
+ dispatch ( 'registerPendingMessage' , {
1123
+ transactionId : res . transactionId ,
1124
+ messageId,
1125
+ recipientId
1125
1126
} )
1126
1127
return res
1127
1128
} )
1128
1129
. catch ( ( err ) => {
1129
1130
if ( ! isAllNodesOfflineError ( err ) ) {
1130
- commit ( 'updateMessage' , {
1131
- id : messageId ,
1132
- status : TS . REJECTED ,
1133
- partnerId : recipientId
1131
+ dispatch ( 'rejectPendingMessage' , {
1132
+ messageId,
1133
+ recipientId
1134
1134
} )
1135
-
1136
- commit ( 'deletePendingMessage' , messageId )
1137
1135
}
1138
1136
1139
1137
throw err
1140
1138
} )
1141
1139
} ,
1142
1140
1141
+ registerPendingMessage ( { commit } , { messageId, recipientId, transactionId } ) {
1142
+ commit ( 'updateMessage' , {
1143
+ id : messageId ,
1144
+ realId : transactionId ,
1145
+ status : TS . REGISTERED ,
1146
+ partnerId : recipientId
1147
+ } )
1148
+
1149
+ commit ( 'deletePendingMessage' , messageId )
1150
+ } ,
1151
+
1152
+ rejectPendingMessage ( { commit } , { messageId, recipientId } ) {
1153
+ commit ( 'updateMessage' , {
1154
+ messageId,
1155
+ status : TS . REJECTED ,
1156
+ partnerId : recipientId
1157
+ } )
1158
+
1159
+ commit ( 'deletePendingMessage' , messageId )
1160
+ } ,
1161
+
1143
1162
/**
1144
1163
* React to a message with emoji.
1145
1164
* After confirmation, `id` and `status` will be updated.
0 commit comments