From c8ceec3c46d05fd5e46d9efd22673718fdb76374 Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Wed, 21 Jul 2021 15:16:15 +0300 Subject: [PATCH] Fix generating random long --- src/helpers/random.ts | 2 +- src/layer.d.ts | 5 ++- src/lib/appManagers/appMessagesManager.ts | 33 +++++++++++--------- src/lib/mtproto/authorizer.ts | 5 +-- src/scripts/in/schema_additional_params.json | 5 ++- 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/helpers/random.ts b/src/helpers/random.ts index ede11304..97345544 100644 --- a/src/helpers/random.ts +++ b/src/helpers/random.ts @@ -16,5 +16,5 @@ export function nextRandomUint(bits: 8 | 16 | 32) { } export function randomLong() { - return '' + nextRandomUint(32) + nextRandomUint(32); + return '' + nextRandomUint(32) + nextRandomUint(32) % 0xFFFFFF; } diff --git a/src/layer.d.ts b/src/layer.d.ts index bdda752d..52645516 100644 --- a/src/layer.d.ts +++ b/src/layer.d.ts @@ -856,7 +856,10 @@ export namespace Message { random_id?: string, rReply?: string, viaBotId?: number, - clear_history?: boolean + clear_history?: boolean, + pending?: boolean, + error?: any, + send?: () => Promise }; export type messageService = { diff --git a/src/lib/appManagers/appMessagesManager.ts b/src/lib/appManagers/appMessagesManager.ts index 09aeec0f..ed264ffc 100644 --- a/src/lib/appManagers/appMessagesManager.ts +++ b/src/lib/appManagers/appMessagesManager.ts @@ -480,7 +480,7 @@ export class AppMessagesManager { }; } - const toggleError = (on: any) => { + const toggleError = (on: boolean) => { if(on) { message.error = true; } else { @@ -525,7 +525,9 @@ export class AppMessagesManager { } */ //this.log('sendText', message.mid); - apiPromise.then((updates: Updates) => { + this.pendingAfterMsgs[peerId] = sentRequestOptions; + + return apiPromise.then((updates: Updates) => { //this.log('sendText sent', message.mid); //if(is(updates, updates._ === 'updateShortSentMessage')) { if(updates._ === 'updateShortSentMessage') { @@ -579,9 +581,7 @@ export class AppMessagesManager { delete this.pendingAfterMsgs[peerId]; } }); - - this.pendingAfterMsgs[peerId] = sentRequestOptions; - } + }; this.beforeMessageSending(message, { isScheduled: !!options.scheduleDate || undefined, @@ -831,7 +831,7 @@ export class AppMessagesManager { _: 'messageMediaDocument', pFlags: {}, document: file - } : media; + } as MessageMedia.messageMediaDocument : media as any; const toggleError = (on: boolean) => { if(on) { @@ -888,6 +888,7 @@ export class AppMessagesManager { this.log('appMessagesManager: sendFile uploaded:', inputFile); } */ + // @ts-ignore delete message.media.preloader; inputFile.name = apiFileName; @@ -1163,7 +1164,7 @@ export class AppMessagesManager { const message = this.generateOutgoingMessage(peerId, options); const replyToMsgId = options.replyToMsgId ? this.getServerMessageId(options.replyToMsgId) : undefined; - let media; + let media: MessageMedia; switch(inputMedia._) { case 'inputMediaPoll': { inputMedia.poll.id = message.id; @@ -1291,7 +1292,9 @@ export class AppMessagesManager { }, sentRequestOptions); } - apiPromise.then((updates) => { + this.pendingAfterMsgs[peerId] = sentRequestOptions; + + return apiPromise.then((updates) => { if(updates.updates) { updates.updates.forEach((update: any) => { if(update._ === 'updateDraftMessage') { @@ -1308,8 +1311,7 @@ export class AppMessagesManager { delete this.pendingAfterMsgs[peerId]; } }); - this.pendingAfterMsgs[peerId] = sentRequestOptions; - } + }; this.beforeMessageSending(message, { isScheduled: !!options.scheduleDate || undefined, @@ -1402,7 +1404,7 @@ export class AppMessagesManager { options.replyToMsgId = options.threadId; } - const message: any = { + const message: Message.message = { _: 'message', id: this.generateTempMessageId(peerId), from_id: this.generateFromId(peerId), @@ -5102,17 +5104,18 @@ export class AppMessagesManager { } private handleReleasingMessage(message: MyMessage) { - if('media' in message) { + const media = (message as Message.message).media; + if(media) { // @ts-ignore - const c = message.media.webpage || message.media; + const c = media.webpage || media; const smth: Photo.photo | MyDocument = c.photo || c.document; if(smth?.file_reference) { referenceDatabase.deleteContext(smth.file_reference, {type: 'message', peerId: message.peerId, messageId: message.mid}); } - if('webpage' in message.media) { - appWebPagesManager.deleteWebPageFromPending(message.media.webpage, message.mid); + if('webpage' in media) { + appWebPagesManager.deleteWebPageFromPending(media.webpage, message.mid); } } } diff --git a/src/lib/mtproto/authorizer.ts b/src/lib/mtproto/authorizer.ts index 1bad3f41..3bd79554 100644 --- a/src/lib/mtproto/authorizer.ts +++ b/src/lib/mtproto/authorizer.ts @@ -534,7 +534,8 @@ export class Authorizer { const newNonceHash1 = (await CryptoWorker.invokeCrypto('sha1-hash', auth.newNonce.concat([1], authKeyAux))).slice(-16); if(!bytesCmp(newNonceHash1, response.new_nonce_hash1)) { - throw new Error('[MT] Set_client_DH_params_answer new_nonce_hash1 mismatch'); + this.log.error('Set_client_DH_params_answer new_nonce_hash1 mismatch', newNonceHash1, response); + throw new Error('new_nonce_hash1 mismatch'); } const serverSalt = bytesXor(auth.newNonce.slice(0, 8), auth.serverNonce.slice(0, 8)); @@ -591,7 +592,7 @@ export class Authorizer { this.cached[dcId] = promise; return await promise; } catch(err) { - if(err.originalError === -404 && auth.localTry <= 3) { + if(/* err.originalError === -404 && */auth.localTry <= 3) { return this.sendReqPQ({ dcId: auth.dcId, nonce: new Uint8Array(16).randomize(), diff --git a/src/scripts/in/schema_additional_params.json b/src/scripts/in/schema_additional_params.json index 1c47cf03..1101bad2 100644 --- a/src/scripts/in/schema_additional_params.json +++ b/src/scripts/in/schema_additional_params.json @@ -53,7 +53,10 @@ {"name": "is_outgoing", "type": "true"}, {"name": "rReply", "type": "string"}, {"name": "viaBotId", "type": "number"}, - {"name": "clear_history", "type": "boolean"} + {"name": "clear_history", "type": "boolean"}, + {"name": "pending", "type": "boolean"}, + {"name": "error", "type": "any"}, + {"name": "send", "type": "() => Promise"} ] }, { "predicate": "messageService",