Skip to content

Commit

Permalink
[Core] Parse PrivateFile in MessagePacker instead of FileEntity
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan authored and Linwenxuan committed Oct 28, 2023
1 parent c79b98c commit 6b07b91
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 33 deletions.
8 changes: 7 additions & 1 deletion Lagrange.Core/Internal/Service/Message/PushMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,18 @@ protected override bool Parse(byte[] input, BotKeystore keystore, BotAppInfo app
extraEvents = new List<ProtocolEvent>();
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;
Expand Down
12 changes: 1 addition & 11 deletions Lagrange.Core/Message/Entity/FileEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -82,15 +82,5 @@ private FileEntity(long fileSize, string fileName, byte[] fileMd5, string fileUu
return null;
}

IMessageEntity? IMessageEntity.UnpackMessageContent(ReadOnlySpan<byte> content)
{
var extra = Serializer.Deserialize<FileExtra>(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"}";
}
2 changes: 0 additions & 2 deletions Lagrange.Core/Message/IMessageEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public interface IMessageEntity

internal IMessageEntity? UnpackElement(Elem elem); // abstract method

internal IMessageEntity? UnpackMessageContent(ReadOnlySpan<byte> content) => null; // virtual method

internal void SetSelfUid(string selfUid) { } // virtual method

public string ToPreviewString(); // abstract method
Expand Down
40 changes: 21 additions & 19 deletions Lagrange.Core/Message/MessagePacker.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<FileExtra>(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()
Expand Down

0 comments on commit 6b07b91

Please sign in to comment.