From 8504e3bf1484e0539423c5746964ec6050156577 Mon Sep 17 00:00:00 2001 From: teidesu <86301490+teidesu@users.noreply.github.com> Date: Thu, 5 Aug 2021 14:23:51 +0300 Subject: [PATCH] fix(client): fixed qts fetching and storing --- packages/client/src/methods/updates.ts | 61 +++++++++++++++----------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/packages/client/src/methods/updates.ts b/packages/client/src/methods/updates.ts index cb191b6d..ca84d3d8 100644 --- a/packages/client/src/methods/updates.ts +++ b/packages/client/src/methods/updates.ts @@ -71,11 +71,38 @@ function _initializeUpdates(this: TelegramClient) { * @internal */ export async function _fetchUpdatesState(this: TelegramClient): Promise { - const state = await this.call({ _: 'updates.getState' }) + let state = await this.call({ _: 'updates.getState' }) + + // for some unknown fucking reason getState may return old qts + // call getDifference to get actual values :shrug: + loop: for (;;) { + const diff = await this.call({ + _: 'updates.getDifference', + pts: state.pts, + qts: state.qts, + date: state.date, + }) + + switch (diff._) { + case 'updates.differenceEmpty': + break loop + case 'updates.differenceTooLong': // shouldn't happen, but who knows? + ;(state as tl.Mutable).pts = diff.pts + break + case 'updates.differenceSlice': + state = diff.intermediateState + break + default: + state = diff.state + break loop + } + } + this._qts = state.qts this._pts = state.pts this._date = state.date this._seq = state.seq + debug( 'loaded initial state: pts=%d, qts=%d, date=%d, seq=%d', state.pts, @@ -134,7 +161,7 @@ export async function _saveStorage( if (this._oldPts === undefined || this._oldPts !== this._pts) await this.storage.setUpdatesPts(this._pts) if (this._oldQts === undefined || this._oldQts !== this._qts) - await this.storage.setUpdatesPts(this._qts!) + await this.storage.setUpdatesQts(this._qts!) if (this._oldDate === undefined || this._oldDate !== this._date) await this.storage.setUpdatesDate(this._date!) if (this._oldSeq === undefined || this._oldSeq !== this._seq) @@ -736,10 +763,7 @@ async function _processSingleUpdate( if (!noDispatch) { if (!peers) { // this is a short update, let's fetch cached peers - peers = await _fetchPeersForShort.call( - this, - upd - ) + peers = await _fetchPeersForShort.call(this, upd) if (!peers) { // some peer is not cached. // need to re-fetch the thing, and cache them on the way @@ -859,12 +883,7 @@ export function _handleUpdate( case 'updateShort': { const upd = update.update - await _processSingleUpdate.call( - this, - upd, - null, - noDispatch - ) + await _processSingleUpdate.call(this, upd, null, noDispatch) this._date = update.date @@ -899,15 +918,10 @@ export function _handleUpdate( _: 'updateNewMessage', message, pts: update.pts, - ptsCount: update.ptsCount + ptsCount: update.ptsCount, } - await _processSingleUpdate.call( - this, - upd, - null, - noDispatch - ) + await _processSingleUpdate.call(this, upd, null, noDispatch) break } @@ -940,15 +954,10 @@ export function _handleUpdate( _: 'updateNewMessage', message, pts: update.pts, - ptsCount: update.ptsCount + ptsCount: update.ptsCount, } - await _processSingleUpdate.call( - this, - upd, - null, - noDispatch - ) + await _processSingleUpdate.call(this, upd, null, noDispatch) break }