mirror of
https://github.com/danog/telegram-tt.git
synced 2024-12-14 10:09:07 +01:00
2 lines
5.7 KiB
JavaScript
2 lines
5.7 KiB
JavaScript
(()=>{"use strict";const e="tt-media-progressive",t="tt-assets",n=("undefined"!=typeof window&&window.innerHeight,Math.round(425),new Set(["newMessage","newScheduledMessage","deleteMessages","deleteScheduledMessages","deleteHistory"]),new Set(["image/png","image/gif","image/jpeg"])),s=new Set(["video/mp4"]);new Set([...n,...s]);const a=e=>new Promise((t=>{setTimeout((()=>t()),e)})),i=524288,r=new Map;async function o(e){const n=await async function(n,s){try{return await Promise.race([a(3e3).then((()=>Promise.reject(new Error("TIMEOUT")))),(async()=>{const n=await self.caches.open(t),s=await n.match(e.request);return{cache:n,cached:s}})()])}catch(e){return void console.error(e)}}(),{cache:s,cached:i}=n||{};if(s&&i){if(i.ok)return i;await s.delete(e.request)}const r=await fetch(e.request);return r.ok&&s&&s.put(e.request,r.clone()),r}var c,d;self.addEventListener("message",(e=>{const{type:t,messageId:n,result:s}=e.data;if("partResponse"===t){const e=r.get(n);e&&e.resolve(s)}})),(d=c||(c={})).True="1",d.False="0";let l=(new Date).valueOf();const u=new Set,f={};function g(e){return e.custom.from_id?parseInt(e.custom.from_id,10):e.custom.chat_id?-1*parseInt(e.custom.chat_id,10):e.custom.channel_id?-1*parseInt(e.custom.channel_id,10):void 0}function p(e){if(e.custom.msg_id)return parseInt(e.custom.msg_id,10)}async function w(){const e=new URL(self.registration.scope).origin;return(await self.clients.matchAll({type:"window"})).filter((t=>new URL(t.url).origin===e))}async function m(e){const t=(await w())[0];t&&t.postMessage({type:"playNotificationSound",payload:{id:e}})}function h({chatId:e,messageId:t,body:n,title:s,icon:a}){const i=(new Date).valueOf()-l<1e3,r={body:n,data:{chatId:e,messageId:t,count:1},icon:a||"icon-192x192.png",badge:"icon-192x192.png",tag:String(i?0:e||0),vibrate:[200,100,200]};return Promise.all([m(t||e||0),self.registration.showNotification(s,r)])}async function y(e,t){const{chatId:n,messageId:s}=t;if(n){e.postMessage({type:"focusMessage",payload:{chatId:n,messageId:s}});try{await e.focus()}catch(e){}}}self.onsync=()=>{l=(new Date).valueOf()};const I=/[0-9a-f]{20}.*\.(js|css|woff2?|svg|png|jpg|jpeg|json|wasm)$/;self.addEventListener("install",(e=>{e.waitUntil(self.skipWaiting())})),self.addEventListener("activate",(e=>{e.waitUntil(Promise.race([a(3e3),Promise.all([self.caches.delete(t),self.clients.claim()])]))})),self.addEventListener("fetch",(t=>{const{url:n}=t.request;return n.includes("/progressive/")?(t.respondWith(async function(t){const{url:n}=t.request,s=t.request.headers.get("range"),o=/^bytes=(\d+)-(\d+)?$/g.exec(s||""),c=Number(o[1]);let d=Number(o[2]);if((!d||d-c+1>i)&&(d=c+i-1),0===c&&1===d){const e=t.request.url.match(/fileSize=(\d+)&mimeType=([\w/]+)/),n=e&&Number(e[1]),s=null==e?void 0:e[2];if(n&&s)return new Response(new Uint8Array(2).buffer,{status:206,statusText:"Partial Content",headers:[["Content-Range",`bytes 0-1/${n}`],["Accept-Ranges","bytes"],["Content-Length","2"],["Content-Type",s]]})}const l=`${n}?start=${c}&end=${d}`,[u,f]=await async function(t){const n=await self.caches.open(e);return Promise.all([n.match(`${t}&type=arrayBuffer`).then((e=>e?e.arrayBuffer():void 0)),n.match(`${t}&type=headers`).then((e=>e?e.json():void 0))])}(l);if(u)return new Response(u,{status:206,statusText:"Partial Content",headers:f});let g;try{g=await async function(e,t){if(!e.clientId)return;const n=await self.clients.get(e.clientId);if(!n)return;const s=(e=>{let t;do{t=String(Math.random()).replace("0.","id")}while(e.hasOwnProperty(t));return t})(r),i={},o=Promise.race([a(6e4).then((()=>Promise.reject(new Error("ERROR_PART_TIMEOUT")))),new Promise(((e,t)=>{Object.assign(i,{resolve:e,reject:t})}))]);return r.set(s,i),o.catch((()=>{})).finally((()=>{r.delete(s)})),n.postMessage({type:"requestPart",messageId:s,params:t}),o}(t,{url:n,start:c,end:d})}catch(e){}if(!g)return new Response("",{status:500,statusText:"Failed to fetch progressive part"});const{arrayBuffer:p,fullSize:w,mimeType:m}=g,h=Math.min(d-c+1,p.byteLength);d=c+h-1;const y=p.slice(0,h),I=[["Content-Range",`bytes ${c}-${d}/${w}`],["Accept-Ranges","bytes"],["Content-Length",String(h)],["Content-Type",m]];return h<=524288&&d<2097151&&async function(t,n,s){const a=await self.caches.open(e);Promise.all([a.put(new Request(`${t}&type=arrayBuffer`),new Response(n)),a.put(new Request(`${t}&type=headers`),new Response(JSON.stringify(s)))])}(l,y,I),new Response(y,{status:206,statusText:"Partial Content",headers:I})}(t)),!0):!(!n.startsWith("http")||!n.match(I)||(t.respondWith(o(t)),0))})),self.addEventListener("push",(function(e){const t=function(e){try{return e.data.json()}catch(e){return}}(e);if(!t||t.mute===c.True)return;const n=function(e){return{chatId:g(e),messageId:p(e),title:e.title||"Telegram WebZ",body:e.description}}(t);u.has(n.messageId)?u.delete(n.messageId):e.waitUntil(h(n))})),self.addEventListener("notificationclick",(function(e){const t=new URL(self.registration.scope).origin;e.notification.close();const{data:n}=e.notification;e.waitUntil((async()=>{const e=await w();if(await Promise.all(e.map((e=>(f[e.id]=n,y(e,n))))),self.clients.openWindow&&!(e.length>0)){f[0]=n;try{const e=await self.clients.openWindow(t);e&&(f[e.id]=n)}catch(e){}}})())})),self.addEventListener("message",(function(e){if(!e.data)return;const t=e.source;if("clientReady"===e.data.type){const n=f[t.id]||f[0];n&&(delete f[t.id],delete f[0],e.waitUntil(y(t,n)))}if("newMessageNotification"===e.data.type){const t=e.data.payload;u.add(t.messageId),e.waitUntil(h(t))}"closeMessageNotifications"===e.data.type&&e.waitUntil(async function({chatId:e,lastReadInboxMessageId:t}){const n=await self.registration.getNotifications(),s=t||Number.MAX_VALUE;n.forEach((t=>{("0"===t.tag||t.data.chatId===e&&t.data.messageId<=s)&&t.close()}))}(e.data.payload))}))})();
|
|
//# sourceMappingURL=524.974793b4b91d06551469.js.map
|