2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-06-10 06:45:11 +02:00
|
|
|
import { Inject, Injectable, OnApplicationShutdown } from '@nestjs/common';
|
2022-09-17 20:27:08 +02:00
|
|
|
import push from 'web-push';
|
2023-04-14 06:50:05 +02:00
|
|
|
import * as Redis from 'ioredis';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { Config } from '@/config.js';
|
2023-06-25 14:13:15 +02:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { getNoteSummary } from '@/misc/get-note-summary.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { MiSwSubscription, SwSubscriptionsRepository } from '@/models/_.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';
|
2023-04-11 07:20:16 +02:00
|
|
|
import { RedisKVCache } from '@/misc/cache.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2022-12-18 11:50:02 +01:00
|
|
|
// Defined also packages/sw/types.ts#L13
|
2023-02-22 06:51:34 +01:00
|
|
|
type PushNotificationsTypes = {
|
2022-09-17 20:27:08 +02:00
|
|
|
'notification': Packed<'Notification'>;
|
2022-12-18 11:50:02 +01:00
|
|
|
'unreadAntennaNote': {
|
|
|
|
antenna: { id: string, name: string };
|
|
|
|
note: Packed<'Note'>;
|
|
|
|
};
|
2023-04-11 07:11:39 +02:00
|
|
|
'readAllNotifications': 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
|
2023-02-22 06:58:41 +01:00
|
|
|
function truncateBody<T extends keyof PushNotificationsTypes>(type: T, body: PushNotificationsTypes[T]): PushNotificationsTypes[T] {
|
2023-02-20 00:13:37 +01:00
|
|
|
if (typeof body !== 'object') return body;
|
2022-12-18 11:50:02 +01:00
|
|
|
|
|
|
|
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),
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-12-18 11:50:02 +01:00
|
|
|
cw: undefined,
|
|
|
|
reply: undefined,
|
|
|
|
renote: undefined,
|
|
|
|
user: type === 'notification' ? undefined as any : body.note.user,
|
2023-02-15 05:06:06 +01:00
|
|
|
},
|
2022-12-18 11:50:02 +01:00
|
|
|
} : {}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
2023-06-10 06:45:11 +02:00
|
|
|
export class PushNotificationService implements OnApplicationShutdown {
|
2023-08-16 10:51:28 +02:00
|
|
|
private subscriptionsCache: RedisKVCache<MiSwSubscription[]>;
|
2023-04-11 07:20:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
2023-04-11 07:20:16 +02:00
|
|
|
@Inject(DI.redis)
|
|
|
|
private redisClient: Redis.Redis,
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Inject(DI.swSubscriptionsRepository)
|
|
|
|
private swSubscriptionsRepository: SwSubscriptionsRepository,
|
|
|
|
|
|
|
|
private metaService: MetaService,
|
|
|
|
) {
|
2023-08-16 10:51:28 +02:00
|
|
|
this.subscriptionsCache = new RedisKVCache<MiSwSubscription[]>(this.redisClient, 'userSwSubscriptions', {
|
2023-04-11 07:20:16 +02:00
|
|
|
lifetime: 1000 * 60 * 60 * 1, // 1h
|
|
|
|
memoryCacheLifetime: 1000 * 60 * 3, // 3m
|
|
|
|
fetcher: (key) => this.swSubscriptionsRepository.findBy({ userId: key }),
|
|
|
|
toRedisConverter: (value) => JSON.stringify(value),
|
|
|
|
fromRedisConverter: (value) => JSON.parse(value),
|
|
|
|
});
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2022-12-06 01:17:37 +01:00
|
|
|
@bindThis
|
2023-02-22 06:51:34 +01:00
|
|
|
public async pushNotification<T extends keyof PushNotificationsTypes>(userId: string, type: T, body: PushNotificationsTypes[T]) {
|
2022-09-17 20:27:08 +02:00
|
|
|
const meta = await this.metaService.fetch();
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
if (!meta.enableServiceWorker || meta.swPublicKey == null || meta.swPrivateKey == null) return;
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
// アプリケーションの連絡先と、サーバーサイドの鍵ペアの情報を登録
|
|
|
|
push.setVapidDetails(this.config.url,
|
|
|
|
meta.swPublicKey,
|
|
|
|
meta.swPrivateKey);
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2023-04-11 07:20:16 +02:00
|
|
|
const subscriptions = await this.subscriptionsCache.fetch(userId);
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
for (const subscription of subscriptions) {
|
2023-04-11 07:11:39 +02:00
|
|
|
if ([
|
|
|
|
'readAllNotifications',
|
|
|
|
].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);
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
if (err.statusCode === 410) {
|
|
|
|
this.swSubscriptionsRepository.delete({
|
|
|
|
userId: userId,
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
auth: subscription.auth,
|
|
|
|
publickey: subscription.publickey,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-06-10 06:45:11 +02:00
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public dispose(): void {
|
|
|
|
this.subscriptionsCache.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public onApplicationShutdown(signal?: string | undefined): void {
|
|
|
|
this.dispose();
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|