11using System . Collections . Concurrent ;
2+ using System . Threading . Tasks ;
23using Lagrange . Core . Common . Entity ;
34using 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