Media: Update alt document handling (#5035)
This commit is contained in:
parent
eda7c3ee77
commit
b059d151c8
@ -118,8 +118,8 @@ export function buildMessageMediaContent(
|
|||||||
if (photo) return { photo };
|
if (photo) return { photo };
|
||||||
|
|
||||||
const video = buildVideo(media);
|
const video = buildVideo(media);
|
||||||
const altVideo = buildAltVideo(media);
|
const altVideos = buildAltVideos(media);
|
||||||
if (video) return { video, altVideo };
|
if (video) return { video, altVideos };
|
||||||
|
|
||||||
const audio = buildAudio(media);
|
const audio = buildAudio(media);
|
||||||
if (audio) return { audio };
|
if (audio) return { audio };
|
||||||
@ -280,16 +280,20 @@ function buildVideo(media: GramJs.TypeMessageMedia): ApiVideo | undefined {
|
|||||||
return buildVideoFromDocument(media.document, media.spoiler);
|
return buildVideoFromDocument(media.document, media.spoiler);
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildAltVideo(media: GramJs.TypeMessageMedia): ApiVideo | undefined {
|
function buildAltVideos(media: GramJs.TypeMessageMedia): ApiVideo[] | undefined {
|
||||||
if (
|
if (!(media instanceof GramJs.MessageMediaDocument) || !media.altDocuments) {
|
||||||
!(media instanceof GramJs.MessageMediaDocument)
|
|
||||||
|| !(media.altDocument instanceof GramJs.Document)
|
|
||||||
|| !media.altDocument.mimeType.startsWith('video')
|
|
||||||
) {
|
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
return buildVideoFromDocument(media.altDocument, media.spoiler);
|
const altVideos = media.altDocuments.filter((d): d is GramJs.Document => (
|
||||||
|
d instanceof GramJs.Document && d.mimeType.startsWith('video')
|
||||||
|
)).map((alt) => buildVideoFromDocument(alt, media.spoiler))
|
||||||
|
.filter(Boolean);
|
||||||
|
if (!altVideos.length) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return altVideos;
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildAudio(media: GramJs.TypeMessageMedia): ApiAudio | undefined {
|
function buildAudio(media: GramJs.TypeMessageMedia): ApiAudio | undefined {
|
||||||
|
|||||||
@ -126,9 +126,11 @@ export function addStoryToLocalDb(story: GramJs.TypeStoryItem, peerId: string) {
|
|||||||
addDocumentToLocalDb(doc);
|
addDocumentToLocalDb(doc);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (story.media.altDocument instanceof GramJs.Document) {
|
if (story.media.altDocuments) {
|
||||||
const doc = addStoryRepairInfo(story.media.altDocument, peerId, story);
|
for (const altDocument of story.media.altDocuments) {
|
||||||
addDocumentToLocalDb(doc);
|
const doc = addStoryRepairInfo(altDocument, peerId, story);
|
||||||
|
addDocumentToLocalDb(doc);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -601,7 +601,7 @@ export type MediaContent = {
|
|||||||
text?: ApiFormattedText;
|
text?: ApiFormattedText;
|
||||||
photo?: ApiPhoto;
|
photo?: ApiPhoto;
|
||||||
video?: ApiVideo;
|
video?: ApiVideo;
|
||||||
altVideo?: ApiVideo;
|
altVideos?: ApiVideo[];
|
||||||
document?: ApiDocument;
|
document?: ApiDocument;
|
||||||
sticker?: ApiSticker;
|
sticker?: ApiSticker;
|
||||||
contact?: ApiContact;
|
contact?: ApiContact;
|
||||||
|
|||||||
@ -101,7 +101,7 @@ function getPreloadMediaHashes(peerId: string, storyId: number) {
|
|||||||
});
|
});
|
||||||
// Thumbnail
|
// Thumbnail
|
||||||
mediaHashes.push({ hash: getStoryMediaHash(story), format: ApiMediaFormat.BlobUrl });
|
mediaHashes.push({ hash: getStoryMediaHash(story), format: ApiMediaFormat.BlobUrl });
|
||||||
if (story.content.altVideo) {
|
if (story.content.altVideos) {
|
||||||
mediaHashes.push({
|
mediaHashes.push({
|
||||||
hash: getStoryMediaHash(story, 'full', true)!,
|
hash: getStoryMediaHash(story, 'full', true)!,
|
||||||
format: ApiMediaFormat.Progressive,
|
format: ApiMediaFormat.Progressive,
|
||||||
|
|||||||
@ -1,8 +1,9 @@
|
|||||||
import type { ApiStory } from '../../api/types';
|
import type { ApiStory, ApiVideo } from '../../api/types';
|
||||||
|
|
||||||
import { getPhotoMediaHash, getVideoMediaHash } from './messageMedia';
|
import { getPhotoMediaHash, getVideoMediaHash } from './messageMedia';
|
||||||
|
|
||||||
type StorySize = 'pictogram' | 'preview' | 'full' | 'download';
|
type StorySize = 'pictogram' | 'preview' | 'full' | 'download';
|
||||||
|
const STORY_ALT_VIDEO_WIDTH = 480;
|
||||||
|
|
||||||
export function getStoryMediaHash(
|
export function getStoryMediaHash(
|
||||||
story: ApiStory, size: StorySize, isAlt: true,
|
story: ApiStory, size: StorySize, isAlt: true,
|
||||||
@ -15,14 +16,22 @@ export function getStoryMediaHash(
|
|||||||
const isVideo = Boolean(story.content.video);
|
const isVideo = Boolean(story.content.video);
|
||||||
|
|
||||||
if (isVideo) {
|
if (isVideo) {
|
||||||
if (isAlt && !story.content.altVideo) return undefined;
|
if (isAlt && !story.content.altVideos) return undefined;
|
||||||
const media = isAlt ? story.content.altVideo! : story.content.video!;
|
const media = isAlt ? getPreferredAlt(story.content.altVideos!) : story.content.video!;
|
||||||
return getVideoMediaHash(media, size);
|
return getVideoMediaHash(media, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
return getPhotoMediaHash(story.content.photo!, size);
|
return getPhotoMediaHash(story.content.photo!, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getPreferredAlt(alts: ApiVideo[]) {
|
||||||
|
const alt = alts.reduce((prev, curr) => (
|
||||||
|
Math.abs((curr.width || 0) - STORY_ALT_VIDEO_WIDTH) < Math.abs((prev.width || 0) - STORY_ALT_VIDEO_WIDTH)
|
||||||
|
? curr : prev
|
||||||
|
));
|
||||||
|
return alt;
|
||||||
|
}
|
||||||
|
|
||||||
export function getStoryKey(chatId: string, storyId: number) {
|
export function getStoryKey(chatId: string, storyId: number) {
|
||||||
return `story${chatId}-${storyId}`;
|
return `story${chatId}-${storyId}`;
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/lib/gramjs/tl/api.d.ts
vendored
10
src/lib/gramjs/tl/api.d.ts
vendored
@ -403,7 +403,7 @@ namespace Api {
|
|||||||
export type TypeAccessPointRule = AccessPointRule;
|
export type TypeAccessPointRule = AccessPointRule;
|
||||||
export type TypeTlsClientHello = TlsClientHello;
|
export type TypeTlsClientHello = TlsClientHello;
|
||||||
export type TypeTlsBlock = TlsBlockString | TlsBlockRandom | TlsBlockZero | TlsBlockDomain | TlsBlockGrease | TlsBlockScope;
|
export type TypeTlsBlock = TlsBlockString | TlsBlockRandom | TlsBlockZero | TlsBlockDomain | TlsBlockGrease | TlsBlockScope;
|
||||||
|
|
||||||
|
|
||||||
export namespace storage {
|
export namespace storage {
|
||||||
export type TypeFileType = storage.FileUnknown | storage.FilePartial | storage.FileJpeg | storage.FileGif | storage.FilePng | storage.FilePdf | storage.FileMp3 | storage.FileMov | storage.FileMp4 | storage.FileWebp;
|
export type TypeFileType = storage.FileUnknown | storage.FilePartial | storage.FileJpeg | storage.FileGif | storage.FilePng | storage.FilePdf | storage.FileMp3 | storage.FileMov | storage.FileMp4 | storage.FileWebp;
|
||||||
@ -1788,7 +1788,7 @@ namespace Api {
|
|||||||
round?: true;
|
round?: true;
|
||||||
voice?: true;
|
voice?: true;
|
||||||
document?: Api.TypeDocument;
|
document?: Api.TypeDocument;
|
||||||
altDocument?: Api.TypeDocument;
|
altDocuments?: Api.TypeDocument[];
|
||||||
ttlSeconds?: int;
|
ttlSeconds?: int;
|
||||||
} | void> {
|
} | void> {
|
||||||
// flags: undefined;
|
// flags: undefined;
|
||||||
@ -1798,7 +1798,7 @@ namespace Api {
|
|||||||
round?: true;
|
round?: true;
|
||||||
voice?: true;
|
voice?: true;
|
||||||
document?: Api.TypeDocument;
|
document?: Api.TypeDocument;
|
||||||
altDocument?: Api.TypeDocument;
|
altDocuments?: Api.TypeDocument[];
|
||||||
ttlSeconds?: int;
|
ttlSeconds?: int;
|
||||||
};
|
};
|
||||||
export class MessageMediaWebPage extends VirtualClass<{
|
export class MessageMediaWebPage extends VirtualClass<{
|
||||||
@ -10791,7 +10791,7 @@ namespace Api {
|
|||||||
}> {
|
}> {
|
||||||
entries: Api.TypeTlsBlock[];
|
entries: Api.TypeTlsBlock[];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export namespace storage {
|
export namespace storage {
|
||||||
export class FileUnknown extends VirtualClass<void> {};
|
export class FileUnknown extends VirtualClass<void> {};
|
||||||
@ -13402,7 +13402,7 @@ namespace Api {
|
|||||||
}>, Api.TypeDestroySessionRes> {
|
}>, Api.TypeDestroySessionRes> {
|
||||||
sessionId: long;
|
sessionId: long;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export namespace auth {
|
export namespace auth {
|
||||||
export class SendCode extends Request<Partial<{
|
export class SendCode extends Request<Partial<{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user