jellyfin-server/MediaBrowser.Api/NotificationsService.cs

184 lines
7.5 KiB
C#
Raw Normal View History

2014-04-27 03:42:05 +00:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
2014-04-27 03:42:05 +00:00
using MediaBrowser.Controller.Notifications;
2013-07-06 21:23:32 +00:00
using MediaBrowser.Model.Notifications;
using System;
2014-04-25 20:15:50 +00:00
using System.Collections.Generic;
2014-04-27 03:42:05 +00:00
using System.Linq;
2013-07-06 21:23:32 +00:00
using System.Threading;
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services;
2013-07-06 21:23:32 +00:00
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")]
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")]
2014-04-25 20:15:50 +00:00
public string UserId { get; set; }
2013-07-06 21:23:32 +00:00
}
2014-04-27 03:42:05 +00:00
[Route("/Notifications/Types", "GET", Summary = "Gets notification types")]
public class GetNotificationTypes : IReturn<List<NotificationTypeInfo>>
{
}
[Route("/Notifications/Services", "GET", Summary = "Gets notification types")]
public class GetNotificationServices : IReturn<List<NotificationServiceInfo>>
{
}
2014-04-27 04:35:04 +00:00
[Route("/Notifications/Admin", "POST", Summary = "Sends a notification to all admin users")]
2014-04-27 03:42:05 +00:00
public class AddAdminNotification : IReturnVoid
2013-07-06 21:23:32 +00:00
{
2014-04-27 03:42:05 +00:00
[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; }
2013-07-06 21:23:32 +00:00
2014-04-27 03:42:05 +00:00
[ApiMember(Name = "ImageUrl", Description = "The notification's image url", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ImageUrl { get; set; }
[ApiMember(Name = "Url", Description = "The notification's info url", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Url { 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")]
2014-04-25 20:15:50 +00:00
public string UserId { get; set; }
2013-07-06 21:23:32 +00:00
[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")]
2014-04-25 20:15:50 +00:00
public string UserId { get; set; }
2013-07-06 21:23:32 +00:00
[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; }
}
[Authenticated]
2013-07-06 21:23:32 +00:00
public class NotificationsService : BaseApiService
{
private readonly INotificationsRepository _notificationsRepo;
2014-04-25 20:15:50 +00:00
private readonly INotificationManager _notificationManager;
2014-04-27 03:42:05 +00:00
private readonly IUserManager _userManager;
2013-07-06 21:23:32 +00:00
2014-04-27 03:42:05 +00:00
public NotificationsService(INotificationsRepository notificationsRepo, INotificationManager notificationManager, IUserManager userManager)
2013-07-06 21:23:32 +00:00
{
_notificationsRepo = notificationsRepo;
2014-04-25 20:15:50 +00:00
_notificationManager = notificationManager;
2014-04-27 03:42:05 +00:00
_userManager = userManager;
2013-07-06 21:23:32 +00:00
}
2014-04-27 03:42:05 +00:00
public object Get(GetNotificationTypes request)
{
2017-08-19 19:43:35 +00:00
var result = _notificationManager.GetNotificationTypes();
2014-04-27 03:42:05 +00:00
return ToOptimizedResult(result);
}
public object Get(GetNotificationServices request)
{
var result = _notificationManager.GetNotificationServices().ToList();
return ToOptimizedResult(result);
}
2013-07-06 21:23:32 +00:00
public object Get(GetNotificationsSummary request)
{
var result = _notificationsRepo.GetNotificationsSummary(request.UserId);
2014-04-27 03:42:05 +00:00
return ToOptimizedResult(result);
2013-07-06 21:23:32 +00:00
}
2014-04-27 03:42:05 +00:00
public void Post(AddAdminNotification request)
{
// This endpoint really just exists as post of a real with sickbeard
var task = AddNotification(request);
Task.WaitAll(task);
}
private async Task AddNotification(AddAdminNotification request)
{
var notification = new NotificationRequest
{
Date = DateTime.UtcNow,
Description = request.Description,
Level = request.Level,
Name = request.Name,
Url = request.Url,
2014-12-20 06:06:27 +00:00
UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id.ToString("N")).ToList()
2014-04-27 03:42:05 +00:00
};
2014-04-25 20:15:50 +00:00
await _notificationManager.SendNotification(notification, CancellationToken.None).ConfigureAwait(false);
2013-07-06 21:23:32 +00:00
}
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);
}
2014-04-25 20:15:50 +00:00
private Task MarkRead(string idList, string userId, bool read)
2013-07-06 21:23:32 +00:00
{
2014-10-04 18:05:24 +00:00
var ids = (idList ?? string.Empty).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
if (ids.Length == 0)
{
return _notificationsRepo.MarkAllRead(userId, read, CancellationToken.None);
}
2013-07-06 21:23:32 +00:00
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);
}
}
}