Weigh staff likes higher when calculating scores. New site setting: `staff_like_weight`

can set the factor (default is 3)
This commit is contained in:
Robin Ward 2013-05-27 12:45:10 -04:00
parent 8b0b77c161
commit 197909246c
6 changed files with 59 additions and 15 deletions

View File

@ -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

View File

@ -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)

View File

@ -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}"

View File

@ -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

View File

@ -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,

View File

@ -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