Skip to content

Commit

Permalink
Fixes to transfer correct play state and dates.
Browse files Browse the repository at this point in the history
Add more logging for debugging purposes.
  • Loading branch information
m-soltys committed Jun 3, 2022
1 parent a23012d commit 62f5620
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 15 deletions.
4 changes: 2 additions & 2 deletions AccountSync/AccountSync.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<TargetFrameworks>netstandard2.0;</TargetFrameworks>
<AssemblyVersion>1.0.0.2</AssemblyVersion>
<FileVersion>1.0.0.3</FileVersion>
<AssemblyVersion>1.2.0.0</AssemblyVersion>
<FileVersion>1.2.0.0</FileVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions AccountSync/AccountSyncScheduledTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public Task Execute(CancellationToken cancellationToken, IProgress<double> 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" } });

Expand Down
3 changes: 0 additions & 3 deletions AccountSync/Configuration/PluginConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}

Expand Down
16 changes: 16 additions & 0 deletions AccountSync/Extensions.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
12 changes: 6 additions & 6 deletions AccountSync/ServerEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace AccountSync
{
using System;
using System.Linq;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Plugins;
Expand All @@ -14,28 +13,29 @@ 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;
}

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()
Expand Down
19 changes: 17 additions & 2 deletions AccountSync/Synchronize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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()
Expand Down

0 comments on commit 62f5620

Please sign in to comment.