UserManager.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. using System.Security.Cryptography;
  2. using System.Text;
  3. using MediaBrowser.Common.Events;
  4. using MediaBrowser.Common.Extensions;
  5. using MediaBrowser.Controller;
  6. using MediaBrowser.Controller.Configuration;
  7. using MediaBrowser.Controller.Entities;
  8. using MediaBrowser.Controller.Library;
  9. using MediaBrowser.Model.Connectivity;
  10. using MediaBrowser.Model.Logging;
  11. using System;
  12. using System.Collections.Concurrent;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Threading;
  16. using System.Threading.Tasks;
  17. namespace MediaBrowser.Server.Implementations.Library
  18. {
  19. /// <summary>
  20. /// Class UserManager
  21. /// </summary>
  22. public class UserManager : IUserManager
  23. {
  24. /// <summary>
  25. /// The _active connections
  26. /// </summary>
  27. private readonly ConcurrentBag<ClientConnectionInfo> _activeConnections =
  28. new ConcurrentBag<ClientConnectionInfo>();
  29. /// <summary>
  30. /// The _users
  31. /// </summary>
  32. private IEnumerable<User> _users;
  33. /// <summary>
  34. /// The _user lock
  35. /// </summary>
  36. private object _usersSyncLock = new object();
  37. /// <summary>
  38. /// The _users initialized
  39. /// </summary>
  40. private bool _usersInitialized;
  41. /// <summary>
  42. /// Gets the users.
  43. /// </summary>
  44. /// <value>The users.</value>
  45. public IEnumerable<User> Users
  46. {
  47. get
  48. {
  49. // Call ToList to exhaust the stream because we'll be iterating over this multiple times
  50. LazyInitializer.EnsureInitialized(ref _users, ref _usersInitialized, ref _usersSyncLock, LoadUsers);
  51. return _users;
  52. }
  53. internal set
  54. {
  55. _users = value;
  56. if (value == null)
  57. {
  58. _usersInitialized = false;
  59. }
  60. }
  61. }
  62. /// <summary>
  63. /// Gets all connections.
  64. /// </summary>
  65. /// <value>All connections.</value>
  66. private IEnumerable<ClientConnectionInfo> AllConnections
  67. {
  68. get { return _activeConnections.Where(c => GetUserById(c.UserId) != null).OrderByDescending(c => c.LastActivityDate); }
  69. }
  70. /// <summary>
  71. /// Gets the active connections.
  72. /// </summary>
  73. /// <value>The active connections.</value>
  74. public IEnumerable<ClientConnectionInfo> ConnectedUsers
  75. {
  76. get { return AllConnections.Where(c => (DateTime.UtcNow - c.LastActivityDate).TotalMinutes <= 10); }
  77. }
  78. /// <summary>
  79. /// The _logger
  80. /// </summary>
  81. private readonly ILogger _logger;
  82. /// <summary>
  83. /// Gets or sets the kernel.
  84. /// </summary>
  85. /// <value>The kernel.</value>
  86. private Kernel Kernel { get; set; }
  87. /// <summary>
  88. /// Gets or sets the configuration manager.
  89. /// </summary>
  90. /// <value>The configuration manager.</value>
  91. private IServerConfigurationManager ConfigurationManager { get; set; }
  92. /// <summary>
  93. /// Initializes a new instance of the <see cref="UserManager" /> class.
  94. /// </summary>
  95. /// <param name="kernel">The kernel.</param>
  96. /// <param name="logger">The logger.</param>
  97. /// <param name="configurationManager">The configuration manager.</param>
  98. public UserManager(Kernel kernel, ILogger logger, IServerConfigurationManager configurationManager)
  99. {
  100. _logger = logger;
  101. Kernel = kernel;
  102. ConfigurationManager = configurationManager;
  103. }
  104. #region Events
  105. /// <summary>
  106. /// Occurs when [playback start].
  107. /// </summary>
  108. public event EventHandler<PlaybackProgressEventArgs> PlaybackStart;
  109. /// <summary>
  110. /// Occurs when [playback progress].
  111. /// </summary>
  112. public event EventHandler<PlaybackProgressEventArgs> PlaybackProgress;
  113. /// <summary>
  114. /// Occurs when [playback stopped].
  115. /// </summary>
  116. public event EventHandler<PlaybackProgressEventArgs> PlaybackStopped;
  117. #endregion
  118. #region UserUpdated Event
  119. /// <summary>
  120. /// Occurs when [user updated].
  121. /// </summary>
  122. public event EventHandler<GenericEventArgs<User>> UserUpdated;
  123. /// <summary>
  124. /// Called when [user updated].
  125. /// </summary>
  126. /// <param name="user">The user.</param>
  127. private void OnUserUpdated(User user)
  128. {
  129. EventHelper.QueueEventIfNotNull(UserUpdated, this, new GenericEventArgs<User> { Argument = user }, _logger);
  130. }
  131. #endregion
  132. #region UserDeleted Event
  133. /// <summary>
  134. /// Occurs when [user deleted].
  135. /// </summary>
  136. public event EventHandler<GenericEventArgs<User>> UserDeleted;
  137. /// <summary>
  138. /// Called when [user deleted].
  139. /// </summary>
  140. /// <param name="user">The user.</param>
  141. private void OnUserDeleted(User user)
  142. {
  143. EventHelper.QueueEventIfNotNull(UserDeleted, this, new GenericEventArgs<User> { Argument = user }, _logger);
  144. }
  145. #endregion
  146. /// <summary>
  147. /// Gets a User by Id
  148. /// </summary>
  149. /// <param name="id">The id.</param>
  150. /// <returns>User.</returns>
  151. /// <exception cref="System.ArgumentNullException"></exception>
  152. public User GetUserById(Guid id)
  153. {
  154. if (id == Guid.Empty)
  155. {
  156. throw new ArgumentNullException();
  157. }
  158. return Users.FirstOrDefault(u => u.Id == id);
  159. }
  160. /// <summary>
  161. /// Authenticates a User and returns a result indicating whether or not it succeeded
  162. /// </summary>
  163. /// <param name="user">The user.</param>
  164. /// <param name="password">The password.</param>
  165. /// <returns>Task{System.Boolean}.</returns>
  166. /// <exception cref="System.ArgumentNullException">user</exception>
  167. public async Task<bool> AuthenticateUser(User user, string password)
  168. {
  169. if (user == null)
  170. {
  171. throw new ArgumentNullException("user");
  172. }
  173. var existingPasswordString = string.IsNullOrEmpty(user.Password) ? GetSha1String(string.Empty) : user.Password;
  174. var success = string.Equals(existingPasswordString, password.Replace("-", string.Empty), StringComparison.OrdinalIgnoreCase);
  175. // Update LastActivityDate and LastLoginDate, then save
  176. if (success)
  177. {
  178. user.LastActivityDate = user.LastLoginDate = DateTime.UtcNow;
  179. await UpdateUser(user).ConfigureAwait(false);
  180. }
  181. _logger.Info("Authentication request for {0} {1}.", user.Name, (success ? "has succeeded" : "has been denied"));
  182. return success;
  183. }
  184. /// <summary>
  185. /// Gets the sha1 string.
  186. /// </summary>
  187. /// <param name="str">The STR.</param>
  188. /// <returns>System.String.</returns>
  189. private static string GetSha1String(string str)
  190. {
  191. using (var provider = SHA1.Create())
  192. {
  193. var hash = provider.ComputeHash(Encoding.UTF8.GetBytes(str));
  194. return BitConverter.ToString(hash).Replace("-", string.Empty);
  195. }
  196. }
  197. /// <summary>
  198. /// Logs the user activity.
  199. /// </summary>
  200. /// <param name="user">The user.</param>
  201. /// <param name="clientType">Type of the client.</param>
  202. /// <param name="deviceName">Name of the device.</param>
  203. /// <returns>Task.</returns>
  204. /// <exception cref="System.ArgumentNullException">user</exception>
  205. public Task LogUserActivity(User user, ClientType clientType, string deviceName)
  206. {
  207. if (user == null)
  208. {
  209. throw new ArgumentNullException("user");
  210. }
  211. var activityDate = DateTime.UtcNow;
  212. user.LastActivityDate = activityDate;
  213. LogConnection(user.Id, clientType, deviceName, activityDate);
  214. // Save this directly. No need to fire off all the events for this.
  215. return Kernel.UserRepository.SaveUser(user, CancellationToken.None);
  216. }
  217. /// <summary>
  218. /// Updates the now playing item id.
  219. /// </summary>
  220. /// <param name="user">The user.</param>
  221. /// <param name="clientType">Type of the client.</param>
  222. /// <param name="deviceName">Name of the device.</param>
  223. /// <param name="item">The item.</param>
  224. /// <param name="currentPositionTicks">The current position ticks.</param>
  225. private void UpdateNowPlayingItemId(User user, ClientType clientType, string deviceName, BaseItem item, long? currentPositionTicks = null)
  226. {
  227. var conn = GetConnection(user.Id, clientType, deviceName);
  228. conn.NowPlayingPositionTicks = currentPositionTicks;
  229. conn.NowPlayingItem = DtoBuilder.GetBaseItemInfo(item);
  230. }
  231. /// <summary>
  232. /// Removes the now playing item id.
  233. /// </summary>
  234. /// <param name="user">The user.</param>
  235. /// <param name="clientType">Type of the client.</param>
  236. /// <param name="deviceName">Name of the device.</param>
  237. /// <param name="item">The item.</param>
  238. private void RemoveNowPlayingItemId(User user, ClientType clientType, string deviceName, BaseItem item)
  239. {
  240. var conn = GetConnection(user.Id, clientType, deviceName);
  241. if (conn.NowPlayingItem != null && conn.NowPlayingItem.Id.Equals(item.Id.ToString()))
  242. {
  243. conn.NowPlayingItem = null;
  244. conn.NowPlayingPositionTicks = null;
  245. }
  246. }
  247. /// <summary>
  248. /// Logs the connection.
  249. /// </summary>
  250. /// <param name="userId">The user id.</param>
  251. /// <param name="clientType">Type of the client.</param>
  252. /// <param name="deviceName">Name of the device.</param>
  253. /// <param name="lastActivityDate">The last activity date.</param>
  254. private void LogConnection(Guid userId, ClientType clientType, string deviceName, DateTime lastActivityDate)
  255. {
  256. GetConnection(userId, clientType, deviceName).LastActivityDate = lastActivityDate;
  257. }
  258. /// <summary>
  259. /// Gets the connection.
  260. /// </summary>
  261. /// <param name="userId">The user id.</param>
  262. /// <param name="clientType">Type of the client.</param>
  263. /// <param name="deviceName">Name of the device.</param>
  264. /// <returns>ClientConnectionInfo.</returns>
  265. private ClientConnectionInfo GetConnection(Guid userId, ClientType clientType, string deviceName)
  266. {
  267. var conn = _activeConnections.FirstOrDefault(c => c.UserId == userId && c.ClientType == clientType && string.Equals(deviceName, c.DeviceName, StringComparison.OrdinalIgnoreCase));
  268. if (conn == null)
  269. {
  270. conn = new ClientConnectionInfo
  271. {
  272. UserId = userId,
  273. ClientType = clientType,
  274. DeviceName = deviceName
  275. };
  276. _activeConnections.Add(conn);
  277. }
  278. return conn;
  279. }
  280. /// <summary>
  281. /// Loads the users from the repository
  282. /// </summary>
  283. /// <returns>IEnumerable{User}.</returns>
  284. private IEnumerable<User> LoadUsers()
  285. {
  286. var users = Kernel.UserRepository.RetrieveAllUsers().ToList();
  287. // There always has to be at least one user.
  288. if (users.Count == 0)
  289. {
  290. var name = Environment.UserName;
  291. var user = InstantiateNewUser(name);
  292. var task = Kernel.UserRepository.SaveUser(user, CancellationToken.None);
  293. // Hate having to block threads
  294. Task.WaitAll(task);
  295. users.Add(user);
  296. }
  297. return users;
  298. }
  299. /// <summary>
  300. /// Refreshes metadata for each user
  301. /// </summary>
  302. /// <param name="cancellationToken">The cancellation token.</param>
  303. /// <param name="force">if set to <c>true</c> [force].</param>
  304. /// <returns>Task.</returns>
  305. public Task RefreshUsersMetadata(CancellationToken cancellationToken, bool force = false)
  306. {
  307. var tasks = Users.Select(user => user.RefreshMetadata(cancellationToken, forceRefresh: force)).ToList();
  308. return Task.WhenAll(tasks);
  309. }
  310. /// <summary>
  311. /// Renames the user.
  312. /// </summary>
  313. /// <param name="user">The user.</param>
  314. /// <param name="newName">The new name.</param>
  315. /// <returns>Task.</returns>
  316. /// <exception cref="System.ArgumentNullException">user</exception>
  317. /// <exception cref="System.ArgumentException"></exception>
  318. public async Task RenameUser(User user, string newName)
  319. {
  320. if (user == null)
  321. {
  322. throw new ArgumentNullException("user");
  323. }
  324. if (string.IsNullOrEmpty(newName))
  325. {
  326. throw new ArgumentNullException("newName");
  327. }
  328. if (Users.Any(u => u.Id != user.Id && u.Name.Equals(newName, StringComparison.OrdinalIgnoreCase)))
  329. {
  330. throw new ArgumentException(string.Format("A user with the name '{0}' already exists.", newName));
  331. }
  332. if (user.Name.Equals(newName, StringComparison.Ordinal))
  333. {
  334. throw new ArgumentException("The new and old names must be different.");
  335. }
  336. await user.Rename(newName);
  337. OnUserUpdated(user);
  338. }
  339. /// <summary>
  340. /// Updates the user.
  341. /// </summary>
  342. /// <param name="user">The user.</param>
  343. /// <exception cref="System.ArgumentNullException">user</exception>
  344. /// <exception cref="System.ArgumentException"></exception>
  345. public async Task UpdateUser(User user)
  346. {
  347. if (user == null)
  348. {
  349. throw new ArgumentNullException("user");
  350. }
  351. if (user.Id == Guid.Empty || !Users.Any(u => u.Id.Equals(user.Id)))
  352. {
  353. throw new ArgumentException(string.Format("User with name '{0}' and Id {1} does not exist.", user.Name, user.Id));
  354. }
  355. user.DateModified = DateTime.UtcNow;
  356. await Kernel.UserRepository.SaveUser(user, CancellationToken.None).ConfigureAwait(false);
  357. OnUserUpdated(user);
  358. }
  359. /// <summary>
  360. /// Creates the user.
  361. /// </summary>
  362. /// <param name="name">The name.</param>
  363. /// <returns>User.</returns>
  364. /// <exception cref="System.ArgumentNullException">name</exception>
  365. /// <exception cref="System.ArgumentException"></exception>
  366. public async Task<User> CreateUser(string name)
  367. {
  368. if (string.IsNullOrEmpty(name))
  369. {
  370. throw new ArgumentNullException("name");
  371. }
  372. if (Users.Any(u => u.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
  373. {
  374. throw new ArgumentException(string.Format("A user with the name '{0}' already exists.", name));
  375. }
  376. var user = InstantiateNewUser(name);
  377. var list = Users.ToList();
  378. list.Add(user);
  379. Users = list;
  380. await Kernel.UserRepository.SaveUser(user, CancellationToken.None).ConfigureAwait(false);
  381. return user;
  382. }
  383. /// <summary>
  384. /// Deletes the user.
  385. /// </summary>
  386. /// <param name="user">The user.</param>
  387. /// <returns>Task.</returns>
  388. /// <exception cref="System.ArgumentNullException">user</exception>
  389. /// <exception cref="System.ArgumentException"></exception>
  390. public async Task DeleteUser(User user)
  391. {
  392. if (user == null)
  393. {
  394. throw new ArgumentNullException("user");
  395. }
  396. if (Users.FirstOrDefault(u => u.Id == user.Id) == null)
  397. {
  398. throw new ArgumentException(string.Format("The user cannot be deleted because there is no user with the Name {0} and Id {1}.", user.Name, user.Id));
  399. }
  400. if (Users.Count() == 1)
  401. {
  402. throw new ArgumentException(string.Format("The user '{0}' be deleted because there must be at least one user in the system.", user.Name));
  403. }
  404. await Kernel.UserRepository.DeleteUser(user, CancellationToken.None).ConfigureAwait(false);
  405. OnUserDeleted(user);
  406. // Force this to be lazy loaded again
  407. Users = null;
  408. }
  409. /// <summary>
  410. /// Resets the password by clearing it.
  411. /// </summary>
  412. /// <returns>Task.</returns>
  413. public Task ResetPassword(User user)
  414. {
  415. return ChangePassword(user, string.Empty);
  416. }
  417. /// <summary>
  418. /// Changes the password.
  419. /// </summary>
  420. /// <param name="user">The user.</param>
  421. /// <param name="newPassword">The new password.</param>
  422. /// <returns>Task.</returns>
  423. public Task ChangePassword(User user, string newPassword)
  424. {
  425. if (user == null)
  426. {
  427. throw new ArgumentNullException("user");
  428. }
  429. user.Password = string.IsNullOrEmpty(newPassword) ? string.Empty : GetSha1String(newPassword);
  430. return UpdateUser(user);
  431. }
  432. /// <summary>
  433. /// Instantiates the new user.
  434. /// </summary>
  435. /// <param name="name">The name.</param>
  436. /// <returns>User.</returns>
  437. private User InstantiateNewUser(string name)
  438. {
  439. return new User
  440. {
  441. Name = name,
  442. Id = ("MBUser" + name).GetMD5(),
  443. DateCreated = DateTime.UtcNow,
  444. DateModified = DateTime.UtcNow
  445. };
  446. }
  447. /// <summary>
  448. /// Used to report that playback has started for an item
  449. /// </summary>
  450. /// <param name="user">The user.</param>
  451. /// <param name="item">The item.</param>
  452. /// <param name="clientType">Type of the client.</param>
  453. /// <param name="deviceName">Name of the device.</param>
  454. /// <exception cref="System.ArgumentNullException"></exception>
  455. public void OnPlaybackStart(User user, BaseItem item, ClientType clientType, string deviceName)
  456. {
  457. if (user == null)
  458. {
  459. throw new ArgumentNullException();
  460. }
  461. if (item == null)
  462. {
  463. throw new ArgumentNullException();
  464. }
  465. UpdateNowPlayingItemId(user, clientType, deviceName, item);
  466. // Nothing to save here
  467. // Fire events to inform plugins
  468. EventHelper.QueueEventIfNotNull(PlaybackStart, this, new PlaybackProgressEventArgs
  469. {
  470. Argument = item,
  471. User = user
  472. }, _logger);
  473. }
  474. /// <summary>
  475. /// Used to report playback progress for an item
  476. /// </summary>
  477. /// <param name="user">The user.</param>
  478. /// <param name="item">The item.</param>
  479. /// <param name="positionTicks">The position ticks.</param>
  480. /// <param name="clientType">Type of the client.</param>
  481. /// <param name="deviceName">Name of the device.</param>
  482. /// <returns>Task.</returns>
  483. /// <exception cref="System.ArgumentNullException"></exception>
  484. public async Task OnPlaybackProgress(User user, BaseItem item, long? positionTicks, ClientType clientType, string deviceName)
  485. {
  486. if (user == null)
  487. {
  488. throw new ArgumentNullException();
  489. }
  490. if (item == null)
  491. {
  492. throw new ArgumentNullException();
  493. }
  494. UpdateNowPlayingItemId(user, clientType, deviceName, item, positionTicks);
  495. if (positionTicks.HasValue)
  496. {
  497. var data = item.GetUserData(user, true);
  498. UpdatePlayState(item, data, positionTicks.Value, false);
  499. await SaveUserDataForItem(user, item, data).ConfigureAwait(false);
  500. }
  501. EventHelper.QueueEventIfNotNull(PlaybackProgress, this, new PlaybackProgressEventArgs
  502. {
  503. Argument = item,
  504. User = user,
  505. PlaybackPositionTicks = positionTicks
  506. }, _logger);
  507. }
  508. /// <summary>
  509. /// Used to report that playback has ended for an item
  510. /// </summary>
  511. /// <param name="user">The user.</param>
  512. /// <param name="item">The item.</param>
  513. /// <param name="positionTicks">The position ticks.</param>
  514. /// <param name="clientType">Type of the client.</param>
  515. /// <param name="deviceName">Name of the device.</param>
  516. /// <returns>Task.</returns>
  517. /// <exception cref="System.ArgumentNullException"></exception>
  518. public async Task OnPlaybackStopped(User user, BaseItem item, long? positionTicks, ClientType clientType, string deviceName)
  519. {
  520. if (user == null)
  521. {
  522. throw new ArgumentNullException();
  523. }
  524. if (item == null)
  525. {
  526. throw new ArgumentNullException();
  527. }
  528. RemoveNowPlayingItemId(user, clientType, deviceName, item);
  529. var data = item.GetUserData(user, true);
  530. if (positionTicks.HasValue)
  531. {
  532. UpdatePlayState(item, data, positionTicks.Value, true);
  533. }
  534. else
  535. {
  536. // If the client isn't able to report this, then we'll just have to make an assumption
  537. data.PlayCount++;
  538. data.Played = true;
  539. }
  540. await SaveUserDataForItem(user, item, data).ConfigureAwait(false);
  541. EventHelper.QueueEventIfNotNull(PlaybackStopped, this, new PlaybackProgressEventArgs
  542. {
  543. Argument = item,
  544. User = user,
  545. PlaybackPositionTicks = positionTicks
  546. }, _logger);
  547. }
  548. /// <summary>
  549. /// Updates playstate position for an item but does not save
  550. /// </summary>
  551. /// <param name="item">The item</param>
  552. /// <param name="data">User data for the item</param>
  553. /// <param name="positionTicks">The current playback position</param>
  554. /// <param name="incrementPlayCount">Whether or not to increment playcount</param>
  555. private void UpdatePlayState(BaseItem item, UserItemData data, long positionTicks, bool incrementPlayCount)
  556. {
  557. // If a position has been reported, and if we know the duration
  558. if (positionTicks > 0 && item.RunTimeTicks.HasValue && item.RunTimeTicks > 0)
  559. {
  560. var pctIn = Decimal.Divide(positionTicks, item.RunTimeTicks.Value) * 100;
  561. // Don't track in very beginning
  562. if (pctIn < ConfigurationManager.Configuration.MinResumePct)
  563. {
  564. positionTicks = 0;
  565. incrementPlayCount = false;
  566. }
  567. // If we're at the end, assume completed
  568. else if (pctIn > ConfigurationManager.Configuration.MaxResumePct || positionTicks >= item.RunTimeTicks.Value)
  569. {
  570. positionTicks = 0;
  571. data.Played = true;
  572. }
  573. else
  574. {
  575. // Enforce MinResumeDuration
  576. var durationSeconds = TimeSpan.FromTicks(item.RunTimeTicks.Value).TotalSeconds;
  577. if (durationSeconds < ConfigurationManager.Configuration.MinResumeDurationSeconds)
  578. {
  579. positionTicks = 0;
  580. data.Played = true;
  581. }
  582. }
  583. }
  584. data.PlaybackPositionTicks = positionTicks;
  585. if (incrementPlayCount)
  586. {
  587. data.PlayCount++;
  588. data.LastPlayedDate = DateTime.UtcNow;
  589. }
  590. }
  591. /// <summary>
  592. /// Saves user data for an item
  593. /// </summary>
  594. /// <param name="user">The user.</param>
  595. /// <param name="item">The item.</param>
  596. /// <param name="data">The data.</param>
  597. public Task SaveUserDataForItem(User user, BaseItem item, UserItemData data)
  598. {
  599. item.AddOrUpdateUserData(user, data);
  600. return Kernel.UserDataRepository.SaveUserData(item, CancellationToken.None);
  601. }
  602. }
  603. }