Skip to content

Commit

Permalink
nya nya nya
Browse files Browse the repository at this point in the history
  • Loading branch information
dogdie233 committed Oct 8, 2024
1 parent cc3d489 commit 2b8d104
Show file tree
Hide file tree
Showing 20 changed files with 583 additions and 567 deletions.
18 changes: 9 additions & 9 deletions Lagrange.Core/Common/Interface/Api/BotExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public static class BotExt
/// Fetch the qrcode for QRCode Login
/// </summary>
/// <returns>return url and qrcode image in PNG format</returns>
public static Task<(string Url, byte[] QrCode)?> FetchQrCode(this BotContext bot, CancellationToken ct)
=> bot.ContextCollection.Business.WtExchangeLogic.FetchQrCode(ct);
public static Task<(string Url, byte[] QrCode)?> FetchQrCode(this BotContext bot, CancellationToken cancellationToken)
=> bot.ContextCollection.Business.WtExchangeLogic.FetchQrCode(cancellationToken);

/// <summary>
/// Use this method to login by QrCode, you should call <see cref="FetchQrCode"/> first
Expand All @@ -27,8 +27,8 @@ public static Task LoginByQrCode(this BotContext bot)
/// <summary>
/// Use this method to login by QrCode, you should call <see cref="FetchQrCode"/> first
/// </summary>
public static Task LoginByQrCode(this BotContext bot, CancellationToken ct)
=> bot.ContextCollection.Business.WtExchangeLogic.LoginByQrCode(ct);
public static Task LoginByQrCode(this BotContext bot, CancellationToken cancellationToken)
=> bot.ContextCollection.Business.WtExchangeLogic.LoginByQrCode(cancellationToken);

/// <summary>
/// Use this method to login by password, EasyLogin may be preformed if there is sig in <see cref="BotKeystore"/>
Expand All @@ -39,8 +39,8 @@ public static Task<bool> LoginByPassword(this BotContext bot)
/// <summary>
/// Use this method to login by password, EasyLogin may be preformed if there is sig in <see cref="BotKeystore"/>
/// </summary>
public static Task<bool> LoginByPassword(this BotContext bot, CancellationToken ct)
=> bot.ContextCollection.Business.WtExchangeLogic.LoginByPassword(ct);
public static Task<bool> LoginByPassword(this BotContext bot, CancellationToken cancellationToken)
=> bot.ContextCollection.Business.WtExchangeLogic.LoginByPassword(cancellationToken);

/// <summary>
/// Submit the captcha of the url given by the <see cref="EventInvoker.OnBotCaptchaEvent"/>
Expand All @@ -52,8 +52,8 @@ public static bool SubmitCaptcha(this BotContext bot, string ticket, string rand
public static Task<bool> SetNeedToConfirmSwitch(this BotContext bot, bool needToConfirm)
=> bot.ContextCollection.Business.OperationLogic.SetNeedToConfirmSwitch(needToConfirm, CancellationToken.None);

public static Task<bool> SetNeedToConfirmSwitch(this BotContext bot, bool needToConfirm, CancellationToken ct)
=> bot.ContextCollection.Business.OperationLogic.SetNeedToConfirmSwitch(needToConfirm, ct);
public static Task<bool> SetNeedToConfirmSwitch(this BotContext bot, bool needToConfirm, CancellationToken cancellationToken)
=> bot.ContextCollection.Business.OperationLogic.SetNeedToConfirmSwitch(needToConfirm, cancellationToken);

/// <summary>
/// Use this method to update keystore, so EasyLogin may be preformed next time by using this keystore
Expand All @@ -68,4 +68,4 @@ public static BotKeystore UpdateKeystore(this BotContext bot)
/// <param name="bot"></param>
public static BotDeviceInfo UpdateDeviceInfo(this BotContext bot)
=> bot.ContextCollection.Device;
}
}
140 changes: 70 additions & 70 deletions Lagrange.Core/Common/Interface/Api/GroupExt.cs

Large diffs are not rendered by default.

178 changes: 89 additions & 89 deletions Lagrange.Core/Common/Interface/Api/OperationExt.cs

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions Lagrange.Core/Internal/Context/BusinessContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ private void RegisterLogics()
}
}

