Skip to content

Commit 47086ba

Browse files
committed
[Core & Milky] Implement Core's BotGroupRecallEvent and Milky's partial message_recall
1 parent df0467c commit 47086ba

7 files changed

Lines changed: 143 additions & 18 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Lagrange.Core.Events.EventArgs;
2+
3+
public class BotGroupRecallEvent(long groupUin, ulong sequence, long authorUin, long operatorUin, string tip) : EventBase
4+
{
5+
public long GroupUin { get; } = groupUin;
6+
7+
public ulong Sequence { get; } = sequence;
8+
9+
public long AuthorUin { get; } = authorUin;
10+
11+
public long OperatorUin { get; } = operatorUin;
12+
13+
public string Tip { get; } = tip;
14+
15+
public override string ToEventMessage()
16+
{
17+
return $"{nameof(BotGroupRecallEvent)}: ${OperatorUin} recalled {GroupUin}-{Sequence} of {AuthorUin}";
18+
}
19+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Lagrange.Core.Events.EventArgs;
2+
using Lagrange.Core.Internal.Events.Message;
3+
using Lagrange.Core.Internal.Packets.Notify;
4+
using Lagrange.Core.Utility;
5+
using Lagrange.Core.Utility.Binary;
6+
7+
namespace Lagrange.Core.Internal.Logic.MsgPushProccessors;
8+
9+
[MsgPushProcessor(MsgType.Event0x2DC, 17, true)]
10+
internal class GroupRecallMessageProcessor : MsgPushProcessorBase
11+
{
12+
internal override ValueTask<bool> Handle(BotContext context, MsgType msgType, int subType, PushMessageEvent msgEvt, ReadOnlyMemory<byte>? content)
13+
{
14+
var packet = new BinaryPacket(content!.Value.Span);
15+
// group uin and 1 byte
16+
packet.Skip(4 + 1);
17+
18+
var notify = ProtoHelper.Deserialize<NotifyMessageBody>(packet.ReadBytes(Prefix.Int16 | Prefix.LengthOnly));
19+
foreach (var message in notify.Recall.RecallMessages)
20+
{
21+
var @event = new BotGroupRecallEvent(
22+
notify.GroupUin,
23+
message.Sequence,
24+
context.CacheContext.ResolveUin(message.AuthorUid),
25+
notify.Recall.OperatorUid != null ? context.CacheContext.ResolveUin(notify.Recall.OperatorUid) : 0,
26+
notify.Recall.TipInfo?.Tip ?? string.Empty
27+
);
28+
context.EventInvoker.PostEvent(@event);
29+
}
30+
31+
return ValueTask.FromResult(true);
32+
}
33+
}

Lagrange.Core/Internal/Logic/PushLogic.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,26 @@ namespace Lagrange.Core.Internal.Logic;
2222
internal class PushLogic : ILogic
2323
{
2424

25-
private BotContext context;
26-
25+
private readonly BotContext _context;
26+
2727
private readonly FrozenDictionary<MsgMatchKey, List<MsgPushProcessorBase>> _processors;
2828

2929
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "All the types are preserved in the csproj by using the TrimmerRootAssembly attribute")]
3030
[UnconditionalSuppressMessage("Trimming", "IL2062", Justification = "All the types are preserved in the csproj by using the TrimmerRootAssembly attribute")]
3131
[UnconditionalSuppressMessage("Trimming", "IL2072", Justification = "All the types are preserved in the csproj by using the TrimmerRootAssembly attribute")]
3232
public PushLogic(BotContext ctx)
3333
{
34-
context = ctx;
35-
34+
_context = ctx;
35+
3636
var handlers = new Dictionary<MsgMatchKey, List<MsgPushProcessorBase>>();
3737
foreach (var type in typeof(MsgPushProcessorBase).Assembly.GetTypes())
3838
{
3939
if (!type.HasImplemented<MsgPushProcessorBase>() ||
4040
Activator.CreateInstance(type) is not MsgPushProcessorBase instance)
4141
continue;
42-
42+
4343
var attributes = type.GetCustomAttributes<MsgPushProcessorAttribute>();
44-
44+
4545
foreach (var attribute in attributes)
4646
{
4747
var msgType = new MsgMatchKey(attribute.MsgType, attribute.SubType, attribute.RequireContent);
@@ -50,13 +50,13 @@ public PushLogic(BotContext ctx)
5050
set = [];
5151
handlers[msgType] = set;
5252
}
53-
53+
5454
set.Add(instance);
5555
}
5656
}
5757
_processors = handlers.ToFrozenDictionary();
5858
}
59-
59+
6060
public async ValueTask Incoming(ProtocolEvent e)
6161
{
6262
if (e is not PushMessageEvent msgEvt) return;
@@ -69,16 +69,16 @@ public async ValueTask Incoming(ProtocolEvent e)
6969
{
7070
foreach (var handler in handlers)
7171
{
72-
if (await handler.Handle(context, msgType, subType, msgEvt, content))
72+
if (await handler.Handle(_context, msgType, subType, msgEvt, content))
7373
return;
7474
}
75-
}
76-
75+
}
76+
7777
if (_processors.TryGetValue(new MsgMatchKey(msgType, -1, hasContent), out handlers))
7878
{
7979
foreach (var handler in handlers)
8080
{
81-
if (await handler.Handle(context, msgType, subType, msgEvt, content))
81+
if (await handler.Handle(_context, msgType, subType, msgEvt, content))
8282
return;
8383
}
8484
}
@@ -111,10 +111,10 @@ internal class MsgPushProcessorAttribute : Attribute
111111
public MsgType MsgType { get; init; }
112112
public int SubType { get; }
113113
public bool RequireContent { get; } = false;
114-
114+
115115
public MsgPushProcessorAttribute(MsgType msgType, bool requireContent = false)
116116
: this(msgType, -1, requireContent) { }
117-
117+
118118
public MsgPushProcessorAttribute(MsgType msgType, int subType, bool requireContent = false)
119119
{
120120
MsgType = msgType;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace Lagrange.Milky.Entity.Event;
4+
5+
public class MessageRecallEvent(long time, long selfId, MessageRecallEventData data) : EventBase<MessageRecallEventData>(time, selfId, "message_recall", data) { }
6+
7+
public class MessageRecallEventData(string messageScene, long peerId, long messageSeq, long senderId, long operatorId, string displaySuffix)
8+
{
9+
[JsonPropertyName("message_scene")]
10+
public string MessageScene { get; } = messageScene;
11+
12+
[JsonPropertyName("peer_id")]
13+
public long PeerId { get; } = peerId;
14+
15+
[JsonPropertyName("message_seq")]
16+
public long MessageSeq { get; } = messageSeq;
17+
18+
[JsonPropertyName("sender_id")]
19+
public long SenderId { get; } = senderId;
20+
21+
[JsonPropertyName("operator_id")]
22+
public long OperatorId { get; } = operatorId;
23+
24+
[JsonPropertyName("display_suffix")]
25+
public string DisplaySuffix { get; } = displaySuffix;
26+
}

Lagrange.Milky/Event/EventService.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public Task StartAsync(CancellationToken token)
3232
_bot.EventInvoker.RegisterEvent<LgrEvents.BotGroupMemberIncreaseEvent>(HandleGroupMemberIncreaseEvent);
3333
_bot.EventInvoker.RegisterEvent<LgrEvents.BotGroupMemberDecreaseEvent>(HandleGroupMemberDecreaseEvent);
3434
_bot.EventInvoker.RegisterEvent<LgrEvents.BotFriendRequestEvent>(HandleFriendRequestEvent);
35+
_bot.EventInvoker.RegisterEvent<LgrEvents.BotGroupRecallEvent>(HandleGroupRecallEvent);
3536

3637
return Task.CompletedTask;
3738
}
@@ -150,7 +151,7 @@ private void HandleGroupMemberIncreaseEvent(BotContext bot, LgrEvents.BotGroupMe
150151
_logger.LogHandleEventException(nameof(LgrEvents.BotGroupMemberIncreaseEvent), e);
151152
}
152153
}
153-
154+
154155
private void HandleGroupMemberDecreaseEvent(BotContext bot, LgrEvents.BotGroupMemberDecreaseEvent @event)
155156
{
156157
try
@@ -180,7 +181,7 @@ private void HandleFriendRequestEvent(BotContext bot, LgrEvents.BotFriendRequest
180181
{
181182
try
182183
{
183-
_logger.LogBotFriendRequestEvent(
184+
_logger.LogFriendRequestEvent(
184185
@event.InitiatorUid,
185186
@event.InitiatorUin,
186187
@event.Message,
@@ -202,13 +203,41 @@ private void HandleFriendRequestEvent(BotContext bot, LgrEvents.BotFriendRequest
202203
}
203204
}
204205

206+
private void HandleGroupRecallEvent(BotContext bot, LgrEvents.BotGroupRecallEvent @event)
207+
{
208+
try
209+
{
210+
_logger.LogGroupRecallEvent(
211+
@event.GroupUin,
212+
@event.Sequence,
213+
@event.AuthorUin,
214+
@event.OperatorUin,
215+
@event.Tip
216+
);
217+
var result = _convert.MessageRecallEvent(@event);
218+
byte[] bytes = JsonUtility.SerializeToUtf8Bytes(result.GetType(), result);
219+
using (_lock.UsingReadLock())
220+
{
221+
foreach (var handler in _handlers)
222+
{
223+
handler(bytes);
224+
}
225+
}
226+
}
227+
catch (Exception e)
228+
{
229+
_logger.LogHandleEventException(nameof(LgrEvents.BotGroupRecallEvent), e);
230+
}
231+
}
232+
205233
public Task StopAsync(CancellationToken token)
206234
{
207235
_bot.EventInvoker.UnregisterEvent<LgrEvents.BotMessageEvent>(HandleMessageEvent);
208236
_bot.EventInvoker.UnregisterEvent<LgrEvents.BotMessageEvent>(HandleMessageEvent);
209237
_bot.EventInvoker.UnregisterEvent<LgrEvents.BotGroupNudgeEvent>(HandleGroupNudgeEvent);
210238
_bot.EventInvoker.UnregisterEvent<LgrEvents.BotGroupMemberDecreaseEvent>(HandleGroupMemberDecreaseEvent);
211239
_bot.EventInvoker.UnregisterEvent<LgrEvents.BotFriendRequestEvent>(HandleFriendRequestEvent);
240+
_bot.EventInvoker.UnregisterEvent<LgrEvents.BotGroupRecallEvent>(HandleGroupRecallEvent);
212241

213242
return Task.CompletedTask;
214243
}
@@ -248,14 +277,17 @@ public static partial class EventServiceLoggerExtension
248277
public static partial void LogGroupMemberDecreaseEvent(this ILogger<EventService> logger, long group, long user, long? @operator);
249278

250279
[LoggerMessage(EventId = 5, Level = LogLevel.Debug, Message = "BotFriendRequestEvent {{ request: {request}, user: {user}, message: {message}, source: {source} }}")]
251-
public static partial void LogBotFriendRequestEvent(this ILogger<EventService> logger, string request, long user, string? message, string? source);
280+
public static partial void LogFriendRequestEvent(this ILogger<EventService> logger, string request, long user, string? message, string? source);
252281

253282
[LoggerMessage(EventId = 6, Level = LogLevel.Debug, Message = "BotGroupInviteEvent {{ request: {request}, user: {user}, group: {group} }}")]
254283
public static partial void LogGroupInvitationEvent(this ILogger<EventService> logger, long request, long user, long group);
255284

256285
[LoggerMessage(EventId = 7, Level = LogLevel.Debug, Message = "BotGroupMemberIncreaseEvent {{ group: {group}, user: {user}, operator: {operator}, invitor: {invitor} }}")]
257286
public static partial void LogGroupMemberIncreaseEvent(this ILogger<EventService> logger, long group, long user, long? @operator, long? invitor);
258287

288+
[LoggerMessage(EventId = 8, Level = LogLevel.Debug, Message = "GroupRecallEvent {{ group: {group}, sequence: {sequence}, author: {author}, operator: {operator}, tip: {tip} }}")]
289+
public static partial void LogGroupRecallEvent(this ILogger<EventService> logger, long group, ulong sequence, long author, long @operator, string tip);
290+
259291
[LoggerMessage(EventId = 999, Level = LogLevel.Error, Message = "Handle {event} exception")]
260292
public static partial void LogHandleEventException(this ILogger<EventService> logger, string @event, Exception e);
261293
}

Lagrange.Milky/Utility/EntityConvert.Event.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public partial class EntityConvert
3434
@event.InvitorUin
3535
)
3636
);
37-
37+
3838
public GroupMemberDecreaseEvent GroupMemberDecreaseEvent(LgrEventArgs.BotGroupMemberDecreaseEvent @event) => new(
3939
@event.EventTime.ToUnixTimeSeconds(),
4040
_bot.BotUin,
@@ -55,4 +55,17 @@ public partial class EntityConvert
5555
@event.Source
5656
)
5757
);
58+
59+
public MessageRecallEvent MessageRecallEvent(LgrEventArgs.BotGroupRecallEvent @event) => new(
60+
@event.EventTime.ToUnixTimeSeconds(),
61+
_bot.BotUin,
62+
new MessageRecallEventData(
63+
"group",
64+
@event.GroupUin,
65+
(long)@event.Sequence,
66+
@event.AuthorUin,
67+
@event.OperatorUin,
68+
@event.Tip
69+
)
70+
);
5871
}

Lagrange.Milky/Utility/JsonUtility.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ public static partial class JsonUtility
128128
[JsonSerializable(typeof(FriendRequestEvent))]
129129
// group_invitation
130130
[JsonSerializable(typeof(GroupInvitationEvent))]
131+
// message_recall
132+
[JsonSerializable(typeof(MessageRecallEvent))]
131133
private partial class JsonContext : JsonSerializerContext;
132134

133135
public static string Serialize<T>(T value) where T : class

0 commit comments

Comments
 (0)