From 3e3f5454772cd878c9405a90a8a1c3777e98ed02 Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Tue, 1 Feb 2022 11:32:01 +0400 Subject: [PATCH] Display .webm as unsupported in Safari --- src/environment/webmSupport.ts | 6 ++++ src/lib/appManagers/appDocsManager.ts | 10 ++++-- src/lib/appManagers/appMessagesManager.ts | 24 ++++++++++---- src/lib/appManagers/appStickersManager.ts | 39 +++++++++++++++++++++-- 4 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 src/environment/webmSupport.ts diff --git a/src/environment/webmSupport.ts b/src/environment/webmSupport.ts new file mode 100644 index 00000000..9815e12e --- /dev/null +++ b/src/environment/webmSupport.ts @@ -0,0 +1,6 @@ +import { IS_APPLE_MOBILE, IS_SAFARI } from "./userAgent"; + +const IS_WEBM_SUPPORTED = !!document.createElement('video').canPlayType('video/webm') && !IS_SAFARI && !IS_APPLE_MOBILE; + +(window as any).IS_WEBM_SUPPORTED = IS_WEBM_SUPPORTED; +export default IS_WEBM_SUPPORTED; diff --git a/src/lib/appManagers/appDocsManager.ts b/src/lib/appManagers/appDocsManager.ts index c34eb2c4..3a5b15d6 100644 --- a/src/lib/appManagers/appDocsManager.ts +++ b/src/lib/appManagers/appDocsManager.ts @@ -23,6 +23,7 @@ import { MOUNT_CLASS_TO } from '../../config/debug'; import { getFullDate } from '../../helpers/date'; import rootScope from '../rootScope'; import IS_WEBP_SUPPORTED from '../../environment/webpSupport'; +import IS_WEBM_SUPPORTED from '../../environment/webmSupport'; export type MyDocument = Document.document; @@ -97,7 +98,8 @@ export class AppDocsManager { // 'audioPerformer', 'sticker', 'stickerEmoji', 'stickerEmojiRaw', // 'stickerSetInput', 'stickerThumbConverted', 'animated', 'supportsStreaming']); - doc.attributes.forEach(attribute => { + for(let i = 0, length = doc.attributes.length; i < length; ++i) { + const attribute = doc.attributes[i]; switch(attribute._) { case 'documentAttributeFilename': doc.file_name = RichTextProcessor.wrapPlainText(attribute.file_name); @@ -145,6 +147,10 @@ export class AppDocsManager { doc.type = 'sticker'; doc.sticker = 1; } else if(doc.mime_type === 'video/webm') { + if(!IS_WEBM_SUPPORTED) { + return; + } + doc.type = 'sticker'; doc.sticker = 3; doc.animated = true; @@ -165,7 +171,7 @@ export class AppDocsManager { doc.animated = true; break; } - }); + } if(!doc.mime_type) { const ext = (doc.file_name || '').split('.').pop(); diff --git a/src/lib/appManagers/appMessagesManager.ts b/src/lib/appManagers/appMessagesManager.ts index 394b08f8..578c29e0 100644 --- a/src/lib/appManagers/appMessagesManager.ts +++ b/src/lib/appManagers/appMessagesManager.ts @@ -2539,6 +2539,7 @@ export class AppMessagesManager { } */ if(isMessage && message.media) { + let unsupported = false; switch(message.media._) { case 'messageMediaEmpty': { delete message.media; @@ -2547,7 +2548,7 @@ export class AppMessagesManager { case 'messageMediaPhoto': { if(message.media.ttl_seconds) { - message.media = {_: 'messageMediaUnsupported'}; + unsupported = true; } else { message.media.photo = appPhotosManager.savePhoto(message.media.photo, mediaContext); } @@ -2568,9 +2569,14 @@ export class AppMessagesManager { case 'messageMediaDocument': { if(message.media.ttl_seconds) { - message.media = {_: 'messageMediaUnsupported'}; + unsupported = true; } else { - message.media.document = appDocsManager.saveDoc(message.media.document, mediaContext); // 11.04.2020 warning + const originalDoc = message.media.document; + message.media.document = appDocsManager.saveDoc(originalDoc, mediaContext); // 11.04.2020 warning + + if(!message.media.document && originalDoc._ !== 'documentEmpty') { + unsupported = true; + } } break; @@ -2588,17 +2594,23 @@ export class AppMessagesManager { break; */ case 'messageMediaInvoice': { + unsupported = true; message.media = {_: 'messageMediaUnsupported'}; break; } case 'messageMediaUnsupported': { - message.message = ''; - delete message.entities; - delete message.totalEntities; + unsupported = true; break; } } + + if(unsupported) { + message.media = {_: 'messageMediaUnsupported'}; + message.message = ''; + delete message.entities; + delete message.totalEntities; + } } if(!isMessage && message.action) { diff --git a/src/lib/appManagers/appStickersManager.ts b/src/lib/appManagers/appStickersManager.ts index fa26c2db..f11dcf95 100644 --- a/src/lib/appManagers/appStickersManager.ts +++ b/src/lib/appManagers/appStickersManager.ts @@ -20,6 +20,7 @@ import { getEmojiToneIndex } from '../../vendor/emoji'; import RichTextProcessor from '../richtextprocessor'; import assumeType from '../../helpers/assumeType'; import fixBase64String from '../../helpers/fixBase64String'; +import IS_WEBM_SUPPORTED from '../../environment/webmSupport'; const CACHE_TIME = 3600e3; @@ -215,6 +216,7 @@ export class AppStickersManager { method: 'messages.getRecentStickers', processResult: (res) => { assumeType(res); + this.saveStickers(res.stickers); return res; } @@ -379,6 +381,13 @@ export class AppStickersManager { method: 'messages.getFeaturedStickers', processResult: (res) => { assumeType(res); + + forEachReverse(res.sets, (covered, idx, arr) => { + if(covered.set.pFlags.videos && !IS_WEBM_SUPPORTED) { + arr.splice(idx, 1); + } + }); + res.sets.forEach(covered => { this.saveStickerSet({set: covered.set, documents: [], packs: []}, covered.set.id); }); @@ -430,6 +439,12 @@ export class AppStickersManager { processResult: (res) => { assumeType(res); + forEachReverse(res.sets, (covered, idx, arr) => { + if(covered.set.pFlags.videos && !IS_WEBM_SUPPORTED) { + arr.splice(idx, 1); + } + }); + res.sets.forEach(covered => { this.saveStickerSet({set: covered.set, documents: [], packs: []}, covered.set.id); }); @@ -452,7 +467,20 @@ export class AppStickersManager { } public getAllStickers() { - return apiManager.invokeApiHashable({method: 'messages.getAllStickers'}); + return apiManager.invokeApiHashable({ + method: 'messages.getAllStickers', + processResult: (allStickers) => { + assumeType(allStickers); + + forEachReverse(allStickers.sets, (stickerSet, idx, arr) => { + if(stickerSet.pFlags.videos && !IS_WEBM_SUPPORTED) { + arr.splice(idx, 1); + } + }); + + return allStickers; + } + }); } public preloadStickerSets() { @@ -471,7 +499,8 @@ export class AppStickersManager { method: 'messages.getStickers', params: { emoticon - } + }, + processResult: (stickers) => stickers }), includeOurStickers ? this.preloadStickerSets() : [], includeOurStickers ? this.getRecentStickers() : undefined @@ -519,6 +548,12 @@ export class AppStickersManager { const stickers = [...new Set(cachedStickersAnimated.concat(cachedStickersStatic, foundStickers))]/* .filter(doc => !doc.animated) */; + forEachReverse(stickers, (sticker, idx, arr) => { + if(sticker.sticker === 3 && !IS_WEBM_SUPPORTED) { + arr.splice(idx, 1); + } + }); + return stickers; }); }