From 849f0d00f6aee274e2fa9e2c7095bf53a22168b8 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Tue, 31 Jul 2018 23:40:45 -0400 Subject: [PATCH] FEATURE: adds revision_count to moderators_activity (#6218) Co-Authored-By: Simon Cossar --- app/models/report.rb | 21 ++++ config/locales/server.en.yml | 1 + spec/models/report_spec.rb | 199 +++++++++++++++++++++++------------ 3 files changed, 154 insertions(+), 67 deletions(-) diff --git a/app/models/report.rb b/app/models/report.rb index 5d94723c524..1d58b08fb9b 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -721,6 +721,10 @@ class Report { property: :post_count, title: I18n.t("reports.moderators_activity.labels.post_count") + }, + { + property: :revision_count, + title: I18n.t("reports.moderators_activity.labels.revision_count") } ] @@ -787,6 +791,19 @@ class Report ON df.user_id = af.user_id SQL + revision_count_query = <<~SQL + SELECT pr.user_id, + COUNT(*) AS revision_count + FROM post_revisions pr + JOIN users u + ON u.id = pr.user_id + WHERE u.moderator = 'true' + AND u.id > 0 + AND pr.created_at >= '#{report.start_date}' + AND pr.created_at <= '#{report.end_date}' + GROUP BY pr.user_id + SQL + topic_count_query = <<~SQL SELECT t.user_id, COUNT(*) AS topic_count @@ -841,6 +858,10 @@ class Report mod_data[row.user_id][:flag_count] = row.flag_count end + DB.query(revision_count_query).each do |row| + mod_data[row.user_id][:revision_count] = row.revision_count + end + DB.query(topic_count_query).each do |row| mod_data[row.user_id][:topic_count] = row.topic_count end diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index a87cbac2be4..8af8699d4f2 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -864,6 +864,7 @@ en: topic_count: Topics created post_count: Posts created pm_count: PMs created + revision_count: Revisions flags_status: title: "Flags status" values: diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb index 0c2edd963b3..38af9ce4514 100644 --- a/spec/models/report_spec.rb +++ b/spec/models/report_spec.rb @@ -534,94 +534,159 @@ describe Report do end describe 'moderator activity' do - let(:report) { Report.find('moderators_activity', start_date: 1.months.ago.beginning_of_day, end_date: Date.today) } - let(:previous_report) { Report.find('moderators_activity', start_date: 2.months.ago.beginning_of_day, end_date: 1.month.ago.end_of_day) } + let(:report) { + Report.find('moderators_activity') + } + + let(:sam) { Fabricate(:user, moderator: true, username: 'sam') } + + let(:jeff) { Fabricate(:user, moderator: true, username: 'jeff') } include_examples 'no data' context "with moderators" do before do freeze_time(Date.today) - - bob = Fabricate(:user, moderator: true, username: 'bob') - bob.user_visits.create(visited_at: 2.days.ago, time_read: 200) - bob.user_visits.create(visited_at: 1.day.ago, time_read: 100) - Fabricate(:topic, user: bob, created_at: 1.day.ago) - sally = Fabricate(:user, moderator: true, username: 'sally') - sally.user_visits.create(visited_at: 2.days.ago, time_read: 1000) - sally.user_visits.create(visited_at: 1.day.ago, time_read: 2000) - topic = Fabricate(:topic) - 2.times { - Fabricate(:post, user: sally, topic: topic, created_at: 1.day.ago) - } - flag_user = Fabricate(:user) - flag_post = Fabricate(:post, user: flag_user) - action = PostAction.new(user_id: flag_user.id, - post_action_type_id: PostActionType.types[:off_topic], - post_id: flag_post.id, - agreed_by_id: sally.id, - created_at: 1.day.ago, - agreed_at: Time.now) - action.save - bob.user_visits.create(visited_at: 45.days.ago, time_read: 200) - old_topic = Fabricate(:topic, user: bob, created_at: 45.days.ago) - 3.times { - Fabricate(:post, user: bob, topic: old_topic, created_at: 45.days.ago) - } - old_flag_user = Fabricate(:user) - old_flag_post = Fabricate(:post, user: old_flag_user, created_at: 45.days.ago) - old_action = PostAction.new(user_id: old_flag_user.id, - post_action_type_id: PostActionType.types[:spam], - post_id: old_flag_post.id, - agreed_by_id: bob.id, - created_at: 44.days.ago, - agreed_at: 44.days.ago) - old_action.save end - it "returns a report with data" do - expect(report.data).to be_present + context "time read" do + before do + sam.user_visits.create(visited_at: 2.days.ago, time_read: 200) + sam.user_visits.create(visited_at: 1.day.ago, time_read: 100) + + jeff.user_visits.create(visited_at: 2.days.ago, time_read: 1000) + jeff.user_visits.create(visited_at: 1.day.ago, time_read: 2000) + + Fabricate(:topic, created_at: 1.day.ago) + end + + it "returns the correct read times" do + expect(report.data[0][:username]).to eq('sam') + expect(report.data[0][:time_read]).to eq(300) + expect(report.data[1][:username]).to eq('jeff') + expect(report.data[1][:time_read]).to eq(3000) + end end - it "returns data for two moderators" do - expect(report.data.count).to eq(2) + context "flags" do + before do + flagged_post = Fabricate(:post) + PostAction.act(jeff, flagged_post, PostActionType.types[:off_topic]) + PostAction.agree_flags!(flagged_post, jeff) + end + + it "returns the correct read times" do + expect(report.data.count).to eq(1) + expect(report.data[0][:flag_count]).to eq(1) + expect(report.data[0][:username]).to eq("jeff") + end end - it "returns the correct usernames" do - expect(report.data[0][:username]).to eq('bob') - expect(report.data[1][:username]).to eq('sally') + context "topics" do + before do + Fabricate(:topic, user: sam) + Fabricate(:topic, user: sam) + Fabricate(:topic, user: jeff) + end + + it "returns the correct topic count" do + expect(report.data[0][:topic_count]).to eq(2) + expect(report.data[0][:username]).to eq('sam') + expect(report.data[1][:topic_count]).to eq(1) + expect(report.data[1][:username]).to eq('jeff') + end + + context "private messages" do + before do + Fabricate(:private_message_topic, user: jeff) + end + + it "doesn’t count private topic" do + expect(report.data[0][:topic_count]).to eq(2) + expect(report.data[1][:topic_count]).to eq(1) + end + end end - it "returns the correct read times" do - expect(report.data[0][:time_read]).to eq(300) - expect(report.data[1][:time_read]).to eq(3000) + context "posts" do + before do + Fabricate(:post, user: sam) + Fabricate(:post, user: sam) + Fabricate(:post, user: jeff) + end + + it "returns the correct topic count" do + expect(report.data[0][:topic_count]).to eq(2) + expect(report.data[0][:username]).to eq('sam') + expect(report.data[1][:topic_count]).to eq(1) + expect(report.data[1][:username]).to eq('jeff') + end + + context "private messages" do + before do + Fabricate(:private_message_post, user: jeff) + end + + it "doesn’t count private post" do + expect(report.data[0][:post_count]).to eq(2) + expect(report.data[1][:post_count]).to eq(1) + end + end end - it "returns the correct agreed flag count" do - expect(report.data[0][:flag_count]).to be_blank - expect(report.data[1][:flag_count]).to eq(1) + context "private messages" do + before do + Fabricate(:post, user: sam) + Fabricate(:topic, user: sam) + Fabricate(:post, user: jeff) + Fabricate(:private_message_post, user: jeff) + end + + it "returns the correct topic count" do + expect(report.data[0][:pm_count]).to be_blank + expect(report.data[0][:username]).to eq('sam') + expect(report.data[1][:pm_count]).to eq(1) + expect(report.data[1][:username]).to eq('jeff') + end end - it "returns the correct topic count" do - expect(report.data[0][:topic_count]).to eq(1) - expect(report.data[1][:topic_count]).to be_blank + context "revisions" do + before do + post = Fabricate(:post) + post.revise(sam, raw: 'updated body', edit_reason: 'not cool') + end + + it "returns the correct revisions count" do + expect(report.data[0][:revision_count]).to eq(1) + expect(report.data[0][:username]).to eq('sam') + end + + context "revise own post" do + before do + post = Fabricate(:post, user: sam) + Fabricate(:post, user: sam) + .revise(sam, raw: 'updated body', edit_reason: 'not cool') + + Fabricate(:post) + .revise(sam, raw: 'updated body', edit_reason: 'not cool') + end + + it "doesnt count a revison on your own post" do + expect(report.data[0][:revision_count]).to eq(2) + expect(report.data[0][:username]).to eq('sam') + end + end end - it "returns the correct post count" do - expect(report.data[0][:post_count]).to be_blank - expect(report.data[1][:post_count]).to eq(2) - end + context "previous data" do + before do + Fabricate(:topic, user: sam, created_at: 1.year.ago) + end - it "returns the correct data for the time period" do - expect(previous_report.data[0][:flag_count]).to eq(1) - expect(previous_report.data[0][:topic_count]).to eq(1) - expect(previous_report.data[0][:post_count]).to eq(3) - expect(previous_report.data[0][:time_read]).to eq(200) - - expect(previous_report.data[1][:flag_count]).to be_blank - expect(previous_report.data[1][:topic_count]).to be_blank - expect(previous_report.data[1][:post_count]).to be_blank - expect(previous_report.data[1][:time_read]).to be_blank + it "doesn’t count old data" do + expect(report.data[0][:topic_count]).to be_blank + expect(report.data[0][:username]).to eq('sam') + end end end end