Create notification when badge is granted.

This commit is contained in:
Vikhyat Korrapati 2014-04-17 01:29:45 +05:30
parent b55734da91
commit 89f45901bc
6 changed files with 19 additions and 4 deletions

View file

@ -8,6 +8,9 @@ Discourse.NotificationController = Discourse.ObjectController.extend({
}.property(),
link: function() {
if (this.get('data.badge_id')) {
return '<a href="/badges/' + this.get('data.badge_id') + '/' + this.get('data.badge_name').replace(/[^A-Za-z0-9_]+/g, '-').toLowerCase() + '">' + this.get('data.badge_name') + '</a>';
}
if (this.blank("data.topic_title")) {
return "";
}

View file

@ -28,7 +28,7 @@ class Notification < ActiveRecord::Base
@types ||= Enum.new(
:mentioned, :replied, :quoted, :edited, :liked, :private_message,
:invited_to_private_message, :invitee_accepted, :posted, :moved_post,
:linked
:linked, :granted_badge
)
end

View file

@ -23,6 +23,10 @@ class BadgeGranter
if @granted_by != Discourse.system_user
StaffActionLogger.new(@granted_by).log_badge_grant(user_badge)
end
@user.notifications.create(notification_type: Notification.types[:granted_badge],
data: { badge_id: @badge.id,
badge_name: @badge.name }.to_json)
end
end
@ -32,10 +36,14 @@ class BadgeGranter
def self.revoke(user_badge, options={})
UserBadge.transaction do
user_badge.destroy!
Badge.decrement_counter 'grant_count', user_badge.badge.id
Badge.decrement_counter 'grant_count', user_badge.badge_id
if options[:revoked_by]
StaffActionLogger.new(options[:revoked_by]).log_badge_revoke(user_badge)
end
# Revoke badge -- This is inefficient, but not very easy to optimize unless
# the data hash is converted into a hstore.
notification = user_badge.user.notifications.where(notification_type: Notification.types[:granted_badge]).where("data LIKE ?", "%" + user_badge.badge_id.to_s + "%").select {|n| n.data_hash["badge_id"] == user_badge.badge_id }.first
notification && notification.destroy
end
end

View file

@ -598,6 +598,7 @@ en:
moved_post: "<i title='moved post' class='fa fa-arrow-right'></i> {{username}} moved {{link}}"
total_flagged: "total flagged posts"
linked: "<i title='linked post' class='fa fa-arrow-left'></i> {{username}} {{link}}"
granted_badge: "<i title='badge granted' class='fa fa-certificate'></i> {{link}}"
upload_selector:
title: "Add an image"

View file

@ -888,6 +888,7 @@ en:
invited_to_private_message: "%{display_username} invited you to a private message: %{link}"
invitee_accepted: "%{display_username} accepted your invitation"
linked: "%{display_username} linked you in %{link}"
granted_badge: "You were granted the badge %{link}"
search:
within_post: "#%{post_number} by %{username}: %{excerpt}"

View file

@ -40,9 +40,10 @@ describe BadgeGranter do
user_badge.should_not be_present
end
it 'increments grant_count on the badge' do
it 'increments grant_count on the badge and creates a notification' do
BadgeGranter.grant(badge, user)
badge.reload.grant_count.should eq(1)
user.notifications.where(notification_type: Notification.types[:granted_badge]).first.data_hash["badge_id"].should == badge.id
end
end
@ -52,12 +53,13 @@ describe BadgeGranter do
let(:admin) { Fabricate(:admin) }
let!(:user_badge) { BadgeGranter.grant(badge, user) }
it 'revokes the badge and decrements grant_count' do
it 'revokes the badge, deletes the notification and decrements grant_count' do
badge.reload.grant_count.should eq(1)
StaffActionLogger.any_instance.expects(:log_badge_revoke).with(user_badge)
BadgeGranter.revoke(user_badge, revoked_by: admin)
UserBadge.where(user: user, badge: badge).first.should_not be_present
badge.reload.grant_count.should eq(0)
user.notifications.where(notification_type: Notification.types[:granted_badge]).should be_empty
end
end