From a8b63991673a0aec5ce9d395ade895feafe3d5c5 Mon Sep 17 00:00:00 2001
From: Mikal Stordal <mikalstordal@gmail.com>
Date: Fri, 27 Dec 2024 02:30:13 +0100
Subject: [PATCH] fix: remove creator if it's not found on anidb and also
 reschedule any related anime to be downloaded again

---
 .../Direct/AniDB_Anime_StaffRepository.cs     | 11 +++++
 .../Jobs/AniDB/GetAniDBCreatorJob.cs          | 40 +++++++++++++++++++
 2 files changed, 51 insertions(+)

diff --git a/Shoko.Server/Repositories/Direct/AniDB_Anime_StaffRepository.cs b/Shoko.Server/Repositories/Direct/AniDB_Anime_StaffRepository.cs
index f1d4255e3..2c465b0c3 100644
--- a/Shoko.Server/Repositories/Direct/AniDB_Anime_StaffRepository.cs
+++ b/Shoko.Server/Repositories/Direct/AniDB_Anime_StaffRepository.cs
@@ -18,6 +18,17 @@ public List<AniDB_Anime_Staff> GetByAnimeID(int id)
         });
     }
 
+    public List<AniDB_Anime_Staff> GetByCreatorID(int creatorID)
+    {
+        return Lock(() =>
+        {
+            using var session = _databaseFactory.SessionFactory.OpenStatelessSession();
+            return session.Query<AniDB_Anime_Staff>()
+                .Where(a => a.CreatorID == creatorID)
+                .ToList();
+        });
+    }
+
     public AniDB_Anime_StaffRepository(DatabaseFactory databaseFactory) : base(databaseFactory)
     {
     }
diff --git a/Shoko.Server/Scheduling/Jobs/AniDB/GetAniDBCreatorJob.cs b/Shoko.Server/Scheduling/Jobs/AniDB/GetAniDBCreatorJob.cs
index 74438239d..fcb52ab4e 100644
--- a/Shoko.Server/Scheduling/Jobs/AniDB/GetAniDBCreatorJob.cs
+++ b/Shoko.Server/Scheduling/Jobs/AniDB/GetAniDBCreatorJob.cs
@@ -1,8 +1,10 @@
 using System.Collections.Generic;
 using System.IO;
+using System.Linq;
 using System.Threading.Tasks;
 using Microsoft.Extensions.Logging;
 using Quartz;
+using Shoko.Commons.Extensions;
 using Shoko.Plugin.Abstractions.Enums;
 using Shoko.Server.Extensions;
 using Shoko.Server.Providers.AniDB.Interfaces;
@@ -60,6 +62,44 @@ public override async Task Process()
         if (response is null)
         {
             _logger.LogError("Unable to find an AniDB Creator with the given ID: {CreatorID}", CreatorID);
+            var anidbAnimeStaffRoles = RepoFactory.AniDB_Anime_Staff.GetByAnimeID(CreatorID);
+            var anidbCharacterCreators = RepoFactory.AniDB_Character_Creator.GetByCreatorID(CreatorID);
+            var anidbAnimeCharacters = anidbCharacterCreators
+                .SelectMany(c => RepoFactory.AniDB_Anime_Character.GetByCharID(c.CharacterID))
+                .ToList();
+            var animeStaff = RepoFactory.AnimeStaff.GetByAniDBID(CreatorID);
+            var animeStaffRoles = animeStaff is not null ? RepoFactory.CrossRef_Anime_Staff.GetByStaffID(animeStaff.StaffID) : [];
+            var anidbAnime = anidbAnimeStaffRoles.Select(a => a.AnimeID)
+                .Concat(anidbAnimeCharacters.Select(a => a.AnimeID))
+                .Distinct()
+                .Select(RepoFactory.AniDB_Anime.GetByAnimeID)
+                .WhereNotNull()
+                .ToList();
+
+            RepoFactory.AniDB_Creator.Delete(CreatorID);
+            RepoFactory.AniDB_Character_Creator.Delete(anidbCharacterCreators);
+            RepoFactory.AniDB_Anime_Staff.Delete(anidbAnimeStaffRoles);
+            if (animeStaff is not null)
+            {
+                RepoFactory.AnimeStaff.Delete(animeStaff);
+                RepoFactory.CrossRef_Anime_Staff.Delete(animeStaffRoles);
+            }
+
+            if (anidbAnime.Count > 0)
+            {
+                _logger.LogInformation("Scheduling {Count} AniDB Anime for a refresh due to removal of creator: {CreatorID}", anidbAnime.Count, CreatorID);
+                var scheduler = await _schedulerFactory.GetScheduler().ConfigureAwait(false);
+                foreach (var anime in anidbAnime)
+                    await scheduler.StartJob<GetAniDBAnimeJob>(c =>
+                    {
+                        c.AnimeID = anime.AnimeID;
+                        c.ForceRefresh = true;
+                        c.CacheOnly = false;
+                        c.CreateSeriesEntry = false;
+                        c.DownloadRelations = false;
+                    });
+            }
+
             return;
         }