Compare commits

...

2 Commits

View File

@@ -33,52 +33,93 @@ import { EventSubscriber } from 'typeorm';
export class MediaRequestSubscriber export class MediaRequestSubscriber
implements EntitySubscriberInterface<MediaRequest> implements EntitySubscriberInterface<MediaRequest>
{ {
private async notifyAvailableMovie(entity: MediaRequest) { private async notifyAvailableMovie(
entity: MediaRequest,
event?: UpdateEvent<MediaRequest>
) {
// Get fresh media state using event manager
let latestMedia: Media | null = null;
if (event?.manager) {
latestMedia = await event.manager.findOne(Media, {
where: { id: entity.media.id },
});
}
if (!latestMedia) {
const mediaRepository = getRepository(Media);
latestMedia = await mediaRepository.findOne({
where: { id: entity.media.id },
});
}
// Check availability using fresh media state
if ( if (
entity.media[entity.is4k ? 'status4k' : 'status'] === !latestMedia ||
MediaStatus.AVAILABLE latestMedia[entity.is4k ? 'status4k' : 'status'] !== MediaStatus.AVAILABLE
) { ) {
const tmdb = new TheMovieDb(); return;
}
try { const tmdb = new TheMovieDb();
const movie = await tmdb.getMovie({
movieId: entity.media.tmdbId,
});
notificationManager.sendNotification(Notification.MEDIA_AVAILABLE, { try {
event: `${entity.is4k ? '4K ' : ''}Movie Request Now Available`, const movie = await tmdb.getMovie({
notifyAdmin: false, movieId: entity.media.tmdbId,
notifySystem: true, });
notifyUser: entity.requestedBy,
subject: `${movie.title}${ notificationManager.sendNotification(Notification.MEDIA_AVAILABLE, {
movie.release_date ? ` (${movie.release_date.slice(0, 4)})` : '' event: `${entity.is4k ? '4K ' : ''}Movie Request Now Available`,
}`, notifyAdmin: false,
message: truncate(movie.overview, { notifySystem: true,
length: 500, notifyUser: entity.requestedBy,
separator: /\s/, subject: `${movie.title}${
omission: '…', movie.release_date ? ` (${movie.release_date.slice(0, 4)})` : ''
}), }`,
media: entity.media, message: truncate(movie.overview, {
image: `https://image.tmdb.org/t/p/w600_and_h900_bestv2${movie.poster_path}`, length: 500,
request: entity, separator: /\s/,
}); omission: '…',
} catch (e) { }),
logger.error('Something went wrong sending media notification(s)', { media: latestMedia,
label: 'Notifications', image: `https://image.tmdb.org/t/p/w600_and_h900_bestv2${movie.poster_path}`,
errorMessage: e.message, request: entity,
mediaId: entity.id, });
}); } catch (e) {
} logger.error('Something went wrong sending media notification(s)', {
label: 'Notifications',
errorMessage: e.message,
mediaId: entity.id,
});
} }
} }
private async notifyAvailableSeries(entity: MediaRequest) { private async notifyAvailableSeries(
// Find all seasons in the related media entity entity: MediaRequest,
// and see if they are available, then we can check event?: UpdateEvent<MediaRequest>
// if the request contains the same seasons ) {
// Get fresh media state with seasons using event manager
let latestMedia: Media | null = null;
if (event?.manager) {
latestMedia = await event.manager.findOne(Media, {
where: { id: entity.media.id },
relations: { seasons: true },
});
}
if (!latestMedia) {
const mediaRepository = getRepository(Media);
latestMedia = await mediaRepository.findOne({
where: { id: entity.media.id },
relations: { seasons: true },
});
}
if (!latestMedia) {
return;
}
// Check availability using fresh media state
const requestedSeasons = const requestedSeasons =
entity.seasons?.map((entitySeason) => entitySeason.seasonNumber) ?? []; entity.seasons?.map((entitySeason) => entitySeason.seasonNumber) ?? [];
const availableSeasons = entity.media.seasons.filter( const availableSeasons = latestMedia.seasons.filter(
(season) => (season) =>
season[entity.is4k ? 'status4k' : 'status'] === MediaStatus.AVAILABLE && season[entity.is4k ? 'status4k' : 'status'] === MediaStatus.AVAILABLE &&
requestedSeasons.includes(season.seasonNumber) requestedSeasons.includes(season.seasonNumber)
@@ -87,44 +128,46 @@ export class MediaRequestSubscriber
availableSeasons.length > 0 && availableSeasons.length > 0 &&
availableSeasons.length === requestedSeasons.length; availableSeasons.length === requestedSeasons.length;
if (isMediaAvailable) { if (!isMediaAvailable) {
const tmdb = new TheMovieDb(); return;
}
try { const tmdb = new TheMovieDb();
const tv = await tmdb.getTvShow({ tvId: entity.media.tmdbId });
notificationManager.sendNotification(Notification.MEDIA_AVAILABLE, { try {
event: `${entity.is4k ? '4K ' : ''}Series Request Now Available`, const tv = await tmdb.getTvShow({ tvId: entity.media.tmdbId });
subject: `${tv.name}${
tv.first_air_date ? ` (${tv.first_air_date.slice(0, 4)})` : '' notificationManager.sendNotification(Notification.MEDIA_AVAILABLE, {
}`, event: `${entity.is4k ? '4K ' : ''}Series Request Now Available`,
message: truncate(tv.overview, { subject: `${tv.name}${
length: 500, tv.first_air_date ? ` (${tv.first_air_date.slice(0, 4)})` : ''
separator: /\s/, }`,
omission: '…', message: truncate(tv.overview, {
}), length: 500,
notifyAdmin: false, separator: /\s/,
notifySystem: true, omission: '…',
notifyUser: entity.requestedBy, }),
image: `https://image.tmdb.org/t/p/w600_and_h900_bestv2${tv.poster_path}`, notifyAdmin: false,
media: entity.media, notifySystem: true,
extra: [ notifyUser: entity.requestedBy,
{ image: `https://image.tmdb.org/t/p/w600_and_h900_bestv2${tv.poster_path}`,
name: 'Requested Seasons', media: latestMedia,
value: entity.seasons extra: [
.map((season) => season.seasonNumber) {
.join(', '), name: 'Requested Seasons',
}, value: entity.seasons
], .map((season) => season.seasonNumber)
request: entity, .join(', '),
}); },
} catch (e) { ],
logger.error('Something went wrong sending media notification(s)', { request: entity,
label: 'Notifications', });
errorMessage: e.message, } catch (e) {
mediaId: entity.id, logger.error('Something went wrong sending media notification(s)', {
}); label: 'Notifications',
} errorMessage: e.message,
mediaId: entity.id,
});
} }
} }
@@ -782,10 +825,10 @@ export class MediaRequestSubscriber
if (event.entity.status === MediaRequestStatus.COMPLETED) { if (event.entity.status === MediaRequestStatus.COMPLETED) {
if (event.entity.media.mediaType === MediaType.MOVIE) { if (event.entity.media.mediaType === MediaType.MOVIE) {
this.notifyAvailableMovie(event.entity as MediaRequest); this.notifyAvailableMovie(event.entity as MediaRequest, event);
} }
if (event.entity.media.mediaType === MediaType.TV) { if (event.entity.media.mediaType === MediaType.TV) {
this.notifyAvailableSeries(event.entity as MediaRequest); this.notifyAvailableSeries(event.entity as MediaRequest, event);
} }
} }
} }