Skip to content

Commit

Permalink
[All] supported reduce group reaction (#619)
Browse files Browse the repository at this point in the history
Co-authored-by: DarkRRb <[email protected]>
  • Loading branch information
DarkRRb and DarkRRb authored Oct 1, 2024
1 parent a2f1ee5 commit 2380b1b
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 28 deletions.
5 changes: 4 additions & 1 deletion Lagrange.Core/Common/Interface/Api/GroupExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ public static Task<bool> GroupSetSpecialTitle(this BotContext bot, uint groupUin
=> bot.ContextCollection.Business.OperationLogic.GroupSetSpecialTitle(groupUin, targetUin, title);

public static Task<bool> 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<bool> 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,20 @@ public async Task<bool> GroupSetSpecialTitle(uint groupUin, uint targetUin, stri
return await Collection.Business.CachingLogic.GetCachedUsers(uin, refreshCache);
}

public async Task<bool> SetMessageReaction(uint groupUin, uint sequence, string code)
public async Task<bool> 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<bool> SetNeedToConfirmSwitch(bool enableNoNeed)
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
24 changes: 24 additions & 0 deletions Lagrange.Core/Internal/Event/Action/GroupReduceReactionEvent.cs
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ namespace Lagrange.Core.Internal.Packets.Service.Oidb.Request;
// Resharper Disable InconsistentNaming

/// <summary>
/// Group Set Reaction
/// Group add and reduce Reaction
/// </summary>
[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
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,34 @@

namespace Lagrange.Core.Internal.Service.Action;

[EventSubscribe(typeof(GroupSetReactionEvent))]
[EventSubscribe(typeof(GroupAddReactionEvent))]
[Service("OidbSvcTrpcTcp.0x9082_1")]
internal class GroupSetReactionService : BaseService<GroupSetReactionEvent>
internal class GroupAddReactionService : BaseService<GroupAddReactionEvent>
{
protected override bool Build(GroupSetReactionEvent input, BotKeystore keystore, BotAppInfo appInfo,
protected override bool Build(GroupAddReactionEvent input, BotKeystore keystore, BotAppInfo appInfo,
BotDeviceInfo device, out Span<byte> output, out List<Memory<byte>>? extraPackets)
{
var packet = new OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0x9082_1>(new OidbSvcTrpcTcp0x9082_1
var packet = new OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0x9082>(new OidbSvcTrpcTcp0x9082
{
GroupUin = input.GroupUin,
Sequence = input.Sequence,
Code = input.Code,
Field5 = true,
Field6 = false,
Field7 = false
}, false, true);
}, 0x9082, 1, false, true);

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

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

output = GroupAddReactionEvent.Result((int)payload.ErrorCode);
extraEvents = null;
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GroupReduceReactionEvent>
{
protected override bool Build(GroupReduceReactionEvent input, BotKeystore keystore, BotAppInfo appInfo,
BotDeviceInfo device, out Span<byte> output, out List<Memory<byte>>? extraPackets)
{
var packet = new OidbSvcTrpcTcpBase<OidbSvcTrpcTcp0x9082>(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<byte> input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
out GroupReduceReactionEvent output, out List<ProtocolEvent>? extraEvents)
{
var payload = Serializer.Deserialize<OidbSvcTrpcTcpBase<byte[]>>(input);

output = GroupReduceReactionEvent.Result((int)payload.ErrorCode);
extraEvents = null;
return true;
}
}
2 changes: 2 additions & 0 deletions Lagrange.OneBot/Core/Entity/Action/OneBotSetGroupReaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task<OneBotResult> HandleOperation(BotContext context, JsonNode? pa
{
var message = (MessageChain)database.GetCollection<MessageRecord>().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");
}

Expand Down

0 comments on commit 2380b1b

Please sign in to comment.