|
| 1 | +using System; |
| 2 | +using System.ComponentModel.DataAnnotations; |
| 3 | +using System.Net.Mime; |
| 4 | +using System.Threading; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using MediaBrowser.Controller.Library; |
| 7 | +using MediaBrowser.Controller.Notifications; |
| 8 | +using Microsoft.AspNetCore.Authorization; |
| 9 | +using Microsoft.AspNetCore.Http; |
| 10 | +using Microsoft.AspNetCore.Mvc; |
| 11 | +using Microsoft.Extensions.Logging; |
| 12 | + |
| 13 | +namespace MediaBrowser.Plugins.SmtpNotifications.Api |
| 14 | +{ |
| 15 | + [ApiController] |
| 16 | + [Authorize(Policy = "RequiresElevation")] |
| 17 | + [Produces(MediaTypeNames.Application.Json)] |
| 18 | + [Route("Notification/SMTP")] |
| 19 | + public class EmailNotificationController : ControllerBase |
| 20 | + { |
| 21 | + private readonly IUserManager _userManager; |
| 22 | + private readonly ILogger<Notifier> _notifierLogger; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Creates a new instance of the <see cref="EmailNotificationController"/>. |
| 26 | + /// </summary> |
| 27 | + /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param> |
| 28 | + /// <param name="notifierLogger">Instance of the <see cref="ILogger{Notifier}"/> interface.</param> |
| 29 | + public EmailNotificationController( |
| 30 | + IUserManager userManager, |
| 31 | + ILogger<Notifier> notifierLogger) |
| 32 | + { |
| 33 | + _userManager = userManager; |
| 34 | + _notifierLogger = notifierLogger; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Tests the configured notification. |
| 39 | + /// </summary> |
| 40 | + /// <param name="userId">User to test notification for.</param> |
| 41 | + /// <response code="204">Notification tested successfully.</response> |
| 42 | + /// <returns>A <see cref="NoContentResult"/></returns> |
| 43 | + [HttpPost("Test/{userId}")] |
| 44 | + [ProducesResponseType(StatusCodes.Status204NoContent)] |
| 45 | + public async Task<ActionResult> TestNotification([FromRoute, Required] Guid userId) |
| 46 | + { |
| 47 | + await new Notifier(_notifierLogger).SendNotification( |
| 48 | + new UserNotification |
| 49 | + { |
| 50 | + Date = DateTime.UtcNow, |
| 51 | + Description = "This is a test notification from Jellyfin Server", |
| 52 | + Level = Model.Notifications.NotificationLevel.Normal, |
| 53 | + Name = "Test Notification", |
| 54 | + User = _userManager.GetUserById(userId) |
| 55 | + }, CancellationToken.None).ConfigureAwait(false); |
| 56 | + |
| 57 | + return NoContent(); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
0 commit comments