Skip to content

Commit ddfa660

Browse files
committed
[Core & Milky] Implement Core's RecallC2CMessage and Milky's recall_private_message
1 parent 57ebe12 commit ddfa660

9 files changed

Lines changed: 178 additions & 10 deletions

File tree

Lagrange.Core/Common/Interface/MessageExt.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public static Task<List<BotMessage>> GetC2CMessage(this BotContext context, long
3232
public static Task<string> SendGroupFile(this BotContext context, long groupUin, Stream fileStream, string? fileName = null, string parentDirectory = "/")
3333
=> context.EventContext.GetLogic<OperationLogic>().SendGroupFile(groupUin, fileStream, fileName, parentDirectory);
3434

35-
public static Task RecallGroupMessage(this BotContext context, long groupUin, ulong sequence)
36-
=> context.EventContext.GetLogic<MessagingLogic>().RecallGroupMessage(groupUin, sequence);
35+
public static Task RecallMessage(this BotContext context, BotMessage message)
36+
=> context.EventContext.GetLogic<MessagingLogic>().RecallMessage(message);
3737

3838
public static Task<string> GroupFSDownload(this BotContext context, long groupUin, string fileId)
3939
=> context.EventContext.GetLogic<OperationLogic>().GroupFSDownload(groupUin, fileId);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Lagrange.Core.Internal.Events.Message;
2+
3+
internal class C2CRecallMsgEventReq(string targetUid, ulong sequence, ulong clientSequence, uint random, uint timestamp) : ProtocolEvent
4+
{
5+
public string TargetUid { get; } = targetUid;
6+
7+
public ulong Sequence { get; } = sequence;
8+
9+
public ulong ClientSequence { get; } = clientSequence;
10+
11+
public uint Random { get; } = random;
12+
13+
public uint Timestamp { get; } = timestamp;
14+
}
15+
16+
internal class C2CRecallMsgEventResp : ProtocolEvent;

Lagrange.Core/Internal/Logic/MessagingLogic.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Lagrange.Core.Internal.Events.Message;
44
using Lagrange.Core.Internal.Packets.Message;
55
using Lagrange.Core.Message;
6+
using Lagrange.Core.Utility.Extension;
67

78
namespace Lagrange.Core.Internal.Logic;
89

@@ -71,9 +72,25 @@ public async Task<BotMessage> SendGroupMessage(long groupUin, MessageChain chain
7172
return message;
7273
}
7374

74-
public Task RecallGroupMessage(long groupUin, ulong sequence)
75+
public Task RecallMessage(BotMessage message)
7576
{
76-
return context.EventContext.SendEvent<GroupRecallMsgEventResp>(new GroupRecallMsgEventReq(groupUin, sequence)).AsTask();
77+
return message.Contact switch
78+
{
79+
BotGroupMember member => context.EventContext.SendEvent<GroupRecallMsgEventResp>(
80+
new GroupRecallMsgEventReq(
81+
member.Group.GroupUin,
82+
message.Sequence
83+
)
84+
).AsTask(),
85+
BotFriend friend => context.EventContext.SendEvent<C2CRecallMsgEventResp>(new C2CRecallMsgEventReq(
86+
friend.Uin == context.BotUin ? message.Receiver.Uid : friend.Uid,
87+
message.Sequence,
88+
message.ClientSequence,
89+
message.Random,
90+
(uint)new DateTimeOffset(message.Time).ToUnixTimeSeconds()
91+
)).AsTask(),
92+
_ => throw new NotImplementedException(),
93+
};
7794
}
7895

7996
private async Task<BotMessage> BuildMessage(MessageChain chain, BotContact contact, BotContact receiver)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma warning disable CS8618
2+
3+
using Lagrange.Proto;
4+
5+
namespace Lagrange.Core.Internal.Packets.Message;
6+
7+
[ProtoPackable]
8+
internal partial class SsoC2CRecallMsgReq
9+
{
10+
[ProtoMember(1)] public uint Type { get; set; } // 1
11+
12+
[ProtoMember(3)] public string TargetUid { get; set; } // PeerUid in the binary
13+
14+
[ProtoMember(4)] public SsoC2CRecallMsgReqInfo Info { get; set; }
15+
16+
[ProtoMember(5)] public SsoC2CRecallMsgReqSettings Settings { get; set; }
17+
18+
[ProtoMember(6)] public bool Field6 { get; set; } // 1
19+
}
20+
21+
[ProtoPackable]
22+
internal partial class SsoC2CRecallMsgReqInfo
23+
{
24+
[ProtoMember(1)] public ulong Sequence { get; set; }
25+
26+
[ProtoMember(2)] public uint Random { get; set; }
27+
28+
[ProtoMember(3)] public ulong MessageId { get; set; } // 0x01000000 << 32 | Random
29+
30+
[ProtoMember(4)] public uint Timestamp { get; set; }
31+
32+
[ProtoMember(5)] public uint Field5 { get; set; } // 0
33+
34+
[ProtoMember(6)] public ulong ClientSequence { get; set; } // 700
35+
}
36+
37+
[ProtoPackable]
38+
internal partial class SsoC2CRecallMsgReqSettings
39+
{
40+
[ProtoMember(1)] public bool Field1 { get; set; } // 0
41+
42+
[ProtoMember(2)] public bool Field2 { get; set; } // 0
43+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Lagrange.Core.Common;
2+
using Lagrange.Core.Internal.Events;
3+
using Lagrange.Core.Internal.Events.Message;
4+
using Lagrange.Core.Internal.Packets.Message;
5+
using Lagrange.Core.Utility;
6+
7+
namespace Lagrange.Core.Internal.Services.Message;
8+
9+
[Service("trpc.msg.msg_svc.MsgService.SsoC2CRecallMsg")]
10+
[EventSubscribe<C2CRecallMsgEventReq>(Protocols.All)]
11+
internal class C2CRecallMsgService : BaseService<C2CRecallMsgEventReq, C2CRecallMsgEventResp>
12+
{
13+
protected override ValueTask<ReadOnlyMemory<byte>> Build(C2CRecallMsgEventReq input, BotContext context)
14+
{
15+
var request = new SsoC2CRecallMsgReq
16+
{
17+
Type = 1,
18+
TargetUid = input.TargetUid,
19+
Info = new SsoC2CRecallMsgReqInfo
20+
{
21+
Sequence = input.Sequence,
22+
Random = input.Random,
23+
MessageId = 0x01000000UL << 32 | input.Random,
24+
Timestamp = input.Timestamp,
25+
Field5 = 0,
26+
ClientSequence = input.ClientSequence,
27+
},
28+
Settings = new SsoC2CRecallMsgReqSettings
29+
{
30+
Field1 = false,
31+
Field2 = false
32+
},
33+
Field6 = false
34+
};
35+
36+
return ValueTask.FromResult(ProtoHelper.Serialize(request));
37+
}
38+
39+
protected override ValueTask<C2CRecallMsgEventResp> Parse(ReadOnlyMemory<byte> input, BotContext context)
40+
{
41+
return ValueTask.FromResult(new C2CRecallMsgEventResp());
42+
}
43+
}

Lagrange.Core/Internal/Services/Message/SsoGroupRecallMsgService.cs renamed to Lagrange.Core/Internal/Services/Message/GroupRecallMsgService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Lagrange.Core.Common;
2-
using Lagrange.Core.Exceptions;
32
using Lagrange.Core.Internal.Events;
43
using Lagrange.Core.Internal.Events.Message;
54
using Lagrange.Core.Internal.Packets.Message;
@@ -9,7 +8,7 @@ namespace Lagrange.Core.Internal.Services.Message;
98

109
[Service("trpc.msg.msg_svc.MsgService.SsoGroupRecallMsg")]
1110
[EventSubscribe<GroupRecallMsgEventReq>(Protocols.All)]
12-
internal class SsoGroupRecallMsgService : BaseService<GroupRecallMsgEventReq, GroupRecallMsgEventResp>
11+
internal class GroupRecallMsgService : BaseService<GroupRecallMsgEventReq, GroupRecallMsgEventResp>
1312
{
1413
protected override ValueTask<ReadOnlyMemory<byte>> Build(GroupRecallMsgEventReq input, BotContext context)
1514
{

Lagrange.Milky/Api/Handler/Message/RecallGroupMessageHandler.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
1-
21
using System.Text.Json.Serialization;
32
using Lagrange.Core;
43
using Lagrange.Core.Common.Interface;
4+
using Lagrange.Core.Message;
5+
using Lagrange.Milky.Api.Exception;
6+
using Lagrange.Milky.Cache;
57

68
namespace Lagrange.Milky.Api.Handler.Message;
79

810
[Api("recall_group_message")]
9-
public class RecallGroupMessageHandler(BotContext bot) : IEmptyResultApiHandler<RecallGroupMessageParameter>
11+
public class RecallGroupMessageHandler(BotContext bot, MessageCache cache) : IEmptyResultApiHandler<RecallGroupMessageParameter>
1012
{
1113
private readonly BotContext _bot = bot;
14+
private readonly MessageCache _cache = cache;
1215

13-
public Task HandleAsync(RecallGroupMessageParameter parameter, CancellationToken token)
16+
public async Task HandleAsync(RecallGroupMessageParameter parameter, CancellationToken token)
1417
{
15-
return _bot.RecallGroupMessage(parameter.GroupId, (ulong)parameter.MessageSeq);
18+
var message = await _cache.GetMessageAsync(
19+
MessageType.Group,
20+
parameter.GroupId,
21+
(ulong)parameter.MessageSeq,
22+
token
23+
);
24+
if (message == null) throw new ApiException(-2, "message not found");
25+
await _bot.RecallMessage(message);
1626
}
1727
}
1828

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Text.Json.Serialization;
2+
using Lagrange.Core;
3+
using Lagrange.Core.Common.Interface;
4+
using Lagrange.Core.Message;
5+
using Lagrange.Milky.Api.Exception;
6+
using Lagrange.Milky.Cache;
7+
8+
namespace Lagrange.Milky.Api.Handler.Message;
9+
10+
[Api("recall_private_message")]
11+
public class RecallPrivateMessageHandler(BotContext bot, MessageCache cache) : IEmptyResultApiHandler<RecallPrivateMessageParameter>
12+
{
13+
private readonly BotContext _bot = bot;
14+
private readonly MessageCache _cache = cache;
15+
16+
public async Task HandleAsync(RecallPrivateMessageParameter parameter, CancellationToken token)
17+
{
18+
var message = await _cache.GetMessageAsync(
19+
MessageType.Private,
20+
parameter.UserId,
21+
(ulong)parameter.MessageSeq,
22+
token
23+
);
24+
if (message == null) throw new ApiException(-2, "message not found");
25+
await _bot.RecallMessage(message);
26+
}
27+
}
28+
29+
public class RecallPrivateMessageParameter(long userId, long messageSeq)
30+
{
31+
[JsonRequired]
32+
[JsonPropertyName("user_id")]
33+
public long UserId { get; init; } = userId;
34+
35+
[JsonRequired]
36+
[JsonPropertyName("message_seq")]
37+
public long MessageSeq { get; init; } = messageSeq;
38+
}

Lagrange.Milky/Utility/JsonUtility.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ public static partial class JsonUtility
7878
// get_resource_temp_url
7979
[JsonSerializable(typeof(GetResourceTempUrlParameter))]
8080
[JsonSerializable(typeof(GetResourceTempUrlResult))]
81+
// recall_private_message
82+
[JsonSerializable(typeof(RecallPrivateMessageParameter))]
8183
// recall_group_message
8284
[JsonSerializable(typeof(RecallGroupMessageParameter))]
8385
// == friend ==

0 commit comments

Comments
 (0)