FEATURE: Editor badge

This commit is contained in:
Sam 2014-07-07 17:55:25 +10:00
parent ee3f7362e6
commit 0f25bbeaf7
8 changed files with 72 additions and 12 deletions

View File

@ -6,12 +6,25 @@ class Badge < ActiveRecord::Base
GoodPost = 7
GreatPost = 8
Autobiographer = 9
Editor = 10
# other consts
AutobiographerMinBioLength = 10
module Queries
Editor = <<SQL
SELECT p.user_id, min(p.id) post_id, min(p.created_at) granted_at
FROM posts p
JOIN topics t on t.id = p.topic_id
WHERE p.deleted_at IS NULL AND
t.deleted_at IS NULL AND
t.visible AND
p.self_edits > 0
GROUP BY p.user_id
SQL
Welcome = <<SQL
SELECT p.user_id, min(post_id) post_id, min(pa.created_at) granted_at
FROM post_actions pa

View File

@ -1,6 +1,10 @@
class BadgeType < ActiveRecord::Base
has_many :badges
Gold = 1
Silver = 2
Bronze = 3
has_many :badges
validates :name, presence: true, uniqueness: true
end

View File

@ -85,9 +85,26 @@ class BadgeGranter
LEFT JOIN user_badges ub ON
ub.badge_id = :id AND ub.user_id = q.user_id
#{post_clause}
WHERE ub.badge_id IS NULL"
WHERE ub.badge_id IS NULL AND q.user_id <> -1
RETURNING id, user_id, granted_at
"
Badge.exec_sql(sql, id: badge.id)
builder = SqlBuilder.new(sql)
builder.map_exec(OpenStruct, id: badge.id).each do |row|
# old bronze badges do not matter
next if badge.badge_type_id == BadgeType::Bronze and row.granted_at < 2.days.ago
notification = Notification.create!(
user_id: row.user_id,
notification_type: Notification.types[:granted_badge],
data: { badge_id: badge.id, badge_name: badge.name }.to_json )
Badge.exec_sql("UPDATE user_badges SET notification_id = :notification_id WHERE id = :id",
notification_id: notification.id,
id: row.id
)
end
badge.reset_grant_count!

View File

@ -1947,6 +1947,9 @@ en:
other: "%{count} granted"
select_badge_for_title: Select a badge to use as your title
badge:
editor:
name: Editor
description: First post edit.
basic_user:
name: Basic
description: Granted all essential community functions.

View File

@ -1,9 +1,9 @@
# Trust level system badges.
trust_level_badges = [
{id: 1, name: "Basic User", type: 3},
{id: 2, name: "Regular User", type: 3},
{id: 3, name: "Leader", type: 2},
{id: 4, name: "Elder", type: 1}
{id: 1, name: "Basic User", type: BadgeType::Bronze},
{id: 2, name: "Regular User", type: BadgeType::Bronze},
{id: 3, name: "Leader", type: BadgeType::Silver},
{id: 4, name: "Elder", type: BadgeType::Gold}
]
trust_level_badges.each do |spec|
@ -18,7 +18,7 @@ end
Badge.seed do |b|
b.id = Badge::Welcome
b.name = "Welcome"
b.badge_type_id = 3
b.badge_type_id = BadgeType::Bronze
b.multiple_grant = false
b.target_posts = true
b.query = Badge::Queries::Welcome
@ -27,17 +27,25 @@ end
Badge.seed do |b|
b.id = Badge::Autobiographer
b.name = "Autobiographer"
b.badge_type_id = 3
b.badge_type_id = BadgeType::Bronze
b.multiple_grant = false
b.query = Badge::Queries::Autobiographer
end
Badge.seed do |b|
b.id = Badge::Editor
b.name = "Editor"
b.badge_type_id = BadgeType::Bronze
b.multiple_grant = false
b.query = Badge::Queries::Editor
end
#
# Like system badges.
like_badges = [
{id: 6, name: "Nice Post", type: 3, multiple: true},
{id: 7, name: "Good Post", type: 2, multiple: true},
{id: 8, name: "Great Post", type: 1, multiple: true}
{id: 6, name: "Nice Post", type: BadgeType::Bronze, multiple: true},
{id: 7, name: "Good Post", type: BadgeType::Silver, multiple: true},
{id: 8, name: "Great Post", type: BadgeType::Gold, multiple: true}
]
like_badges.each do |spec|

View File

@ -0,0 +1,12 @@
class AddSelfEditsToPosts < ActiveRecord::Migration
def up
add_column :posts, :self_edits, :integer, null: false, default: 0
execute "
UPDATE posts p SET self_edits = (SELECT COUNT(*) FROM post_revisions pr WHERE pr.post_id = p.id AND pr.user_id=p.user_id)
"
end
def down
remove_column :posts, :self_edits
end
end

View File

@ -96,6 +96,7 @@ class PostRevisor
@post.last_editor_id = @editor.id
@post.edit_reason = @opts[:edit_reason] if @opts[:edit_reason]
@post.user_id = @opts[:new_user].id if @opts[:new_user]
@post.self_edits += 1 if @editor == @post.user
if @editor == @post.user && @post.hidden && @post.hidden_reason_id == Post.hidden_reasons[:flag_threshold_reached]
@post.hidden = false

View File

@ -36,6 +36,8 @@ describe BadgeGranter do
# TODO add welcome
post.user.user_badges.pluck(:badge_id).sort.should == [Badge::NicePost,Badge::GoodPost]
post.user.notifications.count.should == 2
Badge.find(Badge::NicePost).grant_count.should == 1
Badge.find(Badge::GoodPost).grant_count.should == 1
end