Skip to content

Commit

Permalink
[OneBot] Instantiate MessageCommon.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Feb 11, 2024
1 parent 78e6b6f commit 6941e3a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 32 deletions.
45 changes: 26 additions & 19 deletions Lagrange.OneBot/Core/Operation/Message/MessageCommon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,32 @@
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Entity.Message;
using Lagrange.OneBot.Core.Message;
using LiteDB;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace Lagrange.OneBot.Core.Operation.Message;

public static partial class MessageCommon
public partial class MessageCommon
{
private static readonly Dictionary<string, SegmentBase> TypeToSegment;
private readonly Dictionary<string, SegmentBase> _typeToSegment;

static MessageCommon()
public MessageCommon(LiteDatabase database)
{
TypeToSegment = new Dictionary<string, SegmentBase>();
_typeToSegment = new Dictionary<string, SegmentBase>();

foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
var attribute = type.GetCustomAttribute<SegmentSubscriberAttribute>();
if (attribute != null) TypeToSegment[attribute.SendType] = (SegmentBase)type.CreateInstance(false);
if (attribute != null)
{
var instance = (SegmentBase)type.CreateInstance(false);
instance.Database = database;
_typeToSegment[attribute.SendType] = instance;
}
}
}

public static MessageBuilder ParseChain(OneBotMessage message)
public MessageBuilder ParseChain(OneBotMessage message)
{
var builder = message.MessageType == "private"
? MessageBuilder.Friend(message.UserId ?? 0)
Expand All @@ -34,7 +41,7 @@ public static MessageBuilder ParseChain(OneBotMessage message)
return builder;
}

public static MessageBuilder ParseChain(OneBotMessageSimple message)
public MessageBuilder ParseChain(OneBotMessageSimple message)
{
var builder = message.MessageType == "private"
? MessageBuilder.Friend(message.UserId ?? 0)
Expand All @@ -54,23 +61,23 @@ public static MessageBuilder ParseChain(OneBotMessageText message)
return builder;
}

public static MessageBuilder ParseChain(OneBotPrivateMessage message)
public MessageBuilder ParseChain(OneBotPrivateMessage message)
{
var builder = MessageBuilder.Friend(message.UserId);
BuildMessages(builder, message.Messages);

return builder;
}

public static MessageBuilder ParseChain(OneBotPrivateMessageSimple message)
public MessageBuilder ParseChain(OneBotPrivateMessageSimple message)
{
var builder = MessageBuilder.Friend(message.UserId);
BuildMessages(builder, message.Messages);

return builder;
}

public static MessageBuilder ParseChain(OneBotPrivateMessageText message)
public MessageBuilder ParseChain(OneBotPrivateMessageText message)
{
var builder = MessageBuilder.Friend(message.UserId);
if (message.AutoEscape == true)
Expand All @@ -85,23 +92,23 @@ public static MessageBuilder ParseChain(OneBotPrivateMessageText message)
}


public static MessageBuilder ParseChain(OneBotGroupMessage message)
public MessageBuilder ParseChain(OneBotGroupMessage message)
{
var builder = MessageBuilder.Group(message.GroupId);
BuildMessages(builder, message.Messages);

return builder;
}

public static MessageBuilder ParseChain(OneBotGroupMessageSimple message)
public MessageBuilder ParseChain(OneBotGroupMessageSimple message)
{
var builder = MessageBuilder.Group(message.GroupId);
BuildMessages(builder, message.Messages);

return builder;
}

