diff --git a/packages/core/src/highlevel/storage/service/peers.ts b/packages/core/src/highlevel/storage/service/peers.ts index 3ca3b139..b3253de9 100644 --- a/packages/core/src/highlevel/storage/service/peers.ts +++ b/packages/core/src/highlevel/storage/service/peers.ts @@ -168,12 +168,84 @@ export class PeersService extends BaseService { } } + let newComplete = peer + + if ((peer as Extract).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).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 // we need to update it in the DB asap, and also update the in-memory cache await this._peers.store(dto) this._cache.set(peer.id, { peer: getInputPeer(dto), - complete: peer, + complete: newComplete, }) // todo: if (!this._cachedSelf?.isBot) {