diff --git a/server/entity/MediaRequest.ts b/server/entity/MediaRequest.ts index 9ad4caa48..e980860c6 100644 --- a/server/entity/MediaRequest.ts +++ b/server/entity/MediaRequest.ts @@ -1163,12 +1163,6 @@ export class MediaRequest { private async sendNotification(media: Media, type: Notification) { const tmdb = new TheMovieDb(); - const requestRepository = getRepository(MediaRequest); - - const pendingRequests = await requestRepository.find({ - where: { status: MediaRequestStatus.PENDING }, - }); - try { const mediaType = this.type === MediaType.MOVIE ? 'Movie' : 'Series'; let event: string | undefined; @@ -1178,9 +1172,11 @@ export class MediaRequest { switch (type) { case Notification.MEDIA_APPROVED: event = `${this.is4k ? '4K ' : ''}${mediaType} Request Approved`; + notifyAdmin = false; break; case Notification.MEDIA_DECLINED: event = `${this.is4k ? '4K ' : ''}${mediaType} Request Declined`; + notifyAdmin = false; break; case Notification.MEDIA_PENDING: event = `New ${this.is4k ? '4K ' : ''}${mediaType} Request`; @@ -1209,9 +1205,7 @@ export class MediaRequest { request: this, notifyAdmin, notifySystem, - notifyUser: Notification.MEDIA_AUTO_REQUESTED - ? this.requestedBy - : undefined, + notifyUser: notifyAdmin ? undefined : this.requestedBy, event, subject: `${movie.title}${ movie.release_date ? ` (${movie.release_date.slice(0, 4)})` : '' @@ -1222,7 +1216,6 @@ export class MediaRequest { omission: '…', }), image: `https://image.tmdb.org/t/p/w600_and_h900_bestv2${movie.poster_path}`, - pendingRequestsCount: pendingRequests.length, }); } else if (this.type === MediaType.TV) { const tv = await tmdb.getTvShow({ tvId: media.tmdbId }); @@ -1250,7 +1243,6 @@ export class MediaRequest { .join(', '), }, ], - pendingRequestsCount: pendingRequests.length, }); } } catch (e) { diff --git a/server/lib/notifications/agents/webpush.ts b/server/lib/notifications/agents/webpush.ts index 3095e52e4..5972b1159 100644 --- a/server/lib/notifications/agents/webpush.ts +++ b/server/lib/notifications/agents/webpush.ts @@ -1,6 +1,7 @@ import { IssueType, IssueTypeName } from '@server/constants/issue'; -import { MediaType } from '@server/constants/media'; +import { MediaRequestStatus, MediaType } from '@server/constants/media'; import { getRepository } from '@server/datasource'; +import MediaRequest from '@server/entity/MediaRequest'; import { User } from '@server/entity/User'; import { UserPushSubscription } from '@server/entity/UserPushSubscription'; import type { NotificationAgentConfig } from '@server/lib/settings'; @@ -156,6 +157,12 @@ class WebPushAgent const mainUser = await userRepository.findOne({ where: { id: 1 } }); + const requestRepository = getRepository(MediaRequest); + + const pendingRequests = await requestRepository.find({ + where: { status: MediaRequestStatus.PENDING }, + }); + const webPushNotification = async ( pushSub: UserPushSubscription, notificationPayload: Buffer @@ -212,7 +219,11 @@ class WebPushAgent pushSubs.push(...notifySubs); } - if (payload.notifyAdmin) { + if ( + payload.notifyAdmin || + type === Notification.MEDIA_APPROVED || + type === Notification.MEDIA_DECLINED + ) { const users = await userRepository.find(); const manageUsers = users.filter( @@ -235,6 +246,8 @@ class WebPushAgent }) .getMany(); + //We only want to send the custom notification when type is approved or declined + //Otherwise, default to the normal notification if ( type === Notification.MEDIA_APPROVED || type === Notification.MEDIA_DECLINED @@ -246,6 +259,7 @@ class WebPushAgent settings.vapidPrivate ); + //Custom payload only for updating the app badge const notificationBadgePayload = Buffer.from( JSON.stringify( this.getNotificationPayload(type, { @@ -253,7 +267,7 @@ class WebPushAgent notifySystem: false, notifyAdmin: true, isAdmin: true, - pendingRequestsCount: payload.pendingRequestsCount, + pendingRequestsCount: pendingRequests.length, }) ), 'utf-8' @@ -277,6 +291,10 @@ class WebPushAgent settings.vapidPrivate ); + if (type === Notification.MEDIA_PENDING) { + payload = { ...payload, pendingRequestsCount: pendingRequests.length }; + } + const notificationPayload = Buffer.from( JSON.stringify(this.getNotificationPayload(type, payload)), 'utf-8'