Skip to content

Commit

Permalink
fix: remove creator if it's not found on anidb
Browse files Browse the repository at this point in the history
and also reschedule any related anime to be downloaded again
  • Loading branch information
revam committed Dec 27, 2024
1 parent 340435b commit a8b6399
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Shoko.Server/Repositories/Direct/AniDB_Anime_StaffRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
Expand Down
40 changes: 40 additions & 0 deletions Shoko.Server/Scheduling/Jobs/AniDB/GetAniDBCreatorJob.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit a8b6399

Please sign in to comment.