Media: Update alt document handling (#5035)

This commit is contained in:
zubiden 2024-10-20 18:52:58 +02:00 committed by Alexander Zinchuk
parent eda7c3ee77
commit b059d151c8
6 changed files with 37 additions and 22 deletions

View File

@ -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 {

View File

@ -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);
}
} }
} }
} }

View File

@ -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;

View File

@ -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,

View File

@ -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}`;
} }

View File

@ -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<{