Skip to content

Commit

Permalink
[Core] Fixed that rkey may be null for NTV2RichMedia
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan04 committed Feb 29, 2024
1 parent 7b76c9f commit 61929c6
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 65 deletions.
133 changes: 70 additions & 63 deletions Lagrange.Core/Internal/Context/Logic/Implementation/MessagingLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,85 +187,92 @@ public override async Task Outgoing(ProtocolEvent e)

private async Task ResolveIncomingChain(MessageChain chain)
{
if (chain.HasTypeOf<FileEntity>())
foreach (var entity in chain) switch (entity)
{
var file = chain.GetEntity<FileEntity>();
if (file?.IsGroup != false || file.FileHash == null || file.FileUuid == null) return;

var @event = FileDownloadEvent.Create(file.FileUuid, file.FileHash, chain.Uid, chain.SelfUid);
var results = await Collection.Business.SendEvent(@event);
if (results.Count != 0)
case FileEntity { IsGroup: true, FileHash: not null, FileUuid: not null } file:
{
var result = (FileDownloadEvent)results[0];
file.FileUrl = result.FileUrl;
var @event = FileDownloadEvent.Create(file.FileUuid, file.FileHash, chain.Uid, chain.SelfUid);
var results = await Collection.Business.SendEvent(@event);
if (results.Count != 0)
{
var result = (FileDownloadEvent)results[0];
file.FileUrl = result.FileUrl;
}

break;
}
}

if (chain.HasTypeOf<MultiMsgEntity>())
{
var multi = chain.GetEntity<MultiMsgEntity>();
if (multi?.ResId == null) return;

var @event = MultiMsgDownloadEvent.Create(chain.Uid ?? "", multi.ResId);
var results = await Collection.Business.SendEvent(@event);
if (results.Count != 0)
case MultiMsgEntity { ResId: not null } multi:
{
var result = (MultiMsgDownloadEvent)results[0];
multi.Chains.AddRange((IEnumerable<MessageChain>?)result.Chains ?? Array.Empty<MessageChain>());
var @event = MultiMsgDownloadEvent.Create(chain.Uid ?? "", multi.ResId);
var results = await Collection.Business.SendEvent(@event);
if (results.Count != 0)
{
var result = (MultiMsgDownloadEvent)results[0];
multi.Chains.AddRange((IEnumerable<MessageChain>?)result.Chains ?? Array.Empty<MessageChain>());
}

break;
}
}

if (chain.HasTypeOf<RecordEntity>())
{
var record = chain.GetEntity<RecordEntity>();
if (record?.MsgInfo == null) return;

string uid = (chain.IsGroup
? await Collection.Business.CachingLogic.ResolveUid(chain.GroupUin, chain.FriendUin)
: chain.Uid) ?? "";

var @event = chain.IsGroup
? RecordGroupDownloadEvent.Create(chain.GroupUin ?? 0, record.MsgInfo)
: RecordDownloadEvent.Create(uid, record.MsgInfo);

var results = await Collection.Business.SendEvent(@event);
if (results.Count != 0)
case RecordEntity { MsgInfo: not null } record:
{
var result = (RecordDownloadEvent)results[0];
record.AudioUrl = result.AudioUrl;
var @event = chain.IsGroup
? RecordGroupDownloadEvent.Create(chain.GroupUin ?? 0, record.MsgInfo)
: RecordDownloadEvent.Create(chain.Uid ?? string.Empty, record.MsgInfo);

var results = await Collection.Business.SendEvent(@event);
if (results.Count != 0)
{
var result = (RecordDownloadEvent)results[0];
record.AudioUrl = result.AudioUrl;
}

break;
}
}

if (chain.HasTypeOf<VideoEntity>())
{
var video = chain.GetEntity<VideoEntity>();
if (video?.VideoUuid == null) return;

string uid = (chain.IsGroup
? await Collection.Business.CachingLogic.ResolveUid(chain.GroupUin, chain.FriendUin)
: chain.Uid) ?? "";
var @event = VideoDownloadEvent.Create(video.VideoUuid, uid, video.FilePath, "", "",chain.IsGroup);
var results = await Collection.Business.SendEvent(@event);
if (results.Count != 0)
case VideoEntity { VideoUuid: not null } video:
{
var result = (VideoDownloadEvent)results[0];
video.VideoUrl = result.AudioUrl;
string uid = (chain.IsGroup
? await Collection.Business.CachingLogic.ResolveUid(chain.GroupUin, chain.FriendUin)
: chain.Uid) ?? "";
var @event = VideoDownloadEvent.Create(video.VideoUuid, uid, video.FilePath, "", "",chain.IsGroup);
var results = await Collection.Business.SendEvent(@event);
if (results.Count != 0)
{
var result = (VideoDownloadEvent)results[0];
video.VideoUrl = result.AudioUrl;
}

break;
}
}

foreach (var mention in chain.OfType<MentionEntity>())
{
if (chain is { IsGroup: true, GroupUin: not null })
case MentionEntity mention when chain is { IsGroup: true, GroupUin: not null }:
{
var members = await Collection.Business.CachingLogic.GetCachedMembers(chain.GroupUin.Value, false);
var member = members.FirstOrDefault(x => x.Uin == mention.Uin)?.MemberCard;
string? member = members.FirstOrDefault(x => x.Uin == mention.Uin)?.MemberCard;
if (member != null) mention.Name = $"@{member}";

break;
}
else
case MentionEntity mention when chain is { IsGroup: false }:
{
var friends = await Collection.Business.CachingLogic.GetCachedFriends(false);
var friend = friends.FirstOrDefault(x => x.Uin == mention.Uin)?.Nickname;
string? friend = friends.FirstOrDefault(x => x.Uin == mention.Uin)?.Nickname;
if (friend != null) mention.Name = $"@{friend}";

break;
}
case ImageEntity image when !image.ImageUrl.Contains("&rkey=") && image.MsgInfo is not null:
{
var @event = chain.IsGroup
? ImageGroupDownloadEvent.Create(chain.GroupUin ?? 0, image.MsgInfo)
: ImageDownloadEvent.Create(chain.Uid ?? string.Empty, image.MsgInfo);

var results = await Collection.Business.SendEvent(@event);
if (results.Count != 0)
{
var result = (ImageDownloadEvent)results[0];
image.ImageUrl = result.ImageUrl;
}

break;
}
}
}
Expand Down
29 changes: 29 additions & 0 deletions Lagrange.Core/Internal/Event/Message/ImageDownloadEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Lagrange.Core.Internal.Packets.Service.Oidb.Common;

