fix(client): proper handling of *TooLong updates

This commit is contained in:
teidesu 2021-04-24 18:51:59 +03:00
parent fa7669f65e
commit 007343fb09

View file

@ -202,12 +202,14 @@ async function _loadDifference(
qts: 0, qts: 0,
}) })
if ( if (diff._ === 'updates.differenceEmpty')
diff._ === 'updates.differenceEmpty' ||
diff._ === 'updates.differenceTooLong'
)
return return
if (diff._ === 'updates.differenceTooLong') {
this._pts = diff.pts
return
}
const state = const state =
diff._ === 'updates.difference' diff._ === 'updates.difference'
? diff.state ? diff.state
@ -232,6 +234,18 @@ async function _loadDifference(
}) })
for (const upd of diff.otherUpdates) { for (const upd of diff.otherUpdates) {
if (upd._ === 'updateChannelTooLong') {
if (upd.pts) {
this._cpts[upd.channelId] = upd.pts
}
await _loadChannelDifference.call(
this,
upd.channelId,
noDispatch
)
continue
}
const cid = extractChannelIdFromUpdate(upd) const cid = extractChannelIdFromUpdate(upd)
const pts = 'pts' in upd ? upd.pts : undefined const pts = 'pts' in upd ? upd.pts : undefined
const ptsCount = 'ptsCount' in upd ? upd.ptsCount : undefined const ptsCount = 'ptsCount' in upd ? upd.ptsCount : undefined
@ -297,6 +311,8 @@ async function _loadChannelDifference(
pts = (await this.storage.getChannelPts(channelId)) ?? 0 pts = (await this.storage.getChannelPts(channelId)) ?? 0
} }
if (!pts) return
for (;;) { for (;;) {
const diff = await this.call({ const diff = await this.call({
_: 'updates.getChannelDifference', _: 'updates.getChannelDifference',
@ -306,16 +322,25 @@ async function _loadChannelDifference(
filter: { _: 'channelMessagesFilterEmpty' }, filter: { _: 'channelMessagesFilterEmpty' },
}) })
if ( if (diff._ === 'updates.channelDifferenceEmpty') return
diff._ === 'updates.channelDifferenceEmpty' ||
diff._ === 'updates.channelDifferenceTooLong'
)
return
await this._cachePeersFrom(diff) await this._cachePeersFrom(diff)
const { users, chats } = createUsersChatsIndex(diff) const { users, chats } = createUsersChatsIndex(diff)
if (diff._ === 'updates.channelDifferenceTooLong') {
if (diff.dialog._ === 'dialog') {
pts = diff.dialog.pts!
}
diff.messages.forEach((message) => {
if (noDispatch && noDispatch.msg[channelId][message.id]) return
this.dispatchUpdate(message, users, chats)
})
break
}
diff.newMessages.forEach((message) => { diff.newMessages.forEach((message) => {
if (noDispatch && noDispatch.msg[channelId][message.id]) return if (noDispatch && noDispatch.msg[channelId][message.id]) return
@ -338,7 +363,8 @@ async function _loadChannelDifference(
pts = diff.pts pts = diff.pts
if (diff.final) break // nice naming bro, final=true means there are more updates
if (!diff.final) break
} }
this._cpts[channelId] = pts this._cpts[channelId] = pts
@ -366,7 +392,9 @@ export function _handleUpdate(
// additionally, locking here blocks updates handling while we are // additionally, locking here blocks updates handling while we are
// loading difference inside update handler. // loading difference inside update handler.
const noDispatchIndex = noDispatch ? _createNoDispatchIndex(update) : undefined const noDispatchIndex = noDispatch
? _createNoDispatchIndex(update)
: undefined
this._updLock this._updLock
.acquire() .acquire()
@ -408,11 +436,12 @@ export function _handleUpdate(
if (upd.pts) { if (upd.pts) {
this._cpts[upd.channelId] = upd.pts this._cpts[upd.channelId] = upd.pts
} }
return await _loadChannelDifference.call( await _loadChannelDifference.call(
this, this,
upd.channelId, upd.channelId,
noDispatchIndex noDispatchIndex
) )
continue
} }
const channelId = extractChannelIdFromUpdate(upd) const channelId = extractChannelIdFromUpdate(upd)
@ -445,14 +474,12 @@ export function _handleUpdate(
if (nextLocalPts < pts) if (nextLocalPts < pts)
if (channelId) { if (channelId) {
// "there's an update gap that must be filled" // "there's an update gap that must be filled"
// same as before, loading diff will also load await _loadChannelDifference.call(
// any of the pending updates, so we don't need
// to bother handling them further.
return await _loadChannelDifference.call(
this, this,
channelId, channelId,
noDispatchIndex noDispatchIndex
) )
continue
} else { } else {
return await _loadDifference.call(this) return await _loadDifference.call(this)
} }