Files
server/src/Notifications/Controllers/NotificationsController.cs

39 lines
1.2 KiB
C#
Raw Normal View History

using System.IO;
using System.Text;
using System.Threading.Tasks;
2018-08-16 12:05:01 -04:00
using Bit.Core.Models;
using Bit.Core.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
2018-08-16 12:05:01 -04:00
2018-08-16 13:45:31 -04:00
namespace Bit.Notifications
2018-08-16 12:05:01 -04:00
{
[Authorize("Internal")]
[SelfHosted(SelfHostedOnly = true)]
2018-08-16 13:35:16 -04:00
public class NotificationsController : Controller
2018-08-16 12:05:01 -04:00
{
2018-08-16 13:45:31 -04:00
private readonly IHubContext<NotificationsHub> _hubContext;
2018-08-16 12:05:01 -04:00
2018-08-16 13:45:31 -04:00
public NotificationsController(IHubContext<NotificationsHub> hubContext)
2018-08-16 12:05:01 -04:00
{
2018-08-16 13:45:31 -04:00
_hubContext = hubContext;
2018-08-16 12:05:01 -04:00
}
2018-08-16 13:35:16 -04:00
[HttpPost("~/notifications")]
public async Task PostNotification()
2018-08-16 12:05:01 -04:00
{
using(var reader = new StreamReader(Request.Body, Encoding.UTF8))
{
var notificationJson = await reader.ReadToEndAsync();
if(!string.IsNullOrWhiteSpace(notificationJson))
{
var notification = JsonConvert.DeserializeObject<PushNotificationData<object>>(notificationJson);
await HubHelpers.SendNotificationToHubAsync(notification.Type, notificationJson, _hubContext);
}
}
2018-08-16 12:05:01 -04:00
}
}
}