nanka iroiro

This commit is contained in:
tamaina 2021-09-21 01:55:36 +09:00
parent e0dc391cf6
commit de472fdc79
8 changed files with 163 additions and 158 deletions

View file

@ -7,21 +7,21 @@ import { getNoteSummary } from '@/misc/get-note-summary';
import getUserName from '@/misc/get-user-name';
import { swLang } from '@client/sw/lang';
import { I18n } from '@/misc/i18n';
import { pushNotificationData } from '@/types';
import { pushNotificationDataMap } from '@client/sw/types';
import { apiFetch } from './operations';
import { getAccountFromId } from '@client/scripts/get-account-from-id';
export async function createNotification(data: pushNotificationData) {
export async function createNotification<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]) {
const n = await composeNotification(data);
if (n) await self.registration.showNotification(...n);
else await createEmptyNotification();
}
async function composeNotification(data: pushNotificationData): Promise<[string, NotificationOptions] | null | undefined> {
async function composeNotification<K extends keyof pushNotificationDataMap>(data: pushNotificationDataMap[K]): Promise<[string, NotificationOptions] | null | undefined> {
if (!swLang.i18n) swLang.fetchLocale();
const i18n = await swLang.i18n as I18n<any>;
const { t } = i18n;
const { body } = data;
switch (data.type) {
/*
case 'driveFileCreated': // TODO (Server Side)
@ -32,13 +32,17 @@ async function composeNotification(data: pushNotificationData): Promise<[string,
}];
*/
case 'notification':
switch (body.type) {
switch (data.body.type) {
case 'follow':
// users/showの型定義をswos.apiへ当てはめるのが困難なのでapiFetch.requestを直接使用
const account = await getAccountFromId(data.userId);
if (!account) return null;
const userDetail = apiFetch.request('users/show', { userId: data.body.userId }, account?.token);
return [t('_notification.youWereFollowed'), {
body: getUserName(body.user),
icon: body.user.avatarUrl,
body: getUserName(data.body.user),
icon: data.body.user.avatarUrl,
data,
actions: [
actions: userDetail.isFollowing ? [] : [
{
action: 'follow',
title: t('_notification._actions.followBack')
@ -47,9 +51,9 @@ async function composeNotification(data: pushNotificationData): Promise<[string,
}];
case 'mention':
return [t('_notification.youGotMention', { name: getUserName(body.user) }), {
body: getNoteSummary(body.note, i18n.locale),
icon: body.user.avatarUrl,
return [t('_notification.youGotMention', { name: getUserName(data.body.user) }), {
body: getNoteSummary(data.body.note, i18n.locale),
icon: data.body.user.avatarUrl,
data,
actions: [
{
@ -60,9 +64,9 @@ async function composeNotification(data: pushNotificationData): Promise<[string,
}];
case 'reply':
return [t('_notification.youGotReply', { name: getUserName(body.user) }), {
body: getNoteSummary(body.note, i18n.locale),
icon: body.user.avatarUrl,
return [t('_notification.youGotReply', { name: getUserName(data.body.user) }), {
body: getNoteSummary(data.body.note, i18n.locale),
icon: data.body.user.avatarUrl,
data,
actions: [
{
@ -73,22 +77,22 @@ async function composeNotification(data: pushNotificationData): Promise<[string,
}];
case 'renote':
return [t('_notification.youRenoted', { name: getUserName(body.user) }), {
body: getNoteSummary(body.note.renote, i18n.locale),
icon: body.user.avatarUrl,
return [t('_notification.youRenoted', { name: getUserName(data.body.user) }), {
body: getNoteSummary(data.body.note.renote, i18n.locale),
icon: data.body.user.avatarUrl,
data,
actions: [
{
action: 'showUser',
title: getUserName(body.user)
title: getUserName(data.body.user)
}
],
}];
case 'quote':
return [t('_notification.youGotQuote', { name: getUserName(body.user) }), {
body: getNoteSummary(body.note, i18n.locale),
icon: body.user.avatarUrl,
return [t('_notification.youGotQuote', { name: getUserName(data.body.user) }), {
body: getNoteSummary(data.body.note, i18n.locale),
icon: data.body.user.avatarUrl,
data,
actions: [
{
@ -103,29 +107,29 @@ async function composeNotification(data: pushNotificationData): Promise<[string,
}];
case 'reaction':
return [`${body.reaction} ${getUserName(body.user)}`, {
body: getNoteSummary(body.note, i18n.locale),
icon: body.user.avatarUrl,
return [`${data.body.reaction} ${getUserName(data.body.user)}`, {
body: getNoteSummary(data.body.note, i18n.locale),
icon: data.body.user.avatarUrl,
data,
actions: [
{
action: 'showUser',
title: getUserName(body.user)
title: getUserName(data.body.user)
}
],
}];
case 'pollVote':
return [t('_notification.youGotPoll', { name: getUserName(body.user) }), {
body: getNoteSummary(body.note, i18n.locale),
icon: body.user.avatarUrl,
return [t('_notification.youGotPoll', { name: getUserName(data.body.user) }), {
body: getNoteSummary(data.body.note, i18n.locale),
icon: data.body.user.avatarUrl,
data,
}];
case 'receiveFollowRequest':
return [t('_notification.youReceivedFollowRequest'), {
body: getUserName(body.user),
icon: body.user.avatarUrl,
body: getUserName(data.body.user),
icon: data.body.user.avatarUrl,
data,
actions: [
{
@ -141,14 +145,14 @@ async function composeNotification(data: pushNotificationData): Promise<[string,
case 'followRequestAccepted':
return [t('_notification.yourFollowRequestAccepted'), {
body: getUserName(body.user),
icon: body.user.avatarUrl,
body: getUserName(data.body.user),
icon: data.body.user.avatarUrl,
data,
}];
case 'groupInvited':
return [t('_notification.youWereInvitedToGroup', { userName: getUserName(body.user) }), {
body: body.invitation.group.name,
return [t('_notification.youWereInvitedToGroup', { userName: getUserName(data.body.group) }), {
body: data.body.invitation.group.name,
data,
actions: [
{
@ -163,27 +167,27 @@ async function composeNotification(data: pushNotificationData): Promise<[string,
}];
case 'app':
return [body.header, {
body: body.body,
icon: body.icon,
return [data.body.header || data.body.body, {
body: data.body.header && data.body.body,
icon: data.body.icon,
data
}];
default:
return null;
}
case 'unreadMessagingMessage':
if (body.groupId === null) {
return [t('_notification.youGotMessagingMessageFromUser', { name: getUserName(body.user) }), {
icon: body.user.avatarUrl,
tag: `messaging:user:${body.userId}`,
if (data.body.groupId === null) {
return [t('_notification.youGotMessagingMessageFromUser', { name: getUserName(data.body.user) }), {
icon: data.body.user.avatarUrl,
tag: `messaging:user:${data.body.userId}`,
data,
renotify: true,
}];
}
return [t('_notification.youGotMessagingMessageFromGroup', { name: body.group.name }), {
icon: body.user.avatarUrl,
tag: `messaging:group:${body.groupId}`,
return [t('_notification.youGotMessagingMessageFromGroup', { name: data.body.group.name }), {
icon: data.body.user.avatarUrl,
tag: `messaging:group:${data.body.groupId}`,
data,
renotify: true,
}];