Skip to content

Commit 634b7f6

Browse files
committed
[Milky] Implemented get_group_notification/send_group_message_reaction/get_cookies api
1 parent dbd5e97 commit 634b7f6

21 files changed

Lines changed: 347 additions & 43 deletions

Lagrange.Core/Common/Entity/BotGroupKickNotification.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace Lagrange.Core.Common.Entity;
22

33
public class BotGroupKickNotification(long group, ulong sequence, long targetUin, string targetUid, long operatorUin, string operatorUid) : BotGroupNotificationBase(group, sequence, BotGroupNotificationType.Exit, targetUin, targetUid)
44
{
5-
public long Operator { get; } = operatorUin;
5+
public long OperatorUin { get; } = operatorUin;
66

77
public string OperatorUid { get; } = operatorUid;
88
}

Lagrange.Core/Common/Entity/BotGroupRejectNotification.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace Lagrange.Core.Common.Entity;
22

33
public class BotGroupUnsetAdminNotification(long groupUin, ulong sequence, long targetUin, string targetUid, long operatorUin, string operatorUid) : BotGroupNotificationBase(groupUin, sequence, BotGroupNotificationType.UnsetAdmin, targetUin, targetUid)
44
{
5-
public long Operator { get; } = operatorUin;
5+
public long OperatorUin { get; } = operatorUin;
66

77
public string OperatorUid { get; } = operatorUid;
88
}

Lagrange.Core/Common/Entity/BotGroupSetAdminNotification.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace Lagrange.Core.Common.Entity;
22

