Browse Source

Fix afterMessageID

master
morethanwords 4 years ago
parent
commit
2a5f859ed7
  1. 14
      src/lib/appManagers/appMessagesManager.ts
  2. 23
      src/lib/mtproto/apiManager.ts
  3. 43
      src/lib/mtproto/mtprotoworker.ts
  4. 21
      src/lib/mtproto/networker.ts
  5. 2
      src/types.d.ts

14
src/lib/appManagers/appMessagesManager.ts

@ -748,9 +748,9 @@ export class AppMessagesManager { @@ -748,9 +748,9 @@ export class AppMessagesManager {
text = splitted[0];
for(let i = 1; i < splitted.length; ++i) {
//setTimeout(() => {
setTimeout(() => {
this.sendText(peerID, splitted[i], options);
//}, i);
}, i);
}
}
@ -837,7 +837,7 @@ export class AppMessagesManager { @@ -837,7 +837,7 @@ export class AppMessagesManager {
var apiPromise: any;
if(options.viaBotID) {
apiPromise = apiManager.invokeApi('messages.sendInlineBotResult', {
apiPromise = apiManager.invokeApiAfter('messages.sendInlineBotResult', {
peer: appPeersManager.getInputPeerByID(peerID),
random_id: randomID as any,
reply_to_msg_id: replyToMsgID ? appMessagesIDsManager.getMessageLocalID(replyToMsgID) : undefined,
@ -846,7 +846,7 @@ export class AppMessagesManager { @@ -846,7 +846,7 @@ export class AppMessagesManager {
clear_draft: options.clearDraft
}, sentRequestOptions);
} else {
apiPromise = apiManager.invokeApi('messages.sendMessage', {
apiPromise = apiManager.invokeApiAfter('messages.sendMessage', {
no_webpage: options.noWebPage,
peer: appPeersManager.getInputPeerByID(peerID),
message: text,
@ -1731,7 +1731,7 @@ export class AppMessagesManager { @@ -1731,7 +1731,7 @@ export class AppMessagesManager {
let apiPromise: Promise<any>;
if(options.viaBotID) {
apiPromise = apiManager.invokeApi('messages.sendInlineBotResult', {
apiPromise = apiManager.invokeApiAfter('messages.sendInlineBotResult', {
peer: appPeersManager.getInputPeerByID(peerID),
random_id: randomID as any,
reply_to_msg_id: replyToMsgID ? appMessagesIDsManager.getMessageLocalID(replyToMsgID) : undefined,
@ -1740,7 +1740,7 @@ export class AppMessagesManager { @@ -1740,7 +1740,7 @@ export class AppMessagesManager {
clear_draft: options.clearDraft
}, sentRequestOptions);
} else {
apiPromise = apiManager.invokeApi('messages.sendMedia', {
apiPromise = apiManager.invokeApiAfter('messages.sendMedia', {
peer: appPeersManager.getInputPeerByID(peerID),
media: inputMedia,
random_id: randomID as any,
@ -2032,7 +2032,7 @@ export class AppMessagesManager { @@ -2032,7 +2032,7 @@ export class AppMessagesManager {
sentRequestOptions.afterMessageID = this.pendingAfterMsgs[peerID].messageID;
}
const promise = apiManager.invokeApi('messages.forwardMessages', {
const promise = apiManager.invokeApiAfter('messages.forwardMessages', {
from_peer: appPeersManager.getInputPeerByID(-channelID),
id: msgIDs,
random_id: randomIDs as any,

23
src/lib/mtproto/apiManager.ts

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
import AppStorage from '../storage';
import MTPNetworker from './networker';
import MTPNetworker, { MTMessage } from './networker';
import { bytesFromHex, bytesToHex, isObject } from '../bin_utils';
import networkerFactory from './networkerFactory';
//import { telegramMeWebService } from './mtproto';
@ -46,6 +46,8 @@ export class ApiManager { @@ -46,6 +46,8 @@ export class ApiManager {
public telegramMeNotified = false;
private log: ReturnType<typeof logger> = logger('API');
private afterMessageTempIDs: {[tempID: string]: string} = {};
constructor() {
//MtpSingleInstanceService.start();
@ -193,6 +195,13 @@ export class ApiManager { @@ -193,6 +195,13 @@ export class ApiManager {
const deferred = deferredPromise<MethodDeclMap[T]['res']>();
const afterMessageIDTemp = options.afterMessageID;
if(afterMessageIDTemp) {
deferred.finally(() => {
delete this.afterMessageTempIDs[afterMessageIDTemp];
});
}
if(MOUNT_CLASS_TO) {
deferred.finally(() => {
clearInterval(interval);
@ -248,9 +257,15 @@ export class ApiManager { @@ -248,9 +257,15 @@ export class ApiManager {
var cachedNetworker: MTPNetworker;
var stack = (new Error()).stack || 'empty stack';
var performRequest = (networker: MTPNetworker) => {
return (cachedNetworker = networker)
.wrapApiCall(method, params, options)
.then(deferred.resolve, (error: ApiError) => {
if(afterMessageIDTemp) {
options.afterMessageID = this.afterMessageTempIDs[afterMessageIDTemp];
}
const promise = (cachedNetworker = networker).wrapApiCall(method, params, options);
if(options.prepareTempMessageID) {
this.afterMessageTempIDs[options.prepareTempMessageID] = (options as MTMessage).messageID;
}
return promise.then(deferred.resolve, (error: ApiError) => {
//if(!options.ignoreErrors) {
if(error.type != 'FILE_REFERENCE_EXPIRED') {
this.log.error('Error', error.code, error.type, this.baseDcID, dcID, method, params);

43
src/lib/mtproto/mtprotoworker.ts

@ -1,17 +1,18 @@ @@ -1,17 +1,18 @@
import {isObject} from '../utils';
import AppStorage from '../storage';
import MTProtoWorker from 'worker-loader!./mtproto.worker';
import type { MethodDeclMap } from '../../layer';
import type { InvokeApiOptions } from '../../types';
import CryptoWorkerMethods from '../crypto/crypto_methods';
import { logger } from '../logger';
import $rootScope from '../rootScope';
import AppStorage from '../storage';
import { isObject } from '../utils';
import webpWorkerController from '../webp/webpWorkerController';
import MTProtoWorker from 'worker-loader!./mtproto.worker';
import type { DownloadOptions } from './apiFileManager';
import { ApiError } from './apiManager';
import type { ServiceWorkerTask, ServiceWorkerTaskResponse } from './mtproto.service';
import { MethodDeclMap } from '../../layer';
import { MOUNT_CLASS_TO } from './mtproto_config';
import $rootScope from '../rootScope';
import type { MTMessage } from './networker';
import referenceDatabase from './referenceDatabase';
import { ApiError } from './apiManager';
import { InvokeApiOptions } from '../../types';
type Task = {
taskID: number,
@ -24,6 +25,7 @@ const USEWORKERASWORKER = true; @@ -24,6 +25,7 @@ const USEWORKERASWORKER = true;
class ApiManagerProxy extends CryptoWorkerMethods {
public worker: Worker;
public postMessage: (...args: any[]) => void;
private afterMessageIDTemp = 0;
private taskID = 0;
private awaiting: {
@ -206,25 +208,22 @@ class ApiManagerProxy extends CryptoWorkerMethods { @@ -206,25 +208,22 @@ class ApiManagerProxy extends CryptoWorkerMethods {
this.updatesProcessor = callback;
}
public invokeApi<T extends keyof MethodDeclMap>(method: T, params: MethodDeclMap[T]['req'] = {}, options: {
dcID?: number,
timeout?: number,
noErrorBox?: boolean,
fileUpload?: boolean,
ignoreErrors?: boolean,
fileDownload?: boolean,
createNetworker?: boolean,
singleInRequest?: boolean,
startMaxLength?: number,
waitTime?: number,
stopTime?: number,
rawError?: any
} = {}): Promise<MethodDeclMap[T]['res']> {
public invokeApi<T extends keyof MethodDeclMap>(method: T, params: MethodDeclMap[T]['req'] = {}, options: InvokeApiOptions = {}): Promise<MethodDeclMap[T]['res']> {
//console.log('will invokeApi:', method, params, options);
return this.performTaskWorker('invokeApi', method, params, options);
}
public invokeApiAfter<T extends keyof MethodDeclMap>(method: T, params: MethodDeclMap[T]['req'] = {}, options: InvokeApiOptions = {}): Promise<MethodDeclMap[T]['res']> {
let o = options;
o.prepareTempMessageID = '' + ++this.afterMessageIDTemp;
o = {...options};
(options as MTMessage).messageID = o.prepareTempMessageID;
//console.log('will invokeApi:', method, params, options);
return this.performTaskWorker('invokeApi', method, params, o);
}
public setBaseDcID(dcID: number) {
return this.performTaskWorker('setBaseDcID', dcID);
}

21
src/lib/mtproto/networker.ts

@ -6,7 +6,6 @@ import {TLDeserialization, TLSerialization} from './tl_utils'; @@ -6,7 +6,6 @@ import {TLDeserialization, TLSerialization} from './tl_utils';
import CryptoWorker from '../crypto/cryptoworker';
import AppStorage from '../storage';
import Schema from './schema';
import timeManager from './timeManager';
import NetworkerFactory from './networkerFactory';
import { logger, LogLevels } from '../logger';
@ -30,7 +29,7 @@ import Socket from './transports/websocket'; @@ -30,7 +29,7 @@ import Socket from './transports/websocket';
//console.error('networker included!', new Error().stack);
type MessageOptions = InvokeApiOptions & Partial<{
export type MTMessageOptions = InvokeApiOptions & Partial<{
noResponse: true,
longPoll: true,
@ -39,7 +38,7 @@ type MessageOptions = InvokeApiOptions & Partial<{ @@ -39,7 +38,7 @@ type MessageOptions = InvokeApiOptions & Partial<{
messageID: string,
}>;
type Message = InvokeApiOptions & MessageOptions & {
export type MTMessage = InvokeApiOptions & MTMessageOptions & {
msg_id: string,
seq_no: number,
body?: Uint8Array | number[],
@ -76,7 +75,7 @@ export default class MTPNetworker { @@ -76,7 +75,7 @@ export default class MTPNetworker {
private lastServerMessages: Array<string> = [];
private sentMessages: {
[msgID: string]: Message
[msgID: string]: MTMessage
} = {};
private pendingMessages: {[msgID: string]: number} = {};
@ -195,7 +194,7 @@ export default class MTPNetworker { @@ -195,7 +194,7 @@ export default class MTPNetworker {
return seqNo;
}
public wrapMtpCall(method: string, params: any = {}, options: MessageOptions = {}) {
public wrapMtpCall(method: string, params: any = {}, options: MTMessageOptions = {}) {
const serializer = new TLSerialization({mtproto: true});
serializer.storeMethod(method, params);
@ -215,7 +214,7 @@ export default class MTPNetworker { @@ -215,7 +214,7 @@ export default class MTPNetworker {
return this.pushMessage(message, options);
}
public wrapMtpMessage(object: any = {}, options: MessageOptions = {}) {
public wrapMtpMessage(object: any = {}, options: MTMessageOptions = {}) {
const serializer = new TLSerialization({mtproto: true});
serializer.storeObject(object, 'Object');
@ -424,7 +423,7 @@ export default class MTPNetworker { @@ -424,7 +423,7 @@ export default class MTPNetworker {
}
private handleSentEncryptedRequestHTTP(promise: ReturnType<MTPNetworker['sendEncryptedRequest']>, message: Message, noResponseMsgs: string[]) {
private handleSentEncryptedRequestHTTP(promise: ReturnType<MTPNetworker['sendEncryptedRequest']>, message: MTMessage, noResponseMsgs: string[]) {
promise
.then((result) => {
this.toggleOffline(false);
@ -481,7 +480,7 @@ export default class MTPNetworker { @@ -481,7 +480,7 @@ export default class MTPNetworker {
seq_no: number,
body: Uint8Array | number[],
isAPI?: boolean
}, options: MessageOptions = {}) {
}, options: MTMessageOptions = {}) {
return new Promise((resolve, reject) => {
this.sentMessages[message.msg_id] = Object.assign(message, options, {
deferred: {resolve, reject}
@ -587,7 +586,7 @@ export default class MTPNetworker { @@ -587,7 +586,7 @@ export default class MTPNetworker {
if(this.pendingResends.length) {
const resendMsgIDs: Array<string> = this.pendingResends.slice();
const resendOpts: MessageOptions = {
const resendOpts: MTMessageOptions = {
noSchedule: true,
notContentRelated: true,
messageID: '' // will set in wrapMtpMessage->pushMessage
@ -706,7 +705,7 @@ export default class MTPNetworker { @@ -706,7 +705,7 @@ export default class MTPNetworker {
}
});
var containerSentMessage: Message = {
var containerSentMessage: MTMessage = {
msg_id: timeManager.generateID(),
seq_no: this.generateSeqNo(true),
container: true,
@ -776,7 +775,7 @@ export default class MTPNetworker { @@ -776,7 +775,7 @@ export default class MTPNetworker {
});
}
public sendEncryptedRequest(message: Message, options: any = {}) {
public sendEncryptedRequest(message: MTMessage, options: any = {}) {
const self = this;
this.log.debug('Send encrypted', message, options, this.authKeyID);

2
src/types.d.ts vendored

@ -11,9 +11,11 @@ export type InvokeApiOptions = Partial<{ @@ -11,9 +11,11 @@ export type InvokeApiOptions = Partial<{
singleInRequest: true,
startMaxLength: number,
prepareTempMessageID: string,
afterMessageID: string,
resultType: string,
timeout: number,
waitTime: number,
stopTime: number,
rawError: any

Loading…
Cancel
Save