fix(client): fixed qts fetching and storing

This commit is contained in:
teidesu 2021-08-05 14:23:51 +03:00
parent 42d7a2286c
commit 8504e3bf14

View file

@ -71,11 +71,38 @@ function _initializeUpdates(this: TelegramClient) {
* @internal * @internal
*/ */
export async function _fetchUpdatesState(this: TelegramClient): Promise<void> { export async function _fetchUpdatesState(this: TelegramClient): Promise<void> {
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<tl.updates.TypeState>).pts = diff.pts
break
case 'updates.differenceSlice':
state = diff.intermediateState
break
default:
state = diff.state
break loop
}
}
this._qts = state.qts this._qts = state.qts
this._pts = state.pts this._pts = state.pts
this._date = state.date this._date = state.date
this._seq = state.seq this._seq = state.seq
debug( debug(
'loaded initial state: pts=%d, qts=%d, date=%d, seq=%d', 'loaded initial state: pts=%d, qts=%d, date=%d, seq=%d',
state.pts, state.pts,
@ -134,7 +161,7 @@ export async function _saveStorage(
if (this._oldPts === undefined || this._oldPts !== this._pts) if (this._oldPts === undefined || this._oldPts !== this._pts)
await this.storage.setUpdatesPts(this._pts) await this.storage.setUpdatesPts(this._pts)
if (this._oldQts === undefined || this._oldQts !== this._qts) 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) if (this._oldDate === undefined || this._oldDate !== this._date)
await this.storage.setUpdatesDate(this._date!) await this.storage.setUpdatesDate(this._date!)
if (this._oldSeq === undefined || this._oldSeq !== this._seq) if (this._oldSeq === undefined || this._oldSeq !== this._seq)
@ -736,10 +763,7 @@ async function _processSingleUpdate(
if (!noDispatch) { if (!noDispatch) {
if (!peers) { if (!peers) {
// this is a short update, let's fetch cached peers // this is a short update, let's fetch cached peers
peers = await _fetchPeersForShort.call( peers = await _fetchPeersForShort.call(this, upd)
this,
upd
)
if (!peers) { if (!peers) {
// some peer is not cached. // some peer is not cached.
// need to re-fetch the thing, and cache them on the way // need to re-fetch the thing, and cache them on the way
@ -859,12 +883,7 @@ export function _handleUpdate(
case 'updateShort': { case 'updateShort': {
const upd = update.update const upd = update.update
await _processSingleUpdate.call( await _processSingleUpdate.call(this, upd, null, noDispatch)
this,
upd,
null,
noDispatch
)
this._date = update.date this._date = update.date
@ -899,15 +918,10 @@ export function _handleUpdate(
_: 'updateNewMessage', _: 'updateNewMessage',
message, message,
pts: update.pts, pts: update.pts,
ptsCount: update.ptsCount ptsCount: update.ptsCount,
} }
await _processSingleUpdate.call( await _processSingleUpdate.call(this, upd, null, noDispatch)
this,
upd,
null,
noDispatch
)
break break
} }
@ -940,15 +954,10 @@ export function _handleUpdate(
_: 'updateNewMessage', _: 'updateNewMessage',
message, message,
pts: update.pts, pts: update.pts,
ptsCount: update.ptsCount ptsCount: update.ptsCount,
} }
await _processSingleUpdate.call( await _processSingleUpdate.call(this, upd, null, noDispatch)
this,
upd,
null,
noDispatch
)
break break
} }