2013-07-06 21:23:32 +00:00
using MediaBrowser.Controller.Notifications ;
using MediaBrowser.Model.Notifications ;
2014-03-23 20:07:02 +00:00
using ServiceStack ;
2013-07-06 21:23:32 +00:00
using System ;
using System.Linq ;
using System.Threading ;
using System.Threading.Tasks ;
namespace MediaBrowser.Api
{
2014-03-23 20:07:02 +00:00
[Route("/Notifications/{UserId}", "GET", Summary = "Gets notifications")]
2013-07-06 21:23:32 +00:00
public class GetNotifications : IReturn < NotificationResult >
{
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2013-07-16 01:33:47 +00:00
public string UserId { get ; set ; }
2013-07-06 21:23:32 +00:00
[ApiMember(Name = "IsRead", Description = "An optional filter by IsRead", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool? IsRead { get ; set ; }
[ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? StartIndex { get ; set ; }
[ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? Limit { get ; set ; }
}
2014-03-23 20:07:02 +00:00
[Route("/Notifications/{UserId}/Summary", "GET", Summary = "Gets a notification summary for a user")]
2013-07-06 21:23:32 +00:00
public class GetNotificationsSummary : IReturn < NotificationsSummary >
{
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public Guid UserId { get ; set ; }
}
2014-03-23 20:07:02 +00:00
[Route("/Notifications/{UserId}", "POST", Summary = "Adds a notifications")]
2014-02-17 21:35:08 +00:00
public class AddUserNotification : IReturn < Notification >
2013-07-06 21:23:32 +00:00
{
[ApiMember(Name = "Id", Description = "The Id of the new notification. If unspecified one will be provided.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public Guid ? Id { get ; set ; }
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get ; set ; }
[ApiMember(Name = "Name", Description = "The notification's name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Name { get ; set ; }
[ApiMember(Name = "Description", Description = "The notification's description", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Description { get ; set ; }
[ApiMember(Name = "Url", Description = "The notification's info url", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Url { get ; set ; }
2013-08-21 20:09:20 +00:00
[ApiMember(Name = "Category", Description = "The notification's category", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
2013-07-06 21:23:32 +00:00
public string Category { get ; set ; }
[ApiMember(Name = "RelatedId", Description = "The notification's related id (item)", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string RelatedId { get ; set ; }
[ApiMember(Name = "Level", Description = "The notification level", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public NotificationLevel Level { get ; set ; }
}
2014-03-23 20:07:02 +00:00
[Route("/Notifications/{UserId}/Read", "POST", Summary = "Marks notifications as read")]
2013-07-06 21:23:32 +00:00
public class MarkRead : IReturnVoid
{
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get ; set ; }
[ApiMember(Name = "Ids", Description = "A list of notification ids, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
public string Ids { get ; set ; }
}
2014-03-23 20:07:02 +00:00
[Route("/Notifications/{UserId}/Unread", "POST", Summary = "Marks notifications as unread")]
2013-07-06 21:23:32 +00:00
public class MarkUnread : IReturnVoid
{
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get ; set ; }
[ApiMember(Name = "Ids", Description = "A list of notification ids, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
public string Ids { get ; set ; }
}
public class NotificationsService : BaseApiService
{
private readonly INotificationsRepository _notificationsRepo ;
public NotificationsService ( INotificationsRepository notificationsRepo )
{
_notificationsRepo = notificationsRepo ;
}
2014-02-17 21:35:08 +00:00
public object Post ( AddUserNotification request )
2013-07-06 21:23:32 +00:00
{
var task = AddNotification ( request ) ;
return ToOptimizedResult ( task . Result ) ;
}
public object Get ( GetNotificationsSummary request )
{
var result = _notificationsRepo . GetNotificationsSummary ( request . UserId ) ;
return result ;
}
2014-02-17 21:35:08 +00:00
private async Task < Notification > AddNotification ( AddUserNotification request )
2013-07-06 21:23:32 +00:00
{
var notification = new Notification
{
Id = request . Id ? ? Guid . NewGuid ( ) ,
Date = DateTime . UtcNow ,
Description = request . Description ,
Level = request . Level ,
Name = request . Name ,
Url = request . Url ,
UserId = request . UserId ,
Category = request . Category ,
RelatedId = request . RelatedId
} ;
await _notificationsRepo . AddNotification ( notification , CancellationToken . None ) . ConfigureAwait ( false ) ;
return notification ;
}
public void Post ( MarkRead request )
{
var task = MarkRead ( request . Ids , request . UserId , true ) ;
Task . WaitAll ( task ) ;
}
public void Post ( MarkUnread request )
{
var task = MarkRead ( request . Ids , request . UserId , false ) ;
Task . WaitAll ( task ) ;
}
private Task MarkRead ( string idList , Guid userId , bool read )
{
var ids = idList . Split ( ',' ) . Select ( i = > new Guid ( i ) ) ;
return _notificationsRepo . MarkRead ( ids , userId , read , CancellationToken . None ) ;
}
public object Get ( GetNotifications request )
{
var result = _notificationsRepo . GetNotifications ( new NotificationQuery
{
IsRead = request . IsRead ,
Limit = request . Limit ,
StartIndex = request . StartIndex ,
UserId = request . UserId
} ) ;
return ToOptimizedResult ( result ) ;
}
}
}