Skip to content

Commit

Permalink
[OneBot] add url key to Segment Properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Feb 2, 2024
1 parent e03ad8c commit ea709e9
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 33 deletions.
4 changes: 4 additions & 0 deletions Lagrange.OneBot/Core/Message/CQPropertyAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Lagrange.OneBot.Core.Message;

[AttributeUsage(AttributeTargets.Property)]
public class CQPropertyAttribute : Attribute;
2 changes: 1 addition & 1 deletion Lagrange.OneBot/Core/Message/Entity/AtSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class AtSegment(uint at)
{
public AtSegment() : this(0) { }

[JsonPropertyName("qq")] public string At { get; set; } = at.ToString();
[JsonPropertyName("qq")] [CQProperty] public string At { get; set; } = at.ToString();
}

[SegmentSubscriber(typeof(MentionEntity), "at")]
Expand Down
2 changes: 1 addition & 1 deletion Lagrange.OneBot/Core/Message/Entity/FaceSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class FaceSegment(int id)
{
public FaceSegment() : this(0) { }

[JsonPropertyName("id")] public string Id { get; set; } = id.ToString();
[JsonPropertyName("id")] [CQProperty] public string Id { get; set; } = id.ToString();
}

[SegmentSubscriber(typeof(FaceEntity), "face")]
Expand Down
2 changes: 1 addition & 1 deletion Lagrange.OneBot/Core/Message/Entity/ForwardSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class ForwardSegment(string name)
{
public ForwardSegment() : this("") { }

[JsonPropertyName("id")] public string Name { get; set; } = name;
[JsonPropertyName("id")] [CQProperty] public string Name { get; set; } = name;
}

[SegmentSubscriber(typeof(MultiMsgEntity), "forward", "node")]
Expand Down
8 changes: 5 additions & 3 deletions Lagrange.OneBot/Core/Message/Entity/ImageSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ public partial class ImageSegment(string url)
{
public ImageSegment() : this("") { }

[JsonPropertyName("file")] public string Url { get; set; } = url;
[JsonPropertyName("file")] [CQProperty] public string File { get; set; } = url;

[JsonPropertyName("url")] public string Url { get; set; } = url;
}

