refactor(client): extracted users/chats index creation to function

This commit is contained in:
teidesu 2021-04-08 20:23:23 +03:00
parent 35dc5561a1
commit 91dbba948e
4 changed files with 19 additions and 18 deletions

View file

@ -1,6 +1,7 @@
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { Message, MtCuteTypeAssertionError } from '../../types' import { Message, MtCuteTypeAssertionError } from '../../types'
import { createUsersChatsIndex } from '../../utils/peer-utils'
/** @internal */ /** @internal */
export function _findMessageInUpdate( export function _findMessageInUpdate(
@ -20,10 +21,7 @@ export function _findMessageInUpdate(
u._ === 'updateNewChannelMessage' || u._ === 'updateNewChannelMessage' ||
u._ === 'updateNewScheduledMessage' u._ === 'updateNewScheduledMessage'
) { ) {
const users: Record<number, tl.TypeUser> = {} const { users, chats } = createUsersChatsIndex(res)
const chats: Record<number, tl.TypeChat> = {}
res.users.forEach((e) => (users[e.id] = e))
res.chats.forEach((e) => (chats[e.id] = e))
return new Message( return new Message(
this, this,

View file

@ -1,6 +1,6 @@
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { MaybeArray } from '@mtcute/core' import { MaybeArray } from '@mtcute/core'
import { normalizeToInputChannel } from '../../utils/peer-utils' import { createUsersChatsIndex, normalizeToInputChannel } from '../../utils/peer-utils'
import { tl } from '@mtcute/tl' import { tl } from '@mtcute/tl'
import { Message, InputPeerLike, MtCuteTypeAssertionError } from '../../types' import { Message, InputPeerLike, MtCuteTypeAssertionError } from '../../types'
@ -81,10 +81,7 @@ export async function getMessages(
res._ res._
) )
const users: Record<number, tl.TypeUser> = {} const { users, chats } = createUsersChatsIndex(res)
const chats: Record<number, tl.TypeChat> = {}
res.users.forEach((e) => (users[e.id] = e))
res.chats.forEach((e) => (chats[e.id] = e))
const ret = res.messages.map((msg) => new Message(this, msg, users, chats)) const ret = res.messages.map((msg) => new Message(this, msg, users, chats))

View file

@ -2,7 +2,7 @@ import { tl } from '@mtcute/tl'
import { TelegramClient } from '../../client' import { TelegramClient } from '../../client'
import { ChannelPrivateError } from '@mtcute/tl/errors' import { ChannelPrivateError } from '@mtcute/tl/errors'
import { MAX_CHANNEL_ID } from '@mtcute/core' 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' import { extractChannelIdFromUpdate } from '../../utils/misc-utils'
const debug = require('debug')('mtcute:upds') const debug = require('debug')('mtcute:upds')
@ -21,10 +21,7 @@ export function _handleUpdate(
if (update._ === 'updates' || update._ === 'updatesCombined') { if (update._ === 'updates' || update._ === 'updatesCombined') {
const isMin = await this._cachePeersFrom(update) const isMin = await this._cachePeersFrom(update)
const users: Record<number, tl.TypeUser> = {} const { users, chats } = createUsersChatsIndex(update)
const chats: Record<number, tl.TypeChat> = {}
update.users.forEach((u) => (users[u.id] = u))
update.chats.forEach((u) => (chats[u.id] = u))
for (const upd of update.updates) { for (const upd of update.updates) {
if (upd._ === 'updateChannelTooLong') { if (upd._ === 'updateChannelTooLong') {
@ -167,10 +164,7 @@ export function _handleUpdate(
if (diff._ === 'updates.difference') { if (diff._ === 'updates.difference') {
if (diff.newMessages.length) { if (diff.newMessages.length) {
const users: Record<number, tl.TypeUser> = {} const { users, chats } = createUsersChatsIndex(diff)
const chats: Record<number, tl.TypeChat> = {}
diff.users.forEach((u) => (users[u.id] = u))
diff.chats.forEach((u) => (chats[u.id] = u))
await this._dispatchUpdate( await this._dispatchUpdate(
{ {

View file

@ -77,3 +77,15 @@ export function inputPeerToPeer(inp: tl.TypeInputPeer): tl.TypePeer {
return inp as never return inp as never
} }
export function createUsersChatsIndex(obj: { users: tl.TypeUser[], chats: tl.TypeChat[] }): {
users: Record<number, tl.TypeUser>
chats: Record<number, tl.TypeChat>
} {
const users: Record<number, tl.TypeUser> = {}
const chats: Record<number, tl.TypeChat> = {}
obj.users.forEach((e) => (users[e.id] = e))
obj.chats.forEach((e) => (chats[e.id] = e))
return { users, chats }
}