Skip to content

Commit 0efa433

Browse files
authored
[Core] Implemented FetchGroupNotifications and its required APIs (#33)
1 parent cede4ef commit 0efa433

23 files changed

Lines changed: 562 additions & 52 deletions
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace Lagrange.Core.Common.Entity;
2+
3+
public class BotGroupExitNotification(long group, ulong sequence, long target) : BotGroupNotificationBase(group, sequence, BotGroupNotificationType.Exit, target);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Lagrange.Core.Common.Entity;
2+
3+
public class BotGroupInviteNotification(long group, ulong sequence, long target, BotGroupNotificationState state, long? @operator, long inviter) : BotGroupNotificationBase(group, sequence, BotGroupNotificationType.Invite, target)
4+
{
5+
public BotGroupNotificationState State { get; } = state;
6+
7+
public long? Operator { get; } = @operator;
8+
9+
public long Inviter { get; } = inviter;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Lagrange.Core.Common.Entity;
2+
3+
public class BotGroupJoinNotification(long group, ulong sequence, long target, BotGroupNotificationState state, long? @operator, string comment) : BotGroupNotificationBase(group, sequence, BotGroupNotificationType.Join, target)
4+
{
5+
public BotGroupNotificationState State { get; } = state;
6+
7+
public long? Operator { get; } = @operator;
8+
9+
public string Comment { get; } = comment;
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Lagrange.Core.Common.Entity;
2+
3+
public class BotGroupKickNotification(long group, ulong sequence, long target, long @operator) : BotGroupNotificationBase(group, sequence, BotGroupNotificationType.Exit, target)
4+
{
5+
public long Operator { get; } = @operator;
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Lagrange.Core.Common.Entity;
2+
3+
public abstract class BotGroupNotificationBase(long group, ulong sequence, BotGroupNotificationType type, long target)
4+
{
5+
public long Group { get; } = group;
6+
7+
public ulong Sequence { get; } = sequence;
8+
9+
public BotGroupNotificationType Type { get; } = type;
10+
11+
public long Target { get; } = target;
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Lagrange.Core.Common.Entity;
2+
3+
public enum BotGroupNotificationState
4+
{
5+
Wait = 1,
6+
Accept,
7+
Reject,
8+
Ignore
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Lagrange.Core.Common.Entity;
2+
3+
public enum BotGroupNotificationType
4+
{
5+
Join = 1,
6+
Exit = 13,
7+
KickOther = 6,
8+
KickSelf = 7,
9+
Invite = 22
10+
}
Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
11
namespace Lagrange.Core.Common.Entity;
22

3-
public class BotStranger(long uin, string nickname, string uid) : BotContact
3+
public class BotStranger(long uin, string nickname, string uid, string personalSign, string remark, ulong level, BotGender gender, DateTime registrationTime, DateTime? birthday, ulong age, string qID) : BotContact
44
{
55
public override long Uin { get; } = uin;
6-
6+
77
public override string Nickname { get; } = nickname ?? string.Empty;
8-
8+
99
public override string Uid { get; } = uid ?? string.Empty;
10-
10+
11+
public string PersonalSign { get; } = personalSign;
12+
13+
public string Remark { get; } = remark;
14+
15+
public ulong Level { get; } = level;
16+
17+
public BotGender Gender { get; } = gender;
18+
19+
public DateTime RegistrationTime { get; } = registrationTime;
20+
21+
public DateTime? Birthday { get; } = birthday;
22+
23+
public ulong Age { get; } = age;
24+
25+
public string QID { get; } = qID;
26+
1127
public long Source { get; init; }
28+
29+
internal BotStranger CloneWithSource(long source) => new(
30+
Uin,
31+
Nickname,
32+
Uid,
33+
PersonalSign,
34+
Remark,
35+
Level,
36+
Gender,
37+
RegistrationTime,
38+
Birthday,
39+
Age,
40+
QID
41+
)
42+
{ Source = source };
1243
}

Lagrange.Core/Common/Interface/OperationExt.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ namespace Lagrange.Core.Common.Interface;
66

77
public static class OperationExt
88
{
9-
public static Task<BotQrCodeInfo?> FetchQrCodeInfo(this BotContext context, byte[] k) =>
9+
public static Task<BotQrCodeInfo?> FetchQrCodeInfo(this BotContext context, byte[] k) =>
1010
context.EventContext.GetLogic<WtExchangeLogic>().FetchQrCodeInfo(k);
11-
11+
1212
public static Task<(bool Success, string Message)> CloseQrCode(this BotContext context, byte[] k, bool confirm) =>
1313
context.EventContext.GetLogic<WtExchangeLogic>().CloseQrCode(k, confirm);
14-
14+
1515
public static Task<Dictionary<string, string>> FetchCookies(this BotContext context, params List<string> domains) =>
1616
context.EventContext.GetLogic<OperationLogic>().FetchCookies(domains);
17-
17+
1818
public static Task<(string Key, uint Expiration)> FetchClientKey(this BotContext context) =>
1919
context.EventContext.GetLogic<OperationLogic>().FetchClientKey();
2020

@@ -26,4 +26,10 @@ public static Task<List<BotGroup>> FetchGroups(this BotContext context, bool ref
2626

2727
public static Task<List<BotGroupMember>> FetchMembers(this BotContext context, long groupUin, bool refresh = false) =>
2828
context.CacheContext.GetMemberList(groupUin, refresh);
29+
30+
public static Task<List<BotGroupNotificationBase>> FetchGroupNotifications(this BotContext context, ulong count) =>
31+
context.EventContext.GetLogic<OperationLogic>().FetchGroupNotifications(count);
32+
33+
public static Task<BotStranger> FetchStranger(this BotContext context, long uin) =>
34+
context.EventContext.GetLogic<OperationLogic>().FetchStranger(uin);
2935
}

Lagrange.Core/Internal/Context/CacheContext.cs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Concurrent;
2+
using System.Threading.Tasks;
23
using Lagrange.Core.Common.Entity;
34
using Lagrange.Core.Internal.Events.System;
45

@@ -11,11 +12,16 @@ internal class CacheContext(BotContext context)
1112
private List<BotGroup>? _groups;
1213

1314
private readonly ConcurrentDictionary<long, string> _uinToUid = new();
15+
private readonly ConcurrentDictionary<string, long> _uidToUin = new();
1416

1517
private readonly ConcurrentDictionary<long, List<BotGroupMember>> _members = new();
1618

17-
private readonly Dictionary<int, BotFriendCategory> _categories = new();
18-
19+
private readonly Dictionary<int, BotFriendCategory> _categories = [];
20+
21+
private readonly Dictionary<string, BotStranger> _strangersWithUid = [];
22+
private readonly Dictionary<long, BotStranger> _strangersWithUin = [];
23+
private readonly SemaphoreSlim _strangersLock = new(1);
24+
1925
public async Task<List<BotFriend>> GetFriendList(bool refresh = false)
2026
{
2127
if (refresh || _friends == null) Interlocked.Exchange(ref _friends, await FetchFriends());
@@ -89,9 +95,48 @@ public async Task<List<BotFriendCategory>> GetCategories(bool refresh = false)
8995
return group;
9096
}
9197

98+
public async Task<BotStranger> ResolveStranger(long uin)
99+
{
100+
await _strangersLock.WaitAsync();
101+
try
102+
{
103+
if (_strangersWithUin.TryGetValue(uin, out BotStranger? stranger)) return stranger;
104+
105+
stranger = await FetchStranger(uin);
106+
_strangersWithUin.Add(uin, stranger);
107+
108+
return stranger;
109+
}
110+
finally { _strangersLock.Release(); }
111+
}
112+
113+
public async Task<BotStranger> ResolveStranger(string uid)
114+
{
115+
await _strangersLock.WaitAsync();
116+
try
117+
{
118+
if (_strangersWithUid.TryGetValue(uid, out BotStranger? stranger)) return stranger;
119+
120+
stranger = await FetchStranger(uid);
121+
_strangersWithUin.TryAdd(stranger.Uin, stranger);
122+
_strangersWithUid.Add(uid, stranger);
123+
124+
return stranger;
125+
}
126+
finally { _strangersLock.Release(); }
127+
}
128+
92129
public string? ResolveCachedUid(long uin) => _uinToUid.GetValueOrDefault(uin);
93-
94-
public long? ResolveCachedUin(string uid) => _uinToUid.FirstOrDefault(kvp => kvp.Value == uid).Key;
130+
131+
public long ResolveUin(string uid)
132+
{
133+
if (_uidToUin.TryGetValue(uid, out long value)) return value;
134+
135+
long uin = _uinToUid.FirstOrDefault(kvp => kvp.Value == uid).Key;
136+
if (uin != 0) return uin;
137+
138+
return ResolveStranger(uid).GetAwaiter().GetResult().Uin;
139+
}
95140

96141
/// <summary>
97142
/// Fetches the friends list from the server.
@@ -136,4 +181,16 @@ private async Task<List<BotGroupMember>> FetchGroupMembers(long groupUin)
136181

137182
return members;
138183
}
184+
185+
private async Task<BotStranger> FetchStranger(long uin)
186+
{
187+
var result = await context.EventContext.SendEvent<FetchStrangerEventResp>(new FetchStrangerByUinEventReq(uin));
188+
return result.Stranger;
189+
}
190+
191+
private async Task<BotStranger> FetchStranger(string uid)
192+
{
193+
var result = await context.EventContext.SendEvent<FetchStrangerEventResp>(new FetchStrangerByUidEventReq(uid));
194+
return result.Stranger;
195+
}
139196
}

0 commit comments

Comments
 (0)