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