Skip to content

Commit 7262c7d

Browse files
authored
[Core] Implemented SetGroupNotification api (#35)
1 parent 7c7ee08 commit 7262c7d

File tree

9 files changed

+170
-26
lines changed

9 files changed

+170
-26
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Lagrange.Core.Common.Entity;
2+
3+
public enum GroupNotificationOperate
4+
{
5+
Allow = 1,
6+
Deny = 2,
7+
Ignore = 3,
8+
}

Lagrange.Core/Common/Interface/OperationExt.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,7 @@ public static Task<List<BotGroupNotificationBase>> FetchGroupNotifications(this
3232

3333
public static Task<BotStranger> FetchStranger(this BotContext context, long uin) =>
3434
context.EventContext.GetLogic<OperationLogic>().FetchStranger(uin);
35+
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);
3538
}

Lagrange.Core/Events/EventArgs/BotGroupMemberDecreaseEvent.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
namespace Lagrange.Core.Events.EventArgs;
22

3-
public class BotGroupMemberDecreaseEvent(Int64 groupUin, Int64 userUin, Int64 operatorUin) : EventBase
3+
public class BotGroupMemberDecreaseEvent(long groupUin, long userUin, long? operatorUin) : EventBase
44
{
5-
public Int64 GroupUin { get; } = groupUin;
6-
7-
public Int64 UserUin { get; } = userUin;
8-
9-
public Int64 OperatorUin { get; } = operatorUin;
5+
public long GroupUin { get; } = groupUin;
6+
7+
public long UserUin { get; } = userUin;
8+
9+
public long? OperatorUin { get; } = operatorUin;
1010

1111
public override string ToEventMessage()
1212
{
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 SetGroupNotificationEventReq(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 SetGroupNotificationEventResp : ProtocolEvent
19+
{
20+
public static readonly SetGroupNotificationEventResp Default = new();
21+
}

Lagrange.Core/Internal/Logic/OperationLogic.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,4 +239,15 @@ public async Task<BotStranger> FetchStranger(long uid)
239239
var resp = await context.EventContext.SendEvent<FetchStrangerEventResp>(req);
240240
return resp.Stranger;
241241
}
242+
243+
public async Task SetGroupNotification(long groupUin, ulong sequence, BotGroupNotificationType type, GroupNotificationOperate operate, string message)
244+
{
245+
await context.EventContext.SendEvent<SetGroupNotificationEventResp>(new SetGroupNotificationEventReq(
246+
groupUin,
247+
sequence,
248+
type,
249+
operate,
250+
message
251+
));
252+
}
242253
}

Lagrange.Core/Internal/Logic/PushLogic.cs

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,26 +31,38 @@ public async ValueTask Incoming(ProtocolEvent e)
3131
case Type.GroupMemberDecreaseNotice when messageEvent.MsgPush.CommonMessage.MessageBody.MsgContent is { } content:
3232
{
3333
var decrease = ProtoHelper.Deserialize<GroupChange>(content.Span);
34-
if (decrease.DecreaseType == 3)
34+
switch ((DecreaseType)decrease.DecreaseType)
3535
{
36-
var op = ProtoHelper.Deserialize<OperatorInfo>(decrease.Operator.AsSpan());
37-
context.EventInvoker.PostEvent(
38-
new BotGroupMemberDecreaseEvent(
36+
case DecreaseType.KickSelf:
37+
{
38+
var op = ProtoHelper.Deserialize<OperatorInfo>(decrease.Operator.AsSpan());
39+
context.EventInvoker.PostEvent(new BotGroupMemberDecreaseEvent(
3940
decrease.GroupUin,
4041
context.CacheContext.ResolveUin(decrease.MemberUid),
41-
context.CacheContext.ResolveUin(op.Operator.Uid ?? "")
42-
)
43-
);
44-
}
45-
else
46-
{
47-
context.EventInvoker.PostEvent(
48-
new BotGroupMemberDecreaseEvent(
42+
op.Operator.Uid != null ? context.CacheContext.ResolveUin(op.Operator.Uid) : null
43+
));
44+
break;
45+
}
46+
case DecreaseType.Exit:
47+
{
48+
await context.CacheContext.GetMemberList(decrease.GroupUin);
49+
context.EventInvoker.PostEvent(new BotGroupMemberDecreaseEvent(
4950
decrease.GroupUin,
5051
context.CacheContext.ResolveUin(decrease.MemberUid),
51-
context.CacheContext.ResolveUin(Encoding.UTF8.GetString(decrease.Operator.AsSpan()))
52-
)
53-
);
52+
null
53+
));
54+
break;
55+
}
56+
case DecreaseType.Kick:
57+
{
58+
await context.CacheContext.GetMemberList(decrease.GroupUin);
59+
goto case DecreaseType.KickSelf;
60+
}
61+
default:
62+
{
63+
context.LogWarning(nameof(PushLogic), "Unknown decrease type: {0}", null, decrease.DecreaseType);
64+
break;
65+
}
5466
}
5567
break;
5668
}
@@ -80,12 +92,12 @@ public async ValueTask Incoming(ProtocolEvent e)
8092
}
8193
case Type.Event0x20D when messageEvent.MsgPush.CommonMessage.MessageBody.MsgContent is { } content:
8294
{
83-
var decrease = ProtoHelper.Deserialize<Event0x20D>(content.Span);
84-
switch ((Event0x20DSubType)decrease.SubType)
95+
var @event = ProtoHelper.Deserialize<Event0x20D>(content.Span);
96+
switch ((Event0x20DSubType)@event.SubType)
8597
{
8698
case Event0x20DSubType.GroupInviteNotification:
8799
{
88-
var body = ProtoHelper.Deserialize<GroupInvite>(decrease.Body);
100+
var body = ProtoHelper.Deserialize<GroupInvite>(@event.Body);
89101

90102
var response = await context.EventContext.SendEvent<FetchGroupNotificationsEventResp>(
91103
new FetchGroupNotificationsEventReq(20)
@@ -108,6 +120,11 @@ public async ValueTask Incoming(ProtocolEvent e)
108120
context.EventInvoker.PostEvent(new BotGroupInviteNotificationEvent(notification));
109121
break;
110122
}
123+
default:
124+
{
125+
context.LogWarning(nameof(PushLogic), "Unknown 0x20D sub type: {0}", null, @event.SubType);
126+
break;
127+
}
111128
}
112129
break;
113130
}
@@ -117,6 +134,7 @@ public async ValueTask Incoming(ProtocolEvent e)
117134
switch (pkgType)
118135
{
119136
case Event0x2DCSubType.GroupGreyTipNotice20 when messageEvent.MsgPush.CommonMessage.MessageBody.MsgContent is { } content:
137+
{
120138
var packet = new BinaryPacket(content);
121139
Int64 groupUin = packet.Read<Int32>(); // group uin
122140
_ = packet.Read<byte>(); // unknown byte
@@ -138,10 +156,20 @@ public async ValueTask Incoming(ProtocolEvent e)
138156
);
139157
}
140158
break;
159+
}
160+
default:
161+
{
162+
context.LogWarning(nameof(PushLogic), "Unknown 0x2DC sub type: {0}", null, pkgType);
163+
break;
164+
}
141165
}
142166
break;
143167
}
144-
default: break;
168+
default:
169+
{
170+
context.LogWarning(nameof(PushLogic), "Unknown push msg type: {0}", null, messageEvent.MsgPush.CommonMessage.ContentHead.Type);
171+
break;
172+
}
145173
}
146174
}
147175