[SegmentSubscriber(typeof(ImageEntity), "image")]
public partial class ImageSegment : ISegment
{
public IMessageEntity ToEntity() => new ImageEntity(Url);
public IMessageEntity ToEntity() => new ImageEntity(File);

public void Build(MessageBuilder builder, ISegment segment)
{
if (segment is ImageSegment imageSegment and not { Url: "" } && CommonResolver.Resolve(imageSegment.Url) is { } image)
if (segment is ImageSegment imageSegment and not { File: "" } && CommonResolver.Resolve(imageSegment.File) is { } image)
{
builder.Image(image);
}
Expand Down
8 changes: 5 additions & 3 deletions Lagrange.OneBot/Core/Message/Entity/RecordSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ public partial class RecordSegment(string url)
{
public RecordSegment() : this("") { }

[JsonPropertyName("file")] public string Url { get; set; } = url;
[JsonPropertyName("file")] [CQProperty] public string File { get; set; } = url;

[JsonPropertyName("url")] public string Url { get; set; } = url;
}

[SegmentSubscriber(typeof(RecordEntity), "record")]
public partial class RecordSegment : ISegment
{
public IMessageEntity ToEntity() => new RecordEntity(Url);
public IMessageEntity ToEntity() => new RecordEntity(File);

public void Build(MessageBuilder builder, ISegment segment)
{
if (segment is RecordSegment recordSegment and not { Url: "" } && CommonResolver.Resolve(recordSegment.Url) is { } image)
if (segment is RecordSegment recordSegment and not { File: "" } && CommonResolver.Resolve(recordSegment.File) is { } image)
{
builder.Record(image);
}
Expand Down
2 changes: 1 addition & 1 deletion Lagrange.OneBot/Core/Message/Entity/TextSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public partial class TextSegment(string text)
{
public TextSegment() : this("") { }

[JsonPropertyName("text")] public string Text { get; set; } = text;
[JsonPropertyName("text")] [CQProperty] public string Text { get; set; } = text;
}

[SegmentSubscriber(typeof(TextEntity), "text")]
Expand Down
8 changes: 5 additions & 3 deletions Lagrange.OneBot/Core/Message/Entity/VideoSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ public partial class VideoSegment(string url)
{
public VideoSegment() : this("") { }

[JsonPropertyName("file")] public string Url { get; set; } = url;
[JsonPropertyName("file")] [CQProperty] public string File { get; set; } = url;

[JsonPropertyName("url")] public string Url { get; set; } = url;
}

[SegmentSubscriber(typeof(VideoEntity), "video")]
public partial class VideoSegment : ISegment
{
public IMessageEntity ToEntity() => new VideoEntity(Url);
public IMessageEntity ToEntity() => new VideoEntity(File);

public void Build(MessageBuilder builder, ISegment segment)
{
if (segment is VideoSegment videoSegment and not { Url: "" } && CommonResolver.Resolve(videoSegment.Url) is { } image)
if (segment is VideoSegment videoSegment and not { File: "" } && CommonResolver.Resolve(videoSegment.File) is { } image)
{
// TODO: Add Video
}
Expand Down
49 changes: 29 additions & 20 deletions Lagrange.OneBot/Core/Message/MessageService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using JsonSerializer = System.Text.Json.JsonSerializer;
using Lagrange.Core;
using Lagrange.Core.Common.Interface.Api;
using Lagrange.Core.Event.EventArg;
using Lagrange.Core.Message;
using Lagrange.Core.Utility.Extension;
Expand All @@ -12,7 +13,6 @@
using Lagrange.OneBot.Database;
using LiteDB;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace Lagrange.OneBot.Core.Message;

Expand All @@ -26,10 +26,12 @@ public sealed class MessageService
private readonly IConfiguration _config;

private static readonly Dictionary<Type, (string, ISegment)> EntityToSegment;
private static readonly IJsonTypeInfoResolver Resolver;

static MessageService()
{
EntityToSegment = new Dictionary<Type, (string, ISegment)>();
Resolver = new DefaultJsonTypeInfoResolver { Modifiers = { ModifyTypeInfo } };
foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
var attribute = type.GetCustomAttribute<SegmentSubscriberAttribute>();
Expand Down Expand Up @@ -109,41 +111,48 @@ public static List<OneBotSegment> Convert(IEnumerable<IMessageEntity> entities)
return result;
}

private static string EscapeText(string str)
{
return str.Replace("&", "&amp;")
.Replace("[", "&#91;")
.Replace("]", "&#93;");
}
private static string EscapeText(string str) => str
.Replace("&", "&amp;")
.Replace("[", "&#91;")
.Replace("]", "&#93;");

private static string EscapeCQ(string str)
{
return EscapeText(str).Replace(",", "&#44;");
}
private static string EscapeCQ(string str) => EscapeText(str).Replace(",", "&#44;");

public static string ToRawMessage(List<OneBotSegment> segments)
private static string ToRawMessage(List<OneBotSegment> segments)
{
var rawMessageBuilder = new StringBuilder();
foreach (var segment in segments)
{
if (segment.Type == "text")
if (segment.Data is TextSegment textSeg)
{
rawMessageBuilder.Append(EscapeText(((TextSegment)segment.Data).Text));
rawMessageBuilder.Append(EscapeText(textSeg.Text));
}
else
{
rawMessageBuilder.Append("[CQ:");
rawMessageBuilder.Append(segment.Type);
foreach (var property in JsonSerializer.SerializeToElement(segment.Data).EnumerateObject())
foreach (var property in JsonSerializer.SerializeToElement(segment.Data, new JsonSerializerOptions { TypeInfoResolver = Resolver }).EnumerateObject())
{
rawMessageBuilder.Append(',');
rawMessageBuilder.Append(property.Name);
rawMessageBuilder.Append('=');
rawMessageBuilder.Append(EscapeCQ(property.Value.GetString()!));
if (property.Value.GetString() is { } content)
{
rawMessageBuilder.Append(',');
rawMessageBuilder.Append(property.Name);
rawMessageBuilder.Append('=');
rawMessageBuilder.Append(EscapeCQ(content));
}
}
rawMessageBuilder.Append(']');
}
}
return rawMessageBuilder.ToString();
}

private static void ModifyTypeInfo(JsonTypeInfo ti)
{
if (ti.Kind != JsonTypeInfoKind.Object) return;
foreach (var info in ti.Properties.Where(x => x.AttributeProvider?.IsDefined(typeof(CQPropertyAttribute), false) == false).ToArray())
{
ti.Properties.Remove(info);
}
}
}

0 comments on commit ea709e9

Please sign in to comment.