feat(client): support for forwarding already uploaded media (for photos and documents)

This commit is contained in:
teidesu 2021-04-23 22:36:00 +03:00
parent 70c01a62a3
commit b28e85ca0a
5 changed files with 73 additions and 4 deletions

View file

@ -91,7 +91,9 @@ export async function sendMedia(
let mime = 'application/octet-stream'
const input = media.file
if (typeof input === 'string' && input.match(/^https?:\/\//)) {
if (typeof input === 'object' && tl.isAnyInputMedia(input)) {
inputMedia = input
} else if (typeof input === 'string' && input.match(/^https?:\/\//)) {
inputMedia = {
_: 'inputMediaDocumentExternal',
url: input,
@ -116,7 +118,9 @@ export async function sendMedia(
if ('thumb' in media && media.thumb) {
const t = media.thumb
if (typeof t === 'string' && t.match(/^https?:\/\//)) {
if (typeof t === 'object' && tl.isAnyInputMedia(t)) {
throw new MtCuteArgumentError("Thumbnail can't be InputMedia")
} else if (typeof t === 'string' && t.match(/^https?:\/\//)) {
throw new MtCuteArgumentError("Thumbnail can't be external")
} else if (isUploadedFile(t)) {
thumb = t.inputFile

View file

@ -94,7 +94,9 @@ export async function sendPhoto(
if (!params) params = {}
let media: tl.TypeInputMedia
if (typeof photo === 'string' && photo.match(/^https?:\/\//)) {
if (typeof photo === 'object' && tl.isAnyInputMedia(photo)) {
media = photo
} else if (typeof photo === 'string' && photo.match(/^https?:\/\//)) {
media = {
_: 'inputMediaPhotoExternal',
url: photo,

View file

@ -30,7 +30,7 @@ export type UploadFileLike =
* can also pass {@link UploadedFile} returned from {@link TelegramClient.uploadFile},
* raw `tl.TypeInputFile` and URLs to remote files
*/
export type InputFileLike = UploadFileLike | UploadedFile | tl.TypeInputFile
export type InputFileLike = UploadFileLike | UploadedFile | tl.TypeInputFile | tl.TypeInputMedia
export interface FileDownloadParameters {
/**

View file

@ -3,6 +3,7 @@ import { tl } from '@mtcute/tl'
import { Thumbnail } from './thumbnail'
import { TelegramClient } from '../../client'
import { makeInspectable } from '../utils'
import { InputMediaLike } from './input-media'
/**
* A file that is represented as a document in MTProto.
@ -93,6 +94,36 @@ export class RawDocument extends FileLocation {
getThumbnail(type: string): Thumbnail | null {
return this.thumbnails.find((it) => it.raw.type === type) ?? null
}
/**
* Input media TL object generated from this object,
* to be used inside {@link InputMediaLike} or {@link TelegramClient.sendPhoto}
*/
get inputMediaTl(): tl.TypeInputMedia {
return {
_: 'inputMediaDocument',
id: {
_: 'inputDocument',
id: this.doc.id,
accessHash: this.doc.accessHash,
fileReference: this.doc.fileReference
}
}
}
/**
* Input media object generated from this object,
* to be used with {@link TelegramClient.sendMedia}
*/
get inputMedia(): InputMediaLike {
return {
// type is only really used for creating tl.InputMedia,
// but since we are providing it directly, we can use `auto`
type: 'auto',
file: this.inputMediaTl
// other fields are not needed since it's a forwarded media
}
}
}
/**

View file

@ -4,6 +4,7 @@ import { TelegramClient } from '../../client'
import { MtCuteArgumentError } from '../errors'
import { Thumbnail } from './thumbnail'
import { makeInspectable } from '../utils'
import { InputMediaLike } from './input-media'
/**
* A photo
@ -105,6 +106,37 @@ export class Photo extends FileLocation {
getThumbnail(type: string): Thumbnail | null {
return this.thumbnails.find((it) => it.raw.type === type) ?? null
}
/**
* Input media generated from this object,
* to be used in {@link TelegramClient.sendPhoto}
* and {@link InputMediaLike}
*/
get inputMediaTl(): tl.TypeInputMedia {
return {
_: 'inputMediaPhoto',
id: {
_: 'inputPhoto',
id: this.raw.id,
accessHash: this.raw.accessHash,
fileReference: this.raw.fileReference
}
}
}
/**
* Input media object generated from this object,
* to be used with {@link TelegramClient.sendMedia}
*/
get inputMedia(): InputMediaLike {
return {
// type is only really used for creating tl.InputMedia,
// but since we are providing it directly, we can use `auto`
type: 'auto',
file: this.inputMediaTl
// other fields are not needed since it's a forwarded media
}
}
}
makeInspectable(Photo, ['fileSize', 'dcId', 'width', 'height'])