From 3da6963523c045dba2295cd7d949829e462b83e1 Mon Sep 17 00:00:00 2001 From: Linwenxuan <116782992+Linwenxuan05@users.noreply.github.com> Date: Wed, 8 Nov 2023 14:09:32 +0800 Subject: [PATCH] [OneBot] Implemented Segment and get_forward_message --- .../Core/Entity/Action/OneBotGetForwardMsg.cs | 9 +++++ .../Response/OneBotGetForwardMsgResponse.cs | 10 ++++++ .../Core/Entity/Message/OneBotNode.cs | 30 ++++++++++++++++ .../Core/Message/Entity/ForwardSegment.cs | 27 ++++++++++++++ .../Core/Message/MessageService.cs | 29 ++++++++------- .../Message/GetForwardMsgOperation.cs | 35 +++++++++++++++++++ README.md | 8 ++--- 7 files changed, 131 insertions(+), 17 deletions(-) create mode 100644 Lagrange.OneBot/Core/Entity/Action/OneBotGetForwardMsg.cs create mode 100644 Lagrange.OneBot/Core/Entity/Action/Response/OneBotGetForwardMsgResponse.cs create mode 100644 Lagrange.OneBot/Core/Entity/Message/OneBotNode.cs create mode 100644 Lagrange.OneBot/Core/Message/Entity/ForwardSegment.cs create mode 100644 Lagrange.OneBot/Core/Operation/Message/GetForwardMsgOperation.cs diff --git a/Lagrange.OneBot/Core/Entity/Action/OneBotGetForwardMsg.cs b/Lagrange.OneBot/Core/Entity/Action/OneBotGetForwardMsg.cs new file mode 100644 index 000000000..4c04b4959 --- /dev/null +++ b/Lagrange.OneBot/Core/Entity/Action/OneBotGetForwardMsg.cs @@ -0,0 +1,9 @@ +using System.Text.Json.Serialization; + +namespace Lagrange.OneBot.Core.Entity.Action; + +[Serializable] +public class OneBotGetForwardMsg +{ + [JsonPropertyName("id")] public string Id { get; set; } = ""; // resId +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Action/Response/OneBotGetForwardMsgResponse.cs b/Lagrange.OneBot/Core/Entity/Action/Response/OneBotGetForwardMsgResponse.cs new file mode 100644 index 000000000..431d37372 --- /dev/null +++ b/Lagrange.OneBot/Core/Entity/Action/Response/OneBotGetForwardMsgResponse.cs @@ -0,0 +1,10 @@ +using System.Text.Json.Serialization; +using Lagrange.OneBot.Core.Entity.Message; + +namespace Lagrange.OneBot.Core.Entity.Action.Response; + +[Serializable] +public class OneBotGetForwardMsgResponse(List message) +{ + [JsonPropertyName("message")] public List Message { get; set; } = message; +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Message/OneBotNode.cs b/Lagrange.OneBot/Core/Entity/Message/OneBotNode.cs new file mode 100644 index 000000000..353aace72 --- /dev/null +++ b/Lagrange.OneBot/Core/Entity/Message/OneBotNode.cs @@ -0,0 +1,30 @@ +using System.Text.Json.Serialization; +using Lagrange.Core.Message; +using Lagrange.OneBot.Core.Message; + +namespace Lagrange.OneBot.Core.Entity.Message; + +[Serializable] +public class OneBotNode(uint userId, string nickName, List content) : ISegment +{ + [JsonPropertyName("user_id")] public uint UserId { get; set; } = userId; + + [JsonPropertyName("nickname")] public string NickName { get; set; } = nickName; + + [JsonPropertyName("content")] public List Content { get; set; } = content; + + public IMessageEntity ToEntity() + { + throw new NotImplementedException(); + } + + public void Build(MessageBuilder builder, ISegment segment) + { + throw new NotImplementedException(); + } + + public ISegment FromEntity(IMessageEntity entity) + { + throw new NotImplementedException(); + } +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Message/Entity/ForwardSegment.cs b/Lagrange.OneBot/Core/Message/Entity/ForwardSegment.cs new file mode 100644 index 000000000..f62b536e9 --- /dev/null +++ b/Lagrange.OneBot/Core/Message/Entity/ForwardSegment.cs @@ -0,0 +1,27 @@ +using System.Text.Json.Serialization; +using Lagrange.Core.Message; +using Lagrange.Core.Message.Entity; + +namespace Lagrange.OneBot.Core.Message.Entity; + +public partial class ForwardSegment(string name) +{ + public ForwardSegment() : this("") { } + + [JsonPropertyName("id")] public string Name { get; set; } = name; +} + +[SegmentSubscriber(typeof(ForwardSegment), "forward")] +public partial class ForwardSegment : ISegment +{ + public IMessageEntity ToEntity() => throw new InvalidOperationException("Only Receive but not send"); + + public void Build(MessageBuilder builder, ISegment segment) => throw new InvalidOperationException(); + + public ISegment FromEntity(IMessageEntity entity) + { + if (entity is not MultiMsgEntity multiMsg) throw new ArgumentException("Invalid entity type."); + + return new ForwardSegment(multiMsg.ResId ?? ""); + } +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Message/MessageService.cs b/Lagrange.OneBot/Core/Message/MessageService.cs index e708cef94..7fb4b9deb 100644 --- a/Lagrange.OneBot/Core/Message/MessageService.cs +++ b/Lagrange.OneBot/Core/Message/MessageService.cs @@ -17,7 +17,20 @@ public sealed class MessageService { private readonly LagrangeWebSvcCollection _service; private readonly ContextBase _context; - private readonly Dictionary _entityToSegment; + private static readonly Dictionary EntityToSegment; + + static MessageService() + { + EntityToSegment = new Dictionary(); + foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) + { + var attribute = type.GetCustomAttribute(); + if (attribute != null) + { + EntityToSegment[attribute.Entity] = (attribute.Type, (ISegment)type.CreateInstance(false)); + } + } + } public MessageService(BotContext bot, LagrangeWebSvcCollection service, ContextBase context) { @@ -29,16 +42,6 @@ public MessageService(BotContext bot, LagrangeWebSvcCollection service, ContextB invoker.OnFriendMessageReceived += OnFriendMessageReceived; invoker.OnGroupMessageReceived += OnGroupMessageReceived; invoker.OnTempMessageReceived += OnTempMessageReceived; - - _entityToSegment = new Dictionary(); - foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) - { - var attribute = type.GetCustomAttribute(); - if (attribute != null) - { - _entityToSegment[attribute.Entity] = (attribute.Type, (ISegment)type.CreateInstance(false)); - } - } } private void OnFriendMessageReceived(BotContext bot, FriendMessageEvent e) @@ -70,13 +73,13 @@ private void OnTempMessageReceived(BotContext bot, TempMessageEvent e) // TODO: Implement temp msg } - private List Convert(IEnumerable entities) + public static List Convert(IEnumerable entities) { var result = new List(); foreach (var entity in entities) { - if (_entityToSegment.TryGetValue(entity.GetType(), out var instance)) + if (EntityToSegment.TryGetValue(entity.GetType(), out var instance)) { result.Add(new OneBotSegment(instance.Item1, instance.Item2.FromEntity(entity))); } diff --git a/Lagrange.OneBot/Core/Operation/Message/GetForwardMsgOperation.cs b/Lagrange.OneBot/Core/Operation/Message/GetForwardMsgOperation.cs new file mode 100644 index 000000000..df0fd95ca --- /dev/null +++ b/Lagrange.OneBot/Core/Operation/Message/GetForwardMsgOperation.cs @@ -0,0 +1,35 @@ +using System.Text.Json; +using System.Text.Json.Nodes; +using Lagrange.Core; +using Lagrange.Core.Internal.Event.Protocol.Message; +using Lagrange.OneBot.Core.Entity.Action; +using Lagrange.OneBot.Core.Entity.Action.Response; +using Lagrange.OneBot.Core.Entity.Message; +using Lagrange.OneBot.Core.Message; + +namespace Lagrange.OneBot.Core.Operation.Message; + +[Operation("get_forward_msg")] +public class GetForwardMsgOperation : IOperation +{ + public async Task HandleOperation(BotContext context, JsonObject? payload) + { + if (payload.Deserialize() is { } forwardMsg) + { + var nodes = new List(); + + var @event = MultiMsgDownloadEvent.Create(context.ContextCollection.Keystore.Uid ?? "", forwardMsg.Id); + var results = await context.ContextCollection.Business.SendEvent(@event); + foreach (var chain in ((MultiMsgDownloadEvent)results[0]).Chains ?? throw new Exception()) + { + var parsed = MessageService.Convert(chain); + var node = new OneBotNode(chain.FriendUin, "", parsed); + nodes.Add(new OneBotSegment("node", node)); + } + + return new OneBotResult(new OneBotGetForwardMsgResponse(nodes), 0, "ok"); + } + + throw new Exception(); + } +} \ No newline at end of file diff --git a/README.md b/README.md index 8d7702aa6..8de25a389 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ Please use Lagrange.Core responsibly and in accordance with the law. | [Location] | 🔴 | | [Music] | 🔴 | | [Reply] | 🔴 | -| [Forward] | 🔴 | +| [Forward] | 🟢 | | [Node] | 🔴 | | [Xml] | 🔴 | | [Json] | 🔴 | @@ -110,7 +110,7 @@ Please use Lagrange.Core responsibly and in accordance with the law. | [/send_msg] | 🟢 | | [/delete_msg] | 🔴 | | [/get_msg] | 🔴 | -| [/get_forward_msg] | 🔴 | +| [/get_forward_msg] | 🟢 | | ~~[/send_like]~~ | 🔴 | | [/set_group_kick] | 🟢 | | [/set_group_ban] | 🟢 | @@ -194,8 +194,8 @@ Please use Lagrange.Core responsibly and in accordance with the law. | Message | [Group Message] | 🟢 | | Notice | [Group File Upload] | 🔴 | | Notice | [Group Admin Change] | 🔴 | -| Notice | [Group Member Decrease] | 🔴 | -| Notice | [Group Member Increase] | 🔴 | +| Notice | [Group Member Decrease] | 🟢 | +| Notice | [Group Member Increase] | 🟢 | | Notice | [Group Mute] | 🔴 | | Notice | [Friend Add] | 🔴 | | Notice | [Group Recall Message] | 🔴 |