From 7b67bdb2e2b54bb41864ab99d6d887e85b56d234 Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Mon, 27 Jul 2020 21:52:00 +0000 Subject: [PATCH] --- .../AyaNova/Controllers/NotifyController.cs | 39 +++++++++++++++++++ .../Controllers/ServerStateController.cs | 2 +- server/AyaNova/biz/NotifyEventProcessor.cs | 11 +++++- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/server/AyaNova/Controllers/NotifyController.cs b/server/AyaNova/Controllers/NotifyController.cs index de20396a..ddbc12ae 100644 --- a/server/AyaNova/Controllers/NotifyController.cs +++ b/server/AyaNova/Controllers/NotifyController.cs @@ -8,6 +8,9 @@ using AyaNova.Api.ControllerHelpers; using AyaNova.Biz; using System.Linq; using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; namespace AyaNova.Api.Controllers { @@ -164,6 +167,42 @@ namespace AyaNova.Api.Controllers } + + /// + /// Send direct message notification to selected users + /// + /// NoContent on success or error + [HttpPost("direct-message")] + public async Task SendNotifyDirectMessage([FromBody] NotifyDirectMessage notifyDirectMessage) + { + if (serverState.IsClosed) + return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason)); + + if (!ModelState.IsValid) + return BadRequest(new ApiErrorResponse(ModelState)); + + var message = $"{UserNameFromContext.Name(HttpContext.Items)}: \"{notifyDirectMessage.Message}\""; + foreach (long l in notifyDirectMessage.Users) + { + if (l != 0) + await NotifyEventProcessor.AddGeneralNotifyEvent(NotifyEventType.GeneralNotification, message, null, l); + } + + return NoContent(); + } + + public class NotifyDirectMessage + { + public NotifyDirectMessage() + { + Users = new List(); + } + [Required] + public string Message { get; set; } + + [Required] + public List Users { get; set; } + } //------------ diff --git a/server/AyaNova/Controllers/ServerStateController.cs b/server/AyaNova/Controllers/ServerStateController.cs index ce9ba64c..b25be837 100644 --- a/server/AyaNova/Controllers/ServerStateController.cs +++ b/server/AyaNova/Controllers/ServerStateController.cs @@ -59,7 +59,7 @@ namespace AyaNova.Api.Controllers /// One of "OpsOnly" or "Open" /// /// {"serverState":"Open"} - /// NoContent 204 + /// New server state [HttpPost] public async Task PostServerState([FromBody] ServerStateModel state) { diff --git a/server/AyaNova/biz/NotifyEventProcessor.cs b/server/AyaNova/biz/NotifyEventProcessor.cs index 5acea4bd..dad321dd 100644 --- a/server/AyaNova/biz/NotifyEventProcessor.cs +++ b/server/AyaNova/biz/NotifyEventProcessor.cs @@ -79,7 +79,16 @@ namespace AyaNova.Biz var UserName = await ct.User.Where(z => z.Id == userId).Select(z => z.Name).FirstOrDefaultAsync(); - NotifyEvent n = new NotifyEvent() { EventType = eventType, UserId = userId, Message = message, NotifySubscriptionId = 0, Name = UserName }; + //if they don't have a regular inapp subscription create one now + var defaultsub = await ct.NotifySubscription.FirstOrDefaultAsync(z => z.EventType == NotifyEventType.GeneralNotification && z.UserId == userId && z.DeliveryMethod == NotifyDeliveryMethod.App); + if (defaultsub == null) + { + defaultsub = new NotifySubscription() { UserId = userId, EventType = NotifyEventType.GeneralNotification, DeliveryMethod = NotifyDeliveryMethod.App }; + await ct.NotifySubscription.AddAsync(defaultsub); + await ct.SaveChangesAsync(); + } + + NotifyEvent n = new NotifyEvent() { EventType = eventType, UserId = userId, Message = message, NotifySubscriptionId = defaultsub.Id, Name = UserName }; await ct.NotifyEvent.AddAsync(n); await ct.SaveChangesAsync(); return;