diff --git a/AccountSync/AccountSync.csproj b/AccountSync/AccountSync.csproj index a44a49a..9d34683 100644 --- a/AccountSync/AccountSync.csproj +++ b/AccountSync/AccountSync.csproj @@ -2,8 +2,8 @@ netstandard2.0; - 1.0.0.2 - 1.0.0.3 + 1.2.0.0 + 1.2.0.0 diff --git a/AccountSync/AccountSyncScheduledTask.cs b/AccountSync/AccountSyncScheduledTask.cs index b36b061..7114663 100644 --- a/AccountSync/AccountSyncScheduledTask.cs +++ b/AccountSync/AccountSyncScheduledTask.cs @@ -42,8 +42,8 @@ public Task Execute(CancellationToken cancellationToken, IProgress progr { foreach (var syncProfile in Plugin.Instance.Configuration.SyncList) { - var syncToUser = UserManager.GetUserById(syncProfile.SyncToAccount); //Sync To - var syncFromUser = UserManager.GetUserById(syncProfile.SyncFromAccount); //Sync From + var syncToUser = UserManager.GetUserById(syncProfile.SyncToAccount); + var syncFromUser = UserManager.GetUserById(syncProfile.SyncFromAccount); var queryResultIds = LibraryManager.GetInternalItemIds(new InternalItemsQuery { IncludeItemTypes = new[] { "Movie", "Episode" } }); diff --git a/AccountSync/Configuration/PluginConfiguration.cs b/AccountSync/Configuration/PluginConfiguration.cs index 5fd4332..40d0a30 100644 --- a/AccountSync/Configuration/PluginConfiguration.cs +++ b/AccountSync/Configuration/PluginConfiguration.cs @@ -5,10 +5,7 @@ namespace AccountSync.Configuration { public class AccountSync { - //Sync To User (Admin) public string SyncToAccount { get; set; } - - //Sync From User (Sync'd to Admin) public string SyncFromAccount { get; set; } } diff --git a/AccountSync/Extensions.cs b/AccountSync/Extensions.cs new file mode 100644 index 0000000..9f516df --- /dev/null +++ b/AccountSync/Extensions.cs @@ -0,0 +1,16 @@ +namespace AccountSync +{ + using System.Linq; + using System.Text; + + public static class Extensions + { + public static string PropertiesToString(this object obj) + => obj.GetType().GetProperties() + .Select(info => (info.Name, Value: info.GetValue(obj, null) ?? "(null)")) + .Aggregate( + new StringBuilder("\n"), + (sb, pair) => sb.AppendLine($"{pair.Name}: {pair.Value}"), + sb => sb.ToString()); + } +} \ No newline at end of file diff --git a/AccountSync/ServerEntryPoint.cs b/AccountSync/ServerEntryPoint.cs index dd69568..ad55d1a 100644 --- a/AccountSync/ServerEntryPoint.cs +++ b/AccountSync/ServerEntryPoint.cs @@ -1,6 +1,5 @@ namespace AccountSync { - using System; using System.Linq; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Plugins; @@ -14,11 +13,11 @@ public class ServerEntryPoint : IServerEntryPoint private IUserManager UserManager { get; } private ILogger Log { get; } - public ServerEntryPoint(ISessionManager sesMan, IUserManager userMan, ILogManager logManager) + public ServerEntryPoint(ISessionManager sessionManager, IUserManager userManager, ILogManager logManager) { Instance = this; - SessionManager = sesMan; - UserManager = userMan; + SessionManager = sessionManager; + UserManager = userManager; Log = logManager.GetLogger(Plugin.Instance.Name); SessionManager.PlaybackStopped += SessionManager_PlaybackStopped; } @@ -26,16 +25,17 @@ public ServerEntryPoint(ISessionManager sesMan, IUserManager userMan, ILogManage private void SessionManager_PlaybackStopped(object sender, PlaybackStopEventArgs e) { var accountSyncs = Plugin.Instance.Configuration.SyncList.Where(user => user.SyncFromAccount == e.Session.UserId).ToList(); - + Log.Debug("Playback stopped. Syncing from {0}", e.Session.UserName); + foreach (var syncToUser in accountSyncs.Select(sync => UserManager.GetUserById(sync.SyncToAccount))) { + Log.Debug("Syncing from {0} to {1}", e.Session.UserName, syncToUser); Synchronize.SynchronizePlayState(syncToUser, e.Item, e.PlaybackPositionTicks, e.PlayedToCompletion); } } public void Dispose() { - throw new NotImplementedException(); } public void Run() diff --git a/AccountSync/Synchronize.cs b/AccountSync/Synchronize.cs index 824821f..b32e0bf 100644 --- a/AccountSync/Synchronize.cs +++ b/AccountSync/Synchronize.cs @@ -26,12 +26,20 @@ public static void SynchronizePlayState( { var syncToItemData = UserDataManager.GetUserData(syncToUser, item); //Sync To var syncFromItemData = UserDataManager.GetUserData(syncFromUser, item); //Sync From - + if ((syncToItemData.PlaybackPositionTicks != syncFromItemData.PlaybackPositionTicks || syncToItemData.Played != syncFromUser.Played) && (syncFromItemData.PlaystateLastModified > syncToItemData.PlaystateLastModified || syncFromItemData.LastPlayedDate > syncToItemData.LastPlayedDate)) { + Log.Debug($"From item data: {syncFromItemData.PropertiesToString()}"); + Log.Debug($"To item data: {syncToItemData.PropertiesToString()}"); + + var now = DateTimeOffset.Now; + syncToItemData.PlaybackPositionTicks = syncFromItemData.Played ? 0 : syncFromItemData.PlaybackPositionTicks; syncToItemData.Played = syncFromItemData.Played; + syncToItemData.PlayCount = syncFromItemData.PlayCount; + syncToItemData.PlaystateLastModified = syncFromItemData.PlaystateLastModified; + syncToItemData.LastPlayedDate = syncFromItemData.LastPlayedDate; UserDataManager.SaveUserData(syncToUser, item, syncToItemData, UserDataSaveReason.PlaybackProgress, CancellationToken.None); } @@ -44,16 +52,23 @@ public static void SynchronizePlayState( bool playedToCompletion) { var syncToUserItemData = UserDataManager.GetUserData(syncToUser, item); //Sync To + + Log.Debug($"Played position: {playbackPositionTicks}, played to completion: {playedToCompletion}"); + Log.Debug($"To item data: {syncToUserItemData.PropertiesToString()}"); + var now = DateTimeOffset.Now; + syncToUserItemData.PlaybackPositionTicks = playedToCompletion ? 0 : playbackPositionTicks ?? 0; syncToUserItemData.Played = playedToCompletion; + syncToUserItemData.PlayCount++; + syncToUserItemData.LastPlayedDate = now; + syncToUserItemData.PlaystateLastModified = now; UserDataManager.SaveUserData(syncToUser, item, syncToUserItemData, UserDataSaveReason.PlaybackProgress, CancellationToken.None); } public void Dispose() { - throw new NotImplementedException(); } public void Run()