diff --git a/Lagrange.OneBot/Database/MessageRecord.cs b/Lagrange.OneBot/Database/MessageRecord.cs index ab1632419..e40333c46 100644 --- a/Lagrange.OneBot/Database/MessageRecord.cs +++ b/Lagrange.OneBot/Database/MessageRecord.cs @@ -72,25 +72,34 @@ public static int CalcMessageHash(ulong msgId, uint seq) return ((ushort)seq << 16) | (ushort)msgId; } - public static implicit operator MessageRecord(MessageChain chain) => new() + public static implicit operator MessageRecord(MessageChain chain) { - Id = CalcMessageHash(chain.MessageId, chain.Sequence), - Type = chain.Type, - Sequence = chain.Sequence, - ClientSequence = chain.ClientSequence, - MessageId = chain.MessageId, - Time = chain.Time, - FromUin = chain.FriendUin, - ToUin = chain.Type switch + // The `static byte[] Serialize(T, MessagePackSerializerOptions?, CancellationToken)` method + // may have resource reuse issues that could lead to incorrect serialization results. + // Use a separate stream to resolve this problem. + using MemoryStream stream = new MemoryStream(); + MessagePackSerializer.Serialize>(stream, chain, OPTIONS); + + return new() { - MessageType.Group => (ulong)chain.GroupUin!, - MessageType.Temp or - MessageType.Friend => chain.TargetUin, - _ => throw new NotImplementedException(), - }, - Style = chain.Style != null ? (MessageStyleRecord)chain.Style : null, - Entities = MessagePackSerializer.Serialize>(chain, OPTIONS) - }; + Id = CalcMessageHash(chain.MessageId, chain.Sequence), + Type = chain.Type, + Sequence = chain.Sequence, + ClientSequence = chain.ClientSequence, + MessageId = chain.MessageId, + Time = chain.Time, + FromUin = chain.FriendUin, + ToUin = chain.Type switch + { + MessageType.Group => (ulong)chain.GroupUin!, + MessageType.Temp or + MessageType.Friend => chain.TargetUin, + _ => throw new NotSupportedException(), + }, + Style = chain.Style != null ? (MessageStyleRecord)chain.Style : null, + Entities = stream.ToArray(), + }; + } public static implicit operator MessageChain(MessageRecord record) { @@ -112,7 +121,7 @@ MessageType.Temp or (uint)record.ClientSequence, record.MessageId ), - _ => throw new NotImplementedException(), + _ => throw new NotSupportedException(), }; var entities = MessagePackSerializer.Deserialize>(record.Entities, OPTIONS);