feat: add duplicate message collapsing

This commit is contained in:
Anna
2022-06-14 12:33:06 -04:00
parent 771e7d787b
commit 02118163ac
7 changed files with 2084 additions and 1239 deletions
+32
View File
@@ -15,6 +15,28 @@ internal class SortCode {
public SortCode() {
}
private bool Equals(SortCode other) {
return this.Type == other.Type && this.Source == other.Source;
}
public override bool Equals(object? obj) {
if (ReferenceEquals(null, obj)) {
return false;
}
if (ReferenceEquals(this, obj)) {
return true;
}
return obj.GetType() == this.GetType() && this.Equals((SortCode) obj);
}
public override int GetHashCode() {
unchecked {
return ((int) this.Type * 397) ^ (int) this.Source;
}
}
}
internal class Message {
@@ -38,6 +60,8 @@ internal class Message {
internal SeString ContentSource { get; }
internal SortCode SortCode { get; }
internal int Hash { get; }
internal Message(ulong receiver, ChatCode code, List<Chunk> sender, List<Chunk> content, SeString senderSource, SeString contentSource) {
this.Receiver = receiver;
@@ -48,6 +72,7 @@ internal class Message {
this.SenderSource = senderSource;
this.ContentSource = contentSource;
this.SortCode = new SortCode(this.Code.Type, this.Code.Source);
this.Hash = this.GenerateHash();
foreach (var chunk in sender.Concat(content)) {
chunk.Message = this;
@@ -65,9 +90,16 @@ internal class Message {
this.SenderSource = BsonMapper.Global.Deserialize<SeString>(senderSource);
this.ContentSource = BsonMapper.Global.Deserialize<SeString>(contentSource);
this.SortCode = BsonMapper.Global.ToObject<SortCode>(sortCode);
this.Hash = this.GenerateHash();
foreach (var chunk in this.Sender.Concat(this.Content)) {
chunk.Message = this;
}
}
private int GenerateHash() {
return this.SortCode.GetHashCode()
^ string.Join("", this.Sender.Select(c => c.StringValue())).GetHashCode()
^ string.Join("", this.Content.Select(c => c.StringValue())).GetHashCode();
}
}