diff --git a/Lagrange.Core/Internal/Service/Message/PushMessageService.cs b/Lagrange.Core/Internal/Service/Message/PushMessageService.cs index 5fc8efc31..fb1cfed08 100644 --- a/Lagrange.Core/Internal/Service/Message/PushMessageService.cs +++ b/Lagrange.Core/Internal/Service/Message/PushMessageService.cs @@ -26,12 +26,18 @@ protected override bool Parse(byte[] input, BotKeystore keystore, BotAppInfo app extraEvents = new List(); switch (packetType) { - case PkgType.PrivateMessage or PkgType.GroupMessage or PkgType.PrivateFileMessage: + case PkgType.PrivateMessage or PkgType.GroupMessage: { var chain = MessagePacker.Parse(message.Message); output = PushMessageEvent.Create(chain); break; } + case PkgType.PrivateFileMessage: + { + var chain = MessagePacker.ParsePrivateFile(message.Message); + output = PushMessageEvent.Create(chain); + break; + } case PkgType.GroupInviteNotice: { if (message.Message.Body?.MsgContent == null) return false; diff --git a/Lagrange.Core/Message/Entity/FileEntity.cs b/Lagrange.Core/Message/Entity/FileEntity.cs index bf205eeba..99f3bd551 100644 --- a/Lagrange.Core/Message/Entity/FileEntity.cs +++ b/Lagrange.Core/Message/Entity/FileEntity.cs @@ -42,7 +42,7 @@ public FileEntity(string path) FileName = Path.GetFileName(path); } - private FileEntity(long fileSize, string fileName, byte[] fileMd5, string fileUuid, string fileHash) + internal FileEntity(long fileSize, string fileName, byte[] fileMd5, string fileUuid, string fileHash) { FileSize = fileSize; FileName = fileName; @@ -82,15 +82,5 @@ private FileEntity(long fileSize, string fileName, byte[] fileMd5, string fileUu return null; } - IMessageEntity? IMessageEntity.UnpackMessageContent(ReadOnlySpan content) - { - var extra = Serializer.Deserialize(content); - var file = extra.File; - - return file is { FileSize: not null, FileName: not null, FileMd5: not null, FileUuid: not null, FileHash: not null } - ? new FileEntity((long)file.FileSize, file.FileName, file.FileMd5, file.FileUuid, file.FileHash) - : null; - } - public string ToPreviewString() => $"[File] {FileName} ({FileSize}): {FileUrl ?? "failed to receive file url"}"; } \ No newline at end of file diff --git a/Lagrange.Core/Message/IMessageEntity.cs b/Lagrange.Core/Message/IMessageEntity.cs index 7d007a661..5214fd5ae 100644 --- a/Lagrange.Core/Message/IMessageEntity.cs +++ b/Lagrange.Core/Message/IMessageEntity.cs @@ -16,8 +16,6 @@ public interface IMessageEntity internal IMessageEntity? UnpackElement(Elem elem); // abstract method - internal IMessageEntity? UnpackMessageContent(ReadOnlySpan content) => null; // virtual method - internal void SetSelfUid(string selfUid) { } // virtual method public string ToPreviewString(); // abstract method diff --git a/Lagrange.Core/Message/MessagePacker.cs b/Lagrange.Core/Message/MessagePacker.cs index 19d78dc71..e318eb45e 100644 --- a/Lagrange.Core/Message/MessagePacker.cs +++ b/Lagrange.Core/Message/MessagePacker.cs @@ -1,6 +1,7 @@ using System.Reflection; using Lagrange.Core.Internal.Packets.Message.C2C; using Lagrange.Core.Internal.Packets.Message.Component; +using Lagrange.Core.Internal.Packets.Message.Component.Extra; using Lagrange.Core.Internal.Packets.Message.Element; using Lagrange.Core.Internal.Packets.Message.Routing; using ProtoBuf; @@ -115,35 +116,36 @@ public static MessageChain Parse(PushMsgBody message) { foreach (var expectElem in expectElems) { - var val = expectElem.GetValueByExpr(element); - if (val != null) + if (expectElem.GetValueByExpr(element) is not null && + Factory[entityType].UnpackElement(element) is { } entity) { - var entity = Factory[entityType].UnpackElement(element); - if (entity != null) - { - chain.Add(entity); - break; - } + chain.Add(entity); + break; } } } } } - if (message.Body is { MsgContent: not null }) + return chain; + } + + public static MessageChain ParsePrivateFile(PushMsgBody message) + { + if (message.Body?.MsgContent == null) throw new Exception(); + + var chain = ParseChain(message); + + var extra = Serializer.Deserialize(message.Body.MsgContent.AsSpan()); + var file = extra.File; + + if ( file is { FileSize: not null, FileName: not null, FileMd5: not null, FileUuid: not null, FileHash: not null }) { - foreach (var factory in MsgFactory) - { - var entity = factory.UnpackMessageContent(message.Body.MsgContent); - if (entity != null) - { - chain.Add(entity); - break; - } - } + chain.Add(new FileEntity((long)file.FileSize, file.FileName, file.FileMd5, file.FileUuid, file.FileHash)); + return chain; } - return chain; + throw new Exception(); } private static Internal.Packets.Message.Message BuildPacketBase(MessageChain chain) => new()