public async Task<bool> PushEvent(ProtocolEvent @event, CancellationToken ct)
public async Task<bool> PushEvent(ProtocolEvent @event, CancellationToken cancellationToken)
{
try
{
var packets = Collection.Service.ResolvePacketByEvent(@event);
foreach (var packet in packets)
{
ct.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();
await Collection.Packet.PostPacket(packet);
}
}
Expand All @@ -95,23 +95,23 @@ public async Task<bool> PushEvent(ProtocolEvent @event, CancellationToken ct)
/// <summary>
/// Send Event to the Server, goes through the given context
/// </summary>
public async Task<List<ProtocolEvent>> SendEvent(ProtocolEvent @event, CancellationToken ct)
public async Task<List<ProtocolEvent>> SendEvent(ProtocolEvent @event, CancellationToken cancellationToken)
{
await HandleOutgoingEvent(@event, ct);
await HandleOutgoingEvent(@event, cancellationToken);
var result = new List<ProtocolEvent>();

ct.ThrowIfCancellationRequested();
cancellationToken.ThrowIfCancellationRequested();

try
{
var packets = Collection.Service.ResolvePacketByEvent(@event);
foreach (var packet in packets)
{
var returnVal = await Collection.Packet.SendPacket(packet, ct);
var returnVal = await Collection.Packet.SendPacket(packet, cancellationToken);
var resolved = Collection.Service.ResolveEventByPacket(returnVal);
foreach (var protocol in resolved)
{
await HandleIncomingEvent(protocol, ct);
await HandleIncomingEvent(protocol, cancellationToken);
result.Add(protocol);
}
}
Expand All @@ -130,7 +130,7 @@ public async Task<List<ProtocolEvent>> SendEvent(ProtocolEvent @event, Cancellat
return result;
}

public async Task<bool> HandleIncomingEvent(ProtocolEvent @event, CancellationToken ct)
public async Task<bool> HandleIncomingEvent(ProtocolEvent @event, CancellationToken cancellationToken)
{
_businessLogics.TryGetValue(typeof(ProtocolEvent), out var baseLogics);
_businessLogics.TryGetValue(@event.GetType(), out var normalLogics);
Expand All @@ -143,7 +143,7 @@ public async Task<bool> HandleIncomingEvent(ProtocolEvent @event, CancellationTo
{
try
{
await logic.Incoming(@event, ct);
await logic.Incoming(@event, cancellationToken);
}
catch (TaskCanceledException)
{
Expand All @@ -161,7 +161,7 @@ public async Task<bool> HandleIncomingEvent(ProtocolEvent @event, CancellationTo
return true;
}

public async Task<bool> HandleOutgoingEvent(ProtocolEvent @event, CancellationToken ct)
public async Task<bool> HandleOutgoingEvent(ProtocolEvent @event, CancellationToken cancellationToken)
{
_businessLogics.TryGetValue(typeof(ProtocolEvent), out var baseLogics);
_businessLogics.TryGetValue(@event.GetType(), out var normalLogics);
Expand All @@ -174,7 +174,7 @@ public async Task<bool> HandleOutgoingEvent(ProtocolEvent @event, CancellationTo
{
try
{
await logic.Outgoing(@event, ct);
await logic.Outgoing(@event, cancellationToken);
}
catch (TaskCanceledException)
{
Expand Down Expand Up @@ -220,4 +220,4 @@ public async Task<bool> HandleServerPacket(SsoPacket packet)

return success;
}
}
}
12 changes: 6 additions & 6 deletions Lagrange.Core/Internal/Context/HighwayContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ public HighwayContext(ContextCollection collection, BotKeystore keystore, BotApp
_concurrent = config.HighwayConcurrent;
}

public async Task UploadResources(MessageChain chain, CancellationToken ct)
public async Task UploadResources(MessageChain chain, CancellationToken cancellationToken)
{
foreach (var entity in chain)
{
if (_uploaders.TryGetValue(entity.GetType(), out var uploader))
{
try
{
if (chain.IsGroup) await uploader.UploadGroup(Collection, chain, entity, ct);
else await uploader.UploadPrivate(Collection, chain, entity, ct);
if (chain.IsGroup) await uploader.UploadGroup(Collection, chain, entity, cancellationToken);
else await uploader.UploadPrivate(Collection, chain, entity, cancellationToken);
}
catch
{
Expand All @@ -76,7 +76,7 @@ public async Task UploadResources(MessageChain chain, CancellationToken ct)
}
}

public async Task ManualUploadEntity(IMessageEntity entity, CancellationToken ct)
public async Task ManualUploadEntity(IMessageEntity entity, CancellationToken cancellationToken)
{
if (_uploaders.TryGetValue(entity.GetType(), out var uploader))
{
Expand All @@ -86,7 +86,7 @@ public async Task ManualUploadEntity(IMessageEntity entity, CancellationToken ct
string uid = Collection.Keystore.Uid ?? "";
var chain = new MessageChain(uin, uid, uid) { entity };

await uploader.UploadPrivate(Collection, chain, entity, ct);
await uploader.UploadPrivate(Collection, chain, entity, cancellationToken);
}
catch
{
Expand Down Expand Up @@ -242,4 +242,4 @@ public void Dispose()
{
_client.Dispose();
}
}
}
56 changes: 28 additions & 28 deletions Lagrange.Core/Internal/Context/Logic/Implementation/CachingLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@ internal CachingLogic(ContextCollection collection) : base(collection)
_cacheUsers = new ConcurrentDictionary<uint, BotUserInfo>();
}

public override Task Incoming(ProtocolEvent e, CancellationToken ct)
public override Task Incoming(ProtocolEvent e, CancellationToken cancellationToken)
{
return e switch
{
GroupSysDecreaseEvent groupSysDecreaseEvent when groupSysDecreaseEvent.MemberUid != Collection.Keystore.Uid => CacheUid(groupSysDecreaseEvent.GroupUin, ct, force: true),
GroupSysIncreaseEvent groupSysIncreaseEvent => CacheUid(groupSysIncreaseEvent.GroupUin, ct, force: true),
GroupSysAdminEvent groupSysAdminEvent => CacheUid(groupSysAdminEvent.GroupUin, ct, force: true),
GroupSysDecreaseEvent groupSysDecreaseEvent when groupSysDecreaseEvent.MemberUid != Collection.Keystore.Uid => CacheUid(groupSysDecreaseEvent.GroupUin, cancellationToken, force: true),
GroupSysIncreaseEvent groupSysIncreaseEvent => CacheUid(groupSysIncreaseEvent.GroupUin, cancellationToken, force: true),
GroupSysAdminEvent groupSysAdminEvent => CacheUid(groupSysAdminEvent.GroupUin, cancellationToken, force: true),
_ => Task.CompletedTask,
};
}

public async Task<List<BotGroup>> GetCachedGroups(bool refreshCache, CancellationToken ct)
public async Task<List<BotGroup>> GetCachedGroups(bool refreshCache, CancellationToken cancellationToken)
{
if (_cachedGroupEntities.Count == 0 || refreshCache)
{
_cachedGroupEntities.Clear();

var fetchGroupsEvent = FetchGroupsEvent.Create();
var events = await Collection.Business.SendEvent(fetchGroupsEvent, ct);
var events = await Collection.Business.SendEvent(fetchGroupsEvent, cancellationToken);
var groups = ((FetchGroupsEvent)events[0]).Groups;

_cachedGroupEntities.AddRange(groups);
Expand All @@ -65,68 +65,68 @@ public async Task<List<BotGroup>> GetCachedGroups(bool refreshCache, Cancellatio
return _cachedGroupEntities;
}

public async Task<string?> ResolveUid(uint? groupUin, uint friendUin, CancellationToken ct)
public async Task<string?> ResolveUid(uint? groupUin, uint friendUin, CancellationToken cancellationToken)
{
if (_uinToUid.Count == 0) await ResolveFriendsUidAndFriendGroups(ct);
if (_uinToUid.Count == 0) await ResolveFriendsUidAndFriendGroups(cancellationToken);
if (groupUin == null) return _uinToUid.GetValueOrDefault(friendUin);

await CacheUid(groupUin.Value, ct);
await CacheUid(groupUin.Value, cancellationToken);

return _uinToUid.GetValueOrDefault(friendUin);
}

public async Task<uint?> ResolveUin(uint? groupUin, string friendUid, CancellationToken ct, bool force = false)
public async Task<uint?> ResolveUin(uint? groupUin, string friendUid, CancellationToken cancellationToken, bool force = false)
{
if (_uinToUid.Count == 0) await ResolveFriendsUidAndFriendGroups(ct);
if (_uinToUid.Count == 0) await ResolveFriendsUidAndFriendGroups(cancellationToken);
if (groupUin == null) return _uinToUid.FirstOrDefault(x => x.Value == friendUid).Key;

await CacheUid(groupUin.Value, ct, force: force);
await CacheUid(groupUin.Value, cancellationToken, force: force);

return _uinToUid.FirstOrDefault(x => x.Value == friendUid).Key;
}

public async Task<List<BotGroupMember>> GetCachedMembers(uint groupUin, bool refreshCache, CancellationToken ct)
public async Task<List<BotGroupMember>> GetCachedMembers(uint groupUin, bool refreshCache, CancellationToken cancellationToken)
{
if (!_cachedGroupMembers.TryGetValue(groupUin, out var members) || refreshCache)
{
await ResolveMembersUid(groupUin, ct);
await ResolveMembersUid(groupUin, cancellationToken);
return _cachedGroupMembers.TryGetValue(groupUin, out members) ? members : new List<BotGroupMember>();
}
return members;
}

public async Task<List<BotFriend>> GetCachedFriends(bool refreshCache, CancellationToken ct)
public async Task<List<BotFriend>> GetCachedFriends(bool refreshCache, CancellationToken cancellationToken)
{
if (_cachedFriends.Count == 0 || refreshCache) await ResolveFriendsUidAndFriendGroups(ct);
if (_cachedFriends.Count == 0 || refreshCache) await ResolveFriendsUidAndFriendGroups(cancellationToken);
return _cachedFriends;
}

public async Task<BotUserInfo?> GetCachedUsers(uint uin, bool refreshCache, CancellationToken ct)
public async Task<BotUserInfo?> GetCachedUsers(uint uin, bool refreshCache, CancellationToken cancellationToken)
{
if (!_cacheUsers.ContainsKey(uin) || refreshCache) await ResolveUser(uin, ct);
if (!_cacheUsers.ContainsKey(uin) || refreshCache) await ResolveUser(uin, cancellationToken);
if (!_cacheUsers.TryGetValue(uin, out BotUserInfo? info)) return null;
return info;
}

private async Task CacheUid(uint groupUin, CancellationToken ct, bool force = false)
private async Task CacheUid(uint groupUin, CancellationToken cancellationToken, bool force = false)
{
if (!_cachedGroups.Contains(groupUin) || force)
{
Collection.Log.LogVerbose(Tag, $"Caching group members: {groupUin}");
await ResolveMembersUid(groupUin, ct);
await ResolveMembersUid(groupUin, cancellationToken);
_cachedGroups.Add(groupUin);
}
}

private async Task ResolveFriendsUidAndFriendGroups(CancellationToken ct)
private async Task ResolveFriendsUidAndFriendGroups(CancellationToken cancellationToken)
{
uint? next = null;
var friends = new List<BotFriend>();
var friendGroups = new Dictionary<uint, string>();
do
{
var @event = FetchFriendsEvent.Create(next);
var results = await Collection.Business.SendEvent(@event, ct);
var results = await Collection.Business.SendEvent(@event, cancellationToken);

if (results.Count == 0)
{
Expand All @@ -153,10 +153,10 @@ private async Task ResolveFriendsUidAndFriendGroups(CancellationToken ct)
_cachedFriends.AddRange(friends);
}

private async Task ResolveMembersUid(uint groupUin, CancellationToken ct)
private async Task ResolveMembersUid(uint groupUin, CancellationToken cancellationToken)
{
var fetchFriendsEvent = FetchMembersEvent.Create(groupUin);
var events = await Collection.Business.SendEvent(fetchFriendsEvent, ct);
var events = await Collection.Business.SendEvent(fetchFriendsEvent, cancellationToken);

if (events.Count != 0)
{
Expand All @@ -166,7 +166,7 @@ private async Task ResolveMembersUid(uint groupUin, CancellationToken ct)
while (token != null)
{
var next = FetchMembersEvent.Create(groupUin, token);
var results = await Collection.Business.SendEvent(next, ct);
var results = await Collection.Business.SendEvent(next, cancellationToken);
@event.Members.AddRange(((FetchMembersEvent)results[0]).Members);
token = ((FetchMembersEvent)results[0]).Token;
}
Expand All @@ -181,13 +181,13 @@ private async Task ResolveMembersUid(uint groupUin, CancellationToken ct)
}
}

private async Task ResolveUser(uint uin, CancellationToken ct)
private async Task ResolveUser(uint uin, CancellationToken cancellationToken)
{
var events = await Collection.Business.SendEvent(FetchUserInfoEvent.Create(uin), ct);
var events = await Collection.Business.SendEvent(FetchUserInfoEvent.Create(uin), cancellationToken);

if (events.Count != 0 && events[0] is FetchUserInfoEvent { } @event)
{
_cacheUsers.AddOrUpdate(uin, @event.UserInfo, (_key, _value) => @event.UserInfo);
}
}
}
}
Loading

0 comments on commit 2b8d104

Please sign in to comment.