Skip to content

Commit

Permalink
fix: prevent season deletion in preference of setting season to unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
OwsleyJr committed Jan 31, 2023
1 parent e9ec1b2 commit 6b6bec4
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 75 deletions.
167 changes: 93 additions & 74 deletions server/lib/availabilitySync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class AvailabilitySync {
//We can not delete media so if both versions do not exist, we will change both columns to unknown or null
if (!mediaExists) {
if (
media.status !== MediaStatus.UNKNOWN &&
media.status !== MediaStatus.UNKNOWN ||
media.status4k !== MediaStatus.UNKNOWN
) {
const request = await requestRepository.find({
Expand All @@ -71,7 +71,9 @@ class AvailabilitySync {
});

logger.debug(
`${media.tmdbId} does not exist in any of your media instances. We will change its status to unknown.`,
`${
media.mediaType === 'tv' ? media.tvdbId : media.tmdbId
} does not exist in any of your media instances. We will change its status to unknown.`,
{ label: 'AvailabilitySync' }
);

Expand All @@ -90,75 +92,107 @@ class AvailabilitySync {

await requestRepository.remove(request);
}
continue;
}

if (media.mediaType === 'tv') {
// ok, the show itself exists, but do all it's seasons?
const seasons = await seasonRepository.find({
where: [
{ status: MediaStatus.AVAILABLE, media: { id: media.id } },
{
status: MediaStatus.PARTIALLY_AVAILABLE,
media: { id: media.id },
},
{ status4k: MediaStatus.AVAILABLE, media: { id: media.id } },
{
status4k: MediaStatus.PARTIALLY_AVAILABLE,
media: { id: media.id },
},
],
});

let didDeleteSeasons = false;
for (const season of seasons) {
const seasonExists = await this.seasonExists(media, season);

if (!seasonExists) {
logger.debug(
`Removing season ${season.seasonNumber}, media id: ${media.tmdbId} because it doesn't appear in any library.`,
{ label: 'AvailabilitySync' }
if (
!mediaExists &&
(season.status !== MediaStatus.UNKNOWN ||
season.status4k !== MediaStatus.UNKNOWN)
) {
await seasonRepository.update(
{ id: season.id },
{
status: MediaStatus.UNKNOWN,
status4k: MediaStatus.UNKNOWN,
}
);

const seasonToBeDeleted =
await seasonRequestRepository.findOne({
relations: {
request: {
media: true,
} else {
const seasonExists = await this.seasonExists(media, season);

if (!seasonExists) {
logger.debug(
`Removing season ${season.seasonNumber}, media id: ${media.tvdbId} because it does not exist in any of your media instances.`,
{ label: 'AvailabilitySync' }
);

if (
season.status !== MediaStatus.UNKNOWN ||
season.status4k !== MediaStatus.UNKNOWN
) {
await seasonRepository.update(
{ id: season.id },
{
status: MediaStatus.UNKNOWN,
status4k: MediaStatus.UNKNOWN,
}
);
}

const seasonToBeDeleted =
await seasonRequestRepository.findOne({
relations: {
request: {
media: true,
},
},
},
where: {
request: {
media: {
id: media.id,
where: {
request: {
media: {
id: media.id,
},
},
seasonNumber: season.seasonNumber,
},
seasonNumber: season.seasonNumber,
},
});
});

await seasonRepository.delete(season.id);
if (seasonToBeDeleted) {
await seasonRequestRepository.remove(seasonToBeDeleted);
}

if (seasonToBeDeleted) {
await seasonRequestRepository.remove(seasonToBeDeleted);
didDeleteSeasons = true;
}

didDeleteSeasons = true;
}
}

if (didDeleteSeasons) {
if (
media.status === MediaStatus.AVAILABLE ||
media.status4k === MediaStatus.AVAILABLE
) {
logger.debug(
`Marking media id: ${media.tmdbId} as PARTIALLY_AVAILABLE because we deleted some of its seasons.`,
{ label: 'AvailabilitySync' }
);

if (media.status === MediaStatus.AVAILABLE) {
await mediaRepository.update(media.id, {
status: MediaStatus.PARTIALLY_AVAILABLE,
});
}

if (media.status4k === MediaStatus.AVAILABLE) {
await mediaRepository.update(media.id, {
status4k: MediaStatus.PARTIALLY_AVAILABLE,
});
if (didDeleteSeasons) {
if (
media.status === MediaStatus.AVAILABLE ||
media.status4k === MediaStatus.AVAILABLE
) {
logger.debug(
`Marking media id: ${media.tvdbId} as PARTIALLY_AVAILABLE because we deleted some of its seasons.`,
{ label: 'AvailabilitySync' }
);

if (media.status === MediaStatus.AVAILABLE) {
await mediaRepository.update(media.id, {
status: MediaStatus.PARTIALLY_AVAILABLE,
});
}

if (media.status4k === MediaStatus.AVAILABLE) {
await mediaRepository.update(media.id, {
status4k: MediaStatus.PARTIALLY_AVAILABLE,
});
}
}
}
}
Expand Down Expand Up @@ -198,20 +232,15 @@ class AvailabilitySync {
{ status4k: MediaStatus.PARTIALLY_AVAILABLE },
];

let mediaPage = await mediaRepository.find({
where: whereOptions,
skip: offset,
take: pageSize,
});
let mediaPage: Media[];

do {
yield mediaPage;
offset += pageSize;
mediaPage = await mediaRepository.find({
yield (mediaPage = await mediaRepository.find({
where: whereOptions,
skip: offset,
take: pageSize,
});
}));
offset += pageSize;
} while (mediaPage.length > 0);
}

Expand Down Expand Up @@ -285,7 +314,6 @@ class AvailabilitySync {

//check if both exist or if a single non-4k or 4k exists
//if both do not exist we will return false

if (!server.is4k && !meta.id) {
existsInRadarr = false;
}
Expand All @@ -309,7 +337,6 @@ class AvailabilitySync {

//if only a single non-4k or 4k exists, then change entity columns accordingly
//related media request will then be deleted

if (!existsInRadarr && existsInRadarr4k && !existsInPlex) {
if (media.status !== MediaStatus.UNKNOWN) {
this.mediaUpdater(media, false);
Expand Down Expand Up @@ -353,7 +380,6 @@ class AvailabilitySync {

//check if both exist or if a single non-4k or 4k exists
//if both do not exist we will return false

if (!server.is4k && !meta.id) {
existsInSonarr = false;
}
Expand All @@ -377,7 +403,6 @@ class AvailabilitySync {

//if only a single non-4k or 4k exists, then change entity columns accordingly
//related media request will then be deleted

if (!existsInSonarr && existsInSonarr4k && !existsInPlex) {
if (media.status !== MediaStatus.UNKNOWN) {
this.mediaUpdater(media, false);
Expand All @@ -402,7 +427,7 @@ class AvailabilitySync {
season: Season,
seasonExistsInPlex: boolean,
seasonExistsInPlex4k: boolean
) {
): Promise<boolean> {
if (!media.tvdbId) {
return false;
}
Expand Down Expand Up @@ -470,7 +495,6 @@ class AvailabilitySync {

//if season does not exist, we will change status to unknown and delete related season request
//if parent media request is empty(all related seasons have been removed), parent is automatically deleted

if (
!seasonExistsInSonarr &&
seasonExistsInSonarr4k &&
Expand All @@ -491,7 +515,7 @@ class AvailabilitySync {

if (media.status === MediaStatus.AVAILABLE) {
logger.debug(
`Marking media id: ${media.tmdbId} as PARTIALLY_AVAILABLE because we deleted one of its seasons.`,
`Marking media id: ${media.tvdbId} as PARTIALLY_AVAILABLE because we deleted one of its seasons.`,
{ label: 'AvailabilitySync' }
);
await mediaRepository.update(media.id, {
Expand Down Expand Up @@ -521,7 +545,7 @@ class AvailabilitySync {

if (media.status4k === MediaStatus.AVAILABLE) {
logger.debug(
`Marking media id: ${media.tmdbId} as PARTIALLY_AVAILABLE because we deleted one of its seasons.`,
`Marking media id: ${media.tvdbId} as PARTIALLY_AVAILABLE because we deleted one of its seasons.`,
{ label: 'AvailabilitySync' }
);
await mediaRepository.update(media.id, {
Expand All @@ -546,7 +570,6 @@ class AvailabilitySync {
let existsInPlex4k = false;

//check each plex instance to see if media exists

try {
if (ratingKey) {
const meta = await this.plexClient?.getMetadata(ratingKey);
Expand All @@ -566,16 +589,13 @@ class AvailabilitySync {
throw ex;
}
}

//base case for if both exist in plex

//base case for if both media versions exist in plex
if (existsInPlex && existsInPlex4k) {
return true;
}

//we then check radarr or sonarr has that specific media. If not, then we will move to delete
//if a non-4k or 4k version exists in at least one of the instances, we will only update that specific version

if (media.mediaType === 'movie') {
const existsInRadarr = await this.mediaExistsInRadarr(
media,
Expand All @@ -584,7 +604,6 @@ class AvailabilitySync {
);

//if true, media exists in at least one radarr or plex instance.

if (existsInRadarr) {
logger.warn(
`${media.tmdbId} exists in at least one radarr or plex instance. Media will be updated if set to available.`,
Expand All @@ -605,10 +624,9 @@ class AvailabilitySync {
);

//if true, media exists in at least one sonarr or plex instance.

if (existsInSonarr) {
logger.warn(
`${media.tmdbId} exists in at least one sonarr or plex instance. Media will be updated if set to available.`,
`${media.tvdbId} exists in at least one sonarr or plex instance. Media will be updated if set to available.`,
{
label: 'AvailabilitySync',
}
Expand Down Expand Up @@ -658,6 +676,7 @@ class AvailabilitySync {
}
}

//base case for if both season versions exist in plex
if (seasonExistsInPlex && seasonExistsInPlex4k) {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/TitleCard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ const TitleCard = ({
: intl.formatMessage(globalMessages.tvshow)}
</div>
</div>
{currentStatus && (
{currentStatus && currentStatus !== MediaStatus.UNKNOWN && (
<div className="pointer-events-none z-40 flex items-center">
<StatusBadgeMini
status={currentStatus}
Expand Down

0 comments on commit 6b6bec4

Please sign in to comment.