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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 104 additions and 24 deletions

View File

@ -1770,6 +1770,7 @@ _notification:
yourFollowRequestAccepted: "フォローリクエストが承認されました" yourFollowRequestAccepted: "フォローリクエストが承認されました"
youWereInvitedToGroup: "{userName}があなたをグループに招待しました" youWereInvitedToGroup: "{userName}があなたをグループに招待しました"
pollEnded: "アンケートの結果が出ました" pollEnded: "アンケートの結果が出ました"
unreadAntennaNote: "アンテナ {name}"
emptyPushNotificationMessage: "プッシュ通知の更新をしました" emptyPushNotificationMessage: "プッシュ通知の更新をしました"
_types: _types:

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

View File

@ -3,9 +3,12 @@ import Redis from 'ioredis';
import type { Antenna } from '@/models/entities/Antenna.js'; import type { Antenna } from '@/models/entities/Antenna.js';
import type { Note } from '@/models/entities/Note.js'; import type { Note } from '@/models/entities/Note.js';
import type { User } from '@/models/entities/User.js'; import type { User } from '@/models/entities/User.js';
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
import { AntennaEntityService } from '@/core/entities/AntennaEntityService.js';
import { IdService } from '@/core/IdService.js'; import { IdService } from '@/core/IdService.js';
import { isUserRelated } from '@/misc/is-user-related.js'; import { isUserRelated } from '@/misc/is-user-related.js';
import { GlobalEventService } from '@/core/GlobalEventService.js'; import { GlobalEventService } from '@/core/GlobalEventService.js';
import { PushNotificationService } from '@/core/PushNotificationService.js';
import * as Acct from '@/misc/acct.js'; import * as Acct from '@/misc/acct.js';
import { Cache } from '@/misc/cache.js'; import { Cache } from '@/misc/cache.js';
import type { Packed } from '@/misc/schema.js'; import type { Packed } from '@/misc/schema.js';
@ -49,6 +52,9 @@ export class AntennaService implements OnApplicationShutdown {
private utilityService: UtilityService, private utilityService: UtilityService,
private idService: IdService, private idService: IdService,
private globalEventServie: GlobalEventService, private globalEventServie: GlobalEventService,
private pushNotificationService: PushNotificationService,
private noteEntityService: NoteEntityService,
private antennaEntityService: AntennaEntityService,
) { ) {
this.antennasFetched = false; this.antennasFetched = false;
this.antennas = []; this.antennas = [];
@ -127,6 +133,10 @@ export class AntennaService implements OnApplicationShutdown {
const unread = await this.antennaNotesRepository.findOneBy({ antennaId: antenna.id, read: false }); const unread = await this.antennaNotesRepository.findOneBy({ antennaId: antenna.id, read: false });
if (unread) { if (unread) {
this.globalEventServie.publishMainStream(antenna.userId, 'unreadAntenna', antenna); this.globalEventServie.publishMainStream(antenna.userId, 'unreadAntenna', antenna);
this.pushNotificationService.pushNotification(antenna.userId, 'unreadAntennaNote', {
antenna: { id: antenna.id, name: antenna.name },
note: await this.noteEntityService.pack(note)
});
} }
}, 2000); }, 2000);
} }

View File

@ -12,6 +12,7 @@ import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { NotificationService } from './NotificationService.js'; import { NotificationService } from './NotificationService.js';
import { AntennaService } from './AntennaService.js'; import { AntennaService } from './AntennaService.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
import { PushNotificationService } from './PushNotificationService.js';
@Injectable() @Injectable()
export class NoteReadService { export class NoteReadService {
@ -42,6 +43,7 @@ export class NoteReadService {
private globalEventServie: GlobalEventService, private globalEventServie: GlobalEventService,
private notificationService: NotificationService, private notificationService: NotificationService,
private antennaService: AntennaService, private antennaService: AntennaService,
private pushNotificationService: PushNotificationService,
) { ) {
} }
@ -205,12 +207,14 @@ export class NoteReadService {
if (count === 0) { if (count === 0) {
this.globalEventServie.publishMainStream(userId, 'readAntenna', antenna); this.globalEventServie.publishMainStream(userId, 'readAntenna', antenna);
this.pushNotificationService.pushNotification(userId, 'readAntenna', { antennaId: antenna.id });
} }
} }
this.userEntityService.getHasUnreadAntenna(userId).then(unread => { this.userEntityService.getHasUnreadAntenna(userId).then(unread => {
if (!unread) { if (!unread) {
this.globalEventServie.publishMainStream(userId, 'readAllAntennas'); this.globalEventServie.publishMainStream(userId, 'readAllAntennas');
this.pushNotificationService.pushNotification(userId, 'readAllAntennas', undefined);
} }
}); });
} }

View File

@ -8,30 +8,58 @@ import type { SwSubscriptionsRepository } from '@/models/index.js';
import { MetaService } from '@/core/MetaService.js'; import { MetaService } from '@/core/MetaService.js';
import { bindThis } from '@/decorators.js'; import { bindThis } from '@/decorators.js';
// Defined also packages/sw/types.ts#L14-L21 // Defined also packages/sw/types.ts#L13
type pushNotificationsTypes = { type pushNotificationsTypes = {
'notification': Packed<'Notification'>; 'notification': Packed<'Notification'>;
'unreadMessagingMessage': Packed<'MessagingMessage'>; 'unreadMessagingMessage': Packed<'MessagingMessage'>;
'unreadAntennaNote': {
antenna: { id: string, name: string };
note: Packed<'Note'>;
};
'readNotifications': { notificationIds: string[] }; 'readNotifications': { notificationIds: string[] };
'readAllNotifications': undefined; 'readAllNotifications': undefined;
'readAllMessagingMessages': undefined; 'readAllMessagingMessages': undefined;
'readAllMessagingMessagesOfARoom': { userId: string } | { groupId: string }; 'readAllMessagingMessagesOfARoom': { userId: string } | { groupId: string };
'readAntenna': { antennaId: string };
'readAllAntennas': undefined;
}; };
// プッシュメッセージサーバーには文字数制限があるため、内容を削減します // Reduce length because push message servers have character limits
function truncateNotification(notification: Packed<'Notification'>): any { function truncateBody<T extends keyof pushNotificationsTypes>(type: T, body: pushNotificationsTypes[T]): pushNotificationsTypes[T] {
if (body === undefined) return body;
return {
...body,
...(('note' in body && body.note) ? {
note: {
...body.note,
// textをgetNoteSummaryしたものに置き換える
text: getNoteSummary(('type' in body && body.type === 'renote') ? body.note.renote as Packed<'Note'> : body.note),
cw: undefined,
reply: undefined,
renote: undefined,
user: type === 'notification' ? undefined as any : body.note.user,
}
} : {}),
};
return body;
}
function truncateUnreadAntennaNote(notification: pushNotificationsTypes['unreadAntennaNote']): pushNotificationsTypes['unreadAntennaNote'] {
if (notification.note) { if (notification.note) {
return { return {
...notification, ...notification,
note: { note: {
...notification.note, ...notification.note,
// textをgetNoteSummaryしたものに置き換える // textをgetNoteSummaryしたものに置き換える
text: getNoteSummary(notification.type === 'renote' ? notification.note.renote as Packed<'Note'> : notification.note), text: getNoteSummary(('type' in notification && notification.type === 'renote') ? notification.note.renote as Packed<'Note'> : notification.note),
cw: undefined, cw: undefined,
reply: undefined, reply: undefined,
renote: undefined, renote: undefined,
user: undefined as any, // 通知を受け取ったユーザーである場合が多いのでこれも捨てる user: undefined as any, // 通知を受け取ったユーザーである場合が多いのでこれも捨てる アンテナの場合も不要なのでいらない
}, },
}; };
} }
@ -75,6 +103,8 @@ export class PushNotificationService {
'readAllNotifications', 'readAllNotifications',
'readAllMessagingMessages', 'readAllMessagingMessages',
'readAllMessagingMessagesOfARoom', 'readAllMessagingMessagesOfARoom',
'readAntenna',
'readAllAntennas',
].includes(type) && !subscription.sendReadMessage) continue; ].includes(type) && !subscription.sendReadMessage) continue;
const pushSubscription = { const pushSubscription = {
@ -87,7 +117,7 @@ export class PushNotificationService {
push.sendNotification(pushSubscription, JSON.stringify({ push.sendNotification(pushSubscription, JSON.stringify({
type, type,
body: type === 'notification' ? truncateNotification(body as Packed<'Notification'>) : body, body: (type === 'notification' || type === 'unreadAntennaNote') ? truncateBody(type, body) : body,
userId, userId,
dateTime: (new Date()).getTime(), dateTime: (new Date()).getTime(),
}), { }), {

View File

@ -252,6 +252,15 @@ async function composeNotification<K extends keyof pushNotificationDataMap>(data
data, data,
renotify: true, 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: default:
return null; return null;
} }

View File

@ -27,6 +27,11 @@ export function openNote(noteId: string, loginId: string) {
return openClient('push', `/notes/${noteId}`, loginId, { noteId }); 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) { export async function openChat(body: any, loginId: string) {
if (body.groupId === null) { if (body.groupId === null) {
return openClient('push', `/my/messaging/${getAcct(body.user)}`, loginId, { body }); return openClient('push', `/my/messaging/${getAcct(body.user)}`, loginId, { body });

View File

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

View File

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