Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
sisi0318 committed Jul 27, 2024
2 parents 82fdd36 + 2860a87 commit 4f195f1
Show file tree
Hide file tree
Showing 64 changed files with 254 additions and 2,232 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/Lagrange.OneBot-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ jobs:
uses: actions/upload-artifact@v4
if: github.event_name != 'pull_request'
with:
name: Lagrange.OneBot_${{ matrix.runtimeIdentifier }}_7.0
name: Lagrange.OneBot_${{ matrix.runtimeIdentifier }}_net7.0_NoSelfContained
path: Lagrange.OneBot/bin/Debug/net7.0/${{ matrix.runtimeIdentifier }}/publish

- name: Upload binary files(${{ matrix.runtimeIdentifier }}) for .NET 8.0
uses: actions/upload-artifact@v4
if: github.event_name != 'pull_request'
with:
name: Lagrange.OneBot_${{ matrix.runtimeIdentifier }}_8.0
name: Lagrange.OneBot_${{ matrix.runtimeIdentifier }}_net8.0_NoSelfContained
path: Lagrange.OneBot/bin/Release/net8.0/${{ matrix.runtimeIdentifier }}/publish
97 changes: 97 additions & 0 deletions .github/workflows/Lagrange.OneBot-release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
name: Lagrange.OneBot Release

on:
workflow_dispatch:

jobs:
Build:
runs-on: windows-latest

strategy:
matrix:
runtimeIdentifier:
[
win-x64,
win-x86,
linux-x64,
linux-arm,
linux-arm64,
osx-x64,
osx-arm64,
linux-musl-x64,
linux-musl-arm,
linux-musl-arm64,
]

steps:
- uses: actions/checkout@v4

- name: Install .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'

- name: Build Lagrange.OneBot .NET 8.0
shell: powershell
run: |
dotnet publish Lagrange.OneBot/Lagrange.OneBot.csproj --self-contained -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true -p:DebugType=none -p:RuntimeIdentifier=${{ matrix.runtimeIdentifier }} --framework net8.0
- name: Upload binary files(${{ matrix.runtimeIdentifier }}) for .NET 8.0
uses: actions/upload-artifact@v3
with:
name: Lagrange.OneBot_${{ matrix.runtimeIdentifier }}_8.0
path: Lagrange.OneBot/bin/Release/net8.0/${{ matrix.runtimeIdentifier }}/publish

- name: Compress files
run: Compress-Archive -Path 'Lagrange.OneBot\bin\Release\net8.0\${{ matrix.runtimeIdentifier }}\publish' -DestinationPath 'Lagrange.OneBot_${{ matrix.runtimeIdentifier }}_net8.0_SelfContained.zip'

- name: Prepare release files
uses: actions/upload-artifact@v3
with:
name: Lagrange.OneBot_all
path: Lagrange.OneBot_${{ matrix.runtimeIdentifier }}_net8.0_SelfContained.zip