namespace Lagrange.Core.Internal.Event.Message;

#pragma warning disable CS8618

internal class ImageDownloadEvent : ProtocolEvent
{
public string SelfUid { get; }

public IndexNode Node { get; }

public string ImageUrl { get; }

protected ImageDownloadEvent(string selfUid, MsgInfo info) : base(true)
{
SelfUid = selfUid;
Node = info.MsgInfoBody[0].Index;
}

protected ImageDownloadEvent(int resultCode, string imageUrl) : base(resultCode)
{
ImageUrl = imageUrl;
}

public static ImageDownloadEvent Create(string selfUid, MsgInfo info) => new(selfUid, info);

public static ImageDownloadEvent Result(int resultCode, string url) => new(resultCode, url);
}
19 changes: 19 additions & 0 deletions Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Lagrange.Core.Internal.Packets.Service.Oidb.Common;

namespace Lagrange.Core.Internal.Event.Message;

internal class ImageGroupDownloadEvent : ImageDownloadEvent
{
public uint GroupUin { get; }

private ImageGroupDownloadEvent(uint groupUin, MsgInfo info) : base("", info)
{
GroupUin = groupUin;
}

private ImageGroupDownloadEvent(int resultCode, string imageUrl) : base(resultCode, imageUrl) { }

public new static ImageGroupDownloadEvent Create(uint groupUin, MsgInfo info) => new(groupUin, info);

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / build

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / build

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (osx-x64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (osx-x64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (win-x64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (win-x64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (osx-arm64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (osx-arm64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (linux-arm)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (linux-arm)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (linux-musl-arm64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (linux-musl-arm64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (win-x86)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (win-x86)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (linux-musl-x64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (linux-musl-x64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (linux-x64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (linux-x64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (linux-arm64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

Check warning on line 16 in Lagrange.Core/Internal/Event/Message/ImageGroupDownloadEvent.cs

View workflow job for this annotation

GitHub Actions / Build (linux-arm64)

The member 'ImageGroupDownloadEvent.Create(uint, MsgInfo)' does not hide an accessible member. The new keyword is not required.

public new static ImageGroupDownloadEvent Result(int resultCode, string url) => new(resultCode, url);
}
72 changes: 72 additions & 0 deletions Lagrange.Core/Internal/Service/Message/ImageDownloadService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Lagrange.Core.Common;
using Lagrange.Core.Internal.Event;
using Lagrange.Core.Internal.Event.Message;
using Lagrange.Core.Internal.Packets.Service.Oidb;
using Lagrange.Core.Internal.Packets.Service.Oidb.Common;
using Lagrange.Core.Utility.Binary;
using Lagrange.Core.Utility.Extension;
using ProtoBuf;

namespace Lagrange.Core.Internal.Service.Message;

[EventSubscribe(typeof(ImageDownloadEvent))]
[Service("OidbSvcTrpcTcp.0x11c5_200")]
internal class ImageDownloadService : BaseService<ImageDownloadEvent>
{
protected override bool Build(ImageDownloadEvent input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
out BinaryPacket output, out List<BinaryPacket>? extraPackets)
{
var packet = new OidbSvcTrpcTcpBase<NTV2RichMediaReq>(new NTV2RichMediaReq
{
ReqHead = new MultiMediaReqHead
{
Common = new CommonHead
{
RequestId = 1,
Command = 200
},
Scene = new SceneInfo
{
RequestType = 2,
BusinessType = 1,
SceneType = 1,
C2C = new C2CUserInfo
{
AccountType = 2,
TargetUid = input.SelfUid
}
},
Client = new ClientMeta { AgentType = 2 }
},
Download = new DownloadReq
{
Node = input.Node,
Download = new DownloadExt
{
Video = new VideoDownloadExt
{
BusiType = 0,
SceneType = 0
}
}
}
}, 0x11c5, 200, false, true);

output = packet.Serialize();
extraPackets = null;

return true;
}

protected override bool Parse(byte[] input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
out ImageDownloadEvent output, out List<ProtocolEvent>? extraEvents)
{
var payload = Serializer.Deserialize<OidbSvcTrpcTcpResponse<NTV2RichMediaResp>>(input.AsSpan());
var body = payload.Body.Download;
string url = $"https://{body.Info.Domain}{body.Info.UrlPath}{body.RKeyParam}";

output = ImageDownloadEvent.Result((int)payload.ErrorCode, url);
extraEvents = null;
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using Lagrange.Core.Common;
using Lagrange.Core.Internal.Event;
using Lagrange.Core.Internal.Event.Message;
using Lagrange.Core.Internal.Packets.Service.Oidb;
using Lagrange.Core.Internal.Packets.Service.Oidb.Common;
using Lagrange.Core.Utility.Binary;
using Lagrange.Core.Utility.Extension;
using ProtoBuf;

namespace Lagrange.Core.Internal.Service.Message;

[EventSubscribe(typeof(ImageGroupDownloadEvent))]
[Service("OidbSvcTrpcTcp.0x11c4_200")]
internal class ImageGroupDownloadService : BaseService<ImageGroupDownloadEvent>
{
protected override bool Build(ImageGroupDownloadEvent input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
out BinaryPacket output, out List<BinaryPacket>? extraPackets)
{
var packet = new OidbSvcTrpcTcpBase<NTV2RichMediaReq>(new NTV2RichMediaReq
{
ReqHead = new MultiMediaReqHead
{
Common = new CommonHead
{
RequestId = 1,
Command = 200
},
Scene = new SceneInfo
{
RequestType = 2,
BusinessType = 1,
SceneType = 2,
Group = new GroupInfo { GroupUin = input.GroupUin }
},
Client = new ClientMeta { AgentType = 2 }
},
Download = new DownloadReq
{
Node = input.Node,
Download = new DownloadExt
{
Video = new VideoDownloadExt
{
BusiType = 0,
SceneType = 0
}
}
}
}, 0x11c4, 200, false, true);

output = packet.Serialize();
extraPackets = null;

return true;
}

protected override bool Parse(byte[] input, BotKeystore keystore, BotAppInfo appInfo, BotDeviceInfo device,
out ImageGroupDownloadEvent output, out List<ProtocolEvent>? extraEvents)
{
var payload = Serializer.Deserialize<OidbSvcTrpcTcpResponse<NTV2RichMediaResp>>(input.AsSpan());
var body = payload.Body.Download;
string url = $"https://{body.Info.Domain}{body.Info.UrlPath}{body.RKeyParam}";

output = ImageGroupDownloadEvent.Result((int)payload.ErrorCode, url);
extraEvents = null;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Lagrange.Core.Utility.Binary;
using Lagrange.Core.Utility.Extension;
using ProtoBuf;
using FileInfo = Lagrange.Core.Internal.Packets.Service.Oidb.Common.FileInfo;

namespace Lagrange.Core.Internal.Service.Message;

Expand Down
3 changes: 2 additions & 1 deletion Lagrange.Core/Message/Entity/ImageEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ IEnumerable<Elem> IMessageEntity.PackElement()
PictureSize = new Vector2(info.Width, info.Height),
FilePath = info.FileName,
ImageSize = info.FileSize,
ImageUrl = url
ImageUrl = url,
MsgInfo = extra
};
}

Expand Down

0 comments on commit 61929c6

Please sign in to comment.