Skip to content

Commit 097b0c0

Browse files
[Core] Implemented group reaction related api (LagrangeDev#45)
Co-authored-by: Linwenxuan04 <llwwxxllwwxx@gmail.com>
1 parent cb1d0ca commit 097b0c0

13 files changed

Lines changed: 217 additions & 30 deletions

File tree

Lagrange.Core/Common/Interface/OperationExt.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,7 @@ public static Task<BotStranger> FetchStranger(this BotContext context, long uin)
3838

3939
public static Task SetGroupNotification(this BotContext context, long groupUin, ulong sequence, BotGroupNotificationType type, bool isFiltered, GroupNotificationOperate operate, string message = "") =>
4040
context.EventContext.GetLogic<OperationLogic>().SetGroupNotification(groupUin, sequence, type, isFiltered, operate, message);
41+
42+
public static Task SetGroupReaction(this BotContext context, long groupUin, ulong sequence, string code, bool isAdd) =>
43+
context.EventContext.GetLogic<OperationLogic>().SetGroupReaction(groupUin, sequence, code, isAdd);
4144
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace Lagrange.Core.Events.EventArgs;
2+
3+
public class BotGroupReactionEvent(long targetGroupUin, ulong targetSequence, long operatorUin, bool isAdd, string code, ulong currentCount) : EventBase
4+
{
5+
public long TargetGroupUin { get; } = targetGroupUin;
6+
7+
public ulong TargetSequence { get; } = targetSequence;
8+
9+
public long OperatorUin { get; } = operatorUin;
10+
11+
public bool IsAdd { get; } = isAdd;
12+
13+
public string Code { get; } = code;
14+
15+
public ulong CurrentCount { get; } = currentCount;
16+
17+
public override string ToEventMessage()
18+
{
19+
return $"{nameof(BotGroupReactionEvent)}: Target {TargetGroupUin} > {TargetSequence} Operator {OperatorUin} {(IsAdd ? "Add" : "Reduce")} {Code} Current {CurrentCount}";
20+
}
21+
}

Lagrange.Core/Internal/Context/EventContext.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ namespace Lagrange.Core.Internal.Context;
1313
internal class EventContext : IDisposable
1414
{
1515
private const string Tag = nameof(EventContext);
16-
16+
1717
private readonly BotContext _context;
18-
18+
1919
private readonly FrozenDictionary<Type, List<ILogic>> _events;
2020

2121
private readonly FrozenDictionary<Type, ILogic> _logics;
@@ -27,7 +27,7 @@ public EventContext(BotContext context)
2727
{
2828
var events = new Dictionary<Type, List<ILogic>>();
2929
var logics = new Dictionary<Type, ILogic>();
30-
30+
3131
foreach (var type in typeof(ILogic).Assembly.GetTypes())
3232
{
3333
if (type.HasImplemented<ILogic>() && Activator.CreateInstance(type, context) is ILogic instance)
@@ -39,30 +39,30 @@ public EventContext(BotContext context)
3939
list = [];
4040
events.Add(@event.EventType, list);
4141
}
42-
42+
4343
list.Add(instance);
4444
}
45-
45+
4646
logics[type] = instance;
4747
}
4848
}
49-
49+
5050
_context = context;
5151
_events = events.ToFrozenDictionary();
5252
_logics = logics.ToFrozenDictionary();
5353
}
54-
54+
5555
public async ValueTask<T> SendEvent<T>(ProtocolEvent @event) where T : ProtocolEvent
5656
{
5757
try
5858
{
5959
await HandleOutgoingEvent(@event);
60-
var (frame, attribute) = await _context.ServiceContext.Resolve(@event);
60+
var (frame, attribute) = await _context.ServiceContext.Resolve(@event);
6161
if (frame.Sequence == 0) throw new LagrangeException("The sequence number is 0 for the SSOFrame");
62-
62+
6363
var @return = await _context.PacketContext.SendPacket(frame, attribute);
6464
var resolved = await _context.ServiceContext.Resolve(@return);
65-
65+
6666
if (resolved is T result)
6767
{
6868
await HandleIncomingEvent(result);
@@ -76,7 +76,7 @@ public async ValueTask<T> SendEvent<T>(ProtocolEvent @event) where T : ProtocolE
7676
throw new LagrangeException("An error occurred while sending the event", e);
7777
}
7878
}
79-
79+
8080
private async ValueTask HandleIncomingEvent(ProtocolEvent @event)
8181
{
8282
if (_events.TryGetValue(@event.GetType(), out var logics))
@@ -112,7 +112,7 @@ private async ValueTask HandleOutgoingEvent(ProtocolEvent @event)
112112
}
113113
}
114114
}
115-
115+
116116
public async ValueTask HandleOutgoingEvent(EventBase @event)
117117
{
118118
if (_events.TryGetValue(@event.GetType(), out var logics))
@@ -142,8 +142,12 @@ public async Task HandleServerPacket(SsoPacket packet)
142142
{
143143
_context.LogWarning(Tag, "Service not found for command: {0}", e, e.Command);
144144
}
145+
catch (Exception e)
146+
{
147+
_context.LogError(Tag, "Handle {0} server packet failed", e, packet.Command);
148+
}
145149
}
146-
150+
147151
public T GetLogic<T>() where T : ILogic => (T)_logics[typeof(T)];
148152

