From 197909246cab0fe86a7578cbf26999288b2364a9 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 27 May 2013 12:45:10 -0400 Subject: [PATCH] Weigh staff likes higher when calculating scores. New site setting: `staff_like_weight` can set the factor (default is 3) --- app/models/post_action.rb | 16 +++++++-- app/models/site_setting.rb | 2 ++ config/locales/server.en.yml | 1 + .../20130527152648_add_like_score_to_posts.rb | 19 +++++++++++ lib/score_calculator.rb | 2 +- spec/models/post_action_spec.rb | 34 +++++++++++++------ 6 files changed, 59 insertions(+), 15 deletions(-) create mode 100644 db/migrate/20130527152648_add_like_score_to_posts.rb diff --git a/app/models/post_action.rb b/app/models/post_action.rb index a749ca8c526..4669bc98486 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -202,12 +202,22 @@ class PostAction < ActiveRecord::Base column = "#{post_action_type.to_s}_count" delta = deleted_at.nil? ? 1 : -1 - # Voting also changes the sort_order - if post_action_type == :vote - Post.update_all ["vote_count = vote_count + :delta, sort_order = :max - (vote_count + :delta)", delta: delta, max: Topic.max_sort_order], id: post_id + # We probably want to refactor this method to something cleaner. + case post_action_type + when :vote + # Voting also changes the sort_order + Post.update_all ["vote_count = vote_count + :delta, sort_order = :max - (vote_count + :delta)", + delta: delta, + max: Topic.max_sort_order], id: post_id + when :like + # `like_score` is weighted higher for staff accounts + Post.update_all ["like_count = like_count + :delta, like_score = like_score + :score_delta", + delta: delta, + score_delta: user.staff? ? delta * SiteSetting.staff_like_weight : delta], id: post_id else Post.update_all ["#{column} = #{column} + ?", delta], id: post_id end + Topic.update_all ["#{column} = #{column} + ?", delta], id: post.topic_id diff --git a/app/models/site_setting.rb b/app/models/site_setting.rb index fe202f48d96..4d6fbbbf251 100644 --- a/app/models/site_setting.rb +++ b/app/models/site_setting.rb @@ -114,6 +114,8 @@ class SiteSetting < ActiveRecord::Base setting(:allow_duplicate_topic_titles, false) + setting(:staff_like_weight, 3) + setting(:add_rel_nofollow_to_user_content, true) setting(:exclude_rel_nofollow_domains, '') setting(:post_excerpt_maxlength, 300) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 25d36cc540f..f563d99c3b0 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -585,6 +585,7 @@ en: privacy_policy_url: "If you have a Privacy Policy document hosted elsewhere that you want to use, provide the full URL here." newuser_spam_host_threshold: "How many times a new user can post a link to the same host within their `newuser_spam_host_posts` posts before being considered spam." + staff_like_weight: "Extra weighting factor given to likes when performed by staff." notification_types: mentioned: "%{display_username} mentioned you in %{link}" diff --git a/db/migrate/20130527152648_add_like_score_to_posts.rb b/db/migrate/20130527152648_add_like_score_to_posts.rb new file mode 100644 index 00000000000..98c61ea9fb9 --- /dev/null +++ b/db/migrate/20130527152648_add_like_score_to_posts.rb @@ -0,0 +1,19 @@ +class AddLikeScoreToPosts < ActiveRecord::Migration + def change + add_column :posts, :like_score, :integer, default: 0, null: false + + execute "UPDATE posts p + set like_score = x.like_score + FROM (SELECT pa.post_id, + SUM(CASE + WHEN u.admin OR u.moderator THEN 3 + ELSE 1 + END) AS like_score + FROM post_actions AS pa + INNER JOIN users AS u ON u.id = pa.user_id + GROUP BY pa.post_id) AS x + WHERE x.post_id = p.id" + end +end + + diff --git a/lib/score_calculator.rb b/lib/score_calculator.rb index 74cfa94f092..8ac2a92561a 100644 --- a/lib/score_calculator.rb +++ b/lib/score_calculator.rb @@ -3,7 +3,7 @@ class ScoreCalculator def self.default_score_weights { reply_count: 5, - like_count: 15, + like_score: 15, incoming_link_count: 5, bookmark_count: 2, avg_time: 0.05, diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index 4b93d8b8d11..e79236dcd37 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -122,18 +122,30 @@ describe PostAction do end describe 'when a user likes something' do - it 'should increase the post counts when a user likes' do - lambda { - PostAction.act(codinghorror, post, PostActionType.types[:like]) - post.reload - }.should change(post, :like_count).by(1) - end + it 'should increase the `like_count` and `like_score` when a user likes something' do + PostAction.act(codinghorror, post, PostActionType.types[:like]) + post.reload + post.like_count.should == 1 + post.like_score.should == 1 + post.topic.reload + post.topic.like_count.should == 1 - it 'should increase the forum topic like count when a user likes' do - lambda { - PostAction.act(codinghorror, post, PostActionType.types[:like]) - post.topic.reload - }.should change(post.topic, :like_count).by(1) + # When a staff member likes it + PostAction.act(moderator, post, PostActionType.types[:like]) + post.reload + post.like_count.should == 2 + post.like_score.should == 4 + + # Removing likes + PostAction.remove_act(codinghorror, post, PostActionType.types[:like]) + post.reload + post.like_count.should == 1 + post.like_score.should == 3 + + PostAction.remove_act(moderator, post, PostActionType.types[:like]) + post.reload + post.like_count.should == 0 + post.like_score.should == 0 end end