diff --git a/app/models/badge.rb b/app/models/badge.rb index da3af7de827..ca6a19166c5 100644 --- a/app/models/badge.rb +++ b/app/models/badge.rb @@ -6,12 +6,25 @@ class Badge < ActiveRecord::Base GoodPost = 7 GreatPost = 8 Autobiographer = 9 + Editor = 10 # other consts AutobiographerMinBioLength = 10 module Queries + + Editor = < 0 + GROUP BY p.user_id +SQL + Welcome = < -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! diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 2a5fba0ce1a..f756cd57662 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -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. diff --git a/db/fixtures/601_badges.rb b/db/fixtures/601_badges.rb index f09c4cd8675..17ef446e314 100644 --- a/db/fixtures/601_badges.rb +++ b/db/fixtures/601_badges.rb @@ -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| diff --git a/db/migrate/20140707071913_add_self_edits_to_posts.rb b/db/migrate/20140707071913_add_self_edits_to_posts.rb new file mode 100644 index 00000000000..cd0727b409c --- /dev/null +++ b/db/migrate/20140707071913_add_self_edits_to_posts.rb @@ -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 diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index dd3db0af102..73047c8be1c 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -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 diff --git a/spec/services/badge_granter_spec.rb b/spec/services/badge_granter_spec.rb index 99588e0e82a..e2dd4bfa572 100644 --- a/spec/services/badge_granter_spec.rb +++ b/spec/services/badge_granter_spec.rb @@ -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