wip
This commit is contained in:
@@ -6,92 +6,100 @@ declare var self: ServiceWorkerGlobalScope;
|
||||
import { getNoteSummary } from '../../misc/get-note-summary';
|
||||
import getUserName from '../../misc/get-user-name';
|
||||
|
||||
export default async function(type, data, i18n): Promise<[string, NotificationOptions] | null | undefined> {
|
||||
export default async function(data, i18n): Promise<[string, NotificationOptions] | null | undefined> {
|
||||
if (!i18n) {
|
||||
console.log('no i18n');
|
||||
return;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
switch (data.type) {
|
||||
case 'driveFileCreated': // TODO (Server Side)
|
||||
return [i18n.t('_notification.fileUploaded'), {
|
||||
body: data.name,
|
||||
icon: data.url
|
||||
body: data.body.name,
|
||||
icon: data.body.url,
|
||||
data
|
||||
}];
|
||||
case 'notification':
|
||||
switch (data.type) {
|
||||
switch (data.body.type) {
|
||||
case 'mention':
|
||||
return [i18n.t('_notification.youGotMention', { name: getUserName(data.user) }), {
|
||||
body: getNoteSummary(data.note, i18n.locale),
|
||||
icon: data.user.avatarUrl
|
||||
return [i18n.t('_notification.youGotMention', { name: getUserName(data.body.user) }), {
|
||||
body: getNoteSummary(data.body.note, i18n.locale),
|
||||
icon: data.body.user.avatarUrl,
|
||||
data,
|
||||
actions: [
|
||||
{
|
||||
action: 'showUser',
|
||||
title: 'showUser'
|
||||
}
|
||||
]
|
||||
}];
|
||||
|
||||
case 'reply':
|
||||
return [i18n.t('_notification.youGotReply', { name: getUserName(data.user) }), {
|
||||
body: getNoteSummary(data.note, i18n.locale),
|
||||
icon: data.user.avatarUrl
|
||||
return [i18n.t('_notification.youGotReply', { name: getUserName(data.body.user) }), {
|
||||
body: getNoteSummary(data.body.note, i18n.locale),
|
||||
icon: data.body.user.avatarUrl
|
||||
}];
|
||||
|
||||
case 'renote':
|
||||
return [i18n.t('_notification.youRenoted', { name: getUserName(data.user) }), {
|
||||
body: getNoteSummary(data.note, i18n.locale),
|
||||
icon: data.user.avatarUrl
|
||||
return [i18n.t('_notification.youRenoted', { name: getUserName(data.body.user) }), {
|
||||
body: getNoteSummary(data.body.note, i18n.locale),
|
||||
icon: data.body.user.avatarUrl
|
||||
}];
|
||||
|
||||
case 'quote':
|
||||
return [i18n.t('_notification.youGotQuote', { name: getUserName(data.user) }), {
|
||||
body: getNoteSummary(data.note, i18n.locale),
|
||||
icon: data.user.avatarUrl
|
||||
return [i18n.t('_notification.youGotQuote', { name: getUserName(data.body.user) }), {
|
||||
body: getNoteSummary(data.body.note, i18n.locale),
|
||||
icon: data.body.user.avatarUrl
|
||||
}];
|
||||
|
||||
case 'reaction':
|
||||
return [`${data.reaction} ${getUserName(data.user)}`, {
|
||||
body: getNoteSummary(data.note, i18n.locale),
|
||||
icon: data.user.avatarUrl
|
||||
return [`${data.body.reaction} ${getUserName(data.body.user)}`, {
|
||||
body: getNoteSummary(data.body.note, i18n.locale),
|
||||
icon: data.body.user.avatarUrl
|
||||
}];
|
||||
|
||||
case 'pollVote':
|
||||
return [i18n.t('_notification.youGotPoll', { name: getUserName(data.user) }), {
|
||||
body: getNoteSummary(data.note, i18n.locale),
|
||||
icon: data.user.avatarUrl
|
||||
return [i18n.t('_notification.youGotPoll', { name: getUserName(data.body.user) }), {
|
||||
body: getNoteSummary(data.body.note, i18n.locale),
|
||||
icon: data.body.user.avatarUrl
|
||||
}];
|
||||
|
||||
case 'follow':
|
||||
return [i18n.t('_notification.youWereFollowed'), {
|
||||
body: getUserName(data.user),
|
||||
icon: data.user.avatarUrl
|
||||
body: getUserName(data.body.user),
|
||||
icon: data.body.user.avatarUrl
|
||||
}];
|
||||
|
||||
case 'receiveFollowRequest':
|
||||
return [i18n.t('_notification.youReceivedFollowRequest'), {
|
||||
body: getUserName(data.user),
|
||||
icon: data.user.avatarUrl
|
||||
body: getUserName(data.body.user),
|
||||
icon: data.body.user.avatarUrl
|
||||
}];
|
||||
|
||||
case 'followRequestAccepted':
|
||||
return [i18n.t('_notification.yourFollowRequestAccepted'), {
|
||||
body: getUserName(data.user),
|
||||
icon: data.user.avatarUrl
|
||||
body: getUserName(data.body.user),
|
||||
icon: data.body.user.avatarUrl
|
||||
}];
|
||||
|
||||
case 'groupInvited':
|
||||
return [i18n.t('_notification.youWereInvitedToGroup'), {
|
||||
body: data.group.name
|
||||
body: data.body.group.name
|
||||
}];
|
||||
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
case 'unreadMessagingMessage':
|
||||
if (data.groupId === null) {
|
||||
return [i18n.t('_notification.youGotMessagingMessageFromUser', { name: getUserName(data.user) }), {
|
||||
icon: data.user.avatarUrl,
|
||||
tag: `messaging:user:${data.user.id}`
|
||||
if (data.body.groupId === null) {
|
||||
return [i18n.t('_notification.youGotMessagingMessageFromUser', { name: getUserName(data.body.user) }), {
|
||||
icon: data.body.user.avatarUrl,
|
||||
tag: `messaging:user:${data.body.user.id}`
|
||||
}];
|
||||
}
|
||||
return [i18n.t('_notification.youGotMessagingMessageFromGroup', { name: data.group.name }), {
|
||||
icon: data.user.avatarUrl,
|
||||
tag: `messaging:group:${data.group.id}`
|
||||
return [i18n.t('_notification.youGotMessagingMessageFromGroup', { name: data.body.group.name }), {
|
||||
icon: data.body.user.avatarUrl,
|
||||
tag: `messaging:group:${data.body.group.id}`
|
||||
}];
|
||||
default:
|
||||
return null;
|
||||
|
||||
@@ -74,20 +74,69 @@ self.addEventListener('push', ev => {
|
||||
ev.waitUntil(self.clients.matchAll({
|
||||
includeUncontrolled: true
|
||||
}).then(async clients => {
|
||||
// クライアントがあったらストリームに接続しているということなので通知しない
|
||||
if (clients.length != 0) return;
|
||||
// // クライアントがあったらストリームに接続しているということなので通知しない
|
||||
// if (clients.length != 0) return;
|
||||
|
||||
const { type, body } = ev.data?.json();
|
||||
const data = ev.data?.json();
|
||||
|
||||
// localeを読み込めておらずi18nがundefinedだった場合はpushesPoolにためておく
|
||||
if (!i18n) return pushesPool.push({ type, body });
|
||||
if (!i18n) return pushesPool.push(data);
|
||||
|
||||
const n = await composeNotification(type, body, i18n);
|
||||
const n = await composeNotification(data, i18n);
|
||||
if (n) return self.registration.showNotification(...n);
|
||||
}));
|
||||
});
|
||||
//#endregion
|
||||
|
||||
//#region Notification
|
||||
self.addEventListener('notificationclick', ev => {
|
||||
const { action, notification } = ev;
|
||||
const { data } = notification;
|
||||
const { origin } = location;
|
||||
|
||||
switch (action) {
|
||||
case 'showUser':
|
||||
switch (data.body.type) {
|
||||
case 'reaction':
|
||||
self.clients.openWindow(`${origin}/users/${data.body.user.id}`);
|
||||
break;
|
||||
|
||||
default:
|
||||
if ('note' in data.body) {
|
||||
self.clients.openWindow(`${origin}/notes/${data.body.note.id}`);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
}
|
||||
|
||||
notification.close();
|
||||
});
|
||||
|
||||
self.addEventListener('notificationclose', async ev => {
|
||||
self.registration.showNotification('notificationclose');
|
||||
const { notification } = ev;
|
||||
const { data } = notification
|
||||
|
||||
if (data.isNotification) {
|
||||
const { origin } = location;
|
||||
|
||||
const accounts = await get('accounts');
|
||||
const account = accounts.find(i => i.id === data.userId);
|
||||
|
||||
if (!account) return;
|
||||
|
||||
fetch(`${origin}/api/notifications/read`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
i: account.token,
|
||||
notificationIds: [data.data.id]
|
||||
})
|
||||
});
|
||||
}
|
||||
});
|
||||
//#endregion
|
||||
|
||||
//#region When: Caught a message from the client
|
||||
self.addEventListener('message', ev => {
|
||||
switch(ev.data) {
|
||||
@@ -131,8 +180,8 @@ async function fetchLocale() {
|
||||
//#endregion
|
||||
|
||||
//#region i18nをきちんと読み込んだ後にやりたい処理
|
||||
for (const { type, body } of pushesPool) {
|
||||
const n = await composeNotification(type, body, i18n);
|
||||
for (const data of pushesPool) {
|
||||
const n = await composeNotification(data, i18n);
|
||||
if (n) self.registration.showNotification(...n);
|
||||
}
|
||||
pushesPool = [];
|
||||
|
||||
Reference in New Issue
Block a user