Skip to content

Commit ba8fe02

Browse files
authored
[Core] Implemented filtered group notification related API (#36)
1 parent 323e8f1 commit ba8fe02

11 files changed

Lines changed: 218 additions & 21 deletions

Lagrange.Core/Common/Entity/BotGroupInviteNotification.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Lagrange.Core.Common.Entity;
22

3-
public class BotGroupInviteNotification(long group, ulong sequence, long targetUin, string targetUid, BotGroupNotificationState state, long? operatorUin, string? operatorUid, long inviterUin, string inviterUid) : BotGroupNotificationBase(group, sequence, BotGroupNotificationType.Invite, targetUin, targetUid)
3+
public class BotGroupInviteNotification(long group, ulong sequence, long targetUin, string targetUid, BotGroupNotificationState state, long? operatorUin, string? operatorUid, long inviterUin, string inviterUid, bool isFiltered) : BotGroupNotificationBase(group, sequence, BotGroupNotificationType.Invite, targetUin, targetUid)
44
{
55
public BotGroupNotificationState State { get; } = state;
66

@@ -11,4 +11,6 @@ public class BotGroupInviteNotification(long group, ulong sequence, long targetU
1111
public long InviterUin { get; } = inviterUin;
1212

1313
public string InviterUid { get; } = inviterUid;
14+
15+
public bool IsFiltered { get; } = isFiltered;
1416
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Lagrange.Core.Common.Entity;
22

3-
public class BotGroupJoinNotification(long group, ulong sequence, long targetUin, string targetUid, BotGroupNotificationState state, long? operatorUin, string? operatorUid, string comment) : BotGroupNotificationBase(group, sequence, BotGroupNotificationType.Join, targetUin, targetUid)
3+
public class BotGroupJoinNotification(long group, ulong sequence, long targetUin, string targetUid, BotGroupNotificationState state, long? operatorUin, string? operatorUid, string comment, bool isFiltered) : BotGroupNotificationBase(group, sequence, BotGroupNotificationType.Join, targetUin, targetUid)
44
{
55
public BotGroupNotificationState State { get; } = state;
66

@@ -9,4 +9,6 @@ public class BotGroupJoinNotification(long group, ulong sequence, long targetUin
99
public string? OperatorUid { get; } = operatorUid;
1010

1111
public string Comment { get; } = comment;
12+
13+
public bool IsFiltered { get; } = isFiltered;
1214
}

Lagrange.Core/Common/Interface/OperationExt.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,15 @@ public static Task<List<BotGroup>> FetchGroups(this BotContext context, bool ref
2727
public static Task<List<BotGroupMember>> FetchMembers(this BotContext context, long groupUin, bool refresh = false) =>
2828
context.CacheContext.GetMemberList(groupUin, refresh);
2929

30-
public static Task<List<BotGroupNotificationBase>> FetchGroupNotifications(this BotContext context, ulong count) =>
31-
context.EventContext.GetLogic<OperationLogic>().FetchGroupNotifications(count);
30+
public static Task<List<BotGroupNotificationBase>> FetchGroupNotifications(this BotContext context, ulong count, ulong start = 0) =>
31+
context.EventContext.GetLogic<OperationLogic>().FetchGroupNotifications(count, start);
32+
33+
public static Task<List<BotGroupNotificationBase>> FetchFilteredGroupNotifications(this BotContext context, ulong count, ulong start = 0) =>
34+
context.EventContext.GetLogic<OperationLogic>().FetchFilteredGroupNotifications(count, start);
3235

3336
public static Task<BotStranger> FetchStranger(this BotContext context, long uin) =>
3437
context.EventContext.GetLogic<OperationLogic>().FetchStranger(uin);
3538

36-
public static Task SetGroupNotification(this BotContext context, long groupUin, ulong sequence, BotGroupNotificationType type, GroupNotificationOperate operate, string message = "") =>
37-
context.EventContext.GetLogic<OperationLogic>().SetGroupNotification(groupUin, sequence, type, operate, message);
39+
public static Task SetGroupNotification(this BotContext context, long groupUin, ulong sequence, BotGroupNotificationType type, bool isFiltered, GroupNotificationOperate operate, string message = "") =>
40+
context.EventContext.GetLogic<OperationLogic>().SetGroupNotification(groupUin, sequence, type, isFiltered, operate, message);
3841
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Lagrange.Core.Common.Entity;
3+
4+
namespace Lagrange.Core.Internal.Events.System;
5+
6+
internal class FetchFilteredGroupNotificationsEventReq(ulong count, ulong start = 0) : ProtocolEvent
7+
{
8+
public ulong Count { get; } = count;
9+
10+
public ulong Start { get; } = start;
11+
}
12+
13+
internal class FetchFilteredGroupNotificationsEventResp(List<BotGroupNotificationBase> groupNotifications) : ProtocolEvent
14+
{
15+
public List<BotGroupNotificationBase> GroupNotifications { get; } = groupNotifications;
16+
}

Lagrange.Core/Internal/Events/System/FetchGroupNotificationsEvent.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
namespace Lagrange.Core.Internal.Events.System;
55

6-
internal class FetchGroupNotificationsEventReq(ulong count) : ProtocolEvent
6+
internal class FetchGroupNotificationsEventReq(ulong count, ulong start = 0) : ProtocolEvent
77
{
88
public ulong Count { get; } = count;
9+
10+
public ulong Start { get; } = start;
911
}
1012

1113
internal class FetchGroupNotificationsEventResp(List<BotGroupNotificationBase> groupNotifications) : ProtocolEvent
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Lagrange.Core.Common.Entity;
2+
3+
namespace Lagrange.Core.Internal.Events.System;
4+
5+
internal class SetFilteredGroupNotificationEventReq(long groupUin, ulong sequence, BotGroupNotificationType type, GroupNotificationOperate operate, string message) : ProtocolEvent
6+
{
7+
public long GroupUin { get; } = groupUin;
8+
9+
public ulong Sequence { get; } = sequence;
10+
11+
public BotGroupNotificationType Type { get; } = type;
12+
13+
public GroupNotificationOperate Operate { get; } = operate;
14+
15+
public string Message { get; } = message;
16+
}
17+
18+
internal class SetFilteredGroupNotificationEventResp : ProtocolEvent
19+
{
20+
public static readonly SetFilteredGroupNotificationEventResp Default = new();
21+
}

Lagrange.Core/Internal/Logic/OperationLogic.cs

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,28 +226,52 @@ public async Task<string> SendGroupFile(long groupUin, Stream fileStream, string
226226
return uploadResp.FileId;
227227
}
228228

229-
public async Task<List<BotGroupNotificationBase>> FetchGroupNotifications(ulong count)
229+
public async Task<List<BotGroupNotificationBase>> FetchGroupNotifications(ulong count, ulong start)
230230
{
231-
var req = new FetchGroupNotificationsEventReq(count);
231+
var req = new FetchGroupNotificationsEventReq(count, start);
232232
var resp = await context.EventContext.SendEvent<FetchGroupNotificationsEventResp>(req);
233233
return resp.GroupNotifications;
234234
}
235235

236+
public async Task<List<BotGroupNotificationBase>> FetchFilteredGroupNotifications(ulong count, ulong start)
237+
{
238+
var req = new FetchFilteredGroupNotificationsEventReq(count, start);
239+
var resp = await context.EventContext.SendEvent<FetchFilteredGroupNotificationsEventResp>(req);
240+
return resp.GroupNotifications;
241+
}
242+
236243
public async Task<BotStranger> FetchStranger(long uid)
237244
{
238245
var req = new FetchStrangerByUinEventReq(uid);
239246
var resp = await context.EventContext.SendEvent<FetchStrangerEventResp>(req);
240247
return resp.Stranger;
241248
}
242249

243-
public async Task SetGroupNotification(long groupUin, ulong sequence, BotGroupNotificationType type, GroupNotificationOperate operate, string message)
250+
public async Task SetGroupNotification(long groupUin, ulong sequence, BotGroupNotificationType type, bool isFiltered, GroupNotificationOperate operate, string message)
244251
{
245-
await context.EventContext.SendEvent<SetGroupNotificationEventResp>(new SetGroupNotificationEventReq(
246-
groupUin,
247-
sequence,
248-
type,
249-
operate,
250-
message
251-
));
252+
if (isFiltered)
253+
{
254+
await context.EventContext.SendEvent<SetFilteredGroupNotificationEventResp>(
255+
new SetFilteredGroupNotificationEventReq(
256+
groupUin,
257+
sequence,
258+
type,
259+
operate,
260+
message
261+
)
262+
);
263+
}
264+
else
265+
{
266+
await context.EventContext.SendEvent<SetGroupNotificationEventResp>(
267+
new SetGroupNotificationEventReq(
268+
groupUin,
269+
sequence,
270+
type,
271+
operate,
272+
message
273+
)
274+
);
275+
}
252276
}
253277
}

Lagrange.Core/Internal/Packets/Service/FetchGroupNotifications.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ namespace Lagrange.Core.Internal.Packets.Service;
88
internal partial class FetchGroupNotificationsRequest
99
{
1010
[ProtoMember(1)] public ulong Count { get; set; }
11+
12+
[ProtoMember(2)] public ulong StartSequence { get; set; }
1113
}
1214

1315

1416
[ProtoPackable]
1517
internal partial class FetchGroupNotificationsResponse
1618
{
17-
[ProtoMember(1)] public List<FetchGroupNotificationsResponseNotification> GroupNotifications { get; set; }
19+
// If empty, it is null
20+
[ProtoMember(1)] public List<FetchGroupNotificationsResponseNotification>? GroupNotifications { get; set; }
1821
}
1922

2023
[ProtoPackable]
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
using Lagrange.Core.Common;
2+
using Lagrange.Core.Common.Entity;
3+
using Lagrange.Core.Internal.Events;
4+
using Lagrange.Core.Internal.Events.System;
5+
using Lagrange.Core.Internal.Packets.Service;
6+
7+
namespace Lagrange.Core.Internal.Services.System;
8+
9+
[EventSubscribe<FetchFilteredGroupNotificationsEventReq>(Protocols.All)]
10+
[Service("OidbSvcTrpcTcp.0x10c0_2")]
11+
internal class FetchFilteredGroupNotificationsService : OidbService<FetchFilteredGroupNotificationsEventReq, FetchFilteredGroupNotificationsEventResp, FetchGroupNotificationsRequest, FetchGroupNotificationsResponse>
12+
{
13+
private protected override uint Command => 0x10c0;
14+
15+
private protected override uint Service => 2;
16+
17+
private protected override Task<FetchGroupNotificationsRequest> ProcessRequest(FetchFilteredGroupNotificationsEventReq request, BotContext context)
18+
{
19+
return Task.FromResult(new FetchGroupNotificationsRequest
20+
{
21+
Count = request.Count,
22+
StartSequence = request.Start
23+
});
24+
}
25+
26+
private protected override Task<FetchFilteredGroupNotificationsEventResp> ProcessResponse(FetchGroupNotificationsResponse response, BotContext context)
27+
{
28+
if (response.GroupNotifications == null)
29+
{
30+
return Task.FromResult(new FetchFilteredGroupNotificationsEventResp([]));
31+
}
32+
33+
List<BotGroupNotificationBase> notifications = [];
34+
foreach (var request in response.GroupNotifications)
35+
{
36+
var targetUin = context.CacheContext.ResolveUin(request.Target.Uid);
37+
long? operatorUin = request.Operator != null
38+
? context.CacheContext.ResolveUin(request.Operator.Uid)
39+
: null;
40+
long? inviterUin = request.Inviter != null
41+
? context.CacheContext.ResolveUin(request.Inviter.Uid)
42+
: null;
43+
44+
var notification = request.Type switch
45+
{
46+
1 => new BotGroupJoinNotification(
47+
request.Group.GroupUin,
48+
request.Sequence,
49+
targetUin,
50+
request.Target.Uid,
51+
(BotGroupNotificationState)request.State,
52+
operatorUin,
53+
request.Operator?.Uid,
54+
request.Comment,
55+
true
56+
),
57+
22 => new BotGroupInviteNotification(
58+
request.Group.GroupUin,
59+
request.Sequence,
60+
targetUin,
61+
request.Target.Uid,
62+
(BotGroupNotificationState)request.State,
63+
operatorUin,
64+
request.Operator?.Uid,
65+
inviterUin ?? 0,
66+
request.Inviter?.Uid ?? string.Empty,
67+
true
68+
),
69+
_ => LogUnknownNotificationType(context, request.Type),
70+
};
71+
if (notification != null) notifications.Add(notification);
72+
}
73+
return Task.FromResult(new FetchFilteredGroupNotificationsEventResp(notifications));
74+
}
75+
76+
private BotGroupNotificationBase? LogUnknownNotificationType(BotContext context, ulong type)
77+
{
78+
context.LogWarning(nameof(FetchFilteredGroupNotificationsService), "Unknown filtered notification type: {0}", null, type);
79+
return null;
80+
}
81+
}

Lagrange.Core/Internal/Services/System/FetchGroupNotificationsService.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ private protected override Task<FetchGroupNotificationsRequest> ProcessRequest(F
1818
{
1919
return Task.FromResult(new FetchGroupNotificationsRequest
2020
{
21-
Count = request.Count
21+
Count = request.Count,
22+
StartSequence = request.Start
2223
});
2324
}
2425

2526
private protected override Task<FetchGroupNotificationsEventResp> ProcessResponse(FetchGroupNotificationsResponse response, BotContext context)
2627
{
28+
if (response.GroupNotifications == null)
29+
{
30+
return Task.FromResult(new FetchGroupNotificationsEventResp([]));
31+
}
32+
2733
List<BotGroupNotificationBase> notifications = [];
2834
foreach (var request in response.GroupNotifications)
2935
{
@@ -45,7 +51,8 @@ private protected override Task<FetchGroupNotificationsEventResp> ProcessRespons
4551
(BotGroupNotificationState)request.State,
4652
operatorUin,
4753
request.Operator?.Uid,
48-
request.Comment
54+
request.Comment,
55+
false
4956
),
5057
3 => new BotGroupSetAdminNotification(
5158
request.Group.GroupUin,
@@ -86,7 +93,8 @@ private protected override Task<FetchGroupNotificationsEventResp> ProcessRespons
8693
operatorUin,
8794
request.Operator?.Uid,
8895
inviterUin ?? 0,
89-
request.Inviter?.Uid ?? string.Empty
96+
request.Inviter?.Uid ?? string.Empty,
97+
false
9098
),
9199
_ => LogUnknownNotificationType(context, request.Type),
92100
};

0 commit comments

Comments
 (0)