From 0def92c8c8eec582dcd6d1a69d86586641edaddf Mon Sep 17 00:00:00 2001 From: ayesha waris <73840786+ayesha-waris@users.noreply.github.com> Date: Mon, 8 Jan 2024 16:46:37 +0500 Subject: [PATCH] fix: exclude anonymous posts in build_course_stats (#424) --- models/user.rb | 5 +++-- spec/api/user_spec.rb | 23 +++++++++++++++++++++++ spec/spec_helper.rb | 18 ++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/models/user.rb b/models/user.rb index 455a3e0a6d..6154f6bb5e 100644 --- a/models/user.rb +++ b/models/user.rb @@ -273,7 +273,8 @@ def build_course_stats(course_id) data = Content.collection.aggregate( [ # Match all content in the course by the specified author - { "$match" => { :course_id => course_id, :author_id => self.external_id } }, + { "$match" => { :course_id => course_id, + :author_id => self.external_id, "anonymous_to_peers" => false, "anonymous" => false } }, # Keep a count of flags for each entry { "$addFields" => { @@ -391,4 +392,4 @@ def update_all_users_in_course(course_id) user.build_course_stats(course_id) end author_usernames -end \ No newline at end of file +end diff --git a/spec/api/user_spec.rb b/spec/api/user_spec.rb index 7bef755954..40e75c3398 100644 --- a/spec/api/user_spec.rb +++ b/spec/api/user_spec.rb @@ -692,6 +692,29 @@ def get_new_stats expect(new_stats["active_flags"]).to eq @original_stats["active_flags"] - 1 end end + describe "build_course_stats" do + let(:user) { create_test_user 3} + let(:course_id) { DFLT_COURSE_ID } + + context 'when the user has made anonymous posts' do + before do + make_anonymous_to_peers_thread(user, "anon thread 1 by author", DFLT_COURSE_ID, "anon_thread_1") + make_anonymous_thread(user, "anon thread 2 by author", DFLT_COURSE_ID, "anon_thread_2") + end + + it 'does not include anonymous posts in the counts after making a non-anonymous post' do + + make_thread(user, "thread by new author #{user}", DFLT_COURSE_ID, "new_thread") + + get "/api/v1/users/#{course_id}/stats" + expect(last_response.status).to eq(200) + stats = parse(last_response.body) + expect(stats["user_stats"][0]["replies"]).to eq(0) + expect(stats["user_stats"][0]["responses"]).to eq(0) + expect(stats["user_stats"][0]["threads"]).to eq(1) + end + end + end end describe "POST /api/v1/users/:course_id/update_stats" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ca11c144e8..fbf43dd582 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -347,6 +347,24 @@ def make_thread(author, text, course_id, commentable_id, thread_type=:discussion thread end +def make_anonymous_to_peers_thread(author, text, course_id, commentable_id, thread_type=:discussion, context=:course) + thread = CommentThread.new(title: text, body: text, course_id: course_id, commentable_id: commentable_id, anonymous_to_peers: true) + thread.thread_type = thread_type + thread.author = author + thread.context = context + thread.save! + thread + end + + def make_anonymous_thread(author, text, course_id, commentable_id, thread_type=:discussion, context=:course) + thread = CommentThread.new(title: text, body: text, course_id: course_id, commentable_id: commentable_id, anonymous: true) + thread.thread_type = thread_type + thread.author = author + thread.context = context + thread.save! + thread + end + def make_comment(author, parent, text) if parent.is_a?(CommentThread) coll = parent.comments