Skip to content

Commit

Permalink
[OneBot] Implemented Segment and get_forward_message
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Nov 8, 2023
1 parent 2cd6194 commit 3da6963
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 17 deletions.
9 changes: 9 additions & 0 deletions Lagrange.OneBot/Core/Entity/Action/OneBotGetForwardMsg.cs
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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<OneBotSegment> message)
{
[JsonPropertyName("message")] public List<OneBotSegment> Message { get; set; } = message;
}
30 changes: 30 additions & 0 deletions Lagrange.OneBot/Core/Entity/Message/OneBotNode.cs
Original file line number Diff line number Diff line change
@@ -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<OneBotSegment> content) : ISegment
{
[JsonPropertyName("user_id")] public uint UserId { get; set; } = userId;

[JsonPropertyName("nickname")] public string NickName { get; set; } = nickName;

[JsonPropertyName("content")] public List<OneBotSegment> 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();
}
}
27 changes: 27 additions & 0 deletions Lagrange.OneBot/Core/Message/Entity/ForwardSegment.cs
Original file line number Diff line number Diff line change
@@ -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 ?? "");
}
}
29 changes: 16 additions & 13 deletions Lagrange.OneBot/Core/Message/MessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,20 @@ public sealed class MessageService
{
private readonly LagrangeWebSvcCollection _service;
private readonly ContextBase _context;
private readonly Dictionary<Type, (string, ISegment)> _entityToSegment;
private static readonly Dictionary<Type, (string, ISegment)> EntityToSegment;

static MessageService()
{
EntityToSegment = new Dictionary<Type, (string, ISegment)>();
foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
var attribute = type.GetCustomAttribute<SegmentSubscriberAttribute>();
if (attribute != null)
{
EntityToSegment[attribute.Entity] = (attribute.Type, (ISegment)type.CreateInstance(false));
}
}
}

public MessageService(BotContext bot, LagrangeWebSvcCollection service, ContextBase context)
{
Expand All @@ -29,16 +42,6 @@ public MessageService(BotContext bot, LagrangeWebSvcCollection service, ContextB
invoker.OnFriendMessageReceived += OnFriendMessageReceived;
invoker.OnGroupMessageReceived += OnGroupMessageReceived;
invoker.OnTempMessageReceived += OnTempMessageReceived;

_entityToSegment = new Dictionary<Type, (string, ISegment)>();
foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
var attribute = type.GetCustomAttribute<SegmentSubscriberAttribute>();
if (attribute != null)
{
_entityToSegment[attribute.Entity] = (attribute.Type, (ISegment)type.CreateInstance(false));
}
}
}

private void OnFriendMessageReceived(BotContext bot, FriendMessageEvent e)
Expand Down Expand Up @@ -70,13 +73,13 @@ private void OnTempMessageReceived(BotContext bot, TempMessageEvent e)
// TODO: Implement temp msg
}

private List<OneBotSegment> Convert(IEnumerable<IMessageEntity> entities)
public static List<OneBotSegment> Convert(IEnumerable<IMessageEntity> entities)
{
var result = new List<OneBotSegment>();

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)));
}
Expand Down
35 changes: 35 additions & 0 deletions Lagrange.OneBot/Core/Operation/Message/GetForwardMsgOperation.cs
Original file line number Diff line number Diff line change
@@ -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<OneBotResult> HandleOperation(BotContext context, JsonObject? payload)
{
if (payload.Deserialize<OneBotGetForwardMsg>() is { } forwardMsg)
{
var nodes = new List<OneBotSegment>();

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();
}
}
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Please use Lagrange.Core responsibly and in accordance with the law.
| [Location] | 🔴 |
| [Music] | 🔴 |
| [Reply] | 🔴 |
| [Forward] | 🔴 |
| [Forward] | 🟢 |
| [Node] | 🔴 |
| [Xml] | 🔴 |
| [Json] | 🔴 |
Expand Down Expand Up @@ -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] | 🟢 |
Expand Down Expand Up @@ -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] | 🔴 |
Expand Down

0 comments on commit 3da6963

Please sign in to comment.