11import { encryptMessage , decryptMessageLegacy } from 'pmcrypto' ;
22// import { fromUnixTime } from 'date-fns';
33
4- import { LABEL_LEGACY_MESSAGE , QUERY_LEGACY_MESSAGES_MAX_PAGESIZE , UPDATE_MESSAGE_SUCESS_CODE } from '../constants' ;
4+ import { LABEL_LEGACY_MESSAGE , QUERY_LEGACY_MESSAGES_MAX_PAGESIZE , UPDATE_BODY_SUCESS_CODE } from '../constants' ;
55import { chunk } from '../helpers/array' ;
66import { wait } from '../helpers/promise' ;
77import { noop } from '../helpers/function' ;
88
99const fromUnixTime = ( Time = 0 ) => new Date ( Time * 1000 ) ;
1010
1111const LEGACY_MESSAGES_CHUNK_SIZE = 5 ; // how many messages we want to decrypt and encrypt simultaneously
12+ const RELAX_TIME = 5 * 1000 ; // 5s . Time to wait (for other operations) after a batch of legacy messages has been migrated
1213
1314/**
1415 * Given a list of legacy message IDs, fetch, decrypt, re-encrypt and send them to API
1516 * @param {Array<String> } messageIDs List of IDs of legacy messages
1617 * @param {AbortController.signal } signal { aborted: true/false }
17- * @param {Function } obj.getMessage
18- * @param {Function } obj.updateBody
19- * @param {Function } obj.getPrivateKeys
18+ * @param {Function } obj.getMessage Retrive message from database by ID
19+ * @param {Function } obj.updateBody Replace body of a message identified by ID
20+ * @param {Function } obj.getPrivateKeys Get privateKeys corresponding to an address ID
2021 * @param {Function } obj.onError
22+ *
23+ * @return {Object } { noneMigratedInBatch: false is some message from the batch is succesfully migrated. True otherwise }
24+ *
25+ * @dev The keys returned by getPrivateKeys must have the method toPublic()
2126 */
2227const clearSome = async ( messageIDs = [ ] , { signal, getMessage, updateBody, getPrivateKeys, onError = noop } ) => {
2328 if ( ! messageIDs . length ) {
@@ -68,7 +73,7 @@ const clearSome = async (messageIDs = [], { signal, getMessage, updateBody, getP
6873 }
6974 // Send re-encrypted message to API
7075 const { Code } = await updateBody ( { ID , Body : newBody } ) ;
71- if ( Code === UPDATE_MESSAGE_SUCESS_CODE ) {
76+ if ( Code === UPDATE_BODY_SUCESS_CODE ) {
7277 noneMigratedInBatch = false ;
7378 }
7479 return ;
@@ -84,6 +89,15 @@ const clearSome = async (messageIDs = [], { signal, getMessage, updateBody, getP
8489
8590/**
8691 * Fetch legacy messages, re-encrypt and send them to API
92+ * @param {AbortController.signal } signal { aborted: true/false }
93+ * @param {Number } page Legacy messages query parameter
94+ * @param {Function } obj.queryMessages Retrive legacy messages from database
95+ * @param {Function } obj.getMessage Retrive message from database by ID
96+ * @param {Function } obj.updateBody Replace body of a message identified by ID
97+ * @param {Function } obj.getPrivateKeys Get privateKeys corresponding to an address ID
98+ * @param {Function } obj.onError
99+ *
100+ * @dev The keys returned by getPrivateKeys must have the method toPublic()
87101 */
88102export const clearAll = async ( {
89103 signal,
@@ -94,8 +108,6 @@ export const clearAll = async ({
94108 getPrivateKeys,
95109 onError
96110} ) => {
97- const RELAX_TIME = 5 * 1000 ; // 5s
98-
99111 if ( signal . aborted ) {
100112 return ;
101113 }
0 commit comments