public static MessageBuilder ParseChain(OneBotGroupMessageText message)
public MessageBuilder ParseChain(OneBotGroupMessageText message)
{
var builder = MessageBuilder.Group(message.GroupId);
if (message.AutoEscape == true)
Expand All @@ -127,7 +134,7 @@ public static MessageBuilder ParseChain(OneBotGroupMessageText message)
.Replace("&#93;", "]")
.Replace("&amp;", "&");

private static void BuildMessages(MessageBuilder builder, string message)
private void BuildMessages(MessageBuilder builder, string message)
{
var matches = CQCodeRegex().Matches(message);
int textStart = 0;
Expand All @@ -137,7 +144,7 @@ private static void BuildMessages(MessageBuilder builder, string message)
textStart = match.Index + match.Length;

string type = match.Groups[1].Value;
if (TypeToSegment.TryGetValue(type, out var instance))
if (_typeToSegment.TryGetValue(type, out var instance))
{
var data = new Dictionary<string, string>();
foreach (var capture in match.Groups[2].Captures.Cast<Capture>())
Expand All @@ -153,21 +160,21 @@ private static void BuildMessages(MessageBuilder builder, string message)
if (textStart < message.Length) builder.Text(UnescapeText(message[textStart..]));
}

private static void BuildMessages(MessageBuilder builder, List<OneBotSegment> segments)
private void BuildMessages(MessageBuilder builder, List<OneBotSegment> segments)
{
foreach (var segment in segments)
{
if (TypeToSegment.TryGetValue(segment.Type, out var instance))
if (_typeToSegment.TryGetValue(segment.Type, out var instance))
{
var cast = (SegmentBase)((JsonElement)segment.Data).Deserialize(instance.GetType())!;
instance.Build(builder, cast);
}
}
}

private static void BuildMessages(MessageBuilder builder, OneBotSegment segment)
private void BuildMessages(MessageBuilder builder, OneBotSegment segment)
{
if (TypeToSegment.TryGetValue(segment.Type, out var instance))
if (_typeToSegment.TryGetValue(segment.Type, out var instance))
{
var cast = (SegmentBase)((JsonElement)segment.Data).Deserialize(instance.GetType())!;
instance.Build(builder, cast);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
namespace Lagrange.OneBot.Core.Operation.Message;

[Operation("send_group_msg")]
public sealed class SendGroupMessageOperation : IOperation
public sealed class SendGroupMessageOperation(MessageCommon common) : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonObject? payload)
{
var result = payload.Deserialize<OneBotGroupMessageBase>() switch
{
OneBotGroupMessage message => await context.SendMessage(MessageCommon.ParseChain(message).Build()),
OneBotGroupMessageSimple messageSimple => await context.SendMessage(MessageCommon.ParseChain(messageSimple).Build()),
OneBotGroupMessageText messageText => await context.SendMessage(MessageCommon.ParseChain(messageText).Build()),
OneBotGroupMessage message => await context.SendMessage(common.ParseChain(message).Build()),
OneBotGroupMessageSimple messageSimple => await context.SendMessage(common.ParseChain(messageSimple).Build()),
OneBotGroupMessageText messageText => await context.SendMessage(common.ParseChain(messageText).Build()),
_ => throw new Exception()
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
namespace Lagrange.OneBot.Core.Operation.Message;

[Operation("send_msg")]
public sealed class SendMessageOperation : IOperation
public sealed class SendMessageOperation(MessageCommon common) : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonObject? payload)
{
var result = payload.Deserialize<OneBotMessageBase>() switch
{
OneBotMessage message => await context.SendMessage(MessageCommon.ParseChain(message).Build()),
OneBotMessageSimple messageSimple => await context.SendMessage(MessageCommon.ParseChain(messageSimple).Build()),
OneBotMessage message => await context.SendMessage(common.ParseChain(message).Build()),
OneBotMessageSimple messageSimple => await context.SendMessage(common.ParseChain(messageSimple).Build()),
OneBotMessageText messageText => await context.SendMessage(MessageCommon.ParseChain(messageText).Build()),
_ => throw new Exception()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
namespace Lagrange.OneBot.Core.Operation.Message;

[Operation("send_private_msg")]
public sealed class SendPrivateMessageOperation : IOperation
public sealed class SendPrivateMessageOperation(MessageCommon common) : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonObject? payload)
{

var result = payload.Deserialize<OneBotPrivateMessageBase>() switch
{
OneBotPrivateMessage message => await context.SendMessage(MessageCommon.ParseChain(message).Build()),
OneBotPrivateMessageSimple messageSimple => await context.SendMessage(MessageCommon.ParseChain(messageSimple).Build()),
OneBotPrivateMessageText messageText => await context.SendMessage(MessageCommon.ParseChain(messageText).Build()),
OneBotPrivateMessage message => await context.SendMessage(common.ParseChain(message).Build()),
OneBotPrivateMessageSimple messageSimple => await context.SendMessage(common.ParseChain(messageSimple).Build()),
OneBotPrivateMessageText messageText => await context.SendMessage(common.ParseChain(messageText).Build()),
_ => throw new Exception()
};

Expand Down
7 changes: 5 additions & 2 deletions Lagrange.OneBot/Core/Operation/OperationService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Reflection;
using Lagrange.Core;
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Message;
using Lagrange.OneBot.Core.Network;
using Lagrange.OneBot.Database;
using Lagrange.OneBot.Core.Operation.Message;
using LiteDB;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
Expand All @@ -17,7 +18,7 @@ public sealed class OperationService
private readonly Dictionary<string, Type> _operations;
private readonly ServiceProvider _service;

public OperationService(BotContext bot, ILogger<OperationService> logger, LiteDatabase context)
public OperationService(BotContext bot, ILogger<OperationService> logger, LiteDatabase context, MessageService message)
{
_bot = bot;
_logger = logger;
Expand All @@ -32,6 +33,8 @@ public OperationService(BotContext bot, ILogger<OperationService> logger, LiteDa
var service = new ServiceCollection();
service.AddSingleton(context);
service.AddSingleton(logger);
service.AddSingleton(message);
service.AddSingleton<MessageCommon>();

foreach (var (_, type) in _operations) service.AddScoped(type);
_service = service.BuildServiceProvider();
Expand Down

0 comments on commit 6941e3a

Please sign in to comment.