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,
} from '@mtcute/file-id'
import { extractFileName } from '../../utils/file-utils'
import { assertTypeIs } from '../../utils/type-assertion'
/**
* Normalize an {@link InputMediaLike} to `InputMedia`,
@ -25,10 +26,15 @@ export async function _normalizeInputMedia(
media: InputMediaLike,
params: {
progressCallback?: (uploaded: number, total: number) => void
}
},
uploadMedia = false
): Promise<tl.TypeInputMedia> {
// 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 thumb: tl.TypeInputFile | undefined = undefined
let mime = 'application/octet-stream'
@ -100,11 +106,32 @@ export async function _normalizeInputMedia(
if (!inputFile) throw new Error('should not happen')
if (media.type === 'photo') {
return {
const ret: tl.RawInputMediaUploadedPhoto = {
_: 'inputMediaUploadedPhoto',
file: inputFile,
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) {
@ -158,7 +185,7 @@ export async function _normalizeInputMedia(
})
}
return {
const ret: tl.RawInputMediaUploadedDocument = {
_: 'inputMediaUploadedDocument',
nosoundVideo: media.type === 'video' && media.isAnimated,
forceFile: media.type === 'document',
@ -168,4 +195,26 @@ export async function _normalizeInputMedia(
attributes,
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 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') {
id = parseInlineMessageId(id)
}
@ -105,11 +90,28 @@ export async function editInlineMessage(
if (!(id.dcId in this._connectionsForInline)) {
this._connectionsForInline[
id.dcId
] = await this.createAdditionalConnection(id.dcId)
] = await this.createAdditionalConnection(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(
{
_: 'messages.editInlineBotMessage',

View file

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

View file

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