Browse Source

Migrates the notifications service to use ASP.NET MVC framework

ZadenRB 5 years ago
parent
commit
1180b9746f

+ 0 - 189
Emby.Notifications/Api/NotificationsService.cs

@@ -1,189 +0,0 @@
-#pragma warning disable CS1591
-#pragma warning disable SA1402
-#pragma warning disable SA1649
-
-using System;
-using System.Collections.Generic;
-using System.Diagnostics.CodeAnalysis;
-using System.Linq;
-using System.Threading;
-using System.Threading.Tasks;
-using MediaBrowser.Controller.Library;
-using MediaBrowser.Controller.Net;
-using MediaBrowser.Controller.Notifications;
-using MediaBrowser.Model.Dto;
-using MediaBrowser.Model.Notifications;
-using MediaBrowser.Model.Services;
-
-namespace Emby.Notifications.Api
-{
-    [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")]
-        public string UserId { get; set; } = string.Empty;
-
-        [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
-    {
-        public string Id { get; set; } = string.Empty;
-
-        public string UserId { get; set; } = string.Empty;
-
-        public DateTime Date { get; set; }
-
-        public bool IsRead { get; set; }
-
-        public string Name { get; set; } = string.Empty;
-
-        public string Description { get; set; } = string.Empty;
-
-        public string Url { get; set; } = string.Empty;
-
-        public NotificationLevel Level { get; set; }
-    }
-
-    public class NotificationResult
-    {
-        public IReadOnlyList<Notification> Notifications { get; set; } = Array.Empty<Notification>();
-
-        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")]
-        public string UserId { get; set; } = string.Empty;
-    }
-
-    [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<NameIdPair>>
-    {
-    }
-
-    [Route("/Notifications/Admin", "POST", Summary = "Sends a notification to all admin users")]
-    public class AddAdminNotification : IReturnVoid
-    {
-        [ApiMember(Name = "Name", Description = "The notification's name", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
-        public string Name { get; set; } = string.Empty;
-
-        [ApiMember(Name = "Description", Description = "The notification's description", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
-        public string Description { get; set; } = string.Empty;
-
-        [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; }
-    }
-
-    [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")]
-        public string UserId { get; set; } = string.Empty;
-
-        [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; } = string.Empty;
-    }
-
-    [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")]
-        public string UserId { get; set; } = string.Empty;
-
-        [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; } = string.Empty;
-    }
-
-    [Authenticated]
-    public class NotificationsService : IService
-    {
-        private readonly INotificationManager _notificationManager;
-        private readonly IUserManager _userManager;
-
-        public NotificationsService(INotificationManager notificationManager, IUserManager userManager)
-        {
-            _notificationManager = notificationManager;
-            _userManager = userManager;
-        }
-
-        [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
-        public object Get(GetNotificationTypes request)
-        {
-            return _notificationManager.GetNotificationTypes();
-        }
-
-        [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
-        public object Get(GetNotificationServices request)
-        {
-            return _notificationManager.GetNotificationServices().ToList();
-        }
-
-        [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
-        public object Get(GetNotificationsSummary request)
-        {
-            return new NotificationsSummary
-            {
-            };
-        }
-
-        public Task Post(AddAdminNotification request)
-        {
-            // 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,
-                UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id).ToArray()
-            };
-
-            return _notificationManager.SendNotification(notification, CancellationToken.None);
-        }
-
-        [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
-        public void Post(MarkRead request)
-        {
-        }
-
-        [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
-        public void Post(MarkUnread request)
-        {
-        }
-
-        [SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "request", Justification = "Required for ServiceStack")]
-        public object Get(GetNotifications request)
-        {
-            return new NotificationResult();
-        }
-    }
-}

+ 138 - 0
Jellyfin.Api/Controllers/NotificationsController.cs

@@ -0,0 +1,138 @@
+#nullable enable
+#pragma warning disable CA1801
+#pragma warning disable SA1313
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading;
+using Jellyfin.Api.Models.NotificationDtos;
+using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.Notifications;
+using MediaBrowser.Model.Dto;
+using MediaBrowser.Model.Notifications;
+using Microsoft.AspNetCore.Mvc;
+
+namespace Jellyfin.Api.Controllers
+{
+    /// <summary>
+    /// The notification controller.
+    /// </summary>
+    public class NotificationsController : BaseJellyfinApiController
+    {
+        private readonly INotificationManager _notificationManager;
+        private readonly IUserManager _userManager;
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="NotificationsController" /> class.
+        /// </summary>
+        /// <param name="notificationManager">The notification manager.</param>
+        /// <param name="userManager">The user manager.</param>
+        public NotificationsController(INotificationManager notificationManager, IUserManager userManager)
+        {
+            _notificationManager = notificationManager;
+            _userManager = userManager;
+        }
+
+        /// <summary>
+        /// Endpoint for getting a user's notifications.
+        /// </summary>
+        /// <param name="UserID">The UserID.</param>
+        /// <param name="IsRead">An optional filter by IsRead.</param>
+        /// <param name="StartIndex">The optional index to start at. All notifications with a lower index will be dropped from the results.</param>
+        /// <param name="Limit">An optional limit on the number of notifications returned.</param>
+        /// <returns>A read-only list of all of the user's notifications.</returns>
+        [HttpGet("{UserID}")]
+        public IReadOnlyList<NotificationDto> GetNotifications(
+            [FromRoute] string UserID,
+            [FromQuery] bool? IsRead,
+            [FromQuery] int? StartIndex,
+            [FromQuery] int? Limit)
+        {
+            return new List<NotificationDto>();
+        }
+
+        /// <summary>
+        /// Endpoint for getting a user's notification summary.
+        /// </summary>
+        /// <param name="UserID">The UserID.</param>
+        /// <returns>Notifications summary for the user.</returns>
+        [HttpGet("{UserId}/Summary")]
+        public NotificationsSummaryDto GetNotificationsSummary(
+            [FromRoute] string UserID)
+        {
+            return new NotificationsSummaryDto();
+        }
+
+        /// <summary>
+        /// Endpoint for getting notification types.
+        /// </summary>
+        /// <returns>All notification types.</returns>
+        [HttpGet("Types")]
+        public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
+        {
+            return _notificationManager.GetNotificationTypes();
+        }
+
+        /// <summary>
+        /// Endpoint for getting notification services.
+        /// </summary>
+        /// <returns>All notification services.</returns>
+        [HttpGet("Services")]
+        public IEnumerable<NameIdPair> GetNotificationServices()
+        {
+            return _notificationManager.GetNotificationServices();
+        }
+
+        /// <summary>
+        /// Endpoint to send a notification to all admins.
+        /// </summary>
+        /// <param name="Name">The name of the notification.</param>
+        /// <param name="Description">The description of the notification.</param>
+        /// <param name="URL">The URL of the notification.</param>
+        /// <param name="Level">The level of the notification.</param>
+        [HttpPost("Admin")]
+        public void CreateAdminNotification(
+            [FromForm] string Name,
+            [FromForm] string Description,
+            [FromForm] string? URL,
+            [FromForm] NotificationLevel Level)
+        {
+            var notification = new NotificationRequest
+            {
+                Name = Name,
+                Description = Description,
+                Url = URL,
+                Level = Level,
+                UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id).ToArray(),
+                Date = DateTime.UtcNow,
+            };
+
+            _notificationManager.SendNotification(notification, CancellationToken.None);
+        }
+
+        /// <summary>
+        /// Endpoint to set notifications as read.
+        /// </summary>
+        /// <param name="UserID">The UserID.</param>
+        /// <param name="IDs">The IDs of notifications which should be set as read.</param>
+        [HttpPost("{UserID}/Read")]
+        public void SetRead(
+            [FromRoute] string UserID,
+            [FromForm] List<string> IDs)
+        {
+        }
+
+        /// <summary>
+        /// Endpoint to set notifications as unread.
+        /// </summary>
+        /// <param name="UserID">The UserID.</param>
+        /// <param name="IDs">The IDs of notifications which should be set as unread.</param>
+        [HttpPost("{UserID}/Unread")]
+        public void SetUnread(
+            [FromRoute] string UserID,
+            [FromForm] List<string> IDs)
+        {
+        }
+    }
+}

+ 51 - 0
Jellyfin.Api/Models/NotificationDtos/NotificationDto.cs

@@ -0,0 +1,51 @@
+using System;
+using MediaBrowser.Model.Notifications;
+
+namespace Jellyfin.Api.Models.NotificationDtos
+{
+    /// <summary>
+    /// The notification DTO.
+    /// </summary>
+    public class NotificationDto
+    {
+        /// <summary>
+        /// Gets or sets the notification ID. Defaults to an empty string.
+        /// </summary>
+        public string Id { get; set; } = string.Empty;
+
+        /// <summary>
+        /// Gets or sets the notification's user ID. Defaults to an empty string.
+        /// </summary>
+        public string UserId { get; set; } = string.Empty;
+
+        /// <summary>
+        /// Gets or sets the notification date.
+        /// </summary>
+        public DateTime Date { get; set; }
+
+        /// <summary>
+        /// Gets or sets a value indicating whether the notification has been read.
+        /// </summary>
+        public bool IsRead { get; set; }
+
+        /// <summary>
+        /// Gets or sets the notification's name.
+        /// </summary>
+        public string Name { get; set; } = string.Empty;
+
+        /// <summary>
+        /// Gets or sets the notification's description.
+        /// </summary>
+        public string Description { get; set; } = string.Empty;
+
+        /// <summary>
+        /// Gets or sets the notification's URL.
+        /// </summary>
+        public string Url { get; set; } = string.Empty;
+
+        /// <summary>
+        /// Gets or sets the notification level.
+        /// </summary>
+        public NotificationLevel Level { get; set; }
+    }
+}

+ 20 - 0
Jellyfin.Api/Models/NotificationDtos/NotificationsSummaryDto.cs

@@ -0,0 +1,20 @@
+using MediaBrowser.Model.Notifications;
+
+namespace Jellyfin.Api.Models.NotificationDtos
+{
+    /// <summary>
+    /// The notification summary DTO.
+    /// </summary>
+    public class NotificationsSummaryDto
+    {
+        /// <summary>
+        /// Gets or sets the number of unread notifications.
+        /// </summary>
+        public int UnreadCount { get; set; }
+
+        /// <summary>
+        /// Gets or sets the maximum unread notification level.
+        /// </summary>
+        public NotificationLevel MaxUnreadNotificationLevel { get; set; }
+    }
+}

+ 1 - 0
Jellyfin.Server/Extensions/ApiServiceCollectionExtensions.cs

@@ -71,6 +71,7 @@ namespace Jellyfin.Server.Extensions
                 // Clear app parts to avoid other assemblies being picked up
                 // Clear app parts to avoid other assemblies being picked up
                 .ConfigureApplicationPartManager(a => a.ApplicationParts.Clear())
                 .ConfigureApplicationPartManager(a => a.ApplicationParts.Clear())
                 .AddApplicationPart(typeof(StartupController).Assembly)
                 .AddApplicationPart(typeof(StartupController).Assembly)
+                .AddApplicationPart(typeof(NotificationsController).Assembly)
                 .AddControllersAsServices();
                 .AddControllersAsServices();
         }
         }