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

[OneBot] get_rkey #742

Merged
merged 5 commits into from
Jan 20, 2025
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
16 changes: 8 additions & 8 deletions Lagrange.Core/Internal/Event/System/FetchRKeyEvent.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
using Lagrange.Core.Internal.Packets.Service.Oidb.Common;

namespace Lagrange.Core.Internal.Event.System;

internal class FetchRKeyEvent : ProtocolEvent
{
public List<string> RKeys { get; } = new();
public List<RKeyInfo> RKeys { get; } = new();

private FetchRKeyEvent() : base(true)
{
}
private FetchRKeyEvent() : base(true) { }

private FetchRKeyEvent(int resultCode, List<string> rKeys) : base(resultCode)
private FetchRKeyEvent(int resultCode, List<RKeyInfo> rKeys) : base(resultCode)
{
RKeys = rKeys;
}

public static FetchRKeyEvent Create() => new();
public static FetchRKeyEvent Result(int resultCode, List<string> rKeys) => new(resultCode, rKeys);

public static FetchRKeyEvent Result(int resultCode, List<RKeyInfo> rKeys) => new(resultCode, rKeys);
}
4 changes: 2 additions & 2 deletions Lagrange.Core/Internal/Service/System/FetchRKeyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ protected override bool Build(FetchRKeyEvent input, BotKeystore keystore, BotApp
return true;
}

protected override bool Parse(Span<byte> input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
protected override bool Parse(Span<byte> input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
out FetchRKeyEvent output, out List<ProtocolEvent>? extraEvents)
{
var payload = Serializer.Deserialize<OidbSvcTrpcTcpBase<NTV2RichMediaResp>>(input);

output = FetchRKeyEvent.Result((int)payload.ErrorCode, payload.Body.DownloadRKey.RKeys.Select(x => x.Rkey).ToList());
output = FetchRKeyEvent.Result((int)payload.ErrorCode, payload.Body.DownloadRKey.RKeys);
extraEvents = null;
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Text.Json.Serialization;

namespace Lagrange.OneBot.Core.Entity.Action.Response;

[Serializable]
public class OneBotGetRkeyResponse(List<OneBotRkey> rkeys)
{
[JsonPropertyName("rkeys")] public List<OneBotRkey> Rkeys { get; set; } = rkeys;
}

[Serializable]
public class OneBotRkey
{
[JsonPropertyName("type")] public string? Type { get; set; }
[JsonPropertyName("rkey")] public string? Rkey { get; set; }
[JsonPropertyName("created_at")] public uint? CreateTime { get; set; }
[JsonPropertyName("ttl")] public ulong? TtlSeconds { get; set; }
}
30 changes: 30 additions & 0 deletions Lagrange.OneBot/Core/Operation/Generic/GetRkey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Text.Json.Nodes;
using Lagrange.Core;
using Lagrange.Core.Internal.Event.System;
using Lagrange.OneBot.Core.Entity.Action;
using Lagrange.OneBot.Core.Entity.Action.Response;

namespace Lagrange.OneBot.Core.Operation.Generic;

[Operation("get_rkey")]
public class GetRkey : IOperation
{
public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? payload)
{
var fetchRKeyEvent = FetchRKeyEvent.Create();
var events = await context.ContextCollection.Business.SendEvent(fetchRKeyEvent);
var rKeyEvent = (FetchRKeyEvent)events[0];
if (rKeyEvent.ResultCode != 0) return new OneBotResult(null, rKeyEvent.ResultCode, "failed");
var response = new OneBotGetRkeyResponse(rKeyEvent.RKeys.Select(x => new OneBotRkey
{
Type = x.Type == 10
? "private"
: "group",
Rkey = x.Rkey,
CreateTime = x.RkeyCreateTime,
TtlSeconds = x.RkeyTtlSec
})
.ToList());
return new OneBotResult(response, 0, "ok");
}
}
Loading