mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-27 02:03:09 +02:00
refactor(sw): Fix type errors in packages/sw (#9909)
* Fix type errors in packages/sw * mouhitotsu * @typesは越境しない * Update packages/sw/src/scripts/create-notification.ts --------- Co-authored-by: tamaina <tamaina@hotmail.co.jp>
This commit is contained in:
parent
a71682f6f0
commit
9f0e0dc8ce
5 changed files with 41 additions and 30 deletions
7
packages/sw/src/@types/global.d.ts
vendored
Normal file
7
packages/sw/src/@types/global.d.ts
vendored
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
type FIXME = any;
|
||||||
|
|
||||||
|
declare const _LANGS_: string[][];
|
||||||
|
declare const _VERSION_: string;
|
||||||
|
declare const _ENV_: string;
|
||||||
|
declare const _DEV_: boolean;
|
||||||
|
declare const _PERF_PREFIX_: string;
|
|
@ -30,7 +30,7 @@ export async function createNotification<K extends keyof pushNotificationDataMap
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function composeNotification<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]): Promise<[string, NotificationOptions] | null> {
|
async function composeNotification(data: pushNotificationDataMap[keyof pushNotificationDataMap]): Promise<[string, NotificationOptions] | null> {
|
||||||
if (!swLang.i18n) swLang.fetchLocale();
|
if (!swLang.i18n) swLang.fetchLocale();
|
||||||
const i18n = await swLang.i18n as I18n<any>;
|
const i18n = await swLang.i18n as I18n<any>;
|
||||||
const { t } = i18n;
|
const { t } = i18n;
|
||||||
|
@ -66,7 +66,7 @@ async function composeNotification<K extends keyof pushNotificationDataMap>(data
|
||||||
|
|
||||||
case 'mention':
|
case 'mention':
|
||||||
return [t('_notification.youGotMention', { name: getUserName(data.body.user) }), {
|
return [t('_notification.youGotMention', { name: getUserName(data.body.user) }), {
|
||||||
body: data.body.note.text || '',
|
body: data.body.note.text ?? '',
|
||||||
icon: data.body.user.avatarUrl,
|
icon: data.body.user.avatarUrl,
|
||||||
badge: iconUrl('at'),
|
badge: iconUrl('at'),
|
||||||
data,
|
data,
|
||||||
|
@ -80,7 +80,7 @@ async function composeNotification<K extends keyof pushNotificationDataMap>(data
|
||||||
|
|
||||||
case 'reply':
|
case 'reply':
|
||||||
return [t('_notification.youGotReply', { name: getUserName(data.body.user) }), {
|
return [t('_notification.youGotReply', { name: getUserName(data.body.user) }), {
|
||||||
body: data.body.note.text || '',
|
body: data.body.note.text ?? '',
|
||||||
icon: data.body.user.avatarUrl,
|
icon: data.body.user.avatarUrl,
|
||||||
badge: iconUrl('arrow-back-up'),
|
badge: iconUrl('arrow-back-up'),
|
||||||
data,
|
data,
|
||||||
|
@ -94,7 +94,7 @@ async function composeNotification<K extends keyof pushNotificationDataMap>(data
|
||||||
|
|
||||||
case 'renote':
|
case 'renote':
|
||||||
return [t('_notification.youRenoted', { name: getUserName(data.body.user) }), {
|
return [t('_notification.youRenoted', { name: getUserName(data.body.user) }), {
|
||||||
body: data.body.note.text || '',
|
body: data.body.note.text ?? '',
|
||||||
icon: data.body.user.avatarUrl,
|
icon: data.body.user.avatarUrl,
|
||||||
badge: iconUrl('repeat'),
|
badge: iconUrl('repeat'),
|
||||||
data,
|
data,
|
||||||
|
@ -108,7 +108,7 @@ async function composeNotification<K extends keyof pushNotificationDataMap>(data
|
||||||
|
|
||||||
case 'quote':
|
case 'quote':
|
||||||
return [t('_notification.youGotQuote', { name: getUserName(data.body.user) }), {
|
return [t('_notification.youGotQuote', { name: getUserName(data.body.user) }), {
|
||||||
body: data.body.note.text || '',
|
body: data.body.note.text ?? '',
|
||||||
icon: data.body.user.avatarUrl,
|
icon: data.body.user.avatarUrl,
|
||||||
badge: iconUrl('quote'),
|
badge: iconUrl('quote'),
|
||||||
data,
|
data,
|
||||||
|
@ -162,7 +162,7 @@ async function composeNotification<K extends keyof pushNotificationDataMap>(data
|
||||||
}
|
}
|
||||||
|
|
||||||
return [`${reaction} ${getUserName(data.body.user)}`, {
|
return [`${reaction} ${getUserName(data.body.user)}`, {
|
||||||
body: data.body.note.text || '',
|
body: data.body.note.text ?? '',
|
||||||
icon: data.body.user.avatarUrl,
|
icon: data.body.user.avatarUrl,
|
||||||
badge,
|
badge,
|
||||||
data,
|
data,
|
||||||
|
@ -227,9 +227,9 @@ async function composeNotification<K extends keyof pushNotificationDataMap>(data
|
||||||
}];
|
}];
|
||||||
|
|
||||||
case 'app':
|
case 'app':
|
||||||
return [data.body.header || data.body.body, {
|
return [data.body.header ?? data.body.body, {
|
||||||
body: data.body.header && data.body.body,
|
body: data.body.header ? data.body.body : '',
|
||||||
icon: data.body.icon,
|
icon: data.body.icon ?? undefined,
|
||||||
data,
|
data,
|
||||||
}];
|
}];
|
||||||
|
|
||||||
|
@ -246,7 +246,7 @@ async function composeNotification<K extends keyof pushNotificationDataMap>(data
|
||||||
renotify: true,
|
renotify: true,
|
||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
return [t('_notification.youGotMessagingMessageFromGroup', { name: data.body.group.name }), {
|
return [t('_notification.youGotMessagingMessageFromGroup', { name: data.body.group?.name ?? '' }), {
|
||||||
icon: data.body.user.avatarUrl,
|
icon: data.body.user.avatarUrl,
|
||||||
badge: iconUrl('messages'),
|
badge: iconUrl('messages'),
|
||||||
tag: `messaging:group:${data.body.groupId}`,
|
tag: `messaging:group:${data.body.groupId}`,
|
||||||
|
@ -255,7 +255,7 @@ async function composeNotification<K extends keyof pushNotificationDataMap>(data
|
||||||
}];
|
}];
|
||||||
case 'unreadAntennaNote':
|
case 'unreadAntennaNote':
|
||||||
return [t('_notification.unreadAntennaNote', { name: data.body.antenna.name }), {
|
return [t('_notification.unreadAntennaNote', { name: data.body.antenna.name }), {
|
||||||
body: `${getUserName(data.body.note.user)}: ${data.body.note.text || ''}`,
|
body: `${getUserName(data.body.note.user)}: ${data.body.note.text ?? ''}`,
|
||||||
icon: data.body.note.user.avatarUrl,
|
icon: data.body.note.user.avatarUrl,
|
||||||
badge: iconUrl('antenna'),
|
badge: iconUrl('antenna'),
|
||||||
tag: `antenna:${data.body.antenna.id}`,
|
tag: `antenna:${data.body.antenna.id}`,
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
export default function(user: { name?: string | null, username: string }): string {
|
export default function(user: { name?: string | null, username: string }): string {
|
||||||
|
// Show username if name is empty.
|
||||||
|
// XXX: typescript-eslint has no configuration to allow using `||` against string.
|
||||||
|
// https://github.com/typescript-eslint/typescript-eslint/issues/4906
|
||||||
|
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
||||||
return user.name || user.username;
|
return user.name || user.username;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ class SwNotificationReadManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
// プッシュ通知の既読をサーバーに送信
|
// プッシュ通知の既読をサーバーに送信
|
||||||
public async read<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]) {
|
public async read(data: pushNotificationDataMap[keyof pushNotificationDataMap]) {
|
||||||
if (data.type !== 'notification' || !(data.userId in this.accounts)) return;
|
if (data.type !== 'notification' || !(data.userId in this.accounts)) return;
|
||||||
|
|
||||||
const account = this.accounts[data.userId];
|
const account = this.accounts[data.userId];
|
||||||
|
|
|
@ -5,11 +5,11 @@ import { pushNotificationDataMap } from '@/types';
|
||||||
import * as swos from '@/scripts/operations';
|
import * as swos from '@/scripts/operations';
|
||||||
import { acct as getAcct } from '@/filters/user';
|
import { acct as getAcct } from '@/filters/user';
|
||||||
|
|
||||||
self.addEventListener('install', ev => {
|
globalThis.addEventListener('install', ev => {
|
||||||
ev.waitUntil(self.skipWaiting());
|
ev.waitUntil(self.skipWaiting());
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('activate', ev => {
|
globalThis.addEventListener('activate', ev => {
|
||||||
ev.waitUntil(
|
ev.waitUntil(
|
||||||
caches.keys()
|
caches.keys()
|
||||||
.then(cacheNames => Promise.all(
|
.then(cacheNames => Promise.all(
|
||||||
|
@ -21,7 +21,7 @@ self.addEventListener('activate', ev => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('fetch', ev => {
|
globalThis.addEventListener('fetch', ev => {
|
||||||
let isHTMLRequest = false;
|
let isHTMLRequest = false;
|
||||||
if (ev.request.headers.get('sec-fetch-dest') === 'document') {
|
if (ev.request.headers.get('sec-fetch-dest') === 'document') {
|
||||||
isHTMLRequest = true;
|
isHTMLRequest = true;
|
||||||
|
@ -38,13 +38,13 @@ self.addEventListener('fetch', ev => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('push', ev => {
|
globalThis.addEventListener('push', ev => {
|
||||||
// クライアント取得
|
// クライアント取得
|
||||||
ev.waitUntil(self.clients.matchAll({
|
ev.waitUntil(self.clients.matchAll({
|
||||||
includeUncontrolled: true,
|
includeUncontrolled: true,
|
||||||
type: 'window'
|
type: 'window'
|
||||||
}).then(async <K extends keyof pushNotificationDataMap>(clients: readonly WindowClient[]) => {
|
}).then(async (clients: readonly WindowClient[]) => {
|
||||||
const data: pushNotificationDataMap[K] = ev.data?.json();
|
const data: pushNotificationDataMap[keyof pushNotificationDataMap] = ev.data?.json();
|
||||||
|
|
||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
// case 'driveFileCreated':
|
// case 'driveFileCreated':
|
||||||
|
@ -104,14 +104,14 @@ self.addEventListener('push', ev => {
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('notificationclick', <K extends keyof pushNotificationDataMap>(ev: ServiceWorkerGlobalScopeEventMap['notificationclick']) => {
|
globalThis.addEventListener('notificationclick', (ev: ServiceWorkerGlobalScopeEventMap['notificationclick']) => {
|
||||||
ev.waitUntil((async () => {
|
ev.waitUntil((async () => {
|
||||||
if (_DEV_) {
|
if (_DEV_) {
|
||||||
console.log('notificationclick', ev.action, ev.notification.data);
|
console.log('notificationclick', ev.action, ev.notification.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
const { action, notification } = ev;
|
const { action, notification } = ev;
|
||||||
const data: pushNotificationDataMap[K] = notification.data;
|
const data: pushNotificationDataMap[keyof pushNotificationDataMap] = notification.data;
|
||||||
const { userId: loginId } = data;
|
const { userId: loginId } = data;
|
||||||
let client: WindowClient | null = null;
|
let client: WindowClient | null = null;
|
||||||
|
|
||||||
|
@ -192,15 +192,15 @@ self.addEventListener('notificationclick', <K extends keyof pushNotificationData
|
||||||
})());
|
})());
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('notificationclose', <K extends keyof pushNotificationDataMap>(ev: ServiceWorkerGlobalScopeEventMap['notificationclose']) => {
|
globalThis.addEventListener('notificationclose', (ev: ServiceWorkerGlobalScopeEventMap['notificationclose']) => {
|
||||||
const data: pushNotificationDataMap[K] = ev.notification.data;
|
const data: pushNotificationDataMap[keyof pushNotificationDataMap] = ev.notification.data;
|
||||||
|
|
||||||
if (data.type === 'notification') {
|
if (data.type === 'notification') {
|
||||||
swNotificationRead.then(that => that.read(data));
|
swNotificationRead.then(that => that.read(data));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener('message', (ev: ServiceWorkerGlobalScopeEventMap['message']) => {
|
globalThis.addEventListener('message', (ev: ServiceWorkerGlobalScopeEventMap['message']) => {
|
||||||
ev.waitUntil((async () => {
|
ev.waitUntil((async () => {
|
||||||
switch (ev.data) {
|
switch (ev.data) {
|
||||||
case 'clear':
|
case 'clear':
|
||||||
|
|
Loading…
Reference in a new issue