diff --git a/osu.Game.Tests/Visual/Online/TestSceneOfflineCommentsContainer.cs b/osu.Game.Tests/Visual/Online/TestSceneOfflineCommentsContainer.cs index b69cc46bb8..4040f36ef8 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneOfflineCommentsContainer.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneOfflineCommentsContainer.cs @@ -51,6 +51,15 @@ namespace osu.Game.Tests.Visual.Online AddStep("Add empty comment bundle", () => comments.ShowComments(getEmptyCommentBundle())); } + [Test] + public void TestAppendDuplicatedComment() + { + AddStep("Add comment bundle", () => comments.ShowComments(getCommentBundle())); + AddUntilStep("Dictionary length is 10", () => comments.DictionaryLength == 10); + AddStep("Append existing comment", () => comments.AppendComments(getCommentSubBundle())); + AddAssert("Dictionary length is 10", () => comments.DictionaryLength == 10); + } + private CommentBundle getEmptyCommentBundle() => new CommentBundle { Comments = new List(), @@ -164,8 +173,28 @@ namespace osu.Game.Tests.Visual.Online Total = 10 }; + private CommentBundle getCommentSubBundle() => new CommentBundle + { + Comments = new List + { + new Comment + { + Id = 1, + Message = "Simple test comment", + LegacyName = "TestUser1", + CreatedAt = DateTimeOffset.Now, + VotesCount = 5 + }, + }, + IncludedComments = new List(), + }; + private class TestCommentsContainer : CommentsContainer { + public int DictionaryLength => CommentDictionary.Count; + + public new void AppendComments(CommentBundle bundle) => base.AppendComments(bundle); + public void ShowComments(CommentBundle bundle) { CommentCounter.Current.Value = 0; diff --git a/osu.Game/Overlays/Comments/CommentsContainer.cs b/osu.Game/Overlays/Comments/CommentsContainer.cs index da2cdbc335..0381d73583 100644 --- a/osu.Game/Overlays/Comments/CommentsContainer.cs +++ b/osu.Game/Overlays/Comments/CommentsContainer.cs @@ -167,10 +167,10 @@ namespace osu.Game.Overlays.Comments moreButton.Show(); moreButton.IsLoading = true; content.Clear(); - commentDictionary.Clear(); + CommentDictionary.Clear(); } - private readonly Dictionary commentDictionary = new Dictionary(); + protected readonly Dictionary CommentDictionary = new Dictionary(); protected void OnSuccess(CommentBundle response) { @@ -181,7 +181,7 @@ namespace osu.Game.Overlays.Comments return; } - var topLevelComments = appendComments(response); + var topLevelComments = AppendComments(response); LoadComponentsAsync(topLevelComments, loaded => { @@ -209,7 +209,7 @@ namespace osu.Game.Overlays.Comments /// Appends retrieved comments to the subtree rooted of comments in this page. /// /// The bundle of comments to add. - private List appendComments([NotNull] CommentBundle bundle) + protected List AppendComments([NotNull] CommentBundle bundle) { var topLevelComments = new List(); var orphaned = new List(); @@ -217,7 +217,7 @@ namespace osu.Game.Overlays.Comments foreach (var comment in bundle.Comments.Concat(bundle.IncludedComments)) { // Exclude possible duplicated comments. - if (commentDictionary.ContainsKey(comment.Id)) + if (CommentDictionary.ContainsKey(comment.Id)) continue; addNewComment(comment); @@ -238,7 +238,7 @@ namespace osu.Game.Overlays.Comments // Comments that have no parent are added as top-level comments to the flow. topLevelComments.Add(drawableComment); } - else if (commentDictionary.TryGetValue(comment.ParentId.Value, out var parentDrawable)) + else if (CommentDictionary.TryGetValue(comment.ParentId.Value, out var parentDrawable)) { // The comment's parent has already been seen, so the parent<-> child links can be added. comment.ParentComment = parentDrawable.Comment; @@ -255,10 +255,10 @@ namespace osu.Game.Overlays.Comments private DrawableComment getDrawableComment(Comment comment) { - if (commentDictionary.TryGetValue(comment.Id, out var existing)) + if (CommentDictionary.TryGetValue(comment.Id, out var existing)) return existing; - return commentDictionary[comment.Id] = new DrawableComment(comment) + return CommentDictionary[comment.Id] = new DrawableComment(comment) { ShowDeleted = { BindTarget = ShowDeleted }, Sort = { BindTarget = Sort }, @@ -270,7 +270,7 @@ namespace osu.Game.Overlays.Comments { var req = new GetCommentsRequest(id.Value, type.Value, Sort.Value, page, drawableComment.Comment.Id); - req.Success += response => Schedule(() => appendComments(response)); + req.Success += response => Schedule(() => AppendComments(response)); api.PerformAsync(req); }