1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
using MediaBrowser.Common.Events;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Audio;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Emby.Server.Implementations.Library
{
/// <summary>
/// Class UserDataManager
/// </summary>
public class UserDataManager : IUserDataManager
{
public event EventHandler<UserDataSaveEventArgs> UserDataSaved;
private readonly ConcurrentDictionary<string, UserItemData> _userData =
new ConcurrentDictionary<string, UserItemData>(StringComparer.OrdinalIgnoreCase);
private readonly ILogger _logger;
private readonly IServerConfigurationManager _config;
public UserDataManager(ILogManager logManager, IServerConfigurationManager config)
{
_config = config;
_logger = logManager.GetLogger(GetType().Name);
}
/// <summary>
/// Gets or sets the repository.
/// </summary>
/// <value>The repository.</value>
public IUserDataRepository Repository { get; set; }
public async Task SaveUserData(Guid userId, IHasUserData item, UserItemData userData, UserDataSaveReason reason, CancellationToken cancellationToken)
{
if (userData == null)
{
throw new ArgumentNullException("userData");
}
if (item == null)
{
throw new ArgumentNullException("item");
}
if (userId == Guid.Empty)
{
throw new ArgumentNullException("userId");
}
cancellationToken.ThrowIfCancellationRequested();
var keys = item.GetUserDataKeys();
foreach (var key in keys)
{
await Repository.SaveUserData(userId, key, userData, cancellationToken).ConfigureAwait(false);
}
var cacheKey = GetCacheKey(userId, item.Id);
_userData.AddOrUpdate(cacheKey, userData, (k, v) => userData);
EventHelper.FireEventIfNotNull(UserDataSaved, this, new UserDataSaveEventArgs
{
Keys = keys,
UserData = userData,
SaveReason = reason,
UserId = userId,
Item = item
}, _logger);
}
/// <summary>
/// Save the provided user data for the given user. Batch operation. Does not fire any events or update the cache.
/// </summary>
/// <param name="userId"></param>
/// <param name="userData"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task SaveAllUserData(Guid userId, IEnumerable<UserItemData> userData, CancellationToken cancellationToken)
{
if (userData == null)
{
throw new ArgumentNullException("userData");
}
if (userId == Guid.Empty)
{
throw new ArgumentNullException("userId");
}
cancellationToken.ThrowIfCancellationRequested();
await Repository.SaveAllUserData(userId, userData, cancellationToken).ConfigureAwait(false);
}
/// <summary>
/// Retrieve all user data for the given user
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public IEnumerable<UserItemData> GetAllUserData(Guid userId)
{
if (userId == Guid.Empty)
{
throw new ArgumentNullException("userId");
}
return Repository.GetAllUserData(userId);
}
public UserItemData GetUserData(Guid userId, Guid itemId, List<string> keys)
{
if (userId == Guid.Empty)
{
throw new ArgumentNullException("userId");
}
if (keys == null)
{
throw new ArgumentNullException("keys");
}
if (keys.Count == 0)
{
throw new ArgumentException("UserData keys cannot be empty.");
}
var cacheKey = GetCacheKey(userId, itemId);
return _userData.GetOrAdd(cacheKey, k => GetUserDataInternal(userId, keys));
}
private UserItemData GetUserDataInternal(Guid userId, List<string> keys)
{
var userData = Repository.GetUserData(userId, keys);
if (userData != null)
{
return userData;
}
if (keys.Count > 0)
{
return new UserItemData
{
UserId = userId,
Key = keys[0]
};
}
return null;
}
/// <summary>
/// Gets the internal key.
/// </summary>
/// <returns>System.String.</returns>
private string GetCacheKey(Guid userId, Guid itemId)
{
return userId.ToString("N") + itemId.ToString("N");
}
public UserItemData GetUserData(IHasUserData user, IHasUserData item)
{
return GetUserData(user.Id, item);
}
public UserItemData GetUserData(string userId, IHasUserData item)
{
return GetUserData(new Guid(userId), item);
}
public UserItemData GetUserData(Guid userId, IHasUserData item)
{
return GetUserData(userId, item.Id, item.GetUserDataKeys());
}
public async Task<UserItemDataDto> GetUserDataDto(IHasUserData item, User user)
{
var userData = GetUserData(user.Id, item);
var dto = GetUserItemDataDto(userData);
await item.FillUserDataDtoValues(dto, userData, null, user).ConfigureAwait(false);
return dto;
}
public async Task<UserItemDataDto> GetUserDataDto(IHasUserData item, BaseItemDto itemDto, User user)
{
var userData = GetUserData(user.Id, item);
var dto = GetUserItemDataDto(userData);
await item.FillUserDataDtoValues(dto, userData, itemDto, user).ConfigureAwait(false);
return dto;
}
/// <summary>
/// Converts a UserItemData to a DTOUserItemData
/// </summary>
/// <param name="data">The data.</param>
/// <returns>DtoUserItemData.</returns>
/// <exception cref="System.ArgumentNullException"></exception>
private UserItemDataDto GetUserItemDataDto(UserItemData data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
return new UserItemDataDto
{
IsFavorite = data.IsFavorite,
Likes = data.Likes,
PlaybackPositionTicks = data.PlaybackPositionTicks,
PlayCount = data.PlayCount,
Rating = data.Rating,
Played = data.Played,
LastPlayedDate = data.LastPlayedDate,
Key = data.Key
};
}
public bool UpdatePlayState(BaseItem item, UserItemData data, long? reportedPositionTicks)
{
var playedToCompletion = false;
var positionTicks = reportedPositionTicks ?? item.RunTimeTicks ?? 0;
var hasRuntime = item.RunTimeTicks.HasValue && item.RunTimeTicks > 0;
// If a position has been reported, and if we know the duration
if (positionTicks > 0 && hasRuntime)
{
var pctIn = Decimal.Divide(positionTicks, item.RunTimeTicks.Value) * 100;
// Don't track in very beginning
if (pctIn < _config.Configuration.MinResumePct)
{
positionTicks = 0;
}
// If we're at the end, assume completed
else if (pctIn > _config.Configuration.MaxResumePct || positionTicks >= item.RunTimeTicks.Value)
{
positionTicks = 0;
data.Played = playedToCompletion = true;
}
else
{
// Enforce MinResumeDuration
var durationSeconds = TimeSpan.FromTicks(item.RunTimeTicks.Value).TotalSeconds;
if (durationSeconds < _config.Configuration.MinResumeDurationSeconds)
{
positionTicks = 0;
data.Played = playedToCompletion = true;
}
}
}
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;
}
if (!item.SupportsPlayedStatus)
{
positionTicks = 0;
data.Played = false;
}
if (!item.SupportsPositionTicksResume)
{
positionTicks = 0;
}
data.PlaybackPositionTicks = positionTicks;
return playedToCompletion;
}
}
}
|