From 157f8fc45549cbf162b67269685dac031a2250ba Mon Sep 17 00:00:00 2001 From: AlanMoonbase Date: Sun, 2 Mar 2025 18:52:22 -0800 Subject: [PATCH] implement endpoints in ``MessagingController`` --- src/Controllers/Common/MessagingController.cs | 71 +++++++++++++++---- src/Services/MessagingService.cs | 8 +-- 2 files changed, 63 insertions(+), 16 deletions(-) diff --git a/src/Controllers/Common/MessagingController.cs b/src/Controllers/Common/MessagingController.cs index b4e71de..1385c70 100644 --- a/src/Controllers/Common/MessagingController.cs +++ b/src/Controllers/Common/MessagingController.cs @@ -1,39 +1,86 @@ using Microsoft.AspNetCore.Mvc; +using sodoff.Attributes; +using sodoff.Model; using sodoff.Schema; +using sodoff.Services; +using sodoff.Util; namespace sodoff.Controllers.Common; 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] [Produces("application/xml")] [Route("MessagingWebService.asmx/GetUserMessageQueue")] - public ArrayOfMessageInfo? GetUserMessageQueue() { - // TODO: this is a placeholder - return null; + [VikingSession] + public ArrayOfMessageInfo? GetUserMessageQueue(Viking viking, [FromForm] bool showOldMessages, [FromForm] bool showDeletedMessages) { + return messagingService.ConstructUserMessageInfoArray(viking, showOldMessages, showDeletedMessages); } [HttpPost] [Produces("application/xml")] [Route("MessagingWebService.asmx/SendMessage")] - public IActionResult SendMessage() { - // TODO: this is a placeholder - return Ok(false); + [VikingSession] + public IActionResult SendMessage(Viking viking, [FromForm] Guid toUser, [FromForm] int messageID, [FromForm] string data) { + // 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(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] [Produces("application/xml")] [Route("MessagingWebService.asmx/SaveMessage")] - public IActionResult SaveMessage() { - // TODO: this is a placeholder - return Ok(false); + public IActionResult SaveMessage([FromForm] int userMessageQueueId, [FromForm] bool isNew, [FromForm] bool IsDeleted) { + var updatedMessage = messagingService.UpdateMessageByQueueID(userMessageQueueId, isNew, IsDeleted); + if (updatedMessage != null) return Ok(true); + else return Ok(false); } [HttpPost] [Produces("application/xml")] [Route("MessageWebService.asmx/GetCombinedListMessage")] - public IActionResult GetCombinedListMessage() + public ArrayOfCombinedListMessage? GetCombinedListMessage([FromForm] Guid userId) { - // TODO - placeholder - return Ok(new ArrayOfMessageInfo()); + // find viking + 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); } } diff --git a/src/Services/MessagingService.cs b/src/Services/MessagingService.cs index 5c9c3ce..aa21370 100644 --- a/src/Services/MessagingService.cs +++ b/src/Services/MessagingService.cs @@ -76,13 +76,13 @@ public class MessagingService } 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 DateTime now = DateTime.UtcNow; // 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) { @@ -207,14 +207,14 @@ public class MessagingService .OrderBy(e => e.QueueID) .ToList(); - List messageInfos = new List(0); + List messageInfos = new List(); ArrayOfMessageInfo response = new ArrayOfMessageInfo(); foreach(var message in messages) { 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; messageInfos.Add(new MessageInfo {