Skip to content

Commit

Permalink
[OneBot] Fix private chat file message push and get_msg to get the …
Browse files Browse the repository at this point in the history
…private chat file record (#706)

* [OneBot] impl FileSegment

Signed-off-by: ishkong <[email protected]>

* [Core] Save key information needed to refresh private chat file links

Signed-off-by: ishkong <[email protected]>

---------

Signed-off-by: ishkong <[email protected]>
  • Loading branch information
ishkong authored Dec 7, 2024
1 parent f3ddb19 commit 0d27bab
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Lagrange.Core/Message/Entity/FileEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public class FileEntity : IMessageEntity
/// </summary>
public string? FileId { get; set; }

internal string? FileUuid { get; set; }
public string? FileUuid { get; set; }

internal string? FileHash { get; set; }
public string? FileHash { get; set; }

internal Stream? FileStream { get; set; }

Expand Down
45 changes: 45 additions & 0 deletions Lagrange.OneBot/Message/Entity/FileSegment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.Text.Json.Serialization;
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;

namespace Lagrange.OneBot.Message.Entity;

[Serializable]
public partial class FileSegment(string fileName, string fileHash, string fileId, string url)
{
public FileSegment() : this("", "", "", "") { }

[JsonPropertyName("filename")] public string Filename { get; set; } = fileName;

[JsonPropertyName("filehash")] public string Filehash { get; set; } = fileHash;

[JsonPropertyName("id")] public string Fileid { get; set; } = fileId;

[JsonPropertyName("url")] public string Url { get; set; } = url;

}

[SegmentSubscriber(typeof(FileEntity), "file")]
public partial class FileSegment : SegmentBase
{
public override void Build(MessageBuilder builder, SegmentBase segment)
{
if (segment is FileSegment fileSegment and not { Fileid: "" })
{
builder.Add(new FileEntity
{
FileName = fileSegment.Filename,
FileUrl = fileSegment.Url,
FileId = fileSegment.Fileid,
FileHash = fileSegment.Filehash
});
}
}

public override SegmentBase FromEntity(MessageChain chain, IMessageEntity entity)
{
if (entity is not FileEntity fileEntity) throw new ArgumentException("Invalid entity type.");

return new FileSegment(fileEntity.FileName, fileEntity.FileHash ?? "", fileEntity.FileId ?? fileEntity.FileUuid ?? "", fileEntity.FileUrl ?? "");
}
}

0 comments on commit 0d27bab

Please sign in to comment.