diff options
| author | syuilo <Syuilotan@yahoo.co.jp> | 2023-04-11 15:51:07 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-11 15:51:07 +0900 |
| commit | 75b28d6782d9e1a37dd40444fbccffdf4331737a (patch) | |
| tree | 1b6a1b33ca19a579d7d24d92fef4bfb05ae1e86e /packages/backend/src/core/PushNotificationService.ts | |
| parent | Merge pull request #10543 from misskey-dev/develop (diff) | |
| parent | fix(client): noPaging: true with gallery/featured (diff) | |
| download | misskey-75b28d6782d9e1a37dd40444fbccffdf4331737a.tar.gz misskey-75b28d6782d9e1a37dd40444fbccffdf4331737a.tar.bz2 misskey-75b28d6782d9e1a37dd40444fbccffdf4331737a.zip | |
Merge pull request #10578 from misskey-dev/develop
Release: 13.11.2
Diffstat (limited to 'packages/backend/src/core/PushNotificationService.ts')
| -rw-r--r-- | packages/backend/src/core/PushNotificationService.ts | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/packages/backend/src/core/PushNotificationService.ts b/packages/backend/src/core/PushNotificationService.ts index 69020f7e84..9b44cf6413 100644 --- a/packages/backend/src/core/PushNotificationService.ts +++ b/packages/backend/src/core/PushNotificationService.ts @@ -1,12 +1,14 @@ import { Inject, Injectable } from '@nestjs/common'; import push from 'web-push'; +import Redis from 'ioredis'; import { DI } from '@/di-symbols.js'; import type { Config } from '@/config.js'; import type { Packed } from '@/misc/json-schema'; import { getNoteSummary } from '@/misc/get-note-summary.js'; -import type { SwSubscriptionsRepository } from '@/models/index.js'; +import type { SwSubscription, SwSubscriptionsRepository } from '@/models/index.js'; import { MetaService } from '@/core/MetaService.js'; import { bindThis } from '@/decorators.js'; +import { RedisKVCache } from '@/misc/cache.js'; // Defined also packages/sw/types.ts#L13 type PushNotificationsTypes = { @@ -15,6 +17,7 @@ type PushNotificationsTypes = { antenna: { id: string, name: string }; note: Packed<'Note'>; }; + 'readAllNotifications': undefined; }; // Reduce length because push message servers have character limits @@ -40,15 +43,27 @@ function truncateBody<T extends keyof PushNotificationsTypes>(type: T, body: Pus @Injectable() export class PushNotificationService { + private subscriptionsCache: RedisKVCache<SwSubscription[]>; + constructor( @Inject(DI.config) private config: Config, + @Inject(DI.redis) + private redisClient: Redis.Redis, + @Inject(DI.swSubscriptionsRepository) private swSubscriptionsRepository: SwSubscriptionsRepository, private metaService: MetaService, ) { + this.subscriptionsCache = new RedisKVCache<SwSubscription[]>(this.redisClient, 'userSwSubscriptions', { + 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), + }); } @bindThis @@ -62,12 +77,13 @@ export class PushNotificationService { meta.swPublicKey, meta.swPrivateKey); - // Fetch - const subscriptions = await this.swSubscriptionsRepository.findBy({ - userId: userId, - }); + const subscriptions = await this.subscriptionsCache.fetch(userId); for (const subscription of subscriptions) { + if ([ + 'readAllNotifications', + ].includes(type) && !subscription.sendReadMessage) continue; + const pushSubscription = { endpoint: subscription.endpoint, keys: { |