2025-01-18 16:17:26 +00:00
|
|
|
#pragma warning disable RS0030 // Do not use banned APIs
|
|
|
|
|
|
2019-01-13 20:54:44 +01:00
|
|
|
using System;
|
2019-01-13 20:21:32 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
2024-09-07 19:07:34 +00:00
|
|
|
using System.Linq;
|
2019-01-13 20:21:32 +01:00
|
|
|
using System.Threading;
|
2025-03-27 18:16:54 -06:00
|
|
|
using BitFaster.Caching.Lru;
|
2025-03-25 15:30:22 +00:00
|
|
|
using Jellyfin.Database.Implementations;
|
2025-03-25 16:45:00 +01:00
|
|
|
using Jellyfin.Database.Implementations.Entities;
|
2019-01-13 20:21:32 +01:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2013-10-02 15:08:58 -04:00
|
|
|
using MediaBrowser.Controller.Entities;
|
2013-10-02 12:08:58 -04:00
|
|
|
using MediaBrowser.Controller.Library;
|
2014-07-03 22:22:57 -04:00
|
|
|
using MediaBrowser.Model.Dto;
|
2013-10-02 13:23:10 -04:00
|
|
|
using MediaBrowser.Model.Entities;
|
2024-09-07 19:07:34 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2020-12-30 08:48:33 -05:00
|
|
|
using AudioBook = MediaBrowser.Controller.Entities.AudioBook;
|
2021-04-17 11:37:55 +01:00
|
|
|
using Book = MediaBrowser.Controller.Entities.Book;
|
2013-10-02 12:08:58 -04:00
|
|
|
|
2016-11-03 03:14:14 -04:00
|
|
|
namespace Emby.Server.Implementations.Library
|
2013-10-02 12:08:58 -04:00
|
|
|
{
|
|
|
|
|
/// <summary>
|
2019-11-01 18:38:54 +01:00
|
|
|
/// Class UserDataManager.
|
2013-10-02 12:08:58 -04:00
|
|
|
/// </summary>
|
|
|
|
|
public class UserDataManager : IUserDataManager
|
|
|
|
|
{
|
2014-12-26 12:45:06 -05:00
|
|
|
private readonly IServerConfigurationManager _config;
|
2024-09-07 19:07:34 +00:00
|
|
|
private readonly IDbContextFactory<JellyfinDbContext> _repository;
|
2025-03-27 18:16:54 -06:00
|
|
|
private readonly FastConcurrentLru<string, UserItemData> _cache;
|
2020-04-04 19:57:26 -04:00
|
|
|
|
2024-08-30 15:08:56 +02:00
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a new instance of the <see cref="UserDataManager"/> class.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="config">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
|
2024-09-07 19:07:34 +00:00
|
|
|
/// <param name="repository">Instance of the <see cref="IDbContextFactory{JellyfinDbContext}"/> interface.</param>
|
2020-04-04 19:57:26 -04:00
|
|
|
public UserDataManager(
|
|
|
|
|
IServerConfigurationManager config,
|
2024-09-07 19:07:34 +00:00
|
|
|
IDbContextFactory<JellyfinDbContext> repository)
|
2013-10-02 12:58:30 -04:00
|
|
|
{
|
2014-12-26 12:45:06 -05:00
|
|
|
_config = config;
|
2020-04-04 19:57:26 -04:00
|
|
|
_repository = repository;
|
2025-03-27 18:16:54 -06:00
|
|
|
_cache = new FastConcurrentLru<string, UserItemData>(Environment.ProcessorCount, _config.Configuration.CacheSize, StringComparer.OrdinalIgnoreCase);
|
2013-10-02 12:58:30 -04:00
|
|
|
}
|
|
|
|
|
|
2024-08-30 15:08:56 +02:00
|
|
|
/// <inheritdoc />
|
|
|
|
|
public event EventHandler<UserDataSaveEventArgs>? UserDataSaved;
|
2018-09-12 19:26:21 +02:00
|
|
|
|
2024-08-30 15:08:56 +02:00
|
|
|
/// <inheritdoc />
|
2020-05-20 13:07:53 -04:00
|
|
|
public void SaveUserData(User user, BaseItem item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken)
|
2013-10-02 12:08:58 -04:00
|
|
|
{
|
2022-10-06 20:21:23 +02:00
|
|
|
ArgumentNullException.ThrowIfNull(userData);
|
2019-11-17 23:05:39 +01:00
|
|
|
|
2022-10-06 20:21:23 +02:00
|
|
|
ArgumentNullException.ThrowIfNull(item);
|
2013-10-02 12:58:30 -04:00
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
|
2016-04-30 19:05:21 -04:00
|
|
|
var keys = item.GetUserDataKeys();
|
2013-10-23 12:03:12 -04:00
|
|
|
|
2024-11-14 21:48:21 +00:00
|
|
|
using var dbContext = _repository.CreateDbContext();
|
|
|
|
|
using var transaction = dbContext.Database.BeginTransaction();
|
2024-09-07 19:07:34 +00:00
|
|
|
|
2016-04-30 19:05:21 -04:00
|
|
|
foreach (var key in keys)
|
2013-10-02 12:58:30 -04:00
|
|
|
{
|
2024-09-07 19:07:34 +00:00
|
|
|
userData.Key = key;
|
2024-11-11 17:39:50 +00:00
|
|
|
var userDataEntry = Map(userData, user.Id, item.Id);
|
2024-11-14 21:48:21 +00:00
|
|
|
if (dbContext.UserData.Any(f => f.ItemId == userDataEntry.ItemId && f.UserId == userDataEntry.UserId && f.CustomDataKey == userDataEntry.CustomDataKey))
|
2024-11-11 17:39:50 +00:00
|
|
|
{
|
2024-11-14 21:48:21 +00:00
|
|
|
dbContext.UserData.Attach(userDataEntry).State = EntityState.Modified;
|
2024-11-11 17:39:50 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-11-14 21:48:21 +00:00
|
|
|
dbContext.UserData.Add(userDataEntry);
|
2024-11-11 17:39:50 +00:00
|
|
|
}
|
2013-10-02 12:58:30 -04:00
|
|
|
}
|
2013-10-02 15:08:58 -04:00
|
|
|
|
2024-11-14 21:48:21 +00:00
|
|
|
dbContext.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
2024-09-07 19:07:34 +00:00
|
|
|
|
2024-11-14 21:48:21 +00:00
|
|
|
var userId = user.InternalId;
|
2016-06-03 20:15:14 -04:00
|
|
|
var cacheKey = GetCacheKey(userId, item.Id);
|
2025-03-27 18:16:54 -06:00
|
|
|
_cache.AddOrUpdate(cacheKey, userData);
|
2025-09-16 21:08:04 +02:00
|
|
|
item.UserData = dbContext.UserData.Where(e => e.ItemId == item.Id).AsNoTracking().ToArray(); // rehydrate the cached userdata
|
2016-06-03 20:15:14 -04:00
|
|
|
|
2018-12-28 15:21:02 +01:00
|
|
|
UserDataSaved?.Invoke(this, new UserDataSaveEventArgs
|
2013-10-02 15:08:58 -04:00
|
|
|
{
|
2016-04-30 19:05:21 -04:00
|
|
|
Keys = keys,
|
2013-10-02 15:08:58 -04:00
|
|
|
UserData = userData,
|
|
|
|
|
SaveReason = reason,
|
2018-09-12 19:26:21 +02:00
|
|
|
UserId = user.Id,
|
2013-10-23 12:03:12 -04:00
|
|
|
Item = item
|
2018-12-28 15:21:02 +01:00
|
|
|
});
|
2013-10-02 12:08:58 -04:00
|
|
|
}
|
|
|
|
|
|
2024-08-30 15:08:56 +02:00
|
|
|
/// <inheritdoc />
|
2023-11-15 13:55:14 +03:00
|
|
|
public void SaveUserData(User user, BaseItem item, UpdateUserItemDataDto userDataDto, UserDataSaveReason reason)
|
2023-11-13 15:51:06 +03:00
|
|
|
{
|
|
|
|
|
ArgumentNullException.ThrowIfNull(user);
|
|
|
|
|
ArgumentNullException.ThrowIfNull(item);
|
|
|
|
|
ArgumentNullException.ThrowIfNull(userDataDto);
|
|
|
|
|
|
2025-01-18 16:17:26 +00:00
|
|
|
var userData = GetUserData(user, item) ?? throw new InvalidOperationException("UserData should not be null.");
|
2023-11-13 15:51:06 +03:00
|
|
|
|
2023-11-13 17:32:24 +03:00
|
|
|
if (userDataDto.PlaybackPositionTicks.HasValue)
|
|
|
|
|
{
|
|
|
|
|
userData.PlaybackPositionTicks = userDataDto.PlaybackPositionTicks.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (userDataDto.PlayCount.HasValue)
|
|
|
|
|
{
|
|
|
|
|
userData.PlayCount = userDataDto.PlayCount.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (userDataDto.IsFavorite.HasValue)
|
|
|
|
|
{
|
|
|
|
|
userData.IsFavorite = userDataDto.IsFavorite.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (userDataDto.Likes.HasValue)
|
|
|
|
|
{
|
|
|
|
|
userData.Likes = userDataDto.Likes.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (userDataDto.Played.HasValue)
|
|
|
|
|
{
|
|
|
|
|
userData.Played = userDataDto.Played.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (userDataDto.LastPlayedDate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
userData.LastPlayedDate = userDataDto.LastPlayedDate.Value;
|
|
|
|
|
}
|
2023-11-13 15:51:06 +03:00
|
|
|
|
2023-11-13 17:32:24 +03:00
|
|
|
if (userDataDto.Rating.HasValue)
|
2023-11-13 15:51:06 +03:00
|
|
|
{
|
2023-11-13 17:32:24 +03:00
|
|
|
userData.Rating = userDataDto.Rating.Value;
|
2023-11-13 15:51:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SaveUserData(user, item, userData, reason, CancellationToken.None);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 17:39:50 +00:00
|
|
|
private UserData Map(UserItemData dto, Guid userId, Guid itemId)
|
2018-09-12 19:26:21 +02:00
|
|
|
{
|
2024-09-07 19:07:34 +00:00
|
|
|
return new UserData()
|
|
|
|
|
{
|
2024-11-11 17:39:50 +00:00
|
|
|
ItemId = itemId,
|
|
|
|
|
CustomDataKey = dto.Key,
|
2025-01-15 20:12:41 +00:00
|
|
|
Item = null,
|
|
|
|
|
User = null,
|
2024-09-07 19:07:34 +00:00
|
|
|
AudioStreamIndex = dto.AudioStreamIndex,
|
|
|
|
|
IsFavorite = dto.IsFavorite,
|
|
|
|
|
LastPlayedDate = dto.LastPlayedDate,
|
|
|
|
|
Likes = dto.Likes,
|
|
|
|
|
PlaybackPositionTicks = dto.PlaybackPositionTicks,
|
|
|
|
|
PlayCount = dto.PlayCount,
|
|
|
|
|
Played = dto.Played,
|
|
|
|
|
Rating = dto.Rating,
|
|
|
|
|
UserId = userId,
|
|
|
|
|
SubtitleStreamIndex = dto.SubtitleStreamIndex,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-16 21:08:04 +02:00
|
|
|
private static UserItemData Map(UserData dto)
|
2024-09-07 19:07:34 +00:00
|
|
|
{
|
|
|
|
|
return new UserItemData()
|
|
|
|
|
{
|
2024-11-11 17:39:50 +00:00
|
|
|
Key = dto.CustomDataKey!,
|
2024-09-07 19:07:34 +00:00
|
|
|
AudioStreamIndex = dto.AudioStreamIndex,
|
|
|
|
|
IsFavorite = dto.IsFavorite,
|
|
|
|
|
LastPlayedDate = dto.LastPlayedDate,
|
|
|
|
|
Likes = dto.Likes,
|
|
|
|
|
PlaybackPositionTicks = dto.PlaybackPositionTicks,
|
|
|
|
|
PlayCount = dto.PlayCount,
|
|
|
|
|
Played = dto.Played,
|
|
|
|
|
Rating = dto.Rating,
|
|
|
|
|
SubtitleStreamIndex = dto.SubtitleStreamIndex,
|
|
|
|
|
};
|
|
|
|
|
}
|
2016-06-03 20:15:14 -04:00
|
|
|
|
2024-09-07 19:07:34 +00:00
|
|
|
private UserItemData? GetUserData(User user, Guid itemId, List<string> keys)
|
|
|
|
|
{
|
|
|
|
|
var cacheKey = GetCacheKey(user.InternalId, itemId);
|
2025-01-15 20:12:41 +00:00
|
|
|
|
2025-03-27 18:16:54 -06:00
|
|
|
if (_cache.TryGet(cacheKey, out var data))
|
2025-01-15 20:12:41 +00:00
|
|
|
{
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data = GetUserDataInternal(user.Id, itemId, keys);
|
2016-06-03 20:15:14 -04:00
|
|
|
|
2024-09-07 19:07:34 +00:00
|
|
|
if (data is null)
|
|
|
|
|
{
|
2024-11-11 17:39:50 +00:00
|
|
|
return new UserItemData()
|
|
|
|
|
{
|
|
|
|
|
Key = keys[0],
|
|
|
|
|
};
|
2024-09-07 19:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
2025-03-27 18:16:54 -06:00
|
|
|
return _cache.GetOrAdd(cacheKey, _ => data);
|
2016-06-03 20:15:14 -04:00
|
|
|
}
|
2016-05-11 10:36:28 -04:00
|
|
|
|
2024-11-12 16:14:17 +00:00
|
|
|
private UserItemData? GetUserDataInternal(Guid userId, Guid itemId, List<string> keys)
|
2016-06-03 20:15:14 -04:00
|
|
|
{
|
2024-11-12 16:14:17 +00:00
|
|
|
if (keys.Count == 0)
|
2016-05-11 10:36:28 -04:00
|
|
|
{
|
2024-11-12 16:14:17 +00:00
|
|
|
return null;
|
2016-05-24 12:58:36 -04:00
|
|
|
}
|
2016-05-11 10:36:28 -04:00
|
|
|
|
2024-11-12 16:14:17 +00:00
|
|
|
using var context = _repository.CreateDbContext();
|
|
|
|
|
var userData = context.UserData.AsNoTracking().Where(e => e.ItemId == itemId && keys.Contains(e.CustomDataKey) && e.UserId.Equals(userId)).ToArray();
|
|
|
|
|
|
|
|
|
|
if (userData.Length > 0)
|
2016-05-24 12:58:36 -04:00
|
|
|
{
|
2024-11-12 16:14:17 +00:00
|
|
|
var directDataReference = userData.FirstOrDefault(e => e.CustomDataKey == itemId.ToString("N"));
|
|
|
|
|
if (directDataReference is not null)
|
2016-05-11 10:36:28 -04:00
|
|
|
{
|
2024-11-12 16:14:17 +00:00
|
|
|
return Map(directDataReference);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Map(userData.First());
|
2016-05-11 10:36:28 -04:00
|
|
|
}
|
2016-05-24 12:58:36 -04:00
|
|
|
|
2024-11-12 16:14:17 +00:00
|
|
|
return new UserItemData
|
|
|
|
|
{
|
|
|
|
|
Key = keys.Last()!
|
|
|
|
|
};
|
2016-05-11 10:36:28 -04:00
|
|
|
}
|
|
|
|
|
|
2013-10-02 12:58:30 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the internal key.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>System.String.</returns>
|
2019-01-06 21:50:43 +01:00
|
|
|
private static string GetCacheKey(long internalUserId, Guid itemId)
|
2013-10-02 12:58:30 -04:00
|
|
|
{
|
2019-02-28 23:22:57 +01:00
|
|
|
return internalUserId.ToString(CultureInfo.InvariantCulture) + "-" + itemId.ToString("N", CultureInfo.InvariantCulture);
|
2013-10-02 12:08:58 -04:00
|
|
|
}
|
2014-07-03 22:22:57 -04:00
|
|
|
|
2024-08-30 15:08:56 +02:00
|
|
|
/// <inheritdoc />
|
2024-09-07 19:07:34 +00:00
|
|
|
public UserItemData? GetUserData(User user, BaseItem item)
|
2016-04-30 18:05:13 -04:00
|
|
|
{
|
2025-09-23 00:31:21 +03:00
|
|
|
return item.UserData?.Where(e => e.UserId.Equals(user.Id)).Select(Map).FirstOrDefault() ?? new UserItemData()
|
2025-09-16 21:08:04 +02:00
|
|
|
{
|
|
|
|
|
Key = item.GetUserDataKeys()[0],
|
|
|
|
|
};
|
2016-04-30 18:05:13 -04:00
|
|
|
}
|
|
|
|
|
|
2024-08-30 15:08:56 +02:00
|
|
|
/// <inheritdoc />
|
2024-11-11 00:27:30 +00:00
|
|
|
public UserItemDataDto? GetUserDataDto(BaseItem item, User user)
|
2024-08-30 15:08:56 +02:00
|
|
|
=> GetUserDataDto(item, null, user, new DtoOptions());
|
2016-06-19 02:18:29 -04:00
|
|
|
|
2021-09-03 18:46:34 +02:00
|
|
|
/// <inheritdoc />
|
2024-11-11 00:27:30 +00:00
|
|
|
public UserItemDataDto? GetUserDataDto(BaseItem item, BaseItemDto? itemDto, User user, DtoOptions options)
|
2016-06-19 02:18:29 -04:00
|
|
|
{
|
2024-11-11 00:27:30 +00:00
|
|
|
var userData = GetUserData(user, item);
|
|
|
|
|
if (userData is null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-11 17:39:50 +00:00
|
|
|
var dto = GetUserItemDataDto(userData, item.Id);
|
2016-06-19 02:18:29 -04:00
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
item.FillUserDataDtoValues(dto, userData, itemDto, user, options);
|
2014-07-03 22:22:57 -04:00
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-06-16 10:37:52 +12:00
|
|
|
/// Converts a UserItemData to a DTOUserItemData.
|
2014-07-03 22:22:57 -04:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="data">The data.</param>
|
2025-01-17 19:19:24 +00:00
|
|
|
/// <param name="itemId">The reference key to an Item.</param>
|
2014-07-03 22:22:57 -04:00
|
|
|
/// <returns>DtoUserItemData.</returns>
|
2021-09-03 18:46:34 +02:00
|
|
|
/// <exception cref="ArgumentNullException"><paramref name="data"/> is <c>null</c>.</exception>
|
2024-11-11 17:39:50 +00:00
|
|
|
private UserItemDataDto GetUserItemDataDto(UserItemData data, Guid itemId)
|
2014-07-03 22:22:57 -04:00
|
|
|
{
|
2022-10-06 20:21:23 +02:00
|
|
|
ArgumentNullException.ThrowIfNull(data);
|
2014-07-03 22:22:57 -04:00
|
|
|
|
|
|
|
|
return new UserItemDataDto
|
|
|
|
|
{
|
|
|
|
|
IsFavorite = data.IsFavorite,
|
|
|
|
|
Likes = data.Likes,
|
|
|
|
|
PlaybackPositionTicks = data.PlaybackPositionTicks,
|
|
|
|
|
PlayCount = data.PlayCount,
|
|
|
|
|
Rating = data.Rating,
|
|
|
|
|
Played = data.Played,
|
|
|
|
|
LastPlayedDate = data.LastPlayedDate,
|
2024-11-11 17:39:50 +00:00
|
|
|
ItemId = itemId,
|
2014-07-03 22:22:57 -04:00
|
|
|
Key = data.Key
|
|
|
|
|
};
|
|
|
|
|
}
|
2014-12-26 12:45:06 -05:00
|
|
|
|
2021-09-03 18:46:34 +02:00
|
|
|
/// <inheritdoc />
|
2015-02-17 13:42:46 -05:00
|
|
|
public bool UpdatePlayState(BaseItem item, UserItemData data, long? reportedPositionTicks)
|
2014-12-26 12:45:06 -05:00
|
|
|
{
|
|
|
|
|
var playedToCompletion = false;
|
|
|
|
|
|
2018-09-12 19:26:21 +02:00
|
|
|
var runtimeTicks = item.GetRunTimeTicksForPlayState();
|
|
|
|
|
|
|
|
|
|
var positionTicks = reportedPositionTicks ?? runtimeTicks;
|
|
|
|
|
var hasRuntime = runtimeTicks > 0;
|
2014-12-26 12:45:06 -05:00
|
|
|
|
|
|
|
|
// If a position has been reported, and if we know the duration
|
2021-05-07 10:35:03 +01:00
|
|
|
if (positionTicks > 0 && hasRuntime && item is not AudioBook && item is not Book)
|
2014-12-26 12:45:06 -05:00
|
|
|
{
|
2019-01-06 21:50:43 +01:00
|
|
|
var pctIn = decimal.Divide(positionTicks, runtimeTicks) * 100;
|
2014-12-26 12:45:06 -05:00
|
|
|
|
|
|
|
|
if (pctIn < _config.Configuration.MinResumePct)
|
|
|
|
|
{
|
2019-12-14 11:36:06 +09:00
|
|
|
// ignore progress during the beginning
|
2014-12-26 12:45:06 -05:00
|
|
|
positionTicks = 0;
|
|
|
|
|
}
|
2025-09-11 17:24:23 -04:00
|
|
|
else if (pctIn > _config.Configuration.MaxResumePct || positionTicks >= (runtimeTicks - TimeSpan.TicksPerSecond))
|
2014-12-26 12:45:06 -05:00
|
|
|
{
|
2019-12-14 11:36:06 +09:00
|
|
|
// mark as completed close to the end
|
2014-12-26 12:45:06 -05:00
|
|
|
positionTicks = 0;
|
|
|
|
|
data.Played = playedToCompletion = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Enforce MinResumeDuration
|
2018-09-12 19:26:21 +02:00
|
|
|
var durationSeconds = TimeSpan.FromTicks(runtimeTicks).TotalSeconds;
|
2021-05-06 19:50:00 +01:00
|
|
|
if (durationSeconds < _config.Configuration.MinResumeDurationSeconds)
|
2014-12-26 12:45:06 -05:00
|
|
|
{
|
|
|
|
|
positionTicks = 0;
|
|
|
|
|
data.Played = playedToCompletion = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-30 09:30:02 -05:00
|
|
|
else if (positionTicks > 0 && hasRuntime && item is AudioBook)
|
2020-12-30 08:48:33 -05:00
|
|
|
{
|
2021-04-21 07:11:14 -04:00
|
|
|
var playbackPositionInMinutes = TimeSpan.FromTicks(positionTicks).TotalMinutes;
|
|
|
|
|
var remainingTimeInMinutes = TimeSpan.FromTicks(runtimeTicks - positionTicks).TotalMinutes;
|
2020-12-30 08:48:33 -05:00
|
|
|
|
2021-04-21 07:11:14 -04:00
|
|
|
if (playbackPositionInMinutes < _config.Configuration.MinAudiobookResume)
|
2020-12-30 08:48:33 -05:00
|
|
|
{
|
|
|
|
|
// ignore progress during the beginning
|
|
|
|
|
positionTicks = 0;
|
|
|
|
|
}
|
2021-04-21 07:11:14 -04:00
|
|
|
else if (remainingTimeInMinutes < _config.Configuration.MaxAudiobookResume || positionTicks >= runtimeTicks)
|
2020-12-30 08:48:33 -05:00
|
|
|
{
|
|
|
|
|
// mark as completed close to the end
|
|
|
|
|
positionTicks = 0;
|
|
|
|
|
data.Played = playedToCompletion = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-12-26 12:45:06 -05:00
|
|
|
else if (!hasRuntime)
|
|
|
|
|
{
|
|
|
|
|
// If we don't know the runtime we'll just have to assume it was fully played
|
|
|
|
|
data.Played = playedToCompletion = true;
|
|
|
|
|
positionTicks = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-11 02:46:59 -04:00
|
|
|
if (!item.SupportsPlayedStatus)
|
2014-12-26 12:45:06 -05:00
|
|
|
{
|
|
|
|
|
positionTicks = 0;
|
2016-10-11 02:46:59 -04:00
|
|
|
data.Played = false;
|
2014-12-26 12:45:06 -05:00
|
|
|
}
|
2019-12-14 11:36:06 +09:00
|
|
|
|
2016-12-12 00:49:19 -05:00
|
|
|
if (!item.SupportsPositionTicksResume)
|
2016-10-11 17:33:38 -04:00
|
|
|
{
|
|
|
|
|
positionTicks = 0;
|
|
|
|
|
}
|
2014-12-26 12:45:06 -05:00
|
|
|
|
|
|
|
|
data.PlaybackPositionTicks = positionTicks;
|
|
|
|
|
|
|
|
|
|
return playedToCompletion;
|
|
|
|
|
}
|
2013-10-02 12:08:58 -04:00
|
|
|
}
|
|
|
|
|
}
|