Skip to content

Commit

Permalink
更新至v2.1.8 (#134)
Browse files Browse the repository at this point in the history
* 解决 MiraiHttpSession.GetUserProfileAsync 调用错误的问题 (#133)

* 修正xml标注错误
修复转发消息无法正常发送的问题 (#132)
更改 ChangeTypeJsonConverter 定义, 并添加一个三泛型参数的 ChangeTypeJsonConverter 以便更好控制(反)序列化

* 添加转发消息构建器 (I)ForwardMessageBuilder
  • Loading branch information
Executor-Cheng authored Aug 24, 2022
1 parent 00dd46e commit 1c20ca3
Show file tree
Hide file tree
Showing 28 changed files with 438 additions and 96 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,4 @@ ASALocalRun/
# BeatPulse healthcheck temp database
healthchecksdb
/Mirai-CSharp/Mirai-CSharp.xml
*.wakatime-project
6 changes: 3 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<OutputType>Library</OutputType>
<NoWin32Manifest>true</NoWin32Manifest>
<Version>2.1.7.0</Version>
<AssemblyVersion>2.1.7.0</AssemblyVersion>
<FileVersion>2.1.7.0</FileVersion>
<PackageVersion>2.1.7.0</PackageVersion>
<AssemblyVersion>2.1.8.0</AssemblyVersion>
<FileVersion>2.1.8.0</FileVersion>
<PackageVersion>2.1.8</PackageVersion>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
<Authors>Executor</Authors>
Expand Down
116 changes: 116 additions & 0 deletions Mirai-CSharp.HttpApi/Builder/ForwardMessageBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using Mirai.CSharp.HttpApi.Models.ChatMessages;
using ISharedChatMessage = Mirai.CSharp.Models.ChatMessages.IChatMessage;
using ISharedForwardMessage = Mirai.CSharp.Models.ChatMessages.IForwardMessage;
using ISharedForwardMessageBuilder = Mirai.CSharp.Builders.IForwardMessageBuilder;
using ISharedForwardMessageNode = Mirai.CSharp.Models.ChatMessages.IForwardMessageNode;
using ISharedMessageChainBuilder = Mirai.CSharp.Builders.IMessageChainBuilder;
using SharedForwardMessageBuilder = Mirai.CSharp.Builders.ForwardMessageBuilder;

namespace Mirai.CSharp.HttpApi.Builders
{
public interface IForwardMessageBuilder : ISharedForwardMessageBuilder
{

}

public class ForwardMessageBuilder : SharedForwardMessageBuilder, IForwardMessageBuilder
{
public override ISharedForwardMessage Build()
{
IForwardMessageNode[] converted = new ForwardMessageNode[_nodes.Count];
int ix = 0;
foreach (var node in _nodes)
{
converted[ix++] = (IForwardMessageNode)node;
}
return new ForwardMessage(converted);
}

public override ISharedForwardMessageBuilder AddNode(ISharedForwardMessageNode node)
{
if (node is not IForwardMessageNode)
{
throw new InvalidOperationException($"添加的消息实例 {node.GetType().FullName} 不实现 {typeof(IForwardMessageNode).FullName}");
}
return base.AddNode(node);
}

public override ISharedForwardMessageBuilder AddNodes(IEnumerable<ISharedForwardMessageNode> nodes)
{
foreach (ISharedForwardMessageNode node in nodes)
{
if (node is not IForwardMessageNode)
{
throw new InvalidOperationException($"添加的消息实例 {node.GetType().FullName} 不实现 {typeof(IChatMessage).FullName}");
}
}
return base.AddNodes(nodes);
}

public override ISharedForwardMessageBuilder AddNode(int id)
{
_nodes.Add(new ForwardMessageNode(id));
return this;
}

public override ISharedForwardMessageBuilder AddNode(string name, long qqNumber, DateTime time, ISharedMessageChainBuilder chainBuilder)
{
return AddNode(name, qqNumber, time, chainBuilder.Build());
}

public override ISharedForwardMessageBuilder AddNode(string name, long qqNumber, DateTime time, params ISharedChatMessage[] messages)
{
if (messages is not IChatMessage[] converted)
{
converted = new IChatMessage[messages.Length];
int ix = 0;
foreach (var message in messages)
{
if (message is not IChatMessage chatMessage)
{
throw new InvalidOperationException($"添加的消息实例 {message.GetType().FullName} 不实现 {typeof(IChatMessage).FullName}");
}
converted[ix++] = chatMessage;
}
}
_nodes.Add(new ForwardMessageNode(name, qqNumber, time, converted));
return this;
}
}

public class ImmutableForwardMessageBuilder : ForwardMessageBuilder
{
protected ISharedForwardMessage? _builtMessage;

public override ISharedForwardMessage Build()
{
if (_builtMessage == null)
{
_builtMessage = base.Build();
_nodes.Clear();
_nodes.Capacity = 0;
}
return _builtMessage;
}

public override ISharedForwardMessageBuilder AddNode(ISharedForwardMessageNode node)
{
if (_builtMessage != null)
{
throw new InvalidOperationException("由于之前进行过构建操作, 无法添加新的消息节点实例");
}
return base.AddNode(node);
}

public override ISharedForwardMessageBuilder AddNodes(IEnumerable<ISharedForwardMessageNode> nodes)
{
if (_builtMessage != null)
{
throw new InvalidOperationException("由于之前进行过构建操作, 无法添加新的消息节点实例");
}
return base.AddNodes(nodes);
}
}
}
51 changes: 51 additions & 0 deletions Mirai-CSharp.HttpApi/Builder/MessageChainBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using Mirai.CSharp.HttpApi.Models.ChatMessages;
using ISharedChatMessage = Mirai.CSharp.Models.ChatMessages.IChatMessage;
using ISharedForwardMessage = Mirai.CSharp.Models.ChatMessages.IForwardMessage;
using ISharedForwardMessageBuilder = Mirai.CSharp.Builders.IForwardMessageBuilder;
using ISharedMessageChainBuilder = Mirai.CSharp.Builders.IMessageChainBuilder;
using ISharedPokeType = Mirai.CSharp.Models.PokeType;
using SharedMessageChainBuilder = Mirai.CSharp.Builders.MessageChainBuilder;
Expand All @@ -14,6 +17,17 @@ public interface IMessageChainBuilder : ISharedMessageChainBuilder

public class MessageChainBuilder : SharedMessageChainBuilder, IMessageChainBuilder
{
public override ISharedChatMessage[] Build()
{
IChatMessage[] converted = new IChatMessage[_list.Count];
int ix = 0;
foreach (var message in _list)
{
converted[ix++] = (IChatMessage)message;
}
return converted;
}

public override ISharedMessageChainBuilder Add(ISharedChatMessage message)
{
if (message is not IChatMessage)
Expand All @@ -23,6 +37,18 @@ public override ISharedMessageChainBuilder Add(ISharedChatMessage message)
return base.Add(message);
}

public override ISharedMessageChainBuilder AddRange(IEnumerable<ISharedChatMessage> messages)
{
foreach (ISharedChatMessage message in messages)
{
if (message is not IChatMessage)
{
throw new InvalidOperationException($"添加的消息实例 {message.GetType().FullName} 不实现 {typeof(IChatMessage).FullName}");
}
}
return base.AddRange(messages);
}

public override ISharedMessageChainBuilder AddPlainMessage(string text)
{
Add(new PlainMessage(text));
Expand Down Expand Up @@ -59,6 +85,17 @@ public override ISharedMessageChainBuilder AddFaceMessage(int id, string? name =
return this;
}

public override ISharedMessageChainBuilder AddForwardMessage(ISharedForwardMessageBuilder builder)
{
ISharedForwardMessage message = builder.Build();
if (message is not IForwardMessage)
{
throw new InvalidOperationException($"给定的转发消息构建器 {builder.GetType().FullName} 不适用于本消息构建器");
}
Add(message);
return this;
}

public override ISharedMessageChainBuilder AddXmlMessage(string xml)
{
Add(new XmlMessage(xml));
Expand Down Expand Up @@ -88,6 +125,11 @@ public override ISharedMessageChainBuilder AddVoiceMessage(string? voiceId = nul
Add(new VoiceMessage(voiceId, url, path));
return this;
}

public override ISharedForwardMessageBuilder GetForwardMessageBuilder()
{
return new ForwardMessageBuilder();
}
}

public class ImmutableMessageChainBuilder : MessageChainBuilder
Expand All @@ -113,5 +155,14 @@ public override ISharedMessageChainBuilder Add(ISharedChatMessage message)
}
return base.Add(message);
}

public override ISharedMessageChainBuilder AddRange(IEnumerable<ISharedChatMessage> messages)
{
if (_builtMessages != null)
{
throw new InvalidOperationException("由于之前进行过构建操作, 无法添加新的消息实例");
}
return base.AddRange(messages);
}
}
}
11 changes: 4 additions & 7 deletions Mirai-CSharp.HttpApi/Mirai-CSharp.HttpApi.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<RootNamespace>Mirai.CSharp.HttpApi</RootNamespace>
Expand All @@ -23,12 +23,9 @@
<PackageReference Include="SkiaSharp" Version="2.80.3" />
</ItemGroup>

<ItemGroup Condition="'$(NETCoreSdkRuntimeIdentifier)' == 'win-x86' or '$(NETCoreSdkRuntimeIdentifier)' == 'win-x64'">
<PackageReference Include="Mirai-CSharp.NativeAssets.Win32" Version="1.0.2"/>
</ItemGroup>

<ItemGroup Condition="'$(NETCoreSdkRuntimeIdentifier)' == 'linux-x64'">
<PackageReference Include="Mirai-CSharp.NativeAssets.Linux" Version="1.0.2"/>
<ItemGroup>
<PackageReference Condition="'$(NETCoreSdkRuntimeIdentifier)' == 'win-x86' or '$(NETCoreSdkRuntimeIdentifier)' == 'win-x64'" Include="Mirai-CSharp.NativeAssets.Win32" Version="1.0.2"/>
<PackageReference Condition="'$(NETCoreSdkRuntimeIdentifier)' == 'linux-x64'" Include="Mirai-CSharp.NativeAssets.Linux" Version="1.0.2"/>
</ItemGroup>

</Project>
8 changes: 4 additions & 4 deletions Mirai-CSharp.HttpApi/Models/ChatMessages/ForwardMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ namespace Mirai.CSharp.HttpApi.Models.ChatMessages
public interface IForwardMessage : ISharedForwardMessage, IChatMessage
{
[JsonPropertyName("nodeList")]
[JsonConverter(typeof(ChangeTypeJsonConverter<ForwardMessageNode[], IForwardMessageNode[]>))]
[JsonConverter(typeof(ChangeTypeJsonConverter<IForwardMessageNode[], object[], ForwardMessageNode[]>))]
new IForwardMessageNode[] Nodes { get; }

#if !NETSTANDARD2_0
[JsonConverter(typeof(ChangeTypeJsonConverter<ForwardMessageNode[], ISharedForwardMessageNode[]>))]
[JsonConverter(typeof(ChangeTypeJsonConverter<ISharedForwardMessageNode[], object[], ForwardMessageNode[]>))]
[JsonPropertyName("nodeList")]
ISharedForwardMessageNode[] ISharedForwardMessage.Nodes => Nodes;
#endif
Expand All @@ -36,7 +36,7 @@ public class ForwardMessage : ChatMessage, IForwardMessage
/// <summary>
/// 转发的消息数组
/// </summary>
[JsonConverter(typeof(ChangeTypeJsonConverter<ForwardMessageNode[], IForwardMessageNode[]>))]
[JsonConverter(typeof(ChangeTypeJsonConverter<IForwardMessageNode[], object[], ForwardMessageNode[]>))]
[JsonPropertyName("nodeList")]
public IForwardMessageNode[] Nodes { get; set; } = null!;

Expand All @@ -57,7 +57,7 @@ public ForwardMessage(IForwardMessageNode[] nodes)
}

#if NETSTANDARD2_0
[JsonConverter(typeof(ChangeTypeJsonConverter<ForwardMessageNode[], ISharedForwardMessageNode[]>))]
[JsonConverter(typeof(ChangeTypeJsonConverter<ISharedForwardMessageNode[], object[], ForwardMessageNode[]>))]
[JsonPropertyName("nodeList")]
ISharedForwardMessageNode[] ISharedForwardMessage.Nodes => Nodes;
#endif
Expand Down
18 changes: 18 additions & 0 deletions Mirai-CSharp.HttpApi/Models/ChatMessages/ForwardMessageNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,24 @@ public class ForwardMessageNode : IForwardMessageNode
[JsonPropertyName("messageChain")]
public IChatMessage[] Chain { get; set; } = null!;

public ForwardMessageNode()
{

}

public ForwardMessageNode(int id)
{
Id = id;
}

public ForwardMessageNode(string name, long qQNumber, DateTime time, IChatMessage[] chain)
{
Name = name;
QQNumber = qQNumber;
Time = time;
Chain = chain;
}

public override string ToString()
{
return $"[mirai:forward:{Chain.Length} nodes]";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace Mirai.CSharp.HttpApi.Models.ChatMessages
public interface IMarketFaceMessage : ISharedMarketFaceMessage, IChatMessage
{
#if NETSTANDARD2_0
/// <inheritdoc cref="ISharedMarketFaceMessaage.Id"/>
/// <inheritdoc cref="ISharedMarketFaceMessage.Id"/>
[JsonPropertyName("id")]
new int Id { get; }
/// <inheritdoc cref="ISharedMarketFaceMessaage.Name"/>
/// <inheritdoc cref="ISharedMarketFaceMessage.Name"/>
[JsonPropertyName("name")]
new string Name { get; }
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public interface IBotJoinedGroupEventArgs : ISharedBotJoinedGroupEventArgs, IGro
public class BotJoinedGroupEventArgs : GroupEventArgs, IBotJoinedGroupEventArgs
{
/// <inheritdoc/>
[JsonConverter(typeof(ChangeTypeJsonConverter<GroupMemberInfo, IGroupMemberInfo>))]
[JsonConverter(typeof(ChangeTypeJsonConverter<IGroupMemberInfo, GroupMemberInfo>))]
[JsonPropertyName("invitor")]
public IGroupMemberInfo? Inviter { get; set; }

Expand All @@ -40,7 +40,7 @@ public BotJoinedGroupEventArgs(GroupInfo group) : base(group)

#if NETSTANDARD2_0
/// <inheritdoc/>
[JsonConverter(typeof(ChangeTypeJsonConverter<GroupMemberInfo, ISharedGroupMemberInfo>))]
[JsonConverter(typeof(ChangeTypeJsonConverter<ISharedGroupMemberInfo, GroupMemberInfo>))]
[JsonPropertyName("invitor")]
ISharedGroupMemberInfo? ISharedInviterEventArgs.Inviter => Inviter;
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ namespace Mirai.CSharp.HttpApi.Models.EventArgs
public interface IFriendEventArgs : ISharedJsonFriendEventArgs, IMiraiHttpMessage
{
[JsonPropertyName("friend")]
[JsonConverter(typeof(ChangeTypeJsonConverter<FriendInfo, IFriendInfo>))]
[JsonConverter(typeof(ChangeTypeJsonConverter<IFriendInfo, FriendInfo>))]
new IFriendInfo Friend { get; }

#if !NETSTANDARD2_0
[JsonPropertyName("friend")]
[JsonConverter(typeof(ChangeTypeJsonConverter<FriendInfo, ISharedFriendInfo>))]
[JsonConverter(typeof(ChangeTypeJsonConverter<ISharedFriendInfo, FriendInfo>))]
ISharedFriendInfo ISharedFriendEventArgs.Friend => Friend;
#endif
}

public abstract class FriendEventArgs : MiraiHttpMessage, IFriendEventArgs
{
[JsonPropertyName("friend")]
[JsonConverter(typeof(ChangeTypeJsonConverter<FriendInfo, IFriendInfo>))]
[JsonConverter(typeof(ChangeTypeJsonConverter<IFriendInfo, FriendInfo>))]
public IFriendInfo Friend { get; set; } = null!;

protected FriendEventArgs()
Expand All @@ -37,7 +37,7 @@ protected FriendEventArgs(IFriendInfo friend)

#if NETSTANDARD2_0
[JsonPropertyName("friend")]
[JsonConverter(typeof(ChangeTypeJsonConverter<FriendInfo, ISharedFriendInfo>))]
[JsonConverter(typeof(ChangeTypeJsonConverter<ISharedFriendInfo, FriendInfo>))]
ISharedFriendInfo ISharedFriendEventArgs.Friend => Friend;
#endif
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Mirai.CSharp.HttpApi.Models.EventArgs
public interface IFriendMessageEventArgs : ISharedJsonFriendMessageEventArgs, ICommonMessageEventArgs
{
/// <inheritdoc cref="ISharedFriendMessageEventArgs.Sender"/>
[JsonConverter(typeof(ChangeTypeJsonConverter<FriendInfo, IFriendInfo>))]
[JsonConverter(typeof(ChangeTypeJsonConverter<IFriendInfo, FriendInfo>))]
[JsonPropertyName("sender")]
new IFriendInfo Sender { get; }

Expand All @@ -29,7 +29,7 @@ public interface IFriendMessageEventArgs : ISharedJsonFriendMessageEventArgs, IC
public class FriendMessageEventArgs : CommonMessageEventArgs, IFriendMessageEventArgs
{
/// <inheritdoc/>
[JsonConverter(typeof(ChangeTypeJsonConverter<FriendInfo, IFriendInfo>))]
[JsonConverter(typeof(ChangeTypeJsonConverter<IFriendInfo, FriendInfo>))]
[JsonPropertyName("sender")]
public IFriendInfo Sender { get; set; } = null!;

Expand Down
Loading

0 comments on commit 1c20ca3

Please sign in to comment.