2017-11-21 03:40:09 +09:00
/ * *
* Service Worker
* /
2020-05-23 13:19:31 +09:00
declare var self : ServiceWorkerGlobalScope ;
2017-11-21 03:40:09 +09:00
2021-02-10 22:19:09 +09:00
import { createNotification } from '@/sw/create-notification' ;
2021-02-10 01:54:07 +09:00
import { swLang } from '@/sw/lang' ;
2021-02-10 22:19:09 +09:00
import { swNotificationRead } from '@/sw/notification-read' ;
import { pushNotificationData } from '../../types' ;
2021-01-21 21:17:22 +09:00
2021-01-23 00:56:06 +09:00
//#region Variables
2021-02-10 01:54:07 +09:00
// const cacheName = `mk-cache-${_VERSION_}`;
2019-09-27 05:12:56 +09:00
const apiUrl = ` ${ location . origin } /api/ ` ;
2021-01-23 00:56:06 +09:00
//#endregion
//#region Lifecycle: Install
2017-11-27 22:00:48 +09:00
self . addEventListener ( 'install' , ev = > {
2021-02-09 20:09:45 +09:00
// Nothing to do
2019-09-27 05:12:56 +09:00
} ) ;
2021-01-23 00:56:06 +09:00
//#endregion
2019-09-27 05:12:56 +09:00
2021-01-23 00:56:06 +09:00
//#region Lifecycle: Activate
2019-09-27 05:12:56 +09:00
self . addEventListener ( 'activate' , ev = > {
ev . waitUntil (
caches . keys ( )
. then ( cacheNames = > Promise . all (
cacheNames
2021-02-10 01:54:07 +09:00
. filter ( ( v ) = > v !== swLang . cacheName )
2019-09-27 05:12:56 +09:00
. map ( name = > caches . delete ( name ) )
) )
. then ( ( ) = > self . clients . claim ( ) )
) ;
} ) ;
2021-01-23 00:56:06 +09:00
//#endregion
2019-09-27 05:12:56 +09:00
2021-02-07 10:46:26 +09:00
// TODO: 消せるかも ref. https://github.com/syuilo/misskey/pull/7108#issuecomment-774573666
2021-01-23 00:56:06 +09:00
//#region When: Fetching
2019-09-27 05:12:56 +09:00
self . addEventListener ( 'fetch' , ev = > {
if ( ev . request . method !== 'GET' || ev . request . url . startsWith ( apiUrl ) ) return ;
ev . respondWith (
caches . match ( ev . request )
. then ( response = > {
return response || fetch ( ev . request ) ;
} )
2021-02-09 20:09:45 +09:00
. catch ( ( ) = > new Response ( 'SW cathces error while fetching. You may not be connected to the Internet, or the server may be down.' , { status : 200 , statusText : 'OK SW' } ) )
2019-09-27 05:12:56 +09:00
) ;
2017-11-27 22:00:48 +09:00
} ) ;
2021-01-23 00:56:06 +09:00
//#endregion
2017-11-27 22:00:48 +09:00
2021-01-23 00:56:06 +09:00
//#region When: Caught Notification
2017-11-21 03:40:09 +09:00
self . addEventListener ( 'push' , ev = > {
// クライアント取得
2017-11-21 07:06:36 +09:00
ev . waitUntil ( self . clients . matchAll ( {
2017-11-21 03:40:09 +09:00
includeUncontrolled : true
2020-05-23 13:19:31 +09:00
} ) . then ( async clients = > {
2021-01-28 03:24:32 +09:00
// // クライアントがあったらストリームに接続しているということなので通知しない
// if (clients.length != 0) return;
2017-11-21 03:40:09 +09:00
2021-02-10 22:19:09 +09:00
const data : pushNotificationData = ev . data ? . json ( ) ;
2017-11-21 07:06:36 +09:00
2021-02-10 01:54:07 +09:00
switch ( data . type ) {
case 'notification' :
2021-02-10 22:19:09 +09:00
// case 'driveFileCreated':
2021-02-10 01:54:07 +09:00
case 'unreadMessagingMessage' :
2021-02-10 22:19:09 +09:00
return createNotification ( data ) ;
2021-02-10 01:54:07 +09:00
case 'readAllNotifications' :
for ( const n of await self . registration . getNotifications ( ) ) {
n . close ( ) ;
}
break ;
case 'readNotifications' :
2021-02-10 22:19:09 +09:00
for ( const notification of await self . registration . getNotifications ( ) ) {
if ( data . body . notificationIds . includes ( notification . data . body . id ) ) {
2021-02-10 22:30:02 +09:00
notification . close ( ) ;
}
2021-02-10 01:54:07 +09:00
}
break ;
}
2017-11-21 07:06:36 +09:00
} ) ) ;
2017-11-21 03:40:09 +09:00
} ) ;
2021-01-23 00:56:06 +09:00
//#endregion
2021-01-21 21:17:22 +09:00
2021-01-28 03:24:32 +09:00
//#region Notification
self . addEventListener ( 'notificationclick' , ev = > {
const { action , notification } = ev ;
2021-02-10 22:19:09 +09:00
const data : pushNotificationData = notification . data ;
2021-01-28 03:24:32 +09:00
const { origin } = location ;
2021-02-10 01:54:07 +09:00
const suffix = ` ?loginId= ${ data . userId } ` ;
2021-01-28 03:24:32 +09:00
switch ( action ) {
case 'showUser' :
switch ( data . body . type ) {
case 'reaction' :
2021-02-10 01:54:07 +09:00
self . clients . openWindow ( ` ${ origin } /users/ ${ data . body . user . id } ${ suffix } ` ) ;
2021-01-28 03:24:32 +09:00
break ;
default :
if ( 'note' in data . body ) {
2021-02-10 01:54:07 +09:00
self . clients . openWindow ( ` ${ origin } /users/ ${ data . body . note . user . id } ${ suffix } ` ) ;
2021-01-28 03:24:32 +09:00
}
}
break ;
default :
}
notification . close ( ) ;
} ) ;
2021-02-10 22:19:09 +09:00
self . addEventListener ( 'notificationclose' , ev = > {
2021-01-28 03:24:32 +09:00
const { notification } = ev ;
2021-02-10 22:19:09 +09:00
2021-02-10 01:54:07 +09:00
if ( notification . title !== 'notificationclose' ) {
2021-02-10 22:19:09 +09:00
self . registration . showNotification ( 'notificationclose' , { body : ` ${ notification . data . body . id } ` } ) ;
2021-02-10 01:54:07 +09:00
}
2021-02-10 22:19:09 +09:00
const data : pushNotificationData = notification . data ;
2021-01-28 03:24:32 +09:00
2021-02-10 22:19:09 +09:00
if ( data . type === 'notification' ) {
console . log ( 'close' , data ) ;
swNotificationRead . then ( that = > that . read ( data ) ) ;
2021-01-28 03:24:32 +09:00
}
} ) ;
//#endregion
2021-01-23 00:56:06 +09:00
//#region When: Caught a message from the client
2021-01-21 21:17:22 +09:00
self . addEventListener ( 'message' , ev = > {
2021-02-10 02:03:05 +09:00
switch ( ev . data ) {
2021-01-21 21:17:22 +09:00
case 'clear' :
return ; // TODO
default :
break ;
}
if ( typeof ev . data === 'object' ) {
2021-01-24 18:17:42 +09:00
// E.g. '[object Array]' → 'array'
2021-01-21 21:17:22 +09:00
const otype = Object . prototype . toString . call ( ev . data ) . slice ( 8 , - 1 ) . toLowerCase ( ) ;
if ( otype === 'object' ) {
if ( ev . data . msg === 'initialize' ) {
2021-02-10 01:54:07 +09:00
swLang . setLang ( ev . data . lang ) ;
2021-01-21 21:17:22 +09:00
}
}
}
} ) ;
2021-01-23 00:56:06 +09:00
//#endregion