@@ -157,6 +185,13 @@ private enum Type
157185
Event0x2DC = 732, // group related event
158186
}
159187

188+
private enum DecreaseType
189+
{
190+
KickSelf = 3,
191+
Exit = 130,
192+
Kick = 131
193+
}
194+
160195
private enum Event0x20DSubType
161196
{
162197
GroupInviteNotification = 87

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal partial class GroupChange
1313

1414
[ProtoMember(3)] public string MemberUid { get; set; }
1515

16-
[ProtoMember(4)] public uint DecreaseType { get; set; } // 131 Kick 130 Exit
16+
[ProtoMember(4)] public uint DecreaseType { get; set; } // 3 KickSelf 130 Exit 131 Kick
1717

1818
[ProtoMember(5)] public byte[]? Operator { get; set; }
1919

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Lagrange.Proto;
2+
3+
namespace Lagrange.Core.Internal.Packets.Service;
4+
5+
#pragma warning disable CS8618
6+
7+
[ProtoPackable]
8+
public partial class SetGroupNotificationRequest
9+
{
10+
[ProtoMember(1)] public ulong Operate { get; set; } // 1 accept 2 reject 3 ignore
11+
12+
[ProtoMember(2)] public SetGroupNotificationRequestBody Body { get; set; }
13+
}
14+
15+
[ProtoPackable]
16+
public partial class SetGroupNotificationRequestBody
17+
{
18+
[ProtoMember(1)] public ulong Sequence { get; set; }
19+
20+
[ProtoMember(2)] public ulong Type { get; set; }
21+
22+
[ProtoMember(3)] public long GroupUin { get; set; }
23+
24+
[ProtoMember(4)] public string Message { get; set; }
25+
}
26+
27+
[ProtoPackable]
28+
public partial class SetGroupNotificationResponse
29+
{
30+
31+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using Lagrange.Core.Common;
2+
using Lagrange.Core.Internal.Events;
3+
using Lagrange.Core.Internal.Events.System;
4+
using Lagrange.Core.Internal.Packets.Service;
5+
6+
namespace Lagrange.Core.Internal.Services.System;
7+
8+
[EventSubscribe<SetGroupNotificationEventReq>(Protocols.All)]
9+
[Service("OidbSvcTrpcTcp.0x10c8_1")]
10+
internal class SetGroupNotificationService : OidbService<SetGroupNotificationEventReq, SetGroupNotificationEventResp, SetGroupNotificationRequest, SetGroupNotificationResponse>
11+
{
12+
private protected override uint Command => 0x10c8;
13+
14+
private protected override uint Service => 1;
15+
16+
private protected override Task<SetGroupNotificationRequest> ProcessRequest(SetGroupNotificationEventReq request, BotContext context)
17+
{
18+
return Task.FromResult(new SetGroupNotificationRequest
19+
{
20+
Operate = (ulong)request.Operate,
21+
Body = new SetGroupNotificationRequestBody
22+
{
23+
Sequence = request.Sequence,
24+
Type = (ulong)request.Type,
25+
GroupUin = request.GroupUin,
26+
Message = request.Message,
27+
}
28+
});
29+
}
30+
31+
private protected override Task<SetGroupNotificationEventResp> ProcessResponse(SetGroupNotificationResponse response, BotContext context)
32+
{
33+
return Task.FromResult(SetGroupNotificationEventResp.Default);
34+
}
35+
}

0 commit comments

Comments
 (0)