diff --git a/packages/client/src/methods/messages/find-in-update.ts b/packages/client/src/methods/messages/find-in-update.ts index dbaa751e..389d4f8f 100644 --- a/packages/client/src/methods/messages/find-in-update.ts +++ b/packages/client/src/methods/messages/find-in-update.ts @@ -1,6 +1,7 @@ import { TelegramClient } from '../../client' import { tl } from '@mtcute/tl' import { Message, MtCuteTypeAssertionError } from '../../types' +import { createUsersChatsIndex } from '../../utils/peer-utils' /** @internal */ export function _findMessageInUpdate( @@ -20,10 +21,7 @@ export function _findMessageInUpdate( u._ === 'updateNewChannelMessage' || u._ === 'updateNewScheduledMessage' ) { - const users: Record = {} - const chats: Record = {} - res.users.forEach((e) => (users[e.id] = e)) - res.chats.forEach((e) => (chats[e.id] = e)) + const { users, chats } = createUsersChatsIndex(res) return new Message( this, diff --git a/packages/client/src/methods/messages/get-messages.ts b/packages/client/src/methods/messages/get-messages.ts index 66d9e152..9124ec6e 100644 --- a/packages/client/src/methods/messages/get-messages.ts +++ b/packages/client/src/methods/messages/get-messages.ts @@ -1,6 +1,6 @@ import { TelegramClient } from '../../client' import { MaybeArray } from '@mtcute/core' -import { normalizeToInputChannel } from '../../utils/peer-utils' +import { createUsersChatsIndex, normalizeToInputChannel } from '../../utils/peer-utils' import { tl } from '@mtcute/tl' import { Message, InputPeerLike, MtCuteTypeAssertionError } from '../../types' @@ -81,10 +81,7 @@ export async function getMessages( res._ ) - const users: Record = {} - const chats: Record = {} - res.users.forEach((e) => (users[e.id] = e)) - res.chats.forEach((e) => (chats[e.id] = e)) + const { users, chats } = createUsersChatsIndex(res) const ret = res.messages.map((msg) => new Message(this, msg, users, chats)) diff --git a/packages/client/src/methods/updates/handle-update.ts b/packages/client/src/methods/updates/handle-update.ts index 1ad5cf5e..138323e9 100644 --- a/packages/client/src/methods/updates/handle-update.ts +++ b/packages/client/src/methods/updates/handle-update.ts @@ -2,7 +2,7 @@ import { tl } from '@mtcute/tl' import { TelegramClient } from '../../client' import { ChannelPrivateError } from '@mtcute/tl/errors' import { MAX_CHANNEL_ID } from '@mtcute/core' -import { normalizeToInputChannel } from '../../utils/peer-utils' +import { createUsersChatsIndex, normalizeToInputChannel } from '../../utils/peer-utils' import { extractChannelIdFromUpdate } from '../../utils/misc-utils' const debug = require('debug')('mtcute:upds') @@ -21,10 +21,7 @@ export function _handleUpdate( if (update._ === 'updates' || update._ === 'updatesCombined') { const isMin = await this._cachePeersFrom(update) - const users: Record = {} - const chats: Record = {} - update.users.forEach((u) => (users[u.id] = u)) - update.chats.forEach((u) => (chats[u.id] = u)) + const { users, chats } = createUsersChatsIndex(update) for (const upd of update.updates) { if (upd._ === 'updateChannelTooLong') { @@ -167,10 +164,7 @@ export function _handleUpdate( if (diff._ === 'updates.difference') { if (diff.newMessages.length) { - const users: Record = {} - const chats: Record = {} - diff.users.forEach((u) => (users[u.id] = u)) - diff.chats.forEach((u) => (chats[u.id] = u)) + const { users, chats } = createUsersChatsIndex(diff) await this._dispatchUpdate( { diff --git a/packages/client/src/utils/peer-utils.ts b/packages/client/src/utils/peer-utils.ts index e60450d2..2f715ceb 100644 --- a/packages/client/src/utils/peer-utils.ts +++ b/packages/client/src/utils/peer-utils.ts @@ -77,3 +77,15 @@ export function inputPeerToPeer(inp: tl.TypeInputPeer): tl.TypePeer { return inp as never } + +export function createUsersChatsIndex(obj: { users: tl.TypeUser[], chats: tl.TypeChat[] }): { + users: Record + chats: Record +} { + const users: Record = {} + const chats: Record = {} + obj.users.forEach((e) => (users[e.id] = e)) + obj.chats.forEach((e) => (chats[e.id] = e)) + + return { users, chats } +}