Skip to content

Commit

Permalink
[All] Support refuse reason & handle filtered request in GroupRequest (
Browse files Browse the repository at this point in the history
…#585)

- close #573, #577
  • Loading branch information
pk5ls20 authored Sep 9, 2024
1 parent f170d2b commit cffa6ca
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 23 deletions.
6 changes: 5 additions & 1 deletion Lagrange.Core/Common/Entity/BotGroupRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ internal BotGroupRequest(
uint state,
ulong sequence,
uint type,
string? comment)
string? comment,
bool isFiltered)
{
GroupUin = groupUin;
InvitorMemberUin = invitorMemberUin;
Expand All @@ -27,6 +28,7 @@ internal BotGroupRequest(
Sequence = sequence;
EventType = (Type)type;
Comment = comment;
IsFiltered = isFiltered;
}

public uint GroupUin { get; set; }
Expand All @@ -50,6 +52,8 @@ internal BotGroupRequest(
internal ulong Sequence { get; set; } // for internal use of Approving Requests

public string? Comment { get; }

public bool IsFiltered { get; set; }

public enum State
{
Expand Down
7 changes: 5 additions & 2 deletions Lagrange.Core/Common/Interface/Api/GroupExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ public static Task<bool> LeaveGroup(this BotContext bot, uint groupUin)
public static Task<bool> InviteGroup(this BotContext bot, uint groupUin, Dictionary<uint, uint?> invitedUins)
=> bot.ContextCollection.Business.OperationLogic.InviteGroup(groupUin, invitedUins);

public static Task<bool> SetGroupRequest(this BotContext bot, BotGroupRequest request, bool accept = true)
=> bot.ContextCollection.Business.OperationLogic.SetGroupRequest(request.GroupUin, request.Sequence, (uint)request.EventType, accept);
public static Task<bool> SetGroupRequest(this BotContext bot, BotGroupRequest request, bool accept = true, string reason = "")
=> bot.ContextCollection.Business.OperationLogic.SetGroupRequest(request.GroupUin, request.Sequence, (uint)request.EventType, accept, reason);

public static Task<bool> SetGroupFilteredRequest(this BotContext bot, BotGroupRequest request, bool accept = true, string reason = "")
=> bot.ContextCollection.Business.OperationLogic.SetGroupFilteredRequest(request.GroupUin, request.Sequence, (uint)request.EventType, accept, reason);

public static Task<bool> SetFriendRequest(this BotContext bot, FriendRequestEvent request, bool accept = true)
=> bot.ContextCollection.Business.OperationLogic.SetFriendRequest(request.SourceUid, accept);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ public async Task<bool> RecallFriendMessage(MessageChain chain)
result.State,
result.Sequence,
result.EventType,
result.Comment
result.Comment,
result.IsFiltered
));
}

Expand Down Expand Up @@ -399,9 +400,16 @@ public async Task<bool> InviteGroup(uint targetGroupUin, Dictionary<uint, uint?>
return events.Count == 0 ? null : ((FetchClientKeyEvent)events[0]).ClientKey;
}

public async Task<bool> SetGroupRequest(uint groupUin, ulong sequence, uint type, bool accept)
public async Task<bool> SetGroupRequest(uint groupUin, ulong sequence, uint type, bool accept, string reason)
{
var inviteEvent = SetGroupRequestEvent.Create(accept, groupUin, sequence, type);
var inviteEvent = SetGroupRequestEvent.Create(accept, groupUin, sequence, type, reason);
var results = await Collection.Business.SendEvent(inviteEvent);
return results.Count != 0 && results[0].ResultCode == 0;
}

