Messaging System #2

Merged
Moonbase merged 5 commits from messaging into master 2025-03-07 18:19:53 -08:00
2 changed files with 63 additions and 16 deletions
Showing only changes of commit 157f8fc455 - Show all commits

View File

@ -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<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]
[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);
}
}

View File

@ -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<MessageInfo> messageInfos = new List<MessageInfo>(0);
List<MessageInfo> messageInfos = new List<MessageInfo>();
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
{