fix(client): further improved handling of updateShortSentMessage
This commit is contained in:
parent
54f95bcaa6
commit
17c2edde7e
1 changed files with 59 additions and 20 deletions
|
@ -13,8 +13,10 @@ import {
|
||||||
ReplyMarkup,
|
ReplyMarkup,
|
||||||
UsersIndex,
|
UsersIndex,
|
||||||
MtCuteTypeAssertionError,
|
MtCuteTypeAssertionError,
|
||||||
|
ChatsIndex,
|
||||||
} from '../../types'
|
} from '../../types'
|
||||||
import { getMarkedPeerId } from '@mtcute/core'
|
import { getMarkedPeerId } from '@mtcute/core'
|
||||||
|
import { createDummyUpdate } from '../../utils/updates-utils'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a text message
|
* Send a text message
|
||||||
|
@ -128,6 +130,7 @@ export async function sendText(
|
||||||
_: 'message',
|
_: 'message',
|
||||||
id: res.id,
|
id: res.id,
|
||||||
peerId: inputPeerToPeer(peer),
|
peerId: inputPeerToPeer(peer),
|
||||||
|
fromId: { _: 'peerUser', userId: this._userId! },
|
||||||
message,
|
message,
|
||||||
date: res.date,
|
date: res.date,
|
||||||
out: res.out,
|
out: res.out,
|
||||||
|
@ -135,29 +138,65 @@ export async function sendText(
|
||||||
entities: res.entities,
|
entities: res.entities,
|
||||||
}
|
}
|
||||||
|
|
||||||
this._pts = res.pts
|
this._handleUpdate(createDummyUpdate(res.pts, res.date))
|
||||||
this._date = res.date
|
|
||||||
|
|
||||||
let user = await this.storage.getFullPeerById(
|
|
||||||
getMarkedPeerId(msg.peerId)
|
|
||||||
)
|
|
||||||
if (!user) {
|
|
||||||
user = await this.call({
|
|
||||||
_: 'users.getUsers',
|
|
||||||
id: [normalizeToInputUser(peer)!],
|
|
||||||
}).then((res) => res[0])
|
|
||||||
}
|
|
||||||
if (!user)
|
|
||||||
throw new MtCuteTypeAssertionError(
|
|
||||||
'sendText (@ users.getUsers)',
|
|
||||||
'user',
|
|
||||||
'null'
|
|
||||||
)
|
|
||||||
|
|
||||||
const users: UsersIndex = {}
|
const users: UsersIndex = {}
|
||||||
users[user.id] = user as tl.RawUser
|
const chats: ChatsIndex = {}
|
||||||
|
|
||||||
return new Message(this, msg, users, {})
|
const fetchPeer = async (
|
||||||
|
peer: tl.TypePeer | tl.TypeInputPeer
|
||||||
|
): Promise<void> => {
|
||||||
|
const id = getMarkedPeerId(peer)
|
||||||
|
|
||||||
|
let cached = await this.storage.getFullPeerById(id)
|
||||||
|
if (!cached) {
|
||||||
|
switch (peer._) {
|
||||||
|
case 'inputPeerChat':
|
||||||
|
case 'peerChat':
|
||||||
|
// resolvePeer does not fetch the chat.
|
||||||
|
// we need to do it manually
|
||||||
|
cached = await this.call({
|
||||||
|
_: 'messages.getChats',
|
||||||
|
id: [peer.chatId]
|
||||||
|
}).then((res) => res.chats[0])
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
await this.resolvePeer(peer)
|
||||||
|
cached = await this.storage.getFullPeerById(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cached) {
|
||||||
|
throw new MtCuteTypeAssertionError(
|
||||||
|
'sendText (@ getFullPeerById)',
|
||||||
|
'user | chat',
|
||||||
|
'null'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (cached._) {
|
||||||
|
case 'user':
|
||||||
|
users[cached.id] = cached
|
||||||
|
break
|
||||||
|
case 'chat':
|
||||||
|
case 'chatForbidden':
|
||||||
|
case 'channel':
|
||||||
|
case 'channelForbidden':
|
||||||
|
chats[cached.id] = cached
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
throw new MtCuteTypeAssertionError(
|
||||||
|
'sendText (@ users.getUsers)',
|
||||||
|
'user | chat | channel', // not very accurate, but good enough
|
||||||
|
cached._
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await fetchPeer(peer)
|
||||||
|
await fetchPeer(msg.fromId!)
|
||||||
|
|
||||||
|
return new Message(this, msg, users, chats)
|
||||||
}
|
}
|
||||||
|
|
||||||
return this._findMessageInUpdate(res)
|
return this._findMessageInUpdate(res)
|
||||||
|
|
Loading…
Reference in a new issue