enhance: Push notification of Antenna note (#9338)

* wip

* wip

* wip

* fix

* fix

* 🎨
This commit is contained in:
tamaina
2022-12-18 19:50:02 +09:00
committed by GitHub
parent c5179e8f1c
commit 867e31c9ff
9 changed files with 104 additions and 24 deletions

View File

@@ -252,6 +252,15 @@ async function composeNotification<K extends keyof pushNotificationDataMap>(data
data,
renotify: true,
}];
case 'unreadAntennaNote':
return [t('_notification.unreadAntennaNote', { name: data.body.antenna.name }), {
body: `${getUserName(data.body.note.user)}: ${data.body.note.text || ''}`,
icon: data.body.note.user.avatarUrl,
badge: iconUrl('satellite'),
tag: `antenna:${data.body.antenna.id}`,
data,
renotify: true,
}];
default:
return null;
}

View File

@@ -27,6 +27,11 @@ export function openNote(noteId: string, loginId: string) {
return openClient('push', `/notes/${noteId}`, loginId, { noteId });
}
// noteIdからートを開く
export function openAntenna(antennaId: string, loginId: string) {
return openClient('push', `/timeline/antenna/${antennaId}`, loginId, { antennaId });
}
export async function openChat(body: any, loginId: string) {
if (body.groupId === null) {
return openClient('push', `/my/messaging/${getAcct(body.user)}`, loginId, { body });

View File

@@ -50,6 +50,7 @@ self.addEventListener('push', ev => {
// case 'driveFileCreated':
case 'notification':
case 'unreadMessagingMessage':
case 'unreadAntennaNote':
// 1日以上経過している場合は無視
if ((new Date()).getTime() - data.dateTime > 1000 * 60 * 60 * 24) break;
@@ -67,6 +68,11 @@ self.addEventListener('push', ev => {
if (n?.data?.type === 'unreadMessagingMessage') n.close();
}
break;
case 'readAllAntennas':
for (const n of await self.registration.getNotifications()) {
if (n?.data?.type === 'unreadAntennaNote') n.close();
}
break;
case 'readNotifications':
for (const n of await self.registration.getNotifications()) {
if (data.body?.notificationIds?.includes(n.data.body.id)) {
@@ -85,6 +91,13 @@ self.addEventListener('push', ev => {
}
}
break;
case 'readAntenna':
for (const n of await self.registration.getNotifications()) {
if (n?.data?.type === 'unreadAntennaNote' && data.body?.antennaId === n.data.body.antenna.id) {
n.close();
}
}
break;
}
return createEmptyNotification();
@@ -99,71 +112,73 @@ self.addEventListener('notificationclick', <K extends keyof pushNotificationData
const { action, notification } = ev;
const data: pushNotificationDataMap[K] = notification.data;
const { userId: id } = data;
const { userId: loginId } = data;
let client: WindowClient | null = null;
switch (data.type) {
case 'notification':
switch (action) {
case 'follow':
if ('userId' in data.body) await swos.api('following/create', id, { userId: data.body.userId });
if ('userId' in data.body) await swos.api('following/create', loginId, { userId: data.body.userId });
break;
case 'showUser':
if ('user' in data.body) client = await swos.openUser(getAcct(data.body.user), id);
if ('user' in data.body) client = await swos.openUser(getAcct(data.body.user), loginId);
break;
case 'reply':
if ('note' in data.body) client = await swos.openPost({ reply: data.body.note }, id);
if ('note' in data.body) client = await swos.openPost({ reply: data.body.note }, loginId);
break;
case 'renote':
if ('note' in data.body) await swos.api('notes/create', id, { renoteId: data.body.note.id });
if ('note' in data.body) await swos.api('notes/create', loginId, { renoteId: data.body.note.id });
break;
case 'accept':
switch (data.body.type) {
case 'receiveFollowRequest':
await swos.api('following/requests/accept', id, { userId: data.body.userId });
await swos.api('following/requests/accept', loginId, { userId: data.body.userId });
break;
case 'groupInvited':
await swos.api('users/groups/invitations/accept', id, { invitationId: data.body.invitation.id });
await swos.api('users/groups/invitations/accept', loginId, { invitationId: data.body.invitation.id });
break;
}
break;
case 'reject':
switch (data.body.type) {
case 'receiveFollowRequest':
await swos.api('following/requests/reject', id, { userId: data.body.userId });
await swos.api('following/requests/reject', loginId, { userId: data.body.userId });
break;
case 'groupInvited':
await swos.api('users/groups/invitations/reject', id, { invitationId: data.body.invitation.id });
await swos.api('users/groups/invitations/reject', loginId, { invitationId: data.body.invitation.id });
break;
}
break;
case 'showFollowRequests':
client = await swos.openClient('push', '/my/follow-requests', id);
client = await swos.openClient('push', '/my/follow-requests', loginId);
break;
default:
switch (data.body.type) {
case 'receiveFollowRequest':
client = await swos.openClient('push', '/my/follow-requests', id);
client = await swos.openClient('push', '/my/follow-requests', loginId);
break;
case 'groupInvited':
client = await swos.openClient('push', '/my/groups', id);
client = await swos.openClient('push', '/my/groups', loginId);
break;
case 'reaction':
client = await swos.openNote(data.body.note.id, id);
client = await swos.openNote(data.body.note.id, loginId);
break;
default:
if ('note' in data.body) {
client = await swos.openNote(data.body.note.id, id);
client = await swos.openNote(data.body.note.id, loginId);
} else if ('user' in data.body) {
client = await swos.openUser(getAcct(data.body.user), id);
client = await swos.openUser(getAcct(data.body.user), loginId);
}
break;
}
}
break;
case 'unreadMessagingMessage':
client = await swos.openChat(data.body, id);
client = await swos.openChat(data.body, loginId);
break;
case 'unreadAntennaNote':
client = await swos.openAntenna(data.body.antenna.id, loginId);
}
if (client) {

View File

@@ -10,14 +10,20 @@ export type SwMessage = {
[x: string]: any;
};
// Defined also @/core/push-notification.ts#L7-L14
// Defined also @/core/PushNotificationService.ts#L12
type pushNotificationDataSourceMap = {
notification: Misskey.entities.Notification;
unreadMessagingMessage: Misskey.entities.MessagingMessage;
unreadAntennaNote: {
antenna: { id: string, name: string };
note: Misskey.entities.Note;
};
readNotifications: { notificationIds: string[] };
readAllNotifications: undefined;
readAllMessagingMessages: undefined;
readAllMessagingMessagesOfARoom: { userId: string } | { groupId: string };
readAntenna: { antennaId: string };
readAllAntennas: undefined;
};
export type pushNotificationData<K extends keyof pushNotificationDataSourceMap> = {