149153
public void Dispose()

Lagrange.Core/Internal/Context/PacketContext.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public ValueTask<SsoPacket> SendPacket(SsoPacket packet, ServiceAttribute option
4242
Task.Run(async () => // Schedule the task to the ThreadPool
4343
{
4444
ReadOnlyMemory<byte> frame;
45-
45+
4646
switch (options.RequestType)
4747
{
4848
case RequestType.D2Auth:
@@ -72,18 +72,18 @@ public ValueTask<SsoPacket> SendPacket(SsoPacket packet, ServiceAttribute option
7272
throw new InvalidOperationException($"Unknown RequestType: {options.RequestType}");
7373
}
7474
}
75-
75+
7676
await _context.SocketContext.Send(frame);
7777
});
78-
78+
7979
return new ValueTask<SsoPacket>(tcs, 0);
8080
}
8181

8282
public void DispatchPacket(ReadOnlySpan<byte> buffer)
8383
{
8484
var service = _servicePacker.Parse(buffer);
8585
var sso = _ssoPacker.Parse(service);
86-
86+
8787
if (_pendingTasks.TryRemove(sso.Sequence, out var tcs))
8888
{
8989
if (sso is { RetCode: not 0, Extra: var extra })
@@ -98,7 +98,7 @@ public void DispatchPacket(ReadOnlySpan<byte> buffer)
9898
}
9999
else
100100
{
101-
_ = _context.EventContext.HandleServerPacket(sso);
101+
Task.Run(() => _context.EventContext.HandleServerPacket(sso));
102102
}
103103
}
104-
}
104+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Lagrange.Core.Internal.Events.System;
2+
3+
internal class AddGroupReactionEventReq(long groupUin, ulong sequence, string code) : ProtocolEvent
4+
{
5+
public long GroupUin { get; } = groupUin;
6+
7+
public ulong Sequence { get; } = sequence;
8+
9+
public string Code { get; } = code;
10+
}
11+
12+
internal class AddGroupReactionEventResp : ProtocolEvent
13+
{
14+
public static readonly AddGroupReactionEventResp Default = new();
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Lagrange.Core.Internal.Events.System;
2+
3+
internal class ReduceGroupReactionEventReq(long groupUin, ulong sequence, string code) : ProtocolEvent
4+
{
5+
public long GroupUin { get; } = groupUin;
6+
7+
public ulong Sequence { get; } = sequence;
8+
9+
public string Code { get; } = code;
10+
}
11+
12+
internal class ReduceGroupReactionEventResp : ProtocolEvent
13+
{
14+
public static readonly ReduceGroupReactionEventResp Default = new();
15+
}

Lagrange.Core/Internal/Logic/OperationLogic.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,14 @@ await context.EventContext.SendEvent<SetGroupNotificationEventResp>(
274274
);
275275
}
276276
}
277+
278+
public async Task SetGroupReaction(long groupUin, ulong sequence, string code, bool isAdd)
279+
{
280+
if (isAdd) await context.EventContext.SendEvent<AddGroupReactionEventResp>(
281+
new AddGroupReactionEventReq(groupUin, sequence, code)
282+
);
283+
else await context.EventContext.SendEvent<ReduceGroupReactionEventResp>(
284+
new ReduceGroupReactionEventReq(groupUin, sequence, code)
285+
);
286+
}
277287
}

