This commit is contained in:
2020-07-27 21:52:00 +00:00
parent 095732da1b
commit 7b67bdb2e2
3 changed files with 50 additions and 2 deletions

View File

@@ -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
}
/// <summary>
/// Send direct message notification to selected users
/// </summary>
/// <returns>NoContent on success or error</returns>
[HttpPost("direct-message")]
public async Task<IActionResult> 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<long>();
}
[Required]
public string Message { get; set; }
[Required]
public List<long> Users { get; set; }
}
//------------

View File

@@ -59,7 +59,7 @@ namespace AyaNova.Api.Controllers
/// One of "OpsOnly" or "Open"
/// </summary>
/// <param name="state">{"serverState":"Open"}</param>
/// <returns>NoContent 204</returns>
/// <returns>New server state</returns>
[HttpPost]
public async Task<IActionResult> PostServerState([FromBody] ServerStateModel state)
{

View File

@@ -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;