implement endpoints in `MessagingController`

This commit is contained in:
Alan Moon 2025-03-02 18:52:22 -08:00
parent c53b6b9348
commit 157f8fc455
2 changed files with 63 additions and 16 deletions

View File

@ -1,39 +1,86 @@
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using sodoff.Attributes;
using sodoff.Model;
using sodoff.Schema; using sodoff.Schema;
using sodoff.Services;
using sodoff.Util;
namespace sodoff.Controllers.Common; namespace sodoff.Controllers.Common;
public class MessagingController : Controller { public class MessagingController : Controller {
private readonly MessagingService messagingService;
private readonly DBContext ctx;
public MessagingController(MessagingService messagingService, DBContext ctx)
{
this.messagingService = messagingService;
this.ctx = ctx;
}
[HttpPost] [HttpPost]
[Produces("application/xml")] [Produces("application/xml")]
[Route("MessagingWebService.asmx/GetUserMessageQueue")] [Route("MessagingWebService.asmx/GetUserMessageQueue")]
public ArrayOfMessageInfo? GetUserMessageQueue() { [VikingSession]
// TODO: this is a placeholder public ArrayOfMessageInfo? GetUserMessageQueue(Viking viking, [FromForm] bool showOldMessages, [FromForm] bool showDeletedMessages) {
return null; return messagingService.ConstructUserMessageInfoArray(viking, showOldMessages, showDeletedMessages);
} }
[HttpPost] [HttpPost]
[Produces("application/xml")] [Produces("application/xml")]
[Route("MessagingWebService.asmx/SendMessage")] [Route("MessagingWebService.asmx/SendMessage")]
public IActionResult SendMessage() { [VikingSession]
// TODO: this is a placeholder public IActionResult SendMessage(Viking viking, [FromForm] Guid toUser, [FromForm] int messageID, [FromForm] string data) {
return Ok(false); // find viking to send to
Viking? toViking = ctx.Vikings.FirstOrDefault(e => e.Uid == toUser);
if (toViking == null) return Ok(false);
// clients (at least older ones) don't send what typeID the message is, its encoded in data
ArrayOfKeyValuePairOfStringString arrayOfKeyValuePairOfStringString = XmlUtil.DeserializeXml<ArrayOfKeyValuePairOfStringString>(data);
MessageTypeID typeID = MessageTypeID.Unknown;
string typeText = "";
switch(arrayOfKeyValuePairOfStringString.KeyValuePairOfStringString[0]?.Value)
{
case "Drawing":
typeID = MessageTypeID.GreetingCard;
typeText = "Card";
break;
case "Photo":
typeID = MessageTypeID.Photo;
typeText = "PhotoBomb";
break;
}
var msg = messagingService.AddMessageToViking(viking, toViking, MessageType.Data, typeID, MessageLevel.WhiteList, data, "[[Line1]]=[[{{BuddyUserName}}]] has sent you a " + typeText + "!", "[[Line1]]=[[{{BuddyUserName}}]] has sent you a " + typeText + "!");
if (msg != null) return Ok(true);
else return Ok(false);
} }
[HttpPost] [HttpPost]
[Produces("application/xml")] [Produces("application/xml")]
[Route("MessagingWebService.asmx/SaveMessage")] [Route("MessagingWebService.asmx/SaveMessage")]
public IActionResult SaveMessage() { public IActionResult SaveMessage([FromForm] int userMessageQueueId, [FromForm] bool isNew, [FromForm] bool IsDeleted) {
// TODO: this is a placeholder var updatedMessage = messagingService.UpdateMessageByQueueID(userMessageQueueId, isNew, IsDeleted);
return Ok(false); if (updatedMessage != null) return Ok(true);
else return Ok(false);
} }
[HttpPost] [HttpPost]
[Produces("application/xml")] [Produces("application/xml")]
[Route("MessageWebService.asmx/GetCombinedListMessage")] [Route("MessageWebService.asmx/GetCombinedListMessage")]
public IActionResult GetCombinedListMessage() public ArrayOfCombinedListMessage? GetCombinedListMessage([FromForm] Guid userId)
{ {
// TODO - placeholder // find viking
return Ok(new ArrayOfMessageInfo()); Viking? viking = ctx.Vikings.FirstOrDefault(e => e.Uid == userId);
if (viking != null) return messagingService.ConstructCombinedMessageArray(viking);
else return new ArrayOfCombinedListMessage();
}
[HttpPost]
[Produces("application/xml")]
[Route("MessageWebService.asmx/RemoveMessageFromBoard")]
public IActionResult RemoveMessageFromBoard([FromForm] int messageID)
{
messagingService.RemoveMessage(messageID);
return Ok(true);
} }
} }

View File

@ -76,13 +76,13 @@ public class MessagingService
} else throw new InvalidOperationException("Tried To Delete A Message That Doesn't Exist"); } else throw new InvalidOperationException("Tried To Delete A Message That Doesn't Exist");
} }
public Model.Message? UpdateMessage(int id, bool isNew, bool IsDeleted) public Model.Message? UpdateMessageByQueueID(int queueId, bool isNew, bool IsDeleted)
{ {
// get execution UTC timestamp // get execution UTC timestamp
DateTime now = DateTime.UtcNow; DateTime now = DateTime.UtcNow;
// find message // find message
Model.Message? message = ctx.Messages.FirstOrDefault(e => e.Id == id); Model.Message? message = ctx.Messages.FirstOrDefault(e => e.QueueID == queueId);
if (message != null) if (message != null)
{ {
@ -207,14 +207,14 @@ public class MessagingService
.OrderBy(e => e.QueueID) .OrderBy(e => e.QueueID)
.ToList(); .ToList();
List<MessageInfo> messageInfos = new List<MessageInfo>(0); List<MessageInfo> messageInfos = new List<MessageInfo>();
ArrayOfMessageInfo response = new ArrayOfMessageInfo(); ArrayOfMessageInfo response = new ArrayOfMessageInfo();
foreach(var message in messages) foreach(var message in messages)
{ {
Viking? msgAuthor = ctx.Vikings.FirstOrDefault(e => e.Id == message.VikingId) ?? new Viking(); Viking? msgAuthor = ctx.Vikings.FirstOrDefault(e => e.Id == message.VikingId) ?? new Viking();
if(!showOldMessages && message.IsNew && DateTime.Compare(message.CreatedAt, now.AddMinutes(30)) > 0 || message.IsDeleted) continue; // sometimes clients won't set IsNew flag when updating messages, so do not add messages more than 30 minutes old to response if(!showOldMessages && message.IsNew && DateTime.Compare(message.CreatedAt.AddMinutes(30), now) > 0 || message.IsDeleted) continue; // sometimes clients won't set IsNew flag when updating messages, so do not add messages more than 30 minutes old to response
if(!message.IsNew && !showOldMessages) continue; if(!message.IsNew && !showOldMessages) continue;
messageInfos.Add(new MessageInfo messageInfos.Add(new MessageInfo
{ {