convert playback checkins to rest

pull/702/head
LukePulverenti 12 years ago
parent 1db7faad89
commit 2d342c02ef

@ -1,110 +0,0 @@
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Connectivity;
using MediaBrowser.Model.Dto;
using System;
using System.Threading.Tasks;
namespace MediaBrowser.Api.HttpHandlers
{
/// <summary>
/// Provides a handler to set played status for an item
/// </summary>
public class PlaybackCheckInHandler : BaseSerializationHandler<Kernel, UserItemDataDto>
{
/// <summary>
/// Gets the object to serialize.
/// </summary>
/// <returns>Task{DtoUserItemData}.</returns>
protected override async Task<UserItemDataDto> GetObjectToSerialize()
{
// Get the user
var user = await this.GetCurrentUser().ConfigureAwait(false);
var clientType = ClientType.Other;
if (!string.IsNullOrEmpty(QueryString["client"]))
{
ClientType type;
if (Enum.TryParse(QueryString["client"], true, out type))
{
clientType = type;
}
}
var device = QueryString["device"];
// Get the item
var item = DtoBuilder.GetItemByClientId(QueryString["id"], user.Id);
// Playback start check-in
if (QueryString["type"].Equals("start", StringComparison.OrdinalIgnoreCase))
{
Kernel.UserDataManager.OnPlaybackStart(user, item, clientType, device);
}
else
{
long? positionTicks = null;
if (!string.IsNullOrEmpty(QueryString["positionTicks"]))
{
positionTicks = long.Parse(QueryString["positionTicks"]);
}
// Progress check-ins require position ticks
if (QueryString["type"].Equals("progress", StringComparison.OrdinalIgnoreCase))
{
await Kernel.UserDataManager.OnPlaybackProgress(user, item, positionTicks, clientType, device).ConfigureAwait(false);
}
else if (QueryString["type"].Equals("stopped", StringComparison.OrdinalIgnoreCase))
{
await Kernel.UserDataManager.OnPlaybackStopped(user, item, positionTicks, clientType, device).ConfigureAwait(false);
}
}
var data = item.GetUserData(user, true);
return new DtoBuilder(null).GetDtoUserItemData(data);
}
/// <summary>
/// Gets the current user.
/// </summary>
/// <returns>User.</returns>
/// <exception cref="System.UnauthorizedAccessException"></exception>
public async Task<User> GetCurrentUser()
{
var handler = this;
var id = handler.QueryString["userid"];
var user = ApiService.GetUserById(id);
if (user == null)
{
throw new UnauthorizedAccessException(string.Format("User with Id {0} does not exist", id));
}
var clientType = ClientType.Other;
if (!string.IsNullOrEmpty(handler.QueryString["client"]))
{
ClientType type;
if (Enum.TryParse(handler.QueryString["client"], true, out type))
{
clientType = type;
}
}
var device = handler.QueryString["device"];
await Controller.Kernel.Instance.UserManager.LogUserActivity(user, clientType, device).ConfigureAwait(false);
return user;
}
}
}

@ -75,7 +75,6 @@
</Compile> </Compile>
<Compile Include="ApiService.cs" /> <Compile Include="ApiService.cs" />
<Compile Include="EnvironmentService.cs" /> <Compile Include="EnvironmentService.cs" />
<Compile Include="HttpHandlers\PlaybackCheckInHandler.cs" />
<Compile Include="Images\ImageRequest.cs" /> <Compile Include="Images\ImageRequest.cs" />
<Compile Include="Images\ImageService.cs" /> <Compile Include="Images\ImageService.cs" />
<Compile Include="Images\ImageWriter.cs" /> <Compile Include="Images\ImageWriter.cs" />

@ -3,6 +3,7 @@ using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies; using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Connectivity;
using MediaBrowser.Model.Dto; using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities; using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Serialization;
@ -215,6 +216,66 @@ namespace MediaBrowser.Api.UserLibrary
public string Id { get; set; } public string Id { get; set; }
} }
[Route("/Users/{UserId}/PlayingItems/{Id}", "POST")]
public class OnPlaybackStart : IReturnVoid
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public string Id { get; set; }
}
[Route("/Users/{UserId}/PlayingItems/{Id}/Progress", "POST")]
public class OnPlaybackProgress : IReturnVoid
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public string Id { get; set; }
/// <summary>
/// Gets or sets the position ticks.
/// </summary>
/// <value>The position ticks.</value>
public long? PositionTicks { get; set; }
}
[Route("/Users/{UserId}/PlayingItems/{Id}", "DELETE")]
public class OnPlaybackStopped : IReturnVoid
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public string Id { get; set; }
/// <summary>
/// Gets or sets the position ticks.
/// </summary>
/// <value>The position ticks.</value>
public long? PositionTicks { get; set; }
}
/// <summary> /// <summary>
/// Class GetLocalTrailers /// Class GetLocalTrailers
/// </summary> /// </summary>
@ -515,6 +576,55 @@ namespace MediaBrowser.Api.UserLibrary
Task.WaitAll(task); Task.WaitAll(task);
} }
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(OnPlaybackStart request)
{
var kernel = (Kernel)Kernel;
var user = kernel.GetUserById(request.UserId);
var item = DtoBuilder.GetItemByClientId(request.Id, user.Id);
kernel.UserDataManager.OnPlaybackStart(user, item, ClientType.Other, string.Empty);
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(OnPlaybackProgress request)
{
var kernel = (Kernel)Kernel;
var user = kernel.GetUserById(request.UserId);
var item = DtoBuilder.GetItemByClientId(request.Id, user.Id);
var task = kernel.UserDataManager.OnPlaybackProgress(user, item, request.PositionTicks, ClientType.Other, string.Empty);
Task.WaitAll(task);
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(OnPlaybackStopped request)
{
var kernel = (Kernel)Kernel;
var user = kernel.GetUserById(request.UserId);
var item = DtoBuilder.GetItemByClientId(request.Id, user.Id);
var task = kernel.UserDataManager.OnPlaybackStopped(user, item, request.PositionTicks, ClientType.Other, string.Empty);
Task.WaitAll(task);
}
/// <summary> /// <summary>
/// Deletes the specified request. /// Deletes the specified request.
/// </summary> /// </summary>

@ -61,7 +61,7 @@ namespace MediaBrowser.Common.Extensions
/// <returns>Guid.</returns> /// <returns>Guid.</returns>
public static Guid GetMD5(this string str) public static Guid GetMD5(this string str)
{ {
using (var provider = new MD5CryptoServiceProvider()) using (var provider = MD5.Create())
{ {
return new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(str))); return new Guid(provider.ComputeHash(Encoding.Unicode.GetBytes(str)));
} }

@ -1,7 +1,6 @@
using MediaBrowser.Common.Events; using MediaBrowser.Common.Events;
using MediaBrowser.Common.Net; using MediaBrowser.Common.Net;
using MediaBrowser.Common.Plugins; using MediaBrowser.Common.Plugins;
using MediaBrowser.Common.ScheduledTasks;
using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Logging; using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Serialization;

@ -1,6 +1,7 @@
using MediaBrowser.Common.Net; using MediaBrowser.Common.Net;
using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Configuration;
using MediaBrowser.Model.Logging; using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Serialization;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@ -12,7 +13,6 @@ using System.Reflection;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MediaBrowser.Model.Serialization;
namespace MediaBrowser.Common.Kernel namespace MediaBrowser.Common.Kernel
{ {

Loading…
Cancel
Save