From 3a90bcc03cc166632fb64aa76130b63a0ad37a64 Mon Sep 17 00:00:00 2001 From: tamaina Date: Tue, 11 Apr 2023 14:11:39 +0900 Subject: sw: なんかもうめっちゃ変えた (#10570) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * sw: なんかいろいろ * remove debug code * never renotify * update changelog.md --- packages/backend/src/core/PushNotificationService.ts | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'packages/backend/src/core/PushNotificationService.ts') diff --git a/packages/backend/src/core/PushNotificationService.ts b/packages/backend/src/core/PushNotificationService.ts index 69020f7e84..fd24831fbc 100644 --- a/packages/backend/src/core/PushNotificationService.ts +++ b/packages/backend/src/core/PushNotificationService.ts @@ -15,6 +15,7 @@ type PushNotificationsTypes = { antenna: { id: string, name: string }; note: Packed<'Note'>; }; + 'readAllNotifications': undefined; }; // Reduce length because push message servers have character limits @@ -68,6 +69,10 @@ export class PushNotificationService { }); for (const subscription of subscriptions) { + if ([ + 'readAllNotifications', + ].includes(type) && !subscription.sendReadMessage) continue; + const pushSubscription = { endpoint: subscription.endpoint, keys: { -- cgit v1.2.3-freya From c10d591bd09f18f939f21650def76a422d01d78b Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 11 Apr 2023 14:20:16 +0900 Subject: perf(backend): cache swSubscriptions --- .../backend/src/core/PushNotificationService.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'packages/backend/src/core/PushNotificationService.ts') diff --git a/packages/backend/src/core/PushNotificationService.ts b/packages/backend/src/core/PushNotificationService.ts index fd24831fbc..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 = { @@ -41,15 +43,27 @@ function truncateBody(type: T, body: Pus @Injectable() export class PushNotificationService { + private subscriptionsCache: RedisKVCache; + 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(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 @@ -63,10 +77,7 @@ 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 ([ -- cgit v1.2.3-freya