jellyfin/Emby.Notifications/Api/NotificationsService.cs

190 lines
8.1 KiB
C#
Raw Normal View History

#pragma warning disable CS1591
#pragma warning disable SA1402
#pragma warning disable SA1649
using System;
2014-04-25 13:15:50 -07:00
using System.Collections.Generic;
2020-02-04 04:29:14 -07:00
using System.Diagnostics.CodeAnalysis;
2014-04-26 20:42:05 -07:00
using System.Linq;
2013-07-06 14:23:32 -07:00
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Notifications;
2018-09-12 10:26:21 -07:00
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Notifications;
using MediaBrowser.Model.Services;
2013-07-06 14:23:32 -07:00
2018-09-12 10:26:21 -07:00
namespace Emby.Notifications.Api
2013-07-06 14:23:32 -07:00
{
2020-02-04 04:29:14 -07:00
[Route("/Notifications/{UserId}", "GET", Summary = "Gets notifications")]
public class GetNotifications : IReturn<NotificationResult>
{
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2020-02-08 14:25:44 -07:00
public string UserId { get; set; } = string.Empty;
2020-02-04 04:29:14 -07: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; }
}
public class Notification
{
2020-02-08 14:25:44 -07:00
public string Id { get; set; } = string.Empty;
2020-02-04 04:29:14 -07:00
2020-02-08 14:25:44 -07:00
public string UserId { get; set; } = string.Empty;
2020-02-04 04:29:14 -07:00
public DateTime Date { get; set; }
public bool IsRead { get; set; }
2020-02-08 14:25:44 -07:00
public string Name { get; set; } = string.Empty;
2020-02-04 04:29:14 -07:00
2020-02-08 14:25:44 -07:00
public string Description { get; set; } = string.Empty;
2020-02-04 04:29:14 -07:00
2020-02-08 14:25:44 -07:00
public string Url { get; set; } = string.Empty;
2020-02-04 04:29:14 -07:00
public NotificationLevel Level { get; set; }
}
public class NotificationResult
{
2020-02-08 14:25:44 -07:00
public IReadOnlyList<Notification> Notifications { get; set; } = Array.Empty<Notification>();
2020-02-04 04:29:14 -07:00
public int TotalRecordCount { get; set; }
}
public class NotificationsSummary
{
public int UnreadCount { get; set; }
public NotificationLevel MaxUnreadNotificationLevel { get; set; }
}
[Route("/Notifications/{UserId}/Summary", "GET", Summary = "Gets a notification summary for a user")]
public class GetNotificationsSummary : IReturn<NotificationsSummary>
{
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
2020-02-08 14:25:44 -07:00
public string UserId { get; set; } = string.Empty;
2020-02-04 04:29:14 -07:00
}
2014-04-26 20:42:05 -07:00
[Route("/Notifications/Types", "GET", Summary = "Gets notification types")]
public class GetNotificationTypes : IReturn<List<NotificationTypeInfo>>
{
}
[Route("/Notifications/Services", "GET", Summary = "Gets notification types")]
2018-09-12 10:26:21 -07:00
public class GetNotificationServices : IReturn<List<NameIdPair>>
2014-04-26 20:42:05 -07:00
{
}
2014-04-26 21:35:04 -07:00
[Route("/Notifications/Admin", "POST", Summary = "Sends a notification to all admin users")]
2014-04-26 20:42:05 -07:00
public class AddAdminNotification : IReturnVoid
2013-07-06 14:23:32 -07:00
{
2014-04-26 20:42:05 -07:00
[ApiMember(Name = "Name", Description = "The notification's name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Name { get; set; } = string.Empty;
2014-04-26 20:42:05 -07:00
[ApiMember(Name = "Description", Description = "The notification's description", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string Description { get; set; } = string.Empty;
2013-07-06 14:23:32 -07:00
2014-04-26 20:42:05 -07:00
[ApiMember(Name = "ImageUrl", Description = "The notification's image url", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string? ImageUrl { get; set; }
2014-04-26 20:42:05 -07:00
[ApiMember(Name = "Url", Description = "The notification's info url", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public string? Url { get; set; }
2014-04-26 20:42:05 -07:00
[ApiMember(Name = "Level", Description = "The notification level", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
public NotificationLevel Level { get; set; }
}
2014-03-23 13:07:02 -07:00
2020-02-04 04:29:14 -07:00
[Route("/Notifications/{UserId}/Read", "POST", Summary = "Marks notifications as read")]
public class MarkRead : IReturnVoid
{
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2020-02-08 14:25:44 -07:00
public string UserId { get; set; } = string.Empty;
2020-02-04 04:29:14 -07:00
[ApiMember(Name = "Ids", Description = "A list of notification ids, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
2020-02-08 14:25:44 -07:00
public string Ids { get; set; } = string.Empty;
2020-02-04 04:29:14 -07:00
}
[Route("/Notifications/{UserId}/Unread", "POST", Summary = "Marks notifications as unread")]
public class MarkUnread : IReturnVoid
{
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
2020-02-08 14:25:44 -07:00
public string UserId { get; set; } = string.Empty;
2020-02-04 04:29:14 -07:00
[ApiMember(Name = "Ids", Description = "A list of notification ids, comma delimited", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
2020-02-08 14:25:44 -07:00
public string Ids { get; set; } = string.Empty;
2020-02-04 04:29:14 -07:00
}
[Authenticated]
2018-09-12 10:26:21 -07:00
public class NotificationsService : IService
2013-07-06 14:23:32 -07:00
{
2014-04-25 13:15:50 -07:00
private readonly INotificationManager _notificationManager;
2014-04-26 20:42:05 -07:00
private readonly IUserManager _userManager;
2013-07-06 14:23:32 -07:00
2018-09-12 10:26:21 -07:00
public NotificationsService(INotificationManager notificationManager, IUserManager userManager)
2013-07-06 14:23:32 -07:00
{
2014-04-25 13:15:50 -07:00
_notificationManager = notificationManager;
2014-04-26 20:42:05 -07:00
_userManager = userManager;
2013-07-06 14:23:32 -07:00
}
2020-04-02 07:49:58 -07:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2014-04-26 20:42:05 -07:00
public object Get(GetNotificationTypes request)
{
2018-09-12 10:26:21 -07:00
return _notificationManager.GetNotificationTypes();
2014-04-26 20:42:05 -07:00
}
2020-04-02 07:49:58 -07:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2014-04-26 20:42:05 -07:00
public object Get(GetNotificationServices request)
{
2018-09-12 10:26:21 -07:00
return _notificationManager.GetNotificationServices().ToList();
2014-04-26 20:42:05 -07:00
}
2020-04-02 07:49:58 -07:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2020-02-04 04:29:14 -07:00
public object Get(GetNotificationsSummary request)
{
return new NotificationsSummary
{
};
}
2018-09-12 10:26:21 -07:00
public Task Post(AddAdminNotification request)
2014-04-26 20:42:05 -07:00
{
// This endpoint really just exists as post of a real with sickbeard
var notification = new NotificationRequest
{
Date = DateTime.UtcNow,
Description = request.Description,
Level = request.Level,
Name = request.Name,
Url = request.Url,
2018-09-12 10:26:21 -07:00
UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id).ToArray()
2014-04-26 20:42:05 -07:00
};
2018-09-12 10:26:21 -07:00
return _notificationManager.SendNotification(notification, CancellationToken.None);
2013-07-06 14:23:32 -07:00
}
2020-02-04 04:29:14 -07:00
2020-04-02 07:49:58 -07:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2020-02-04 04:29:14 -07:00
public void Post(MarkRead request)
{
}
2020-04-02 07:49:58 -07:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2020-02-04 04:29:14 -07:00
public void Post(MarkUnread request)
{
}
2020-04-02 07:49:58 -07:00
[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
2020-02-04 04:29:14 -07:00
public object Get(GetNotifications request)
{
return new NotificationResult();
}
2013-07-06 14:23:32 -07:00
}
}