forked from LagrangeDev/Lagrange.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReplySegment.cs
More file actions
63 lines (49 loc) · 2.11 KB
/
ReplySegment.cs
File metadata and controls
63 lines (49 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using System.Text.Json.Serialization;
using Lagrange.Core.Message;
using Lagrange.Core.Message.Entity;
using Lagrange.OneBot.Core.Entity.Message;
using Lagrange.OneBot.Database;
namespace Lagrange.OneBot.Message.Entity;
[Serializable]
public partial class ReplySegment(uint messageId)
{
public ReplySegment() : this(0) { }
[JsonPropertyName("id")] [CQProperty] public string MessageId { get; set; } = messageId.ToString();
[JsonPropertyName("elem")] public List<OneBotSegment> Elem { get; set; } = new List<OneBotSegment>();
}
[SegmentSubscriber(typeof(ForwardEntity), "reply")]
public partial class ReplySegment : SegmentBase
{
internal MessageChain? TargetChain { get; set; }
internal uint Sequence { get; private set; }
public override void Build(MessageBuilder builder, SegmentBase segment)
{
if (segment is ReplySegment reply && Realm is not null)
{
var chain = Realm.Do<MessageChain>(realm => realm.All<MessageRecord>()
.First(record => record.Id == int.Parse(reply.MessageId)));
reply.TargetChain ??= chain;
var build = MessagePacker.Build(reply.TargetChain, "");
var virtualElem = build.Body?.RichText?.Elems;
if (virtualElem != null) reply.TargetChain.Elements.AddRange(virtualElem);
builder.Forward(reply.TargetChain);
}
}
public override SegmentBase FromEntity(MessageChain chain, IMessageEntity entity)
{
if (entity is not ForwardEntity forward || Realm is null) throw new ArgumentException("The entity is not a forward entity.");
int? id;
if (chain.IsGroup)
{
id = MessageRecord.CalcMessageHash(forward.MessageId, forward.Sequence);
}
else
{
id = Realm.Do(realm => realm.All<MessageRecord>()
.FirstOrDefault(record => record.FromUinLong == chain.FriendUin
&& record.ClientSequenceLong == forward.ClientSequence)?
.Id);
}
return new ReplySegment { MessageId = (id ?? 0).ToString(), Elem = MessageService.Convert(forward.Chain) };
}
}