33
public class BotGroupSetAdminNotification(long group, ulong sequence, long targetUin, string targetUid, long operatorUin, string operatorUid) : BotGroupNotificationBase(group, sequence, BotGroupNotificationType.SetAdmin, targetUin, targetUid)
44
{
5-
public long Operator { get; } = operatorUin;
5+
public long OperatorUin { get; } = operatorUin;
66

77
public string OperatorUid { get; } = operatorUid;
88
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.Text.Json.Serialization;
2+
using Lagrange.Core;
3+
using Lagrange.Core.Common.Entity;
4+
using Lagrange.Core.Common.Interface;
5+
using Lagrange.Milky.Entity;
6+
using Lagrange.Milky.Utility;
7+
8+
namespace Lagrange.Milky.Api.Handler.Group;
9+
10+
[Api("get_group_notifications")]
11+
public class GetGroupNotificationsHandler(BotContext bot, EntityConvert convert) : IApiHandler<GetGroupNotificationsParameter, GetGroupNotificationsResult>
12+
{
13+
private readonly BotContext _bot = bot;
14+
private readonly EntityConvert _convert = convert;
15+
16+
public async Task<GetGroupNotificationsResult> HandleAsync(GetGroupNotificationsParameter parameter, CancellationToken token)
17+
{
18+
List<BotGroupNotificationBase> notifications = await (parameter.IsFiltered
19+
? _bot.FetchFilteredGroupNotifications((ulong)parameter.Limit, (ulong)parameter.StartNotificationSeq)
20+
: _bot.FetchGroupNotifications((ulong)parameter.Limit, (ulong)parameter.StartNotificationSeq));
21+
22+
return new GetGroupNotificationsResult(
23+
notifications.Select(_convert.GroupNotification).Where(n => n != null)!,
24+
notifications.Count == 0 ? null : (long)notifications[^1].Sequence
25+
);
26+
}
27+
}
28+
29+
public class GetGroupNotificationsParameter(long startNotificationSeq = 0, bool isFiltered = false, int limit = 20)
30+
{
31+
[JsonPropertyName("start_notification_seq")]
32+
public long StartNotificationSeq { get; } = startNotificationSeq;
33+
34+
[JsonPropertyName("is_filtered")]
35+
public bool IsFiltered { get; } = isFiltered;
36+
37+
[JsonPropertyName("limit")]
38+
public int Limit { get; } = limit;
39+
}
40+
41+
public class GetGroupNotificationsResult(IEnumerable<GroupNotificationBase> notifications, long? nextNotificationSeq)
42+
{
43+
[JsonPropertyName("notifications")]
44+
public IEnumerable<GroupNotificationBase> Notifications { get; } = notifications;
45+
46+
[JsonPropertyName("next_notification_seq")]
47+
public long? NextNotificationSeq { get; } = nextNotificationSeq;
48+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
using System.Text.Json.Serialization;
3+
using Lagrange.Core;
4+
using Lagrange.Core.Common.Interface;
5+
6+
namespace Lagrange.Milky.Api.Handler.Group;
7+
8+
[Api("send_group_message_reaction")]
9+
public class SendGroupMessageReactionHandler(BotContext bot) : IEmptyResultApiHandler<SendGroupMessageReactionParameter>
10+
{
11+
private readonly BotContext _bot = bot;
12+
13+
public async Task HandleAsync(SendGroupMessageReactionParameter parameter, CancellationToken token)
14+
{
15+
await _bot.SetGroupReaction(parameter.GroupId, (ulong)parameter.MessageSeq, parameter.Reaction, true);
16+
}
17+
}
18+
19+
public class SendGroupMessageReactionParameter(long groupId, long messageSeq, string reaction, bool isAdd)
20+
{
21+
[JsonRequired]
22+
[JsonPropertyName("groupId")]
23+
public long GroupId { get; init; } = groupId;
24+
25+
[JsonRequired]
26+
[JsonPropertyName("messageSeq")]
27+
public long MessageSeq { get; init; } = messageSeq;
28+
29+
[JsonRequired]
30+
[JsonPropertyName("reaction")]
31+
public string Reaction { get; init; } = reaction;
32+
33+
[JsonRequired]
34+
[JsonPropertyName("isAdd")]
35+
public bool IsAdd { get; init; } = isAdd;
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Text.Json.Serialization;
2+
using Lagrange.Core;
3+
using Lagrange.Core.Common.Interface;
4+
using Lagrange.Milky.Api.Exception;
5+
using Lagrange.Milky.Utility;
6+
7+
namespace Lagrange.Milky.Api.Handler.System;
8+
9+
[Api("get_cookies")]
10+
public class GetCookiesHandler(BotContext bot) : IApiHandler<GetCookiesParameter, GetCookiesResult>
11+
{
12+
private readonly BotContext _bot = bot;
13+
14+
public async Task<GetCookiesResult> HandleAsync(GetCookiesParameter parameter, CancellationToken token)
15+
{
16+
var cookies = await _bot.FetchCookies(parameter.Domain);
17+
if (!cookies.TryGetValue(parameter.Domain, out string? cookie))
18+
{
19+
throw new ApiException(-1, "cookie not found");
20+
}
21+
return new GetCookiesResult(cookie);
22+
}
23+
}
24+
25+
public class GetCookiesParameter(string domain)
26+
{
27+
[JsonRequired]
28+
[JsonPropertyName("domain")]
29+
public string Domain { get; init; } = domain;
30+
}
31+
32+
public class GetCookiesResult(string cookies)
33+
{
34+
[JsonPropertyName("cookies")]
35+
public string Cookies { get; } = cookies;
36+
}

Lagrange.Milky/Cache/MessageCache.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,7 @@ private void HandleBotMessageEvent(BotContext bot, BotMessageEvent @event)
4949

5050
public Task StopAsync(CancellationToken cancellationToken)
5151
{
52-
// TODO
53-
// _bot.EventInvoker.UnregisterEvent<BotMessageEvent>(HandleBotMessageEvent);
52+
_bot.EventInvoker.UnregisterEvent<BotMessageEvent>(HandleBotMessageEvent);
5453

5554
return Task.CompletedTask;
5655
}

Lagrange.Milky/Core/CoreLoggerService.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ private void HandleLog(BotContext bot, BotLogEvent @event)
4646

4747
public Task StopAsync(CancellationToken token)
4848
{
49-
// TODO: unregister
50-
// _bot.EventInvoker.UnregisterEvent<BotLogEvent>(HandleLog);
49+
_bot.EventInvoker.UnregisterEvent<BotLogEvent>(HandleLog);
5150

5251
_levelChangeHandlerDisposable?.Dispose();
5352

Lagrange.Milky/Core/CoreLoginService.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,12 @@ private async Task HandleQrCode(BotContext bot, BotQrCodeEvent @event)
107107

108108
public async Task StopAsync(CancellationToken cancellationToken)
109109
{
110-
// TODO: unregister
111-
// _bot.EventInvoker.UnregisterEvent<BotQrCodeEvent>(HandleQrCode);
112-
// _bot.EventInvoker.UnregisterEvent<BotQrCodeQueryEvent>(HandleQrCodeQuery);
113-
// _bot.EventInvoker.UnregisterEvent<BotRefreshKeystoreEvent>(HandleRefreshKeystore);
114-
// _bot.EventInvoker.UnregisterEvent<BotCaptchaEvent>(HandleCaptcha);
115-
// _bot.EventInvoker.UnregisterEvent<BotSMSEvent>(HandleSMS);
116-
// _bot.EventInvoker.UnregisterEvent<BotNewDeviceVerifyEvent>(HandleNewDeviceVerify);
110+
_bot.EventInvoker.UnregisterEvent<BotQrCodeEvent>(HandleQrCode);
111+
_bot.EventInvoker.UnregisterEvent<BotQrCodeQueryEvent>(HandleQrCodeQuery);
112+
_bot.EventInvoker.UnregisterEvent<BotRefreshKeystoreEvent>(HandleRefreshKeystore);
113+
_bot.EventInvoker.UnregisterEvent<BotCaptchaEvent>(HandleCaptcha);
114+
_bot.EventInvoker.UnregisterEvent<BotSMSEvent>(HandleSms);
115+
_bot.EventInvoker.UnregisterEvent<BotNewDeviceVerifyEvent>(HandleNewDeviceVerify);
117116

118117
await _bot.Logout();
119118
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace Lagrange.Milky.Entity;
4+
5+
public class GroupAdminChangeNotification(long groupId, long notificationSeq, long targetUserId, bool isSet, long operatorId) : GroupNotificationBase("admin_change", groupId, notificationSeq)
6+
{
7+
[JsonPropertyName("target_user_id")]
8+
public long TargetUserId { get; } = targetUserId;
9+
10+
[JsonPropertyName("is_set")]
11+
public bool IsSet { get; } = isSet;
12+
13+
[JsonPropertyName("operator_id")]
14+
public long OperatorId { get; } = operatorId;
15+
}

0 commit comments

Comments
 (0)