fix: moved all notify user logic into webpush

pull/3411/head
Brandon 2 years ago committed by Brandon Cohen
parent e3478a18dd
commit 6e4a52188d

@ -1163,12 +1163,6 @@ export class MediaRequest {
private async sendNotification(media: Media, type: Notification) { private async sendNotification(media: Media, type: Notification) {
const tmdb = new TheMovieDb(); const tmdb = new TheMovieDb();
const requestRepository = getRepository(MediaRequest);
const pendingRequests = await requestRepository.find({
where: { status: MediaRequestStatus.PENDING },
});
try { try {
const mediaType = this.type === MediaType.MOVIE ? 'Movie' : 'Series'; const mediaType = this.type === MediaType.MOVIE ? 'Movie' : 'Series';
let event: string | undefined; let event: string | undefined;
@ -1178,9 +1172,11 @@ export class MediaRequest {
switch (type) { switch (type) {
case Notification.MEDIA_APPROVED: case Notification.MEDIA_APPROVED:
event = `${this.is4k ? '4K ' : ''}${mediaType} Request Approved`; event = `${this.is4k ? '4K ' : ''}${mediaType} Request Approved`;
notifyAdmin = false;
break; break;
case Notification.MEDIA_DECLINED: case Notification.MEDIA_DECLINED:
event = `${this.is4k ? '4K ' : ''}${mediaType} Request Declined`; event = `${this.is4k ? '4K ' : ''}${mediaType} Request Declined`;
notifyAdmin = false;
break; break;
case Notification.MEDIA_PENDING: case Notification.MEDIA_PENDING:
event = `New ${this.is4k ? '4K ' : ''}${mediaType} Request`; event = `New ${this.is4k ? '4K ' : ''}${mediaType} Request`;
@ -1209,9 +1205,7 @@ export class MediaRequest {
request: this, request: this,
notifyAdmin, notifyAdmin,
notifySystem, notifySystem,
notifyUser: Notification.MEDIA_AUTO_REQUESTED notifyUser: notifyAdmin ? undefined : this.requestedBy,
? this.requestedBy
: undefined,
event, event,
subject: `${movie.title}${ subject: `${movie.title}${
movie.release_date ? ` (${movie.release_date.slice(0, 4)})` : '' movie.release_date ? ` (${movie.release_date.slice(0, 4)})` : ''
@ -1222,7 +1216,6 @@ export class MediaRequest {
omission: '…', omission: '…',
}), }),
image: `https://image.tmdb.org/t/p/w600_and_h900_bestv2${movie.poster_path}`, image: `https://image.tmdb.org/t/p/w600_and_h900_bestv2${movie.poster_path}`,
pendingRequestsCount: pendingRequests.length,
}); });
} else if (this.type === MediaType.TV) { } else if (this.type === MediaType.TV) {
const tv = await tmdb.getTvShow({ tvId: media.tmdbId }); const tv = await tmdb.getTvShow({ tvId: media.tmdbId });
@ -1250,7 +1243,6 @@ export class MediaRequest {
.join(', '), .join(', '),
}, },
], ],
pendingRequestsCount: pendingRequests.length,
}); });
} }
} catch (e) { } catch (e) {

@ -1,6 +1,7 @@
import { IssueType, IssueTypeName } from '@server/constants/issue'; 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 { getRepository } from '@server/datasource';
import MediaRequest from '@server/entity/MediaRequest';
import { User } from '@server/entity/User'; import { User } from '@server/entity/User';
import { UserPushSubscription } from '@server/entity/UserPushSubscription'; import { UserPushSubscription } from '@server/entity/UserPushSubscription';
import type { NotificationAgentConfig } from '@server/lib/settings'; import type { NotificationAgentConfig } from '@server/lib/settings';
@ -156,6 +157,12 @@ class WebPushAgent
const mainUser = await userRepository.findOne({ where: { id: 1 } }); const mainUser = await userRepository.findOne({ where: { id: 1 } });
const requestRepository = getRepository(MediaRequest);
const pendingRequests = await requestRepository.find({
where: { status: MediaRequestStatus.PENDING },
});
const webPushNotification = async ( const webPushNotification = async (
pushSub: UserPushSubscription, pushSub: UserPushSubscription,
notificationPayload: Buffer notificationPayload: Buffer
@ -212,7 +219,11 @@ class WebPushAgent
pushSubs.push(...notifySubs); pushSubs.push(...notifySubs);
} }
if (payload.notifyAdmin) { if (
payload.notifyAdmin ||
type === Notification.MEDIA_APPROVED ||
type === Notification.MEDIA_DECLINED
) {
const users = await userRepository.find(); const users = await userRepository.find();
const manageUsers = users.filter( const manageUsers = users.filter(
@ -235,6 +246,8 @@ class WebPushAgent
}) })
.getMany(); .getMany();
//We only want to send the custom notification when type is approved or declined
//Otherwise, default to the normal notification
if ( if (
type === Notification.MEDIA_APPROVED || type === Notification.MEDIA_APPROVED ||
type === Notification.MEDIA_DECLINED type === Notification.MEDIA_DECLINED
@ -246,6 +259,7 @@ class WebPushAgent
settings.vapidPrivate settings.vapidPrivate
); );
//Custom payload only for updating the app badge
const notificationBadgePayload = Buffer.from( const notificationBadgePayload = Buffer.from(
JSON.stringify( JSON.stringify(
this.getNotificationPayload(type, { this.getNotificationPayload(type, {
@ -253,7 +267,7 @@ class WebPushAgent
notifySystem: false, notifySystem: false,
notifyAdmin: true, notifyAdmin: true,
isAdmin: true, isAdmin: true,
pendingRequestsCount: payload.pendingRequestsCount, pendingRequestsCount: pendingRequests.length,
}) })
), ),
'utf-8' 'utf-8'
@ -277,6 +291,10 @@ class WebPushAgent
settings.vapidPrivate settings.vapidPrivate
); );
if (type === Notification.MEDIA_PENDING) {
payload = { ...payload, pendingRequestsCount: pendingRequests.length };
}
const notificationPayload = Buffer.from( const notificationPayload = Buffer.from(
JSON.stringify(this.getNotificationPayload(type, payload)), JSON.stringify(this.getNotificationPayload(type, payload)),
'utf-8' 'utf-8'

Loading…
Cancel
Save