2020-04-15 06:01:31 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-03-12 05:45:58 +00:00
|
|
|
using System.ComponentModel.DataAnnotations;
|
2020-04-15 06:01:31 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
2020-08-06 14:17:45 +00:00
|
|
|
using Jellyfin.Api.Constants;
|
2020-04-15 06:01:31 +00:00
|
|
|
using Jellyfin.Api.Models.NotificationDtos;
|
2020-06-15 02:18:06 +00:00
|
|
|
using Jellyfin.Data.Enums;
|
2020-04-15 06:01:31 +00:00
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.Notifications;
|
|
|
|
using MediaBrowser.Model.Dto;
|
|
|
|
using MediaBrowser.Model.Notifications;
|
2020-08-06 14:17:45 +00:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-04-20 03:06:28 +00:00
|
|
|
using Microsoft.AspNetCore.Http;
|
2020-04-15 06:01:31 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace Jellyfin.Api.Controllers
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// The notification controller.
|
|
|
|
/// </summary>
|
2020-08-06 14:17:45 +00:00
|
|
|
[Authorize(Policy = Policies.DefaultAuthorization)]
|
2020-04-15 06:01:31 +00:00
|
|
|
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>
|
2020-04-28 15:30:59 +00:00
|
|
|
/// Gets a user's notifications.
|
2020-04-15 06:01:31 +00:00
|
|
|
/// </summary>
|
2020-04-28 15:30:59 +00:00
|
|
|
/// <response code="200">Notifications returned.</response>
|
|
|
|
/// <returns>An <see cref="OkResult"/> containing a list of notifications.</returns>
|
2020-06-21 00:02:07 +00:00
|
|
|
[HttpGet("{userId}")]
|
2020-04-22 16:00:10 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-06-24 20:43:28 +00:00
|
|
|
public ActionResult<NotificationResultDto> GetNotifications()
|
2020-04-15 06:01:31 +00:00
|
|
|
{
|
2020-04-21 05:53:09 +00:00
|
|
|
return new NotificationResultDto();
|
2020-04-15 06:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-04-28 15:30:59 +00:00
|
|
|
/// Gets a user's notification summary.
|
2020-04-15 06:01:31 +00:00
|
|
|
/// </summary>
|
2020-04-28 15:30:59 +00:00
|
|
|
/// <response code="200">Summary of user's notifications returned.</response>
|
|
|
|
/// <returns>An <cref see="OkResult"/> containing a summary of the users notifications.</returns>
|
2020-06-21 00:02:07 +00:00
|
|
|
[HttpGet("{userId}/Summary")]
|
2020-04-22 16:00:10 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-06-24 20:43:28 +00:00
|
|
|
public ActionResult<NotificationsSummaryDto> GetNotificationsSummary()
|
2020-04-15 06:01:31 +00:00
|
|
|
{
|
2020-04-21 05:53:09 +00:00
|
|
|
return new NotificationsSummaryDto();
|
2020-04-15 06:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-04-28 15:30:59 +00:00
|
|
|
/// Gets notification types.
|
2020-04-15 06:01:31 +00:00
|
|
|
/// </summary>
|
2020-04-28 15:30:59 +00:00
|
|
|
/// <response code="200">All notification types returned.</response>
|
|
|
|
/// <returns>An <cref see="OkResult"/> containing a list of all notification types.</returns>
|
2020-04-15 06:01:31 +00:00
|
|
|
[HttpGet("Types")]
|
2020-04-22 16:00:10 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-04-29 21:53:29 +00:00
|
|
|
public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
|
2020-04-15 06:01:31 +00:00
|
|
|
{
|
2020-04-21 05:53:09 +00:00
|
|
|
return _notificationManager.GetNotificationTypes();
|
2020-04-15 06:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-04-28 15:30:59 +00:00
|
|
|
/// Gets notification services.
|
2020-04-15 06:01:31 +00:00
|
|
|
/// </summary>
|
2020-05-03 20:02:15 +00:00
|
|
|
/// <response code="200">All notification services returned.</response>
|
2020-04-28 15:30:59 +00:00
|
|
|
/// <returns>An <cref see="OkResult"/> containing a list of all notification services.</returns>
|
2020-04-15 06:01:31 +00:00
|
|
|
[HttpGet("Services")]
|
2020-04-22 16:00:10 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
2020-04-29 21:53:29 +00:00
|
|
|
public IEnumerable<NameIdPair> GetNotificationServices()
|
2020-04-15 06:01:31 +00:00
|
|
|
{
|
2020-04-29 21:53:29 +00:00
|
|
|
return _notificationManager.GetNotificationServices();
|
2020-04-15 06:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-04-28 15:30:59 +00:00
|
|
|
/// Sends a notification to all admins.
|
2020-04-15 06:01:31 +00:00
|
|
|
/// </summary>
|
2021-03-12 05:45:58 +00:00
|
|
|
/// <param name="notificationDto">The notification request.</param>
|
2020-06-12 10:38:13 +00:00
|
|
|
/// <response code="204">Notification sent.</response>
|
|
|
|
/// <returns>A <cref see="NoContentResult"/>.</returns>
|
2020-04-15 06:01:31 +00:00
|
|
|
[HttpPost("Admin")]
|
2020-06-12 10:38:13 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2021-03-12 05:45:58 +00:00
|
|
|
public ActionResult CreateAdminNotification([FromBody, Required] AdminNotificationDto notificationDto)
|
2020-04-15 06:01:31 +00:00
|
|
|
{
|
2020-04-21 02:16:58 +00:00
|
|
|
var notification = new NotificationRequest
|
2020-04-15 06:01:31 +00:00
|
|
|
{
|
2021-03-12 05:45:58 +00:00
|
|
|
Name = notificationDto.Name,
|
|
|
|
Description = notificationDto.Description,
|
|
|
|
Url = notificationDto.Url,
|
|
|
|
Level = notificationDto.NotificationLevel ?? NotificationLevel.Normal,
|
2020-06-15 02:18:06 +00:00
|
|
|
UserIds = _userManager.Users
|
|
|
|
.Where(user => user.HasPermission(PermissionKind.IsAdministrator))
|
|
|
|
.Select(user => user.Id)
|
|
|
|
.ToArray(),
|
2020-04-21 02:16:58 +00:00
|
|
|
Date = DateTime.UtcNow,
|
|
|
|
};
|
2020-04-15 06:01:31 +00:00
|
|
|
|
2020-04-21 02:16:58 +00:00
|
|
|
_notificationManager.SendNotification(notification, CancellationToken.None);
|
2020-06-12 10:38:13 +00:00
|
|
|
return NoContent();
|
2020-04-15 06:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-04-28 15:30:59 +00:00
|
|
|
/// Sets notifications as read.
|
2020-04-15 06:01:31 +00:00
|
|
|
/// </summary>
|
2020-06-12 10:38:13 +00:00
|
|
|
/// <response code="204">Notifications set as read.</response>
|
|
|
|
/// <returns>A <cref see="NoContentResult"/>.</returns>
|
2020-06-21 00:02:07 +00:00
|
|
|
[HttpPost("{userId}/Read")]
|
2020-06-12 10:38:13 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-06-24 20:43:28 +00:00
|
|
|
public ActionResult SetRead()
|
2020-04-15 06:01:31 +00:00
|
|
|
{
|
2020-06-12 10:38:13 +00:00
|
|
|
return NoContent();
|
2020-04-15 06:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2020-04-28 15:30:59 +00:00
|
|
|
/// Sets notifications as unread.
|
2020-04-15 06:01:31 +00:00
|
|
|
/// </summary>
|
2020-06-12 10:38:13 +00:00
|
|
|
/// <response code="204">Notifications set as unread.</response>
|
|
|
|
/// <returns>A <cref see="NoContentResult"/>.</returns>
|
2020-06-21 00:02:07 +00:00
|
|
|
[HttpPost("{userId}/Unread")]
|
2020-06-12 10:38:13 +00:00
|
|
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
2020-06-24 20:43:28 +00:00
|
|
|
public ActionResult SetUnread()
|
2020-04-15 06:01:31 +00:00
|
|
|
{
|
2020-06-12 10:38:13 +00:00
|
|
|
return NoContent();
|
2020-04-15 06:01:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|