jellyfin/Jellyfin.Api/Controllers/NotificationsController.cs

139 lines
5.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2021-03-12 05:45:58 +00:00
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading;
2020-08-06 14:17:45 +00:00
using Jellyfin.Api.Constants;
using Jellyfin.Api.Models.NotificationDtos;
2020-06-15 02:18:06 +00:00
using Jellyfin.Data.Enums;
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;
using Microsoft.AspNetCore.Http;
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)]
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>
/// Gets a user's notifications.
/// </summary>
/// <response code="200">Notifications returned.</response>
/// <returns>An <see cref="OkResult"/> containing a list of notifications.</returns>
[HttpGet("{userId}")]
[ProducesResponseType(StatusCodes.Status200OK)]
2020-06-24 20:43:28 +00:00
public ActionResult<NotificationResultDto> GetNotifications()
{
2020-04-21 05:53:09 +00:00
return new NotificationResultDto();
}
/// <summary>
/// Gets a user's notification summary.
/// </summary>
/// <response code="200">Summary of user's notifications returned.</response>
/// <returns>An <cref see="OkResult"/> containing a summary of the users notifications.</returns>
[HttpGet("{userId}/Summary")]
[ProducesResponseType(StatusCodes.Status200OK)]
2020-06-24 20:43:28 +00:00
public ActionResult<NotificationsSummaryDto> GetNotificationsSummary()
{
2020-04-21 05:53:09 +00:00
return new NotificationsSummaryDto();
}
/// <summary>
/// Gets notification types.
/// </summary>
/// <response code="200">All notification types returned.</response>
/// <returns>An <cref see="OkResult"/> containing a list of all notification types.</returns>
[HttpGet("Types")]
[ProducesResponseType(StatusCodes.Status200OK)]
public IEnumerable<NotificationTypeInfo> GetNotificationTypes()
{
2020-04-21 05:53:09 +00:00
return _notificationManager.GetNotificationTypes();
}
/// <summary>
/// Gets notification services.
/// </summary>
2020-05-03 20:02:15 +00:00
/// <response code="200">All notification services returned.</response>
/// <returns>An <cref see="OkResult"/> containing a list of all notification services.</returns>
[HttpGet("Services")]
[ProducesResponseType(StatusCodes.Status200OK)]
public IEnumerable<NameIdPair> GetNotificationServices()
{
return _notificationManager.GetNotificationServices();
}
/// <summary>
/// Sends a notification to all admins.
/// </summary>
2021-03-12 05:45:58 +00:00
/// <param name="notificationDto">The notification request.</param>
/// <response code="204">Notification sent.</response>
/// <returns>A <cref see="NoContentResult"/>.</returns>
[HttpPost("Admin")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
2021-03-12 05:45:58 +00:00
public ActionResult CreateAdminNotification([FromBody, Required] AdminNotificationDto notificationDto)
{
var notification = new NotificationRequest
{
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(),
Date = DateTime.UtcNow,
};
_notificationManager.SendNotification(notification, CancellationToken.None);
return NoContent();
}
/// <summary>
/// Sets notifications as read.
/// </summary>
/// <response code="204">Notifications set as read.</response>
/// <returns>A <cref see="NoContentResult"/>.</returns>
[HttpPost("{userId}/Read")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-06-24 20:43:28 +00:00
public ActionResult SetRead()
{
return NoContent();
}
/// <summary>
/// Sets notifications as unread.
/// </summary>
/// <response code="204">Notifications set as unread.</response>
/// <returns>A <cref see="NoContentResult"/>.</returns>
[HttpPost("{userId}/Unread")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
2020-06-24 20:43:28 +00:00
public ActionResult SetUnread()
{
return NoContent();
}
}
}