Skip to content

Commit

Permalink
[OneBot] get_rkey (#742)
Browse files Browse the repository at this point in the history
  • Loading branch information
RikaCelery authored Jan 20, 2025
1 parent ea4474d commit 20b656e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 10 deletions.
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");
}
}

0 comments on commit 20b656e

Please sign in to comment.