forked from vosmiic/jellyfin-ani-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserDataServerEntry.cs
74 lines (69 loc) · 3.41 KB
/
UserDataServerEntry.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#nullable enable
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.TV;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.IO;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace jellyfin_ani_sync {
public class UserDataServerEntry : IHostedService {
private readonly IUserDataManager _userDataManager;
private readonly IFileSystem _fileSystem;
private readonly ILibraryManager _libraryManager;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IServerApplicationHost _serverApplicationHost;
private readonly IHttpClientFactory _httpClientFactory;
private readonly IMemoryCache _memoryCache;
private readonly IApplicationPaths _applicationPaths;
private readonly TaskProcessMarkedMedia _taskProcessMarkedMedia;
private Task? _updateTask;
public UserDataServerEntry(IUserDataManager userDataManager,
IFileSystem fileSystem,
ILibraryManager libraryManager,
ILoggerFactory loggerFactory,
IHttpContextAccessor httpContextAccessor,
IServerApplicationHost serverApplicationHost,
IHttpClientFactory httpClientFactory,
IMemoryCache memoryCache,
IApplicationPaths applicationPaths) {
_userDataManager = userDataManager;
_fileSystem = fileSystem;
_libraryManager = libraryManager;
loggerFactory.CreateLogger<UpdateProviderStatus>();
_httpContextAccessor = httpContextAccessor;
_serverApplicationHost = serverApplicationHost;
_httpClientFactory = httpClientFactory;
_memoryCache = memoryCache;
_applicationPaths = applicationPaths;
_taskProcessMarkedMedia = new TaskProcessMarkedMedia(loggerFactory, _libraryManager, _fileSystem, _memoryCache, _httpContextAccessor, _serverApplicationHost, _httpClientFactory, _applicationPaths);
}
public Task StartAsync(CancellationToken cancellationToken) {
_userDataManager.UserDataSaved += UserDataManagerOnUserDataSaved;
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken) {
_userDataManager.UserDataSaved -= UserDataManagerOnUserDataSaved;
return Task.CompletedTask;
}
private void UserDataManagerOnUserDataSaved(object? sender, UserDataSaveEventArgs e) {
if (e.SaveReason == UserDataSaveReason.TogglePlayed && Plugin.Instance?.PluginConfiguration.watchedTickboxUpdatesProvider == true) {
if (!e.UserData.Played || e.Item is not Video) return;
// asynchronous call so it doesn't prevent the UI marking the media as watched
Episode? episode = e.Item as Episode;
_taskProcessMarkedMedia.AddToUpdateList((e.UserId, episode?.Season.Id, e.Item as Video));
if (_updateTask == null || _updateTask.IsCompleted) {
_updateTask = _taskProcessMarkedMedia.RunUpdate();
}
}
}
}
}