NightlyRelease:
name: Nightly Release
runs-on: ubuntu-latest
needs: Build
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Update Nightly Tag
run: |
git tag -f nightly
git push -f origin nightly
- name: Download Artifacts
uses: actions/download-artifact@v3
with:
name: Lagrange.OneBot_all
path: ./bin
- name: Update Nightly Release
uses: ncipollo/release-action@v1
with:
tag: nightly
name: "Nightly Release"
prerelease: true
body: "> ⚠️This is a nightly release.\n> ⚠️This is not the latest version."
artifacts: |
./bin/*
allowUpdates: true
removeArtifacts: true

ClearTempArtifacts:
name: Clear Temp Artifacts
if: always()
runs-on: ubuntu-latest
needs:
- NightlyRelease
permissions:
actions: write
steps:
- uses: geekyeggo/delete-artifact@v2
with:
name: |
Lagrange.OneBot_all
6 changes: 0 additions & 6 deletions Lagrange.Core.sln
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "SolutionI
LICENSE = LICENSE
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lagrange.Kritor", "Lagrange.Kritor\Lagrange.Kritor.csproj", "{FD0A15C0-600E-44A9-BAA8-6FAE0A1CA7EE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -38,10 +36,6 @@ Global
{37AEDD3B-9B9F-4782-ADD5-BA2436FB2507}.Debug|Any CPU.Build.0 = Debug|Any CPU
{37AEDD3B-9B9F-4782-ADD5-BA2436FB2507}.Release|Any CPU.ActiveCfg = Release|Any CPU
{37AEDD3B-9B9F-4782-ADD5-BA2436FB2507}.Release|Any CPU.Build.0 = Release|Any CPU
{FD0A15C0-600E-44A9-BAA8-6FAE0A1CA7EE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FD0A15C0-600E-44A9-BAA8-6FAE0A1CA7EE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD0A15C0-600E-44A9-BAA8-6FAE0A1CA7EE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD0A15C0-600E-44A9-BAA8-6FAE0A1CA7EE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 5 additions & 1 deletion Lagrange.Core/Common/Entity/BotFriend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ internal BotFriend()
Nickname = string.Empty;
Remarks = string.Empty;
PersonalSign = string.Empty;
Qid = string.Empty;
}

internal BotFriend(uint uin,string uid, string nickname, string remarks, string personalSign)
internal BotFriend(uint uin,string uid, string nickname, string remarks, string personalSign, string qid)
{
Uin = uin;
Uid = uid;
Nickname = nickname;
Remarks = remarks;
PersonalSign = personalSign;
Qid = qid;
}

public uint Uin { get; set; }
Expand All @@ -32,6 +34,8 @@ internal BotFriend(uint uin,string uid, string nickname, string remarks, string
public string Remarks { get; set; }

public string PersonalSign { get; set; }

public string Qid { get; set; }

public string Avatar => $"https://q1.qlogo.cn/g?b=qq&nk={Uin}&s=640";
}
8 changes: 4 additions & 4 deletions Lagrange.Core/Common/Entity/BotUserInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ namespace Lagrange.Core.Common.Entity;

public class BotUserInfo
{
internal BotUserInfo(uint uin, string uid, string nickname, DateTime birthday, string city, string country, string school, uint age, DateTime registerTime, uint gender)
internal BotUserInfo(uint uin, string nickname, DateTime birthday, string city, string country, string school, uint age, DateTime registerTime, uint gender, string? qid)
{
Uin = uin;
Uid = uid;
Avatar = $"https://q1.qlogo.cn/g?b=qq&nk={Uin}&s=640";
Nickname = nickname;
Birthday = birthday;
Expand All @@ -15,12 +14,11 @@ internal BotUserInfo(uint uin, string uid, string nickname, DateTime birthday, s
Age = age;
RegisterTime = registerTime;
Gender = (GenderInfo)gender;
Qid = qid;
}

public uint Uin { get; set; }

internal string Uid { get; set; }

public string Avatar { get; set; }

public string Nickname { get; set; }
Expand All @@ -38,6 +36,8 @@ internal BotUserInfo(uint uin, string uid, string nickname, DateTime birthday, s
public DateTime RegisterTime { get; set; }

public GenderInfo Gender { get; set; }

public string? Qid { get; set; }

public enum GenderInfo
{
Expand Down
4 changes: 2 additions & 2 deletions Lagrange.Core/Common/Interface/Api/OperationExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ public static Task<bool> Like(this BotContext bot, uint targetUin, uint count =
return bot.ContextCollection.Business.OperationLogic.GetRoamMessage(targetChain.FriendUin, timestamp, count);
}

public static Task<BotUserInfo?> FetchUserInfo(this BotContext bot, uint uin)
=> bot.ContextCollection.Business.OperationLogic.FetchUserInfo(uin);
public static Task<BotUserInfo?> FetchUserInfo(this BotContext bot, uint uin, bool refreshCache = false)
=> bot.ContextCollection.Business.OperationLogic.FetchUserInfo(uin, refreshCache);

public static Task<List<string>?> FetchCustomFace(this BotContext bot)
=> bot.ContextCollection.Business.OperationLogic.FetchCustomFace();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Concurrent;
using Lagrange.Core.Common.Entity;
using Lagrange.Core.Internal.Context.Attributes;
using Lagrange.Core.Internal.Event;
Expand All @@ -22,6 +23,8 @@ internal class CachingLogic : LogicBase
private readonly List<BotFriend> _cachedFriends;
private readonly Dictionary<uint, List<BotGroupMember>> _cachedGroupMembers;

private readonly ConcurrentDictionary<uint, BotUserInfo> _cacheUsers;

internal CachingLogic(ContextCollection collection) : base(collection)
{
_uinToUid = new Dictionary<uint, string>();
Expand All @@ -30,6 +33,8 @@ internal CachingLogic(ContextCollection collection) : base(collection)

_cachedFriends = new List<BotFriend>();
_cachedGroupMembers = new Dictionary<uint, List<BotGroupMember>>();

_cacheUsers = new();
}

public override Task Incoming(ProtocolEvent e)
Expand Down Expand Up @@ -96,6 +101,13 @@ public async Task<List<BotFriend>> GetCachedFriends(bool refreshCache)
return _cachedFriends;
}

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

private async Task CacheUid(uint groupUin, bool force = false)
{
if (!_cachedGroups.Contains(groupUin) || force)
Expand All @@ -110,7 +122,7 @@ private async Task ResolveFriendsUid()
{
var fetchFriendsEvent = FetchFriendsEvent.Create();
var events = await Collection.Business.SendEvent(fetchFriendsEvent);

if (events.Count != 0)
{
var @event = (FetchFriendsEvent)events[0];
Expand All @@ -123,7 +135,7 @@ private async Task ResolveFriendsUid()
@event.Friends.AddRange(((FetchFriendsEvent)results[0]).Friends);
nextUin = ((FetchFriendsEvent)results[0]).NextUin;
}

foreach (var friend in @event.Friends) _uinToUid.TryAdd(friend.Uin, friend.Uid);
_cachedFriends.Clear();
_cachedFriends.AddRange(@event.Friends);
Expand Down Expand Up @@ -161,4 +173,14 @@ private async Task ResolveMembersUid(uint groupUin)
Collection.Log.LogWarning(Tag, $"Failed to resolve group {groupUin} members uid and cache.");
}
}

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

if (events.Count != 0 && events[0] is FetchUserInfoEvent { } @event)
{
_cacheUsers.AddOrUpdate(uin, @event.UserInfo, (_key, _value) => @event.UserInfo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,9 @@ public async Task<bool> GroupSetSpecialTitle(uint groupUin, uint targetUin, stri
return events.Count != 0 && ((GroupSetSpecialTitleEvent)events[0]).ResultCode == 0;
}

public async Task<BotUserInfo?> FetchUserInfo(uint uin)
public async Task<BotUserInfo?> FetchUserInfo(uint uin, bool refreshCache = false)
{
string? uid = await Collection.Business.CachingLogic.ResolveUid(null, uin);
if (uid == null) return null;

var fetchUserInfoEvent = FetchUserInfoEvent.Create(uid);
var events = await Collection.Business.SendEvent(fetchUserInfoEvent);
return events.Count != 0 ? ((FetchUserInfoEvent)events[0]).UserInfo : null;
return await Collection.Business.CachingLogic.GetCachedUsers(uin, refreshCache);
}

public async Task<bool> SetMessageReaction(uint groupUin, uint sequence, string code)
Expand Down
11 changes: 8 additions & 3 deletions Lagrange.Core/Internal/Event/System/FetchUserInfoEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ internal class FetchUserInfoEvent : ProtocolEvent
{
public BotUserInfo UserInfo { get; }

public string Uid { get; }
public uint Uin { get; }

public string? Uid { get; }

private FetchUserInfoEvent(string uid) : base(true)
private FetchUserInfoEvent(uint uin, string? uid) : base(true)
{
Uin = uin;
Uid = uid;
}

Expand All @@ -20,7 +23,9 @@ private FetchUserInfoEvent(int resultCode, BotUserInfo userInfo) : base(resultCo
UserInfo = userInfo;
}

public static FetchUserInfoEvent Create(string uid) => new(uid);
public static FetchUserInfoEvent Create(uint uid) => new(uid, null);

public static FetchUserInfoEvent Create(string uid) => new(0, uid);

public static FetchUserInfoEvent Result(int resultCode, BotUserInfo userInfo) =>
new(resultCode, userInfo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ internal class OidbSvcTrpcTcp0xFE1_2
[ProtoMember(3)] public List<OidbSvcTrpcTcp0xFE1_2Key>? Keys { get; set; } // can be regarded as constants
}

[ProtoContract]
internal class OidbSvcTrpcTcp0xFE1_2Uin
{
[ProtoMember(1)] public uint Uin { get; set; }

[ProtoMember(2)] public uint Field2 { get; set; }

[ProtoMember(3)] public List<OidbSvcTrpcTcp0xFE1_2Key>? Keys { get; set; } // can be regarded as constants
}

[ProtoContract]
internal class OidbSvcTrpcTcp0xFE1_2Key
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class OidbSvcTrpcTcp0xFE1_2Response
[ProtoContract]
internal class OidbSvcTrpcTcp0xFE1_2ResponseBody
{
[ProtoMember(1)] public string Uid { get; set; } = "";
// [ProtoMember(1)] public string Uid { get; set; } = "";

[ProtoMember(2)] public OidbSvcTrpcTcp0xFE1_2ResponseProperty Properties { get; set; }

Expand Down
5 changes: 3 additions & 2 deletions Lagrange.Core/Internal/Service/System/FetchFriendsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ protected override bool Build(FetchFriendsEvent input, BotKeystore keystore, Bot
FriendCount = MaxFriendCount, // max value
Body = new List<OidbSvcTrpcTcp0xFD4_1Body>
{
new() { Type = 1, Number = new OidbNumber { Numbers = { 103, 102, 20002 } } },
new() { Type = 1, Number = new OidbNumber { Numbers = { 103, 102, 20002, 27394 } } },
new() { Type = 4, Number = new OidbNumber { Numbers = { 100, 101, 102 } } }
}
});
Expand All @@ -38,6 +38,7 @@ protected override bool Build(FetchFriendsEvent input, BotKeystore keystore, Bot
* 102:个性签名
* 103:备注
* 20002:昵称
* 27394:QID
*/

output = packet.Serialize();
Expand All @@ -55,7 +56,7 @@ protected override bool Parse(Span<byte> input, BotKeystore keystore, BotAppInfo
{
var additional = raw.Additional.First(x => x.Type == 1);
var properties = Property(additional.Layer1.Properties);
friends.Add(new BotFriend(raw.Uin, raw.Uid, properties[20002], properties[103], properties[102]));
friends.Add(new BotFriend(raw.Uin, raw.Uid, properties[20002], properties[103], properties[102], properties[27394]));
}

output = FetchFriendsEvent.Result(0, friends, packet.Body.Next?.Uin); // 全家4完了才能想出来这种分页的逻辑
Expand Down
Loading

0 comments on commit 4f195f1

Please sign in to comment.