fix(client): support for uploaded media in editInlineMessage

thanks to @pacificescape for pointing out messages.uploadMedia method
This commit is contained in:
teidesu 2021-05-05 18:43:02 +03:00
parent d841188149
commit b409292497
4 changed files with 72 additions and 20 deletions

View file

@ -13,6 +13,7 @@ import {
tdFileId, tdFileId,
} from '@mtcute/file-id' } from '@mtcute/file-id'
import { extractFileName } from '../../utils/file-utils' import { extractFileName } from '../../utils/file-utils'
import { assertTypeIs } from '../../utils/type-assertion'
/** /**
* Normalize an {@link InputMediaLike} to `InputMedia`, * Normalize an {@link InputMediaLike} to `InputMedia`,
@ -25,10 +26,15 @@ export async function _normalizeInputMedia(
media: InputMediaLike, media: InputMediaLike,
params: { params: {
progressCallback?: (uploaded: number, total: number) => void progressCallback?: (uploaded: number, total: number) => void
} },
uploadMedia = false
): Promise<tl.TypeInputMedia> { ): Promise<tl.TypeInputMedia> {
// my condolences to those poor souls who are going to maintain this (myself included) // my condolences to those poor souls who are going to maintain this (myself included)
// thanks to @pacificescape for pointing out messages.uploadMedia method
if (tl.isAnyInputMedia(media)) return media
let inputFile: tl.TypeInputFile | undefined = undefined let inputFile: tl.TypeInputFile | undefined = undefined
let thumb: tl.TypeInputFile | undefined = undefined let thumb: tl.TypeInputFile | undefined = undefined
let mime = 'application/octet-stream' let mime = 'application/octet-stream'
@ -100,11 +106,32 @@ export async function _normalizeInputMedia(
if (!inputFile) throw new Error('should not happen') if (!inputFile) throw new Error('should not happen')
if (media.type === 'photo') { if (media.type === 'photo') {
return { const ret: tl.RawInputMediaUploadedPhoto = {
_: 'inputMediaUploadedPhoto', _: 'inputMediaUploadedPhoto',
file: inputFile, file: inputFile,
ttlSeconds: media.ttlSeconds, ttlSeconds: media.ttlSeconds,
} }
if (!uploadMedia) return ret
const res = await this.call({
_: 'messages.uploadMedia',
peer: { _: 'inputPeerSelf' },
media: ret
})
assertTypeIs('normalizeInputMedia (@ messages.uploadMedia)', res, 'messageMediaPhoto')
assertTypeIs('normalizeInputMedia (@ messages.uploadMedia)', res.photo!, 'photo')
return {
_: 'inputMediaPhoto',
id: {
_: 'inputPhoto',
id: res.photo.id,
accessHash: res.photo.accessHash,
fileReference: res.photo.fileReference
},
ttlSeconds: media.ttlSeconds
}
} }
if ('thumb' in media && media.thumb) { if ('thumb' in media && media.thumb) {
@ -158,7 +185,7 @@ export async function _normalizeInputMedia(
}) })
} }
return { const ret: tl.RawInputMediaUploadedDocument = {
_: 'inputMediaUploadedDocument', _: 'inputMediaUploadedDocument',
nosoundVideo: media.type === 'video' && media.isAnimated, nosoundVideo: media.type === 'video' && media.isAnimated,
forceFile: media.type === 'document', forceFile: media.type === 'document',
@ -168,4 +195,26 @@ export async function _normalizeInputMedia(
attributes, attributes,
ttlSeconds: media.ttlSeconds ttlSeconds: media.ttlSeconds
} }
if (!uploadMedia) return ret
const res = await this.call({
_: 'messages.uploadMedia',
peer: { _: 'inputPeerSelf' },
media: ret
})
assertTypeIs('normalizeInputMedia (@ messages.uploadMedia)', res, 'messageMediaDocument')
assertTypeIs('normalizeInputMedia (@ messages.uploadMedia)', res.document!, 'document')
return {
_: 'inputMediaDocument',
id: {
_: 'inputDocument',
id: res.document.id,
accessHash: res.document.accessHash,
fileReference: res.document.fileReference
},
ttlSeconds: media.ttlSeconds
}
} }

View file

@ -81,21 +81,6 @@ export async function editInlineMessage(
let entities: tl.TypeMessageEntity[] | undefined let entities: tl.TypeMessageEntity[] | undefined
let media: tl.TypeInputMedia | undefined = undefined let media: tl.TypeInputMedia | undefined = undefined
if (params.media) {
media = await this._normalizeInputMedia(params.media, params)
;[content, entities] = await this._parseEntities(
params.media.caption,
params.parseMode,
params.media.entities
)
} else {
;[content, entities] = await this._parseEntities(
params.text,
params.parseMode,
params.entities
)
}
if (typeof id === 'string') { if (typeof id === 'string') {
id = parseInlineMessageId(id) id = parseInlineMessageId(id)
} }
@ -105,11 +90,28 @@ export async function editInlineMessage(
if (!(id.dcId in this._connectionsForInline)) { if (!(id.dcId in this._connectionsForInline)) {
this._connectionsForInline[ this._connectionsForInline[
id.dcId id.dcId
] = await this.createAdditionalConnection(id.dcId) ] = await this.createAdditionalConnection(id.dcId)
} }
connection = this._connectionsForInline[id.dcId] connection = this._connectionsForInline[id.dcId]
} }
if (params.media) {
media = await this._normalizeInputMedia(params.media, params, true)
if ('caption' in params.media) {
;[content, entities] = await this._parseEntities(
params.media.caption,
params.parseMode,
params.media.entities
)
}
} else {
;[content, entities] = await this._parseEntities(
params.text,
params.parseMode,
params.entities
)
}
await this.call( await this.call(
{ {
_: 'messages.editInlineBotMessage', _: 'messages.editInlineBotMessage',

View file

@ -345,7 +345,7 @@ export interface InputInlineResultFile extends BaseInputInlineResult {
/** /**
* Title of the result * Title of the result
*/ */
title?: string title: string
/** /**
* Description of the result * Description of the result

View file

@ -250,6 +250,7 @@ export type InputMediaLike =
| InputMediaVideo | InputMediaVideo
| InputMediaAuto | InputMediaAuto
| InputMediaSticker | InputMediaSticker
| tl.TypeInputMedia
export namespace InputMedia { export namespace InputMedia {
type OmitTypeAndFile<T extends InputMediaLike> = Omit<T, 'type' | 'file'> type OmitTypeAndFile<T extends InputMediaLike> = Omit<T, 'type' | 'file'>