Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion Trakt/Helpers/LibraryManagerEventsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using Microsoft.Extensions.Logging;
using Trakt.Api;
Expand All @@ -19,18 +20,21 @@ internal sealed class LibraryManagerEventsHelper : IDisposable
private readonly List<LibraryEvent> _queuedEvents;
private readonly ILogger<LibraryManagerEventsHelper> _logger;
private readonly TraktApi _traktApi;
private readonly ILibraryManager _libraryManager;
private Timer _queueTimer;

/// <summary>
/// Initializes a new instance of the <see cref="LibraryManagerEventsHelper"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
/// <param name="traktApi">The <see cref="TraktApi"/>.</param>
public LibraryManagerEventsHelper(ILogger<LibraryManagerEventsHelper> logger, TraktApi traktApi)
/// <param name="libraryManager">The <see cref="ILibraryManager"/>.</param>
public LibraryManagerEventsHelper(ILogger<LibraryManagerEventsHelper> logger, TraktApi traktApi, ILibraryManager libraryManager)
{
_queuedEvents = new List<LibraryEvent>();
_logger = logger;
_traktApi = traktApi;
_libraryManager = libraryManager;
}

/// <summary>
Expand Down Expand Up @@ -112,6 +116,8 @@ private async Task OnQueueTimerCallbackInternal()
_queuedEvents.Clear();
}

queue = ReloadQueuedItems(queue);

var queuedMovieDeletes = new List<LibraryEvent>();
var queuedMovieAdds = new List<LibraryEvent>();
var queuedMovieUpdates = new List<LibraryEvent>();
Expand Down Expand Up @@ -187,6 +193,60 @@ private async Task OnQueueTimerCallbackInternal()
}
}

/// <summary>
/// Reloads the queued movies and episodes to add or update from the library database, once per item and user.
/// </summary>
/// <param name="queue">The queued <see cref="LibraryEvent"/> list.</param>
/// <returns>The events to process.</returns>
private List<LibraryEvent> ReloadQueuedItems(List<LibraryEvent> queue)
{
var events = new List<LibraryEvent>();
var queuedItems = new HashSet<(Guid ItemId, Guid UserId)>();

// Add and Update send the same request, so an item that was added and then updated is sent once, as an Add (Adds go first)
foreach (var ev in queue.OrderBy(e => e.EventType != EventType.Add))
{
if (ev.EventType == EventType.Remove || ev.Item is not Movie and not Episode)
{
// A removed item is no longer in the library, and shows are sent as queued
events.Add(ev);
continue;
}

var key = (ev.Item.Id, ev.TraktUser.LinkedMbUserId);
if (!queuedItems.Add(key))
{
continue;
}

// The item from the ItemAdded event can be the copy from before the metadata refresh (a library scan refreshes
// another copy): folder name as title, no imdb id and no default video stream. Read the item as last saved.
BaseItem item;
try
{
item = _libraryManager.RetrieveItem(ev.Item.Id);
}
catch (Exception ex)
{
// Rather than lose the whole batch, send this event as queued and try the item's next event again
_logger.LogWarning(ex, "Could not reload {Item} from the library, syncing it as queued", ev.Item.Path);
queuedItems.Remove(key);
events.Add(ev);
continue;
}

if (item == null)
{
_logger.LogDebug("Not syncing {Item}: it is no longer in the library", ev.Item.Path);
continue;
}

events.Add(new LibraryEvent { Item = item, TraktUser = ev.TraktUser, EventType = ev.EventType });
}

return events;
}

private async Task ProcessQueuedShowEvents(IReadOnlyCollection<LibraryEvent> events, TraktUser traktUser, EventType eventType)
{
if (events.Count == 0)
Expand Down
2 changes: 1 addition & 1 deletion Trakt/ServerMediator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ServerMediator(
_playbackState = new Dictionary<Guid, PlaybackState>();

_traktApi = new TraktApi(loggerFactory.CreateLogger<TraktApi>(), httpClientFactory, appHost, userDataManager, userManager);
_libraryManagerEventsHelper = new LibraryManagerEventsHelper(loggerFactory.CreateLogger<LibraryManagerEventsHelper>(), _traktApi);
_libraryManagerEventsHelper = new LibraryManagerEventsHelper(loggerFactory.CreateLogger<LibraryManagerEventsHelper>(), _traktApi, libraryManager);
_userDataManagerEventsHelper = new UserDataManagerEventsHelper(loggerFactory.CreateLogger<UserDataManagerEventsHelper>(), _traktApi);
}

Expand Down