2
0

UserDataManager.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using MediaBrowser.Common.Events;
  2. using MediaBrowser.Controller.Entities;
  3. using MediaBrowser.Controller.Library;
  4. using MediaBrowser.Controller.Persistence;
  5. using MediaBrowser.Model.Dto;
  6. using MediaBrowser.Model.Entities;
  7. using MediaBrowser.Model.Logging;
  8. using System;
  9. using System.Collections.Concurrent;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace MediaBrowser.Server.Implementations.Library
  13. {
  14. /// <summary>
  15. /// Class UserDataManager
  16. /// </summary>
  17. public class UserDataManager : IUserDataManager
  18. {
  19. public event EventHandler<UserDataSaveEventArgs> UserDataSaved;
  20. private readonly ConcurrentDictionary<string, UserItemData> _userData = new ConcurrentDictionary<string, UserItemData>();
  21. private readonly ILogger _logger;
  22. public UserDataManager(ILogManager logManager)
  23. {
  24. _logger = logManager.GetLogger(GetType().Name);
  25. }
  26. /// <summary>
  27. /// Gets or sets the repository.
  28. /// </summary>
  29. /// <value>The repository.</value>
  30. public IUserDataRepository Repository { get; set; }
  31. /// <summary>
  32. /// Saves the user data.
  33. /// </summary>
  34. /// <param name="userId">The user id.</param>
  35. /// <param name="item">The item.</param>
  36. /// <param name="userData">The user data.</param>
  37. /// <param name="reason">The reason.</param>
  38. /// <param name="cancellationToken">The cancellation token.</param>
  39. /// <returns>Task.</returns>
  40. /// <exception cref="System.ArgumentNullException">userData
  41. /// or
  42. /// cancellationToken
  43. /// or
  44. /// userId
  45. /// or
  46. /// key</exception>
  47. public async Task SaveUserData(Guid userId, IHasUserData item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken)
  48. {
  49. if (userData == null)
  50. {
  51. throw new ArgumentNullException("userData");
  52. }
  53. if (item == null)
  54. {
  55. throw new ArgumentNullException("item");
  56. }
  57. if (userId == Guid.Empty)
  58. {
  59. throw new ArgumentNullException("userId");
  60. }
  61. cancellationToken.ThrowIfCancellationRequested();
  62. var key = item.GetUserDataKey();
  63. try
  64. {
  65. await Repository.SaveUserData(userId, key, userData, cancellationToken).ConfigureAwait(false);
  66. var newValue = userData;
  67. // Once it succeeds, put it into the dictionary to make it available to everyone else
  68. _userData.AddOrUpdate(GetCacheKey(userId, key), newValue, delegate { return newValue; });
  69. }
  70. catch (Exception ex)
  71. {
  72. _logger.ErrorException("Error saving user data", ex);
  73. throw;
  74. }
  75. EventHelper.FireEventIfNotNull(UserDataSaved, this, new UserDataSaveEventArgs
  76. {
  77. Key = key,
  78. UserData = userData,
  79. SaveReason = reason,
  80. UserId = userId,
  81. Item = item
  82. }, _logger);
  83. }
  84. /// <summary>
  85. /// Gets the user data.
  86. /// </summary>
  87. /// <param name="userId">The user id.</param>
  88. /// <param name="key">The key.</param>
  89. /// <returns>Task{UserItemData}.</returns>
  90. public UserItemData GetUserData(Guid userId, string key)
  91. {
  92. if (userId == Guid.Empty)
  93. {
  94. throw new ArgumentNullException("userId");
  95. }
  96. if (string.IsNullOrEmpty(key))
  97. {
  98. throw new ArgumentNullException("key");
  99. }
  100. return _userData.GetOrAdd(GetCacheKey(userId, key), keyName => Repository.GetUserData(userId, key));
  101. }
  102. /// <summary>
  103. /// Gets the internal key.
  104. /// </summary>
  105. /// <param name="userId">The user id.</param>
  106. /// <param name="key">The key.</param>
  107. /// <returns>System.String.</returns>
  108. private string GetCacheKey(Guid userId, string key)
  109. {
  110. return userId + key;
  111. }
  112. public UserItemDataDto GetUserDataDto(IHasUserData item, User user)
  113. {
  114. var userData = GetUserData(user.Id, item.GetUserDataKey());
  115. var dto = GetUserItemDataDto(userData);
  116. item.FillUserDataDtoValues(dto, userData, user);
  117. return dto;
  118. }
  119. /// <summary>
  120. /// Converts a UserItemData to a DTOUserItemData
  121. /// </summary>
  122. /// <param name="data">The data.</param>
  123. /// <returns>DtoUserItemData.</returns>
  124. /// <exception cref="System.ArgumentNullException"></exception>
  125. private UserItemDataDto GetUserItemDataDto(UserItemData data)
  126. {
  127. if (data == null)
  128. {
  129. throw new ArgumentNullException("data");
  130. }
  131. return new UserItemDataDto
  132. {
  133. IsFavorite = data.IsFavorite,
  134. Likes = data.Likes,
  135. PlaybackPositionTicks = data.PlaybackPositionTicks,
  136. PlayCount = data.PlayCount,
  137. Rating = data.Rating,
  138. Played = data.Played,
  139. LastPlayedDate = data.LastPlayedDate,
  140. Key = data.Key
  141. };
  142. }
  143. }
  144. }