From 2380b1b6d8857debd2769d13c9941f7142f2c23f Mon Sep 17 00:00:00 2001 From: DarkRRb <177549718+DarkRRb@users.noreply.github.com> Date: Tue, 1 Oct 2024 13:14:04 +0800 Subject: [PATCH] [All] supported reduce group reaction (#619) Co-authored-by: DarkRRb <177549718+CwkDark@users.noreply.github.com> --- .../Common/Interface/Api/GroupExt.cs | 5 ++- .../Logic/Implementation/OperationLogic.cs | 17 ++++++-- ...ctionEvent.cs => GroupAddReactionEvent.cs} | 12 +++--- .../Event/Action/GroupReduceReactionEvent.cs | 24 +++++++++++ ...Tcp0x9082_1.cs => OidbSvcTrpcTcp0x9082.cs} | 15 ++++--- ...nService.cs => GroupAddReactionService.cs} | 16 +++---- .../Action/GroupReduceReactionService.cs | 42 +++++++++++++++++++ .../Entity/Action/OneBotSetGroupReaction.cs | 2 + .../Group/SetGroupReactionOperation.cs | 2 +- 9 files changed, 107 insertions(+), 28 deletions(-) rename Lagrange.Core/Internal/Event/Action/{GroupSetReactionEvent.cs => GroupAddReactionEvent.cs} (55%) create mode 100644 Lagrange.Core/Internal/Event/Action/GroupReduceReactionEvent.cs rename Lagrange.Core/Internal/Packets/Service/Oidb/Request/{OidbSvcTrpcTcp0x9082_1.cs => OidbSvcTrpcTcp0x9082.cs} (82%) rename Lagrange.Core/Internal/Service/Action/{GroupSetReactionService.cs => GroupAddReactionService.cs} (75%) create mode 100644 Lagrange.Core/Internal/Service/Action/GroupReduceReactionService.cs diff --git a/Lagrange.Core/Common/Interface/Api/GroupExt.cs b/Lagrange.Core/Common/Interface/Api/GroupExt.cs index 2cfa23f40..9925075c9 100644 --- a/Lagrange.Core/Common/Interface/Api/GroupExt.cs +++ b/Lagrange.Core/Common/Interface/Api/GroupExt.cs @@ -91,7 +91,10 @@ public static Task GroupSetSpecialTitle(this BotContext bot, uint groupUin => bot.ContextCollection.Business.OperationLogic.GroupSetSpecialTitle(groupUin, targetUin, title); public static Task GroupSetMessageReaction(this BotContext bot, uint groupUin, uint sequence, string code) - => bot.ContextCollection.Business.OperationLogic.SetMessageReaction(groupUin, sequence, code); + => bot.ContextCollection.Business.OperationLogic.SetMessageReaction(groupUin, sequence, code, true); + + public static Task GroupSetMessageReaction(this BotContext bot, uint groupUin, uint sequence, string code, bool isSet) + => bot.ContextCollection.Business.OperationLogic.SetMessageReaction(groupUin, sequence, code, isSet); #region Group File System diff --git a/Lagrange.Core/Internal/Context/Logic/Implementation/OperationLogic.cs b/Lagrange.Core/Internal/Context/Logic/Implementation/OperationLogic.cs index f9b75e381..10ce69573 100644 --- a/Lagrange.Core/Internal/Context/Logic/Implementation/OperationLogic.cs +++ b/Lagrange.Core/Internal/Context/Logic/Implementation/OperationLogic.cs @@ -528,11 +528,20 @@ public async Task GroupSetSpecialTitle(uint groupUin, uint targetUin, stri return await Collection.Business.CachingLogic.GetCachedUsers(uin, refreshCache); } - public async Task SetMessageReaction(uint groupUin, uint sequence, string code) + public async Task SetMessageReaction(uint groupUin, uint sequence, string code, bool isAdd) { - var setReactionEvent = GroupSetReactionEvent.Create(groupUin, sequence, code); - var results = await Collection.Business.SendEvent(setReactionEvent); - return results.Count != 0 && results[0].ResultCode == 0; + if (isAdd) + { + var addReactionEvent = GroupAddReactionEvent.Create(groupUin, sequence, code); + var results = await Collection.Business.SendEvent(addReactionEvent); + return results.Count != 0 && results[0].ResultCode == 0; + } + else + { + var reduceReactionEvent = GroupReduceReactionEvent.Create(groupUin, sequence, code); + var results = await Collection.Business.SendEvent(reduceReactionEvent); + return results.Count != 0 && results[0].ResultCode == 0; + } } public async Task SetNeedToConfirmSwitch(bool enableNoNeed) diff --git a/Lagrange.Core/Internal/Event/Action/GroupSetReactionEvent.cs b/Lagrange.Core/Internal/Event/Action/GroupAddReactionEvent.cs similarity index 55% rename from Lagrange.Core/Internal/Event/Action/GroupSetReactionEvent.cs rename to Lagrange.Core/Internal/Event/Action/GroupAddReactionEvent.cs index 33a49c057..d735b90ea 100644 --- a/Lagrange.Core/Internal/Event/Action/GroupSetReactionEvent.cs +++ b/Lagrange.Core/Internal/Event/Action/GroupAddReactionEvent.cs @@ -1,24 +1,24 @@ namespace Lagrange.Core.Internal.Event.Action; -internal class GroupSetReactionEvent : ProtocolEvent +internal class GroupAddReactionEvent : ProtocolEvent { public uint GroupUin { get; } - + public uint Sequence { get; } public string Code { get; } = string.Empty; - private GroupSetReactionEvent(uint groupUin, uint sequence, string code) : base(true) + private GroupAddReactionEvent(uint groupUin, uint sequence, string code) : base(true) { GroupUin = groupUin; Sequence = sequence; Code = code; } - private GroupSetReactionEvent(int resultCode) : base(resultCode) { } + private GroupAddReactionEvent(int resultCode) : base(resultCode) { } - public static GroupSetReactionEvent Create(uint groupUin, uint sequence, string code) + public static GroupAddReactionEvent Create(uint groupUin, uint sequence, string code) => new(groupUin, sequence, code); - public static GroupSetReactionEvent Result(int resultCode) => new(resultCode); + public static GroupAddReactionEvent Result(int resultCode) => new(resultCode); } \ No newline at end of file diff --git a/Lagrange.Core/Internal/Event/Action/GroupReduceReactionEvent.cs b/Lagrange.Core/Internal/Event/Action/GroupReduceReactionEvent.cs new file mode 100644 index 000000000..a59a05f5d --- /dev/null +++ b/Lagrange.Core/Internal/Event/Action/GroupReduceReactionEvent.cs @@ -0,0 +1,24 @@ +namespace Lagrange.Core.Internal.Event.Action; + +internal class GroupReduceReactionEvent : ProtocolEvent +{ + public uint GroupUin { get; } + + public uint Sequence { get; } + + public string Code { get; } = string.Empty; + + private GroupReduceReactionEvent(uint groupUin, uint sequence, string code) : base(true) + { + GroupUin = groupUin; + Sequence = sequence; + Code = code; + } + + private GroupReduceReactionEvent(int resultCode) : base(resultCode) { } + + public static GroupReduceReactionEvent Create(uint groupUin, uint sequence, string code) + => new(groupUin, sequence, code); + + public static GroupReduceReactionEvent Result(int resultCode) => new(resultCode); +} \ No newline at end of file diff --git a/Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0x9082_1.cs b/Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0x9082.cs similarity index 82% rename from Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0x9082_1.cs rename to Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0x9082.cs index d2eccb6a6..347990564 100644 --- a/Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0x9082_1.cs +++ b/Lagrange.Core/Internal/Packets/Service/Oidb/Request/OidbSvcTrpcTcp0x9082.cs @@ -5,21 +5,20 @@ namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request; // Resharper Disable InconsistentNaming /// -/// Group Set Reaction +/// Group add and reduce Reaction /// -[OidbSvcTrpcTcp(0x9082, 1)] [ProtoContract] -internal class OidbSvcTrpcTcp0x9082_1 +internal class OidbSvcTrpcTcp0x9082 { [ProtoMember(2)] public uint GroupUin { get; set; } - + [ProtoMember(3)] public uint Sequence { get; set; } - + [ProtoMember(4)] public string? Code { get; set; } - + [ProtoMember(5)] public bool Field5 { get; set; } // true - + [ProtoMember(6)] public bool Field6 { get; set; } // false - + [ProtoMember(7)] public bool Field7 { get; set; } // false } \ No newline at end of file diff --git a/Lagrange.Core/Internal/Service/Action/GroupSetReactionService.cs b/Lagrange.Core/Internal/Service/Action/GroupAddReactionService.cs similarity index 75% rename from Lagrange.Core/Internal/Service/Action/GroupSetReactionService.cs rename to Lagrange.Core/Internal/Service/Action/GroupAddReactionService.cs index 8ca412921..f36c8900a 100644 --- a/Lagrange.Core/Internal/Service/Action/GroupSetReactionService.cs +++ b/Lagrange.Core/Internal/Service/Action/GroupAddReactionService.cs @@ -8,14 +8,14 @@ namespace Lagrange.Core.Internal.Service.Action; -[EventSubscribe(typeof(GroupSetReactionEvent))] +[EventSubscribe(typeof(GroupAddReactionEvent))] [Service("OidbSvcTrpcTcp.0x9082_1")] -internal class GroupSetReactionService : BaseService +internal class GroupAddReactionService : BaseService { - protected override bool Build(GroupSetReactionEvent input, BotKeystore keystore, BotAppInfo appInfo, + protected override bool Build(GroupAddReactionEvent input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device, out Span output, out List>? extraPackets) { - var packet = new OidbSvcTrpcTcpBase(new OidbSvcTrpcTcp0x9082_1 + var packet = new OidbSvcTrpcTcpBase(new OidbSvcTrpcTcp0x9082 { GroupUin = input.GroupUin, Sequence = input.Sequence, @@ -23,7 +23,7 @@ protected override bool Build(GroupSetReactionEvent input, BotKeystore keystore, Field5 = true, Field6 = false, Field7 = false - }, false, true); + }, 0x9082, 1, false, true); output = packet.Serialize(); extraPackets = null; @@ -31,11 +31,11 @@ protected override bool Build(GroupSetReactionEvent input, BotKeystore keystore, } protected override bool Parse(Span input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device, - out GroupSetReactionEvent output, out List? extraEvents) + out GroupAddReactionEvent output, out List? extraEvents) { var payload = Serializer.Deserialize>(input); - - output = GroupSetReactionEvent.Result((int)payload.ErrorCode); + + output = GroupAddReactionEvent.Result((int)payload.ErrorCode); extraEvents = null; return true; } diff --git a/Lagrange.Core/Internal/Service/Action/GroupReduceReactionService.cs b/Lagrange.Core/Internal/Service/Action/GroupReduceReactionService.cs new file mode 100644 index 000000000..4e9a73acc --- /dev/null +++ b/Lagrange.Core/Internal/Service/Action/GroupReduceReactionService.cs @@ -0,0 +1,42 @@ +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(GroupReduceReactionEvent))] +[Service("OidbSvcTrpcTcp.0x9082_2")] +internal class GroupReduceReactionService : BaseService +{ + protected override bool Build(GroupReduceReactionEvent input, BotKeystore keystore, BotAppInfo appInfo, + BotDeviceInfo device, out Span output, out List>? extraPackets) + { + var packet = new OidbSvcTrpcTcpBase(new OidbSvcTrpcTcp0x9082 + { + GroupUin = input.GroupUin, + Sequence = input.Sequence, + Code = input.Code, + Field5 = true, + Field6 = false, + Field7 = false + }, 0x9082, 2, false, true); + + output = packet.Serialize(); + extraPackets = null; + return true; + } + + protected override bool Parse(Span input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device, + out GroupReduceReactionEvent output, out List? extraEvents) + { + var payload = Serializer.Deserialize>(input); + + output = GroupReduceReactionEvent.Result((int)payload.ErrorCode); + extraEvents = null; + return true; + } +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupReaction.cs b/Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupReaction.cs index 7d3a8ad01..3a2ce35ce 100644 --- a/Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupReaction.cs +++ b/Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupReaction.cs @@ -10,4 +10,6 @@ public class OneBotSetGroupReaction [JsonPropertyName("message_id")] public int MessageId { get; set; } [JsonPropertyName("code")] public required string Code { get; set; } + + [JsonPropertyName("is_add")] public bool IsAdd { get; set; } = true; } \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Operation/Group/SetGroupReactionOperation.cs b/Lagrange.OneBot/Core/Operation/Group/SetGroupReactionOperation.cs index 2c21d35c7..e8926a461 100644 --- a/Lagrange.OneBot/Core/Operation/Group/SetGroupReactionOperation.cs +++ b/Lagrange.OneBot/Core/Operation/Group/SetGroupReactionOperation.cs @@ -19,7 +19,7 @@ public async Task HandleOperation(BotContext context, JsonNode? pa { var message = (MessageChain)database.GetCollection().FindById(data.MessageId); - bool result = await context.GroupSetMessageReaction(data.GroupId, message.Sequence, data.Code); + bool result = await context.GroupSetMessageReaction(data.GroupId, message.Sequence, data.Code, data.IsAdd); return new OneBotResult(null, result ? 0 : 1, result ? "ok" : "failed"); }