Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新至v2.1.9 #138

Merged
merged 4 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
<PropertyGroup>
<OutputType>Library</OutputType>
<NoWin32Manifest>true</NoWin32Manifest>
<Version>2.1.7.0</Version>
<AssemblyVersion>2.1.8.0</AssemblyVersion>
<FileVersion>2.1.8.0</FileVersion>
<PackageVersion>2.1.8</PackageVersion>
<Version>2.1.9.0</Version>
<AssemblyVersion>2.1.9.0</AssemblyVersion>
<FileVersion>2.1.9.0</FileVersion>
<PackageVersion>2.1.9</PackageVersion>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
<Authors>Executor</Authors>
Expand Down
1 change: 0 additions & 1 deletion Mirai-CSharp.HttpApi/Builder/MiraiHttpFrameworkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ public static class MiraiHttpFrameworkBuilderExtensions
public static MiraiHttpFrameworkBuilder AddDefaultServices(this MiraiHttpFrameworkBuilder builder)
{
builder.Services.TryAddSingleton<ChatMessageJsonConverter>();
builder.Services.TryAddSingleton<ISilkLameCoder, SilkLameCoder>();
builder.AddInvoker<MiraiHttpMessageHandlerInvoker>();
builder.AddDefaultParsers();
builder.AddDefaultChatParsers();
Expand Down
18 changes: 0 additions & 18 deletions Mirai-CSharp.HttpApi/Exceptions/MissingVoiceCoderException.cs

This file was deleted.

40 changes: 29 additions & 11 deletions Mirai-CSharp.HttpApi/Extensions/ApiResponseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static void EnsureApiRespCode(this JsonElement root)

public static async Task AsApiRespAsync(this Task<HttpResponseMessage> responseTask, CancellationToken token = default)
{
using var j = await responseTask.GetJsonAsync(token);
using var j = await responseTask.GetJsonAsync(token).ConfigureAwait(false);
var root = j.RootElement;
if (!root.CheckApiRespCode(out var code))
{
Expand All @@ -89,38 +89,56 @@ public static async Task AsApiRespAsync(this Task<HttpResponseMessage> responseT

public static Task<TResult> AsApiRespAsync<TResult>(this Task<HttpResponseMessage> responseTask, CancellationToken token = default)
{
return responseTask.AsApiRespAsync<TResult, TResult>(token);
return responseTask.AsApiRespAsync<TResult>(null, token);
}

public static Task<TResult> AsApiRespAsync<TResult>(this Task<HttpResponseMessage> responseTask, JsonSerializerOptions? options, CancellationToken token = default)
{
return responseTask.AsApiRespAsync<TResult, TResult>(options, token);
}

public static Task<TResult> AsApiRespAsync<TResult, TImpl>(this Task<HttpResponseMessage> responseTask, CancellationToken token = default) where TImpl : TResult
{
return responseTask.AsApiRespAsync<TResult, TImpl>(null, token);
}

public static async Task<TResult> AsApiRespAsync<TResult, TImpl>(this Task<HttpResponseMessage> responseTask, CancellationToken token = default) where TImpl : TResult
public static async Task<TResult> AsApiRespAsync<TResult, TImpl>(this Task<HttpResponseMessage> responseTask, JsonSerializerOptions? options, CancellationToken token = default) where TImpl : TResult
{
//using var j = await responseTask.GetJsonAsync(token);
string json = await responseTask.GetStringAsync(token);
using var j = JsonDocument.Parse(json);
using var j = await responseTask.GetJsonAsync(token);
var root = j.RootElement;
if (root.CheckApiRespCode(out var code))
{
return root.Deserialize<TImpl>()!;
return root.Deserialize<TImpl>(options)!;
}
throw GetCommonException(code!.Value, in root);
}

public static Task<TResult> AsApiRespV2Async<TResult>(this Task<HttpResponseMessage> responseTask, CancellationToken token = default)
{
return responseTask.AsApiRespV2Async<TResult, TResult>(token);
return responseTask.AsApiRespV2Async<TResult>(null, token);
}

public static Task<TResult> AsApiRespV2Async<TResult>(this Task<HttpResponseMessage> responseTask, JsonSerializerOptions? options = null, CancellationToken token = default)
{
return responseTask.AsApiRespV2Async<TResult, TResult>(options, token);
}

public static Task<TResult> AsApiRespV2Async<TResult, TImpl>(this Task<HttpResponseMessage> responseTask, CancellationToken token = default) where TImpl : TResult
{
return responseTask.AsApiRespV2Async<TResult, TImpl>(null, token);
}

public static async Task<TResult> AsApiRespV2Async<TResult, TImpl>(this Task<HttpResponseMessage> responseTask, CancellationToken token = default) where TImpl : TResult
public static async Task<TResult> AsApiRespV2Async<TResult, TImpl>(this Task<HttpResponseMessage> responseTask, JsonSerializerOptions? options, CancellationToken token = default) where TImpl : TResult
{
using var j = await responseTask.GetJsonAsync(token);
var root = j.RootElement;
if (root.CheckApiRespCode(out var code))
{
if (root.ValueKind == JsonValueKind.Object && root.TryGetProperty("data", out JsonElement data))
{
return data.Deserialize<TImpl>()!;
return data.Deserialize<TImpl>(options)!;
}
return root.Deserialize<TImpl>()!; // 向前兼容, 不确定 mirai-api-http v1.x 的行为如何
return root.Deserialize<TImpl>(options)!; // 向前兼容, 不确定 mirai-api-http v1.x 的行为如何
}
throw GetCommonException(code!.Value, in root);
}
Expand Down
26 changes: 13 additions & 13 deletions Mirai-CSharp.HttpApi/Extensions/HttpClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static async Task<HttpResponseMessage> SendAsync(this HttpClient client,
using HttpRequestMessage request = new HttpRequestMessage(method, uri);
request.Content = content;
request.Version = DefaultHttpVersion;
return await client.SendAsync(request, token);
return await client.SendAsync(request, token).ConfigureAwait(false);
}

#if NET5_0_OR_GREATER
Expand All @@ -52,7 +52,7 @@ public static async Task<HttpResponseMessage> SendAsync(this HttpClient client,
public static async Task<byte[]> GetBytesAsync(this Task<HttpResponseMessage> responseTask, CancellationToken token = default)
{
using HttpResponseMessage response = await responseTask.ConfigureAwait(false);
return await response.Content.ReadAsByteArrayAsync(token);
return await response.Content.ReadAsByteArrayAsync(token).ConfigureAwait(false);
}

/// <summary>
Expand All @@ -64,7 +64,7 @@ public static async Task<byte[]> GetBytesAsync(this Task<HttpResponseMessage> re
public static async Task<string> GetStringAsync(this Task<HttpResponseMessage> responseTask, CancellationToken token = default)
{
using HttpResponseMessage response = await responseTask.ConfigureAwait(false);
return await response.Content.ReadAsStringAsync(token);
return await response.Content.ReadAsStringAsync(token).ConfigureAwait(false);
}
#else
#pragma warning disable IDE0079 // Remove unnecessary suppression
Expand All @@ -78,7 +78,7 @@ public static async Task<string> GetStringAsync(this Task<HttpResponseMessage> r
public static async Task<byte[]> GetBytesAsync(this Task<HttpResponseMessage> responseTask, CancellationToken token = default)
{
using HttpResponseMessage response = await responseTask.ConfigureAwait(false);
return await response.Content.ReadAsByteArrayAsync();
return await response.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
}

/// <summary>
Expand All @@ -90,7 +90,7 @@ public static async Task<byte[]> GetBytesAsync(this Task<HttpResponseMessage> re
public static async Task<string> GetStringAsync(this Task<HttpResponseMessage> responseTask, CancellationToken token = default)
{
using HttpResponseMessage response = await responseTask.ConfigureAwait(false);
return await response.Content.ReadAsStringAsync();
return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}
#pragma warning restore IDE0060
#pragma warning restore IDE0079
Expand Down Expand Up @@ -136,7 +136,7 @@ public static async Task<string> GetStringAsync(this Task<HttpResponseMessage> r
public static async Task<T?> GetObjectAsync<T>(this Task<HttpResponseMessage> responseTask, JsonSerializerOptions? options, CancellationToken token = default)
{
using HttpResponseMessage response = await responseTask.ConfigureAwait(false);
return await response.Content.ReadFromJsonAsync<T?>(options, token);
return await response.Content.ReadFromJsonAsync<T?>(options, token).ConfigureAwait(false);
}

/// <inheritdoc cref="GetObjectAsync(Task{HttpResponseMessage}, Type, JsonSerializerOptions?, CancellationToken)"/>
Expand All @@ -151,7 +151,7 @@ public static async Task<string> GetStringAsync(this Task<HttpResponseMessage> r
public static async Task<object?> GetObjectAsync(this Task<HttpResponseMessage> responseTask, Type returnType, JsonSerializerOptions? options, CancellationToken token = default)
{
using HttpResponseMessage response = await responseTask.ConfigureAwait(false);
return await response.Content.ReadFromJsonAsync(returnType, options, token);
return await response.Content.ReadFromJsonAsync(returnType, options, token).ConfigureAwait(false);
}

/// <inheritdoc cref="GetJsonAsync(Task{HttpResponseMessage}, JsonDocumentOptions, CancellationToken)"/>
Expand All @@ -178,7 +178,7 @@ public static async Task<JsonDocument> GetJsonAsync(this Task<HttpResponseMessag
}
using (stream)
{
return await JsonDocument.ParseAsync(stream, options, token);
return await JsonDocument.ParseAsync(stream, options, token).ConfigureAwait(false);
}
}
#else
Expand All @@ -200,7 +200,7 @@ public static async Task<JsonDocument> GetJsonAsync(this Task<HttpResponseMessag
{
using HttpResponseMessage response = await responseTask.ConfigureAwait(false);
using Stream stream = await response.Content.ReadAsStreamAsync();
return await JsonSerializer.DeserializeAsync<T?>(stream, options, token);
return await JsonSerializer.DeserializeAsync<T?>(stream, options, token).ConfigureAwait(false);
}

/// <inheritdoc cref="GetObjectAsync(Task{HttpResponseMessage}, Type, JsonSerializerOptions?, CancellationToken)"/>
Expand All @@ -214,8 +214,8 @@ public static async Task<JsonDocument> GetJsonAsync(this Task<HttpResponseMessag
public static async Task<object?> GetObjectAsync(this Task<HttpResponseMessage> responseTask, Type returnType, JsonSerializerOptions? options, CancellationToken token = default)
{
using HttpResponseMessage response = await responseTask.ConfigureAwait(false);
using var stream = await response.Content.ReadAsStreamAsync();
return await JsonSerializer.DeserializeAsync(stream, returnType, options, token);
using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
return await JsonSerializer.DeserializeAsync(stream, returnType, options, token).ConfigureAwait(false);
}

/// <inheritdoc cref="GetJsonAsync(Task{HttpResponseMessage}, JsonDocumentOptions, CancellationToken)"/>
Expand All @@ -237,8 +237,8 @@ public static Task<JsonDocument> GetJsonAsync(this Task<HttpResponseMessage> res
public static async Task<JsonDocument> GetJsonAsync(this Task<HttpResponseMessage> responseTask, JsonDocumentOptions options, CancellationToken token = default)
{
using HttpResponseMessage response = await responseTask.ConfigureAwait(false);
using Stream stream = await response.Content.ReadAsStreamAsync();
return await JsonDocument.ParseAsync(stream, options, token);
using Stream stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
return await JsonDocument.ParseAsync(stream, options, token).ConfigureAwait(false);
}
#endif
/// <summary>
Expand Down
11 changes: 1 addition & 10 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 @@ -19,13 +19,4 @@
<PackageReference Include="System.Reflection.Emit.Lightweight" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="SkiaSharp" Version="2.80.3" />
</ItemGroup>

<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>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public interface IFriendMessageEventArgs : ISharedJsonFriendMessageEventArgs, IC
new IFriendInfo Sender { get; }

#if !NETSTANDARD2_0
[JsonConverter(typeof(ChangeTypeJsonConverter<ISharedFriendInfo, FriendInfo>))]
[JsonPropertyName("sender")]
ISharedFriendInfo ISharedFriendMessageEventArgs.Sender => Sender;
#endif
}
Expand All @@ -46,6 +48,8 @@ public override string ToString()
=> $"{Sender.Name}({Sender.Id}) -> {string.Join("", (IEnumerable<ChatMessage>)Chain)}";

#if NETSTANDARD2_0
[JsonConverter(typeof(ChangeTypeJsonConverter<ISharedFriendInfo, FriendInfo>))]
[JsonPropertyName("sender")]
ISharedFriendInfo ISharedFriendMessageEventArgs.Sender => Sender;
#endif
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;
using Mirai.CSharp.HttpApi.Models.ChatMessages;
using Mirai.CSharp.HttpApi.Parsers.Attributes;
using Mirai.CSharp.HttpApi.Utility.JsonConverters;
using ISharedFriendInfo = Mirai.CSharp.Models.IFriendInfo;
using ISharedFriendSyncMessageEventArgs = Mirai.CSharp.Models.EventArgs.IFriendSyncMessageEventArgs;
using ISharedJsonFriendMessageEventArgs = Mirai.CSharp.Models.EventArgs.IFriendSyncMessageEventArgs<System.Text.Json.JsonElement>;

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
namespace Mirai.CSharp.HttpApi.Models.EventArgs
{
/// <summary>
/// 提供好友同步消息的相关信息接口。继承自 <see cref="ISharedJsonFriendMessageEventArgs"/> 和 <see cref="ICommonMessageEventArgs"/>
/// </summary>
[MappableMiraiHttpMessageKey("FriendSyncMessage")]
public interface IFriendSyncMessageEventArgs : ISharedJsonFriendMessageEventArgs, ICommonMessageEventArgs
{
/// <inheritdoc cref="ISharedFriendSyncMessageEventArgs.Subject"/>
[JsonConverter(typeof(ChangeTypeJsonConverter<IFriendInfo, FriendInfo>))]
[JsonPropertyName("subject")]
new IFriendInfo Subject { get; }

#if !NETSTANDARD2_0
[JsonConverter(typeof(ChangeTypeJsonConverter<ISharedFriendInfo, FriendInfo>))]
[JsonPropertyName("subject")]
ISharedFriendInfo ISharedFriendSyncMessageEventArgs.Subject => Subject;
#endif
}

public class FriendSyncMessageEventArgs : CommonMessageEventArgs, IFriendSyncMessageEventArgs
{
/// <inheritdoc/>
[JsonConverter(typeof(ChangeTypeJsonConverter<IFriendInfo, FriendInfo>))]
[JsonPropertyName("subject")]
public IFriendInfo Subject { get; set; }

[Obsolete("此类不应由用户主动创建实例。")]
public FriendSyncMessageEventArgs() { }

[Obsolete("此类不应由用户主动创建实例。")]
public FriendSyncMessageEventArgs(IChatMessage[] chain, IFriendInfo subject) : base(chain)
{
Subject = subject;
}

public override string ToString()
=> $"{Subject.Name}({Subject.Id})[SYNC] <- {string.Join("", (IEnumerable<ChatMessage>)Chain)}";

#if NETSTANDARD2_0
[JsonConverter(typeof(ChangeTypeJsonConverter<ISharedFriendInfo, FriendInfo>))]
[JsonPropertyName("subject")]
ISharedFriendInfo ISharedFriendSyncMessageEventArgs.Subject => Subject;
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@ public interface IGroupApplyEventArgs : ISharedGroupApplyEventArgs, ICommonGroup

public class GroupApplyEventArgs : CommonGroupApplyEventArgs, IGroupApplyEventArgs
{
/// <inheritdoc/>
public long? InvitorId { get; }

[Obsolete("此类不应由用户主动创建实例。")]
public GroupApplyEventArgs()
{

}

[Obsolete("此类不应由用户主动创建实例。")]
public GroupApplyEventArgs(string fromGroupName, long eventId, long fromGroup, long fromQQ, string nickName, string message) : base(fromGroupName, eventId, fromGroup, fromQQ, nickName, message)
public GroupApplyEventArgs(string fromGroupName, long eventId, long fromGroup, long fromQQ, string nickName, string message, long? invitorId) : base(fromGroupName, eventId, fromGroup, fromQQ, nickName, message)
{

InvitorId = invitorId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using Mirai.CSharp.HttpApi.Parsers.Attributes;
using ISharedGroupDisbandEventArgs = Mirai.CSharp.Models.EventArgs.IGroupDisbandEventArgs<System.Text.Json.JsonElement>;

namespace Mirai.CSharp.HttpApi.Models.EventArgs.Group
{
/// <summary>
/// 提供群被群主解散后bot离开群相关信息的接口。继承自 <see cref="ISharedGroupDisbandEventArgs"/> 和 <see cref="IGroupOperatingEventArgs"/>
/// </summary>
[MappableMiraiHttpMessageKey("BotLeaveEventDisband")]
public interface IGroupDisbandEventArgs : ISharedGroupDisbandEventArgs, IGroupOperatingEventArgs
{

}

public class GroupDisbandEventArgs : GroupOperatingEventArgs, IGroupDisbandEventArgs
{
[Obsolete("此类不应由用户主动创建实例。")]
public GroupDisbandEventArgs()
{

}

[Obsolete("此类不应由用户主动创建实例。")]
public GroupDisbandEventArgs(IGroupInfo group, IGroupMemberInfo @operator) : base(group, @operator)
{

}
}
}
Loading