From 5bd62186f0d7576495d78516df45f1828048d306 Mon Sep 17 00:00:00 2001 From: AlanMoonbase Date: Mon, 3 Mar 2025 17:00:58 -0800 Subject: [PATCH] implement ``RemoveBuddy`` modify ``SendMessage`` add message of typeid 21 when reply is made reworks some things in ``BuddyService`` --- src/Controllers/Common/ContentController.cs | 16 ++++++- src/Controllers/Common/MessagingController.cs | 38 +++++++++++++++- src/Services/BuddyService.cs | 43 ++++++++----------- src/Services/MessagingService.cs | 13 +++++- 4 files changed, 80 insertions(+), 30 deletions(-) diff --git a/src/Controllers/Common/ContentController.cs b/src/Controllers/Common/ContentController.cs index 87cbc23..4a1293f 100644 --- a/src/Controllers/Common/ContentController.cs +++ b/src/Controllers/Common/ContentController.cs @@ -1159,6 +1159,20 @@ public class ContentController : Controller { else return Ok(new BuddyActionResult{ Result = BuddyActionResultType.InvalidFriendCode }); } + [HttpPost] + [Produces("application/xml")] + [Route("ContentWebService.asmx/RemoveBuddy")] + [VikingSession] + public IActionResult RemoveBuddy(Viking viking, [FromForm] Guid buddyUserId) + { + // find buddy viking + Viking? buddyViking = ctx.Vikings.FirstOrDefault(e => e.Uid == buddyUserId); + + if (buddyViking != null) + return Ok(buddyService.RemoveBuddy(viking, buddyViking)); + else return Ok(false); + } + [HttpPost] [Produces("application/xml")] [Route("ContentWebService.asmx/ApproveBuddy")] @@ -1170,7 +1184,7 @@ public class ContentController : Controller { if (buddyViking != null) return Ok(buddyService.UpdateBuddyRelation(viking, buddyViking, BuddyStatus.Approved, BuddyStatus.Approved)); - else return Ok(new BuddyActionResult{ Result = BuddyActionResultType.InvalidFriendCode }); + else return Ok(false); } [HttpPost] diff --git a/src/Controllers/Common/MessagingController.cs b/src/Controllers/Common/MessagingController.cs index cbdd63b..0e13eb0 100644 --- a/src/Controllers/Common/MessagingController.cs +++ b/src/Controllers/Common/MessagingController.cs @@ -35,9 +35,22 @@ public class MessagingController : Controller { // 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)int.Parse(arrayOfKeyValuePairOfStringString.KeyValuePairOfStringString![0].Value ?? "0"); + MessageTypeID typeID = MessageTypeID.Unknown; + string typeText = ""; - var msg = messagingService.AddMessageToViking(viking, toViking, MessageType.Data, typeID, MessageLevel.WhiteList, data, "[[Line1]]=[[{{BuddyUserName}}]] has sent you a {{MessageType}}!", "[[Line1]]=[[{{BuddyUserName}}]] has sent you a {{MessageType}}!"); + switch(arrayOfKeyValuePairOfStringString.KeyValuePairOfStringString![0].Value) + { + case "Photo": + typeID = MessageTypeID.Photo; + typeText = "Photo"; + break; + case "Drawing": + typeID = MessageTypeID.GreetingCard; + typeText = "Card"; + 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); } @@ -71,4 +84,25 @@ public class MessagingController : Controller { messagingService.RemoveMessage(messageID); return Ok(true); } + + [HttpPost] + [Route("Messaging/PostTextMessage")] + public IActionResult PostTextMessage([FromForm] Guid token, [FromForm] Guid userId, [FromForm] string data, [FromForm] int messageLevel, [FromForm] int replyMessageId) + { + // check if session is valid + Session? session = ctx.Sessions.FirstOrDefault(e => e.ApiToken == token); + + // find viking + Viking? viking = ctx.Vikings.FirstOrDefault(e => e.Uid == userId); + + if (session != null && viking != null) + { + // post text message + var message = messagingService.AddMessageToViking(session.Viking, viking, MessageType.Post, MessageTypeID.Messaging, (MessageLevel)messageLevel, data, isReply: replyMessageId > 0, parentMessageId: replyMessageId); + + if (message != null) return Ok(true); + } + + return Ok(false); + } } diff --git a/src/Services/BuddyService.cs b/src/Services/BuddyService.cs index 861cfb7..b309f17 100644 --- a/src/Services/BuddyService.cs +++ b/src/Services/BuddyService.cs @@ -59,11 +59,11 @@ public class BuddyService }; } - public BuddyActionResult UpdateBuddyRelation(Viking viking, Viking buddyViking, BuddyStatus buddyStatus1, BuddyStatus buddyStatus2) + public bool UpdateBuddyRelation(Viking viking, Viking buddyViking, BuddyStatus buddyStatus1, BuddyStatus buddyStatus2) { // find relation - Model.Buddy? buddy = ctx.Buddies.Where(e => e.VikingId == viking.Id) - .FirstOrDefault(e => e.BuddyVikingId == buddyViking.Id); + Model.Buddy? buddy = ctx.Buddies.Where(e => e.VikingId == viking.Id || e.BuddyVikingId == viking.Id) + .FirstOrDefault(e => e.BuddyVikingId == buddyViking.Id || e.VikingId == buddyViking.Id); if (buddy != null) { @@ -73,39 +73,32 @@ public class BuddyService ctx.SaveChanges(); // return result - return new BuddyActionResult - { - Status = buddy.BuddyStatus1, - Result = BuddyActionResultType.Success, - BuddyUserID = buddyViking.Uid.ToString() - }; - } else return new BuddyActionResult { Result = BuddyActionResultType.Unknown }; + return true; + } else return false; } - public void RemoveBuddy(Viking viking, Guid buddyUid) + public bool RemoveBuddy(Viking viking, Viking buddyViking) { - // find buddy viking - Viking? buddyViking = ctx.Vikings.FirstOrDefault(e => e.Uid == buddyUid); - - if (buddyViking != null) - { - // find buddy relationship - Model.Buddy? buddy = ctx.Buddies.Where(e => e.VikingId == viking.Id) - .FirstOrDefault(e => e.BuddyVikingId == buddyViking.Id); + // find relation + Model.Buddy? buddy = ctx.Buddies.Where(e => e.VikingId == viking.Id || e.BuddyVikingId == viking.Id) + .FirstOrDefault(e => e.BuddyVikingId == buddyViking.Id || e.VikingId == buddyViking.Id); - if (buddy != null) - // remove it - ctx.Buddies.Remove(buddy); - else throw new InvalidOperationException("Cannot Remove A Buddy That Doesn't Exist"); - + if (buddy != null) + { + // remove it + ctx.Buddies.Remove(buddy); ctx.SaveChanges(); + + return true; } + else return false; } public BuddyList ConstructBuddyList(Viking viking) { // get all relationships viking has made - List buddies = ctx.Buddies.Where(e => e.BuddyVikingId == viking.Id).ToList(); + List buddies = ctx.Buddies.Where(e => e.VikingId == viking.Id || e.BuddyVikingId == viking.Id) + .ToList(); List schemaBuddies = new(); foreach (var buddy in buddies) { diff --git a/src/Services/MessagingService.cs b/src/Services/MessagingService.cs index 9c60a33..3090580 100644 --- a/src/Services/MessagingService.cs +++ b/src/Services/MessagingService.cs @@ -44,7 +44,7 @@ public class MessagingService IsNew = IsNew }; - if (isReply) + if (isReply && parentMessageId > 0) { // get message this is in reply to Model.Message? messageToReplyTo = ctx.Messages.FirstOrDefault(e => e.Id == parentMessageId); @@ -54,6 +54,9 @@ public class MessagingService message.ParentMessage = messageToReplyTo; message.ParentMessageId = messageToReplyTo.Id; message.ConversationID = messageToReplyTo.ConversationID; + + // add a message to the replier's board saying a thread update has occured (if reply isn't to self) + if (message.VikingId != message.ToVikingId) AddMessageToViking(message.Viking, message.ToViking, MessageType.Data, MessageTypeID.ThreadUpdate, MessageLevel.WhiteList, "[[Line1]]=[[{{BuddyUserName}} Replied To Your Message In {{BuddyUserName}}'s Message Board!]]", "[[Line1]]=[[{{BuddyUserName}} Replied To Your Message In {{BuddyUserName}}'s Message Board!]]", "[[Line1]]=[[{{BuddyUserName}} Replied To Your Message In {{BuddyUserName}}'s Message Board!]]"); } else throw new InvalidOperationException("Tried To Reply To A Message That Doesn't Exist"); } @@ -93,6 +96,8 @@ public class MessagingService message.IsDeleted = IsDeleted; message.LastUpdatedAt = now; + ctx.SaveChanges(); + return message; } else return null; } @@ -242,7 +247,8 @@ public class MessagingService { Viking? msgAuthor = ctx.Vikings.FirstOrDefault(e => e.Id == message.VikingId) ?? new Viking(); - 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.IsDeleted) { ctx.Messages.Remove(message); continue; } + if(DateTime.Compare(now, message.CreatedAt.AddMinutes(30)) > 0) { message.IsNew = false; 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 { @@ -256,6 +262,9 @@ public class MessagingService }); } + // save any database changes + ctx.SaveChanges(); + // add list as array to response response.MessageInfo = messageInfos.ToArray();