fix(core): correctly apply min peers to local cache
This commit is contained in:
parent
b1a756a8ea
commit
04368ec2a0
1 changed files with 73 additions and 1 deletions
|
@ -168,12 +168,84 @@ export class PeersService extends BaseService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let newComplete = peer
|
||||||
|
|
||||||
|
if ((peer as Extract<typeof peer, { min?: unknown }>).min) {
|
||||||
|
// we need to be careful with saving min peers,
|
||||||
|
// as we only need to update *some* fields of the `complete` object.
|
||||||
|
|
||||||
|
const existing = this._cache.get(peer.id)?.complete ?? await this.getCompleteById(peer.id)
|
||||||
|
if (existing && !(existing as Extract<typeof existing, { min?: unknown }>).min) {
|
||||||
|
if (existing._ === 'channel' && peer._ === 'channel') {
|
||||||
|
// ref: https://corefork.telegram.org/constructor/channel
|
||||||
|
newComplete = {
|
||||||
|
...existing,
|
||||||
|
title: peer.title,
|
||||||
|
megagroup: peer.megagroup,
|
||||||
|
color: peer.color,
|
||||||
|
photo: peer.photo,
|
||||||
|
username: peer.username,
|
||||||
|
usernames: peer.usernames,
|
||||||
|
hasGeo: peer.hasGeo,
|
||||||
|
noforwards: peer.noforwards,
|
||||||
|
emojiStatus: peer.emojiStatus,
|
||||||
|
hasLink: peer.hasLink,
|
||||||
|
slowmodeEnabled: peer.slowmodeEnabled,
|
||||||
|
scam: peer.scam,
|
||||||
|
fake: peer.fake,
|
||||||
|
gigagroup: peer.gigagroup,
|
||||||
|
forum: peer.forum,
|
||||||
|
level: peer.level,
|
||||||
|
restricted: peer.restricted,
|
||||||
|
restrictionReason: peer.restrictionReason,
|
||||||
|
joinToSend: peer.joinToSend,
|
||||||
|
joinRequest: peer.joinRequest,
|
||||||
|
verified: peer.verified,
|
||||||
|
defaultBannedRights: peer.defaultBannedRights,
|
||||||
|
}
|
||||||
|
} else if (existing._ === 'user' && peer._ === 'user') {
|
||||||
|
// ref: https://corefork.telegram.org/constructor/user
|
||||||
|
// all fields except the ones marked "do not apply changes to this field if the min flag is set"
|
||||||
|
// are safe to apply
|
||||||
|
newComplete = {
|
||||||
|
...existing,
|
||||||
|
deleted: peer.deleted,
|
||||||
|
bot: peer.bot,
|
||||||
|
botChatHistory: peer.botChatHistory,
|
||||||
|
botNochats: peer.botNochats,
|
||||||
|
verified: peer.verified,
|
||||||
|
restricted: peer.restricted,
|
||||||
|
botInlineGeo: peer.botInlineGeo,
|
||||||
|
support: peer.support,
|
||||||
|
scam: peer.scam,
|
||||||
|
fake: peer.fake,
|
||||||
|
botAttachMenu: peer.botAttachMenu,
|
||||||
|
premium: peer.premium,
|
||||||
|
storiesUnavailable: peer.storiesUnavailable,
|
||||||
|
contactRequirePremium: peer.contactRequirePremium,
|
||||||
|
botBusiness: peer.botBusiness,
|
||||||
|
botHasMainApp: peer.botHasMainApp,
|
||||||
|
photo: peer.applyMinPhoto ? peer.photo : existing.photo,
|
||||||
|
status: !existing.status || existing.status._ === 'userStatusEmpty' ? peer.status : existing.status,
|
||||||
|
botInfoVersion: peer.botInfoVersion,
|
||||||
|
restrictionReason: peer.restrictionReason,
|
||||||
|
botInlinePlaceholder: peer.botInlinePlaceholder,
|
||||||
|
langCode: peer.langCode,
|
||||||
|
emojiStatus: peer.emojiStatus,
|
||||||
|
color: peer.color,
|
||||||
|
profileColor: peer.profileColor,
|
||||||
|
botActiveUsers: peer.botActiveUsers,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// entity is not cached in memory, or the access hash has changed
|
// entity is not cached in memory, or the access hash has changed
|
||||||
// we need to update it in the DB asap, and also update the in-memory cache
|
// we need to update it in the DB asap, and also update the in-memory cache
|
||||||
await this._peers.store(dto)
|
await this._peers.store(dto)
|
||||||
this._cache.set(peer.id, {
|
this._cache.set(peer.id, {
|
||||||
peer: getInputPeer(dto),
|
peer: getInputPeer(dto),
|
||||||
complete: peer,
|
complete: newComplete,
|
||||||
})
|
})
|
||||||
|
|
||||||
// todo: if (!this._cachedSelf?.isBot) {
|
// todo: if (!this._cachedSelf?.isBot) {
|
||||||
|
|
Loading…
Reference in a new issue