public async Task<bool> SetGroupFilteredRequest(uint groupUin, ulong sequence, uint type, bool accept, string reason)
{
var inviteEvent = SetGroupFilteredRequestEvent.Create(accept, groupUin, sequence, type, reason);
var results = await Collection.Business.SendEvent(inviteEvent);
return results.Count != 0 && results[0].ResultCode == 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Lagrange.Core.Internal.Event.Action;

internal class SetGroupFilteredRequestEvent : ProtocolEvent
{
public ulong Sequence { get; set; }

public uint GroupUin { get; set; }

public bool Accept { get; set; }

public uint Type { get; }

public string Reason { get; set; } = string.Empty;

private SetGroupFilteredRequestEvent(bool accept, uint groupUin, ulong sequence, uint type, string? reason) : base(true)
{
Accept = accept;
GroupUin = groupUin;
Sequence = sequence;
Type = type;
Reason = reason ?? "";
}

private SetGroupFilteredRequestEvent(int resultCode) : base(resultCode) { }

public static SetGroupFilteredRequestEvent Create(bool accept, uint groupUin, ulong sequence, uint type, string? reason)
=> new(accept, groupUin, sequence, type, reason);

public static SetGroupFilteredRequestEvent Result(int resultCode) => new(resultCode);
}
9 changes: 6 additions & 3 deletions Lagrange.Core/Internal/Event/Action/SetGroupRequestEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ internal class SetGroupRequestEvent : ProtocolEvent
public bool Accept { get; set; }

public uint Type { get; }

public string Reason { get; set; } = string.Empty;

private SetGroupRequestEvent(bool accept, uint groupUin, ulong sequence, uint type) : base(true)
private SetGroupRequestEvent(bool accept, uint groupUin, ulong sequence, uint type, string? reason) : base(true)
{
Accept = accept;
GroupUin = groupUin;
Sequence = sequence;
Type = type;
Reason = reason ?? "";
}

private SetGroupRequestEvent(int resultCode) : base(resultCode) { }

public static SetGroupRequestEvent Create(bool accept, uint groupUin, ulong sequence, uint type)
=> new(accept, groupUin, sequence, type);
public static SetGroupRequestEvent Create(bool accept, uint groupUin, ulong sequence, uint type, string? reason)
=> new(accept, groupUin, sequence, type, reason);

public static SetGroupRequestEvent Result(int resultCode) => new(resultCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public record RawEvent(
ulong Sequence,
uint State,
uint EventType,
string? Comment
string? Comment,
bool IsFiltered
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request;
/// Accept group request
/// </summary>
[ProtoContract]
[OidbSvcTrpcTcp(0x10c8, 1)]
internal class OidbSvcTrpcTcp0x10C8_1
internal class OidbSvcTrpcTcp0x10C8
{
[ProtoMember(1)] public uint Accept { get; set; } // 2 for reject, 1 for accept, 3 for ignore

[ProtoMember(2)] public OidbSvcTrpcTcp0x10C8_1Body? Body { get; set; }
[ProtoMember(2)] public OidbSvcTrpcTcp0x10C8Body? Body { get; set; }
}

[ProtoContract]
internal class OidbSvcTrpcTcp0x10C8_1Body
internal class OidbSvcTrpcTcp0x10C8Body
{
[ProtoMember(1)] public ulong Sequence { get; set; } // 1

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Lagrange.Core.Common;
using Lagrange.Core.Internal.Event;
using Lagrange.Core.Internal.Event.Action;
using Lagrange.Core.Internal.Packets.Service.Oidb;
using Lagrange.Core.Internal.Packets.Service.Oidb.Request;
using Lagrange.Core.Utility.Extension;
using ProtoBuf;

namespace Lagrange.Core.Internal.Service.Action;

[EventSubscribe(typeof(SetGroupFilteredRequestEvent))]
[Service("OidbSvcTrpcTcp.0x10c8_2")]
internal class SetGroupFilteredRequestService : BaseService<SetGroupFilteredRequestEvent>
{
protected override bool Build(SetGroupFilteredRequestEvent input, BotKeystore keystore, BotAppInfo appInfo,
BotDeviceInfo device, out Span<byte> output, out List<Memory<byte>>? extraPackets)
{
var packet = new OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0x10C8>(new OidbSvcTrpcTcp0x10C8
{
Accept = Convert.ToUInt32(!input.Accept) + 1,
Body = new OidbSvcTrpcTcp0x10C8Body
{
Sequence = input.Sequence,
EventType = input.Type,
GroupUin = input.GroupUin,
Message = input.Reason
}
}, 0x10c8, 2, false, true);

output = packet.Serialize();
extraPackets = null;
return true;
}

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

output = SetGroupFilteredRequestEvent.Result((int)payload.ErrorCode);
extraEvents = null;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ internal class SetGroupRequestService : BaseService<SetGroupRequestEvent>
protected override bool Build(SetGroupRequestEvent input, BotKeystore keystore, BotAppInfo appInfo,
BotDeviceInfo device, out Span<byte> output, out List<Memory<byte>>? extraPackets)
{
var packet = new OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0x10C8_1>(new OidbSvcTrpcTcp0x10C8_1
var packet = new OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0x10C8>(new OidbSvcTrpcTcp0x10C8
{
Accept = Convert.ToUInt32(!input.Accept) + 1,
Body = new OidbSvcTrpcTcp0x10C8_1Body
Body = new OidbSvcTrpcTcp0x10C8Body
{
Sequence = input.Sequence,
EventType = input.Type,
GroupUin = input.GroupUin,
Message = ""
Message = input.Reason
}
});
}, 0x10c8, 1, false, true);

output = packet.Serialize();
extraPackets = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ protected override bool Parse(Span<byte> input, BotKeystore keystore, BotAppInfo
x.Sequence,
x.State,
x.EventType,
x.Comment
x.Comment,
true
)).ToList() ?? new List<FetchGroupRequestsEvent.RawEvent>();

output = FetchGroupRequestsEvent.Result((int)payload.ErrorCode, events);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ protected override bool Parse(Span<byte> input, BotKeystore keystore, BotAppInfo
x.Sequence,
x.State,
x.EventType,
x.Comment
x.Comment,
false
)).ToList() ?? new List<FetchGroupRequestsEvent.RawEvent>();

output = FetchGroupRequestsEvent.Result((int)payload.ErrorCode, events);
Expand Down
2 changes: 2 additions & 0 deletions Lagrange.OneBot/Core/Entity/Action/OneBotSetRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public class OneBotSetRequest
[JsonPropertyName("flag")] public string Flag { get; set; } = string.Empty;

[JsonPropertyName("approve")] public bool Approve { get; set; } = true;

[JsonPropertyName("reason")] public string Reason { get; set; } = string.Empty;
}
2 changes: 1 addition & 1 deletion Lagrange.OneBot/Core/Notify/NotifyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void RegisterEvents()
var requests = await bot.FetchGroupRequests();
if (requests?.FirstOrDefault(x => @event.GroupUin == x.GroupUin && @event.TargetUin == x.TargetMemberUin) is { } request)
{
string flag = $"{request.Sequence}-{request.GroupUin}-{(uint)request.EventType}";
string flag = $"{request.Sequence}-{request.GroupUin}-{(uint)request.EventType}-{Convert.ToInt32(request.IsFiltered)}";
await service.SendJsonAsync(new OneBotGroupRequest(bot.BotUin, @event.TargetUin, @event.GroupUin, "add", request.Comment, flag));
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? pa
ulong sequence = ulong.Parse(split[0]);
uint groupUin = uint.Parse(split[1]);
uint eventType = uint.Parse(split[2]);

bool result = await context.ContextCollection.Business.OperationLogic.SetGroupRequest(groupUin, sequence, eventType, request.Approve);
bool isFiltered = Convert.ToBoolean(uint.Parse(split.Length > 3 ? split[3] : "0"));

bool result = isFiltered
? await context.ContextCollection.Business.OperationLogic.SetGroupFilteredRequest(groupUin, sequence,
eventType, request.Approve, request.Reason)
: await context.ContextCollection.Business.OperationLogic.SetGroupRequest(groupUin, sequence, eventType,
request.Approve, request.Reason);
return new OneBotResult(null, result ? 0 : 1, "ok");
}

Expand Down

0 comments on commit cffa6ca

Please sign in to comment.