aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Api/Controllers/NotificationsController.cs
blob: 76b025fa16d43652f29a9feb0cc36eac05c30681 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#nullable enable
#pragma warning disable CA1801

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.Http;
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 user's ID.</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}")]
        [ProducesResponseType(typeof(IEnumerable<NotificationResultDto>), StatusCodes.Status200OK)]
        public IActionResult GetNotifications(
            [FromRoute] string userId,
            [FromQuery] bool? isRead,
            [FromQuery] int? startIndex,
            [FromQuery] int? limit)
        {
            return Ok(new NotificationResultDto());
        }

        /// <summary>
        /// Endpoint for getting a user's notification summary.
        /// </summary>
        /// <param name="userId">The user's ID.</param>
        /// <returns>Notifications summary for the user.</returns>
        [HttpGet("{UserID}/Summary")]
        [ProducesResponseType(typeof(NotificationsSummaryDto), StatusCodes.Status200OK)]
        public IActionResult GetNotificationsSummary(
            [FromRoute] string userId)
        {
            return Ok(new NotificationsSummaryDto());
        }

        /// <summary>
        /// Endpoint for getting notification types.
        /// </summary>
        /// <returns>All notification types.</returns>
        [HttpGet("Types")]
        [ProducesResponseType(typeof(IEnumerable<NameIdPair>), StatusCodes.Status200OK)]
        [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
        public IActionResult GetNotificationTypes()
        {
            try
            {
                return Ok(_notificationManager.GetNotificationTypes());
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }
        }

        /// <summary>
        /// Endpoint for getting notification services.
        /// </summary>
        /// <returns>All notification services.</returns>
        [HttpGet("Services")]
        [ProducesResponseType(typeof(IEnumerable<NameIdPair>), StatusCodes.Status200OK)]
        [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
        public IActionResult GetNotificationServices()
        {
            try
            {
                return Ok(_notificationManager.GetNotificationServices());
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }
        }

        /// <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>
        /// <returns>Status.</returns>
        [HttpPost("Admin")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
        public IActionResult CreateAdminNotification(
            [FromQuery] string name,
            [FromQuery] string description,
            [FromQuery] string? url,
            [FromQuery] NotificationLevel? level)
        {
            try
            {
                var notification = new NotificationRequest
                {
                    Name = name,
                    Description = description,
                    Url = url,
                    Level = level ?? NotificationLevel.Normal,
                    UserIds = _userManager.Users.Where(i => i.Policy.IsAdministrator).Select(i => i.Id).ToArray(),
                    Date = DateTime.UtcNow,
                };

                _notificationManager.SendNotification(notification, CancellationToken.None);

                return Ok();
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }
        }

        /// <summary>
        /// Endpoint to set notifications as read.
        /// </summary>
        /// <param name="userId">The userID.</param>
        /// <param name="ids">A comma-separated list of the IDs of notifications which should be set as read.</param>
        /// <returns>Status.</returns>
        [HttpPost("{UserID}/Read")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public IActionResult SetRead(
            [FromRoute] string userId,
            [FromQuery] string ids)
        {
            return Ok();
        }

        /// <summary>
        /// Endpoint to set notifications as unread.
        /// </summary>
        /// <param name="userId">The userID.</param>
        /// <param name="ids">A comma-separated list of the IDs of notifications which should be set as unread.</param>
        /// <returns>Status.</returns>
        [HttpPost("{UserID}/Unread")]
        [ProducesResponseType(StatusCodes.Status200OK)]
        public IActionResult SetUnread(
            [FromRoute] string userId,
            [FromQuery] string ids)
        {
            return Ok();
        }
    }
}