Lagrange.Core/Internal/Logic/PushLogic.cs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public async ValueTask Incoming(ProtocolEvent e)
2727
case Type.TempMessage:
2828
{
2929
var message = await context.EventContext.GetLogic<MessagingLogic>().Parse(messageEvent.MsgPush.CommonMessage);
30-
if (message.Entities[0] is LightAppEntity {AppName: "com.tencent.qun.invite"} || message.Entities[0] is LightAppEntity {AppName: "com.tencent.tuwen.lua"})
30+
if (message.Entities[0] is LightAppEntity { AppName: "com.tencent.qun.invite" } || message.Entities[0] is LightAppEntity { AppName: "com.tencent.tuwen.lua" })
3131
{
3232
var app = (LightAppEntity)message.Entities[0];
3333
using var document = JsonDocument.Parse(app.Payload);
@@ -152,8 +152,8 @@ public async ValueTask Incoming(ProtocolEvent e)
152152
var pkgType210 = (Event0x210SubType)messageEvent.MsgPush.CommonMessage.ContentHead.SubType;
153153
switch (pkgType210)
154154
{
155-
case Event0x210SubType.FriendRequestNotice
156-
when messageEvent.MsgPush.CommonMessage.MessageBody.MsgContent is { } content:
155+
case Event0x210SubType.FriendRequestNotice when messageEvent.MsgPush.CommonMessage.MessageBody.MsgContent is { } content:
156+
{
157157
var friendRequest = ProtoHelper.Deserialize<FriendRequest>(content.Span);
158158
context.EventInvoker.PostEvent(new BotFriendRequestEvent(
159159
friendRequest.Info!.SourceUid,
@@ -162,6 +162,12 @@ public async ValueTask Incoming(ProtocolEvent e)
162162
friendRequest.Info.Source ?? string.Empty
163163
));
164164
break;
165+
}
166+
default:
167+
{
168+
context.LogWarning(nameof(PushLogic), "Unknown 0x210 sub type: {0}", null, pkgType210);
169+
break;
170+
}
165171
}
166172

167173
break;
@@ -195,6 +201,40 @@ public async ValueTask Incoming(ProtocolEvent e)
195201
}
196202
break;
197203
}
204+
case Event0x2DCSubType.SubType16 when messageEvent.MsgPush.CommonMessage.MessageBody.MsgContent is { } content:
205+
{
206+
var reader = new BinaryPacket(content);
207+
// group uin and 1 byte
208+
reader.Skip(4 + 1);
209+
var proto = reader.ReadBytes(Prefix.Int16 | Prefix.LengthOnly);
210+
var body = ProtoHelper.Deserialize<NotifyMessageBody>(proto);
211+
212+
switch ((Event0x2DCSubType16SubType)body.SubType)
213+
{
214+
case Event0x2DCSubType16SubType.GroupReactionNotice:
215+
{
216+
var reaction = body.Reaction.Data.Data;
217+
218+
long @operator = context.CacheContext.ResolveUin(reaction.Data.OperatorUid);
219+
220+
context.EventInvoker.PostEvent(new BotGroupReactionEvent(
221+
body.GroupUin,
222+
reaction.Target.Sequence,
223+
@operator,
224+
reaction.Data.Type == 1,
225+
reaction.Data.Code,
226+
reaction.Data.CurrentCount
227+
));
228+
break;
229+
}
230+
default:
231+
{
232+
context.LogWarning(nameof(PushLogic), "Unknown 0x2DCSub16 sub type: {0}", null, body.SubType);
233+
break;
234+
}
235+
}
236+
break;
237+
}
198238
default:
199239
{
200240
context.LogWarning(nameof(PushLogic), "Unknown 0x2DC sub type: {0}", null, pkgType);
@@ -244,14 +284,11 @@ private enum Event0x2DCSubType
244284
GroupGreyTipNotice20 = 20,
245285
}
246286

247-
private enum Event0x2DCSubType16Field13
287+
private enum Event0x2DCSubType16SubType
248288
{
249-
GroupMemberSpecialTitleNotice = 6,
250-
GroupNameChangeNotice = 12,
251-
GroupTodoNotice = 23,
252289
GroupReactionNotice = 35,
253290
}
254-
291+
255292
private enum Event0x210SubType
256293
{
257294
FriendRequestNotice = 35,

Lagrange.Core/Internal/Packets/Notify/GroupReaction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ internal partial class GroupReactionData3
3535
{
3636
[ProtoMember(1)] public string Code { get; set; }
3737

38-
[ProtoMember(3)] public uint Count { get; set; }
38+
[ProtoMember(3)] public uint CurrentCount { get; set; }
3939

4040
[ProtoMember(4)] public string OperatorUid { get; set; }
4141

Lagrange.Core/Internal/Packets/Notify/NotifyMessageBody.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ namespace Lagrange.Core.Internal.Packets.Notify;
77
[ProtoPackable]
88
internal partial class NotifyMessageBody
99
{
10-
[ProtoMember(1)] public uint Type { get; set; }
10+
[ProtoMember(1)] public uint NotifyType { get; set; }
1111

1212
[ProtoMember(4)] public long GroupUin { get; set; }
1313

1414
[ProtoMember(5)] public byte[]? EventParam { get; set; }
1515

1616
[ProtoMember(11)] public GroupRecall Recall { get; set; }
1717

18-
[ProtoMember(13)] public uint? Field13 { get; set; }
18+
[ProtoMember(13)] public uint SubType { get; set; }
1919

2020
[ProtoMember(21)] public string OperatorUid { get; set; }
2121

0 commit comments

Comments
 (0)