Skip to content

A platform for community discussion. Free, open, simple.

FIX: do not log personal message view if there exists a similar log in previous hour

Arpit Jalanf86212297864
diff --git a/lib/topic_view.rb b/lib/topic_view.rb
index 9153a9880c0..b55734b7550 100644
--- a/lib/topic_view.rb
+++ b/lib/topic_view.rb
@@ -496,7 +496,10 @@ class TopicView
raise Discourse::InvalidAccess.new("can't see #{@topic}", @topic) unless @guardian.can_see?(@topic)
# log personal message views
if SiteSetting.log_personal_messages_views && @topic.present? && @topic.private_message? && @topic.all_allowed_users.where(id: @user.id).blank?
- StaffActionLogger.new(@user).log_check_personal_message(@topic)
+ last_pm_log = UserHistory.where(acting_user_id: @user.id, action: UserHistory.actions[:check_personal_message], topic_id: @topic.id).last
+ unless last_pm_log.present? && last_pm_log.created_at > 1.hour.ago
+ StaffActionLogger.new(@user).log_check_personal_message(@topic)
+ end
end
end
diff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rb
index 06615bda10f..830b2a2c36b 100644
--- a/spec/components/topic_view_spec.rb
+++ b/spec/components/topic_view_spec.rb
@@ -143,6 +143,16 @@ describe TopicView do
expect { TopicView.new(private_message.id, Fabricate(:user)) }.to raise_error(Discourse::InvalidAccess)
expect(UserHistory.where(action: UserHistory.actions[:check_personal_message]).count).to eq(0)
end
+
+ it "does not log personal message view if there exists a similar log in previous hour" do
+ 2.times { TopicView.new(private_message.id, evil_trout) }
+ expect(UserHistory.where(action: UserHistory.actions[:check_personal_message]).count).to eq(1)
+
+ freeze_time (2.hours.from_now)
+
+ TopicView.new(private_message.id, evil_trout)
+ expect(UserHistory.where(action: UserHistory.actions[:check_personal_message]).count).to eq(2)
+ end
end
it "provides an absolute url" do