2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import push from 'web-push';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import type { Packed } from '@/misc/schema';
|
|
|
|
import { getNoteSummary } from '@/misc/get-note-summary.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { SwSubscriptionsRepository } from '@/models/index.js';
|
2022-12-04 02:16:03 +01:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2022-12-04 09:05:32 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2022-12-18 11:50:02 +01:00
|
|
|
// Defined also packages/sw/types.ts#L13
|
2022-09-17 20:27:08 +02:00
|
|
|
type pushNotificationsTypes = {
|
|
|
|
'notification': Packed<'Notification'>;
|
|
|
|
'unreadMessagingMessage': Packed<'MessagingMessage'>;
|
2022-12-18 11:50:02 +01:00
|
|
|
'unreadAntennaNote': {
|
|
|
|
antenna: { id: string, name: string };
|
|
|
|
note: Packed<'Note'>;
|
|
|
|
};
|
2022-09-17 20:27:08 +02:00
|
|
|
'readNotifications': { notificationIds: string[] };
|
|
|
|
'readAllNotifications': undefined;
|
|
|
|
'readAllMessagingMessages': undefined;
|
|
|
|
'readAllMessagingMessagesOfARoom': { userId: string } | { groupId: string };
|
2022-12-18 11:50:02 +01:00
|
|
|
'readAntenna': { antennaId: string };
|
|
|
|
'readAllAntennas': undefined;
|
2022-09-17 20:27:08 +02:00
|
|
|
};
|
|
|
|
|
2022-12-18 11:50:02 +01:00
|
|
|
// Reduce length because push message servers have character limits
|
|
|
|
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'] {
|
2022-09-17 20:27:08 +02:00
|
|
|
if (notification.note) {
|
|
|
|
return {
|
|
|
|
...notification,
|
|
|
|
note: {
|
|
|
|
...notification.note,
|
|
|
|
// textをgetNoteSummaryしたものに置き換える
|
2022-12-18 11:50:02 +01:00
|
|
|
text: getNoteSummary(('type' in notification && notification.type === 'renote') ? notification.note.renote as Packed<'Note'> : notification.note),
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
cw: undefined,
|
|
|
|
reply: undefined,
|
|
|
|
renote: undefined,
|
2022-12-18 11:50:02 +01:00
|
|
|
user: undefined as any, // 通知を受け取ったユーザーである場合が多いのでこれも捨てる アンテナの場合も不要なのでいらない
|
2022-09-17 20:27:08 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class PushNotificationService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.swSubscriptionsRepository)
|
|
|
|
private swSubscriptionsRepository: SwSubscriptionsRepository,
|
|
|
|
|
|
|
|
private metaService: MetaService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-06 01:17:37 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async pushNotification<T extends keyof pushNotificationsTypes>(userId: string, type: T, body: pushNotificationsTypes[T]) {
|
|
|
|
const meta = await this.metaService.fetch();
|
|
|
|
|
|
|
|
if (!meta.enableServiceWorker || meta.swPublicKey == null || meta.swPrivateKey == null) return;
|
|
|
|
|
|
|
|
// アプリケーションの連絡先と、サーバーサイドの鍵ペアの情報を登録
|
|
|
|
push.setVapidDetails(this.config.url,
|
|
|
|
meta.swPublicKey,
|
|
|
|
meta.swPrivateKey);
|
|
|
|
|
|
|
|
// Fetch
|
|
|
|
const subscriptions = await this.swSubscriptionsRepository.findBy({
|
|
|
|
userId: userId,
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const subscription of subscriptions) {
|
2022-12-17 17:59:59 +01:00
|
|
|
// Continue if sendReadMessage is false
|
|
|
|
if ([
|
|
|
|
'readNotifications',
|
|
|
|
'readAllNotifications',
|
|
|
|
'readAllMessagingMessages',
|
|
|
|
'readAllMessagingMessagesOfARoom',
|
2022-12-18 11:50:02 +01:00
|
|
|
'readAntenna',
|
|
|
|
'readAllAntennas',
|
2022-12-17 17:59:59 +01:00
|
|
|
].includes(type) && !subscription.sendReadMessage) continue;
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
const pushSubscription = {
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
keys: {
|
|
|
|
auth: subscription.auth,
|
|
|
|
p256dh: subscription.publickey,
|
|
|
|
},
|
|
|
|
};
|
2022-12-18 11:50:02 +01:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
push.sendNotification(pushSubscription, JSON.stringify({
|
|
|
|
type,
|
2022-12-18 11:50:02 +01:00
|
|
|
body: (type === 'notification' || type === 'unreadAntennaNote') ? truncateBody(type, body) : body,
|
2022-09-17 20:27:08 +02:00
|
|
|
userId,
|
|
|
|
dateTime: (new Date()).getTime(),
|
|
|
|
}), {
|
|
|
|
proxy: this.config.proxy,
|
|
|
|
}).catch((err: any) => {
|
|
|
|
//swLogger.info(err.statusCode);
|
|
|
|
//swLogger.info(err.headers);
|
|
|
|
//swLogger.info(err.body);
|
|
|
|
|
|
|
|
if (err.statusCode === 410) {
|
|
|
|
this.swSubscriptionsRepository.delete({
|
|
|
|
userId: userId,
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
auth: subscription.auth,
|
|
|
|
publickey: subscription.publickey,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|