mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Create notification when badge is granted.
This commit is contained in:
parent
b55734da91
commit
89f45901bc
@ -8,6 +8,9 @@ Discourse.NotificationController = Discourse.ObjectController.extend({
|
|||||||
}.property(),
|
}.property(),
|
||||||
|
|
||||||
link: function() {
|
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")) {
|
if (this.blank("data.topic_title")) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,7 @@ class Notification < ActiveRecord::Base
|
|||||||
@types ||= Enum.new(
|
@types ||= Enum.new(
|
||||||
:mentioned, :replied, :quoted, :edited, :liked, :private_message,
|
:mentioned, :replied, :quoted, :edited, :liked, :private_message,
|
||||||
:invited_to_private_message, :invitee_accepted, :posted, :moved_post,
|
:invited_to_private_message, :invitee_accepted, :posted, :moved_post,
|
||||||
:linked
|
:linked, :granted_badge
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -23,6 +23,10 @@ class BadgeGranter
|
|||||||
if @granted_by != Discourse.system_user
|
if @granted_by != Discourse.system_user
|
||||||
StaffActionLogger.new(@granted_by).log_badge_grant(user_badge)
|
StaffActionLogger.new(@granted_by).log_badge_grant(user_badge)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@user.notifications.create(notification_type: Notification.types[:granted_badge],
|
||||||
|
data: { badge_id: @badge.id,
|
||||||
|
badge_name: @badge.name }.to_json)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -32,10 +36,14 @@ class BadgeGranter
|
|||||||
def self.revoke(user_badge, options={})
|
def self.revoke(user_badge, options={})
|
||||||
UserBadge.transaction do
|
UserBadge.transaction do
|
||||||
user_badge.destroy!
|
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]
|
if options[:revoked_by]
|
||||||
StaffActionLogger.new(options[:revoked_by]).log_badge_revoke(user_badge)
|
StaffActionLogger.new(options[:revoked_by]).log_badge_revoke(user_badge)
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -598,6 +598,7 @@ en:
|
|||||||
moved_post: "<i title='moved post' class='fa fa-arrow-right'></i> {{username}} moved {{link}}"
|
moved_post: "<i title='moved post' class='fa fa-arrow-right'></i> {{username}} moved {{link}}"
|
||||||
total_flagged: "total flagged posts"
|
total_flagged: "total flagged posts"
|
||||||
linked: "<i title='linked post' class='fa fa-arrow-left'></i> {{username}} {{link}}"
|
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:
|
upload_selector:
|
||||||
title: "Add an image"
|
title: "Add an image"
|
||||||
|
@ -888,6 +888,7 @@ en:
|
|||||||
invited_to_private_message: "%{display_username} invited you to a private message: %{link}"
|
invited_to_private_message: "%{display_username} invited you to a private message: %{link}"
|
||||||
invitee_accepted: "%{display_username} accepted your invitation"
|
invitee_accepted: "%{display_username} accepted your invitation"
|
||||||
linked: "%{display_username} linked you in %{link}"
|
linked: "%{display_username} linked you in %{link}"
|
||||||
|
granted_badge: "You were granted the badge %{link}"
|
||||||
|
|
||||||
search:
|
search:
|
||||||
within_post: "#%{post_number} by %{username}: %{excerpt}"
|
within_post: "#%{post_number} by %{username}: %{excerpt}"
|
||||||
|
@ -40,9 +40,10 @@ describe BadgeGranter do
|
|||||||
user_badge.should_not be_present
|
user_badge.should_not be_present
|
||||||
end
|
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)
|
BadgeGranter.grant(badge, user)
|
||||||
badge.reload.grant_count.should eq(1)
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
@ -52,12 +53,13 @@ describe BadgeGranter do
|
|||||||
let(:admin) { Fabricate(:admin) }
|
let(:admin) { Fabricate(:admin) }
|
||||||
let!(:user_badge) { BadgeGranter.grant(badge, user) }
|
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)
|
badge.reload.grant_count.should eq(1)
|
||||||
StaffActionLogger.any_instance.expects(:log_badge_revoke).with(user_badge)
|
StaffActionLogger.any_instance.expects(:log_badge_revoke).with(user_badge)
|
||||||
BadgeGranter.revoke(user_badge, revoked_by: admin)
|
BadgeGranter.revoke(user_badge, revoked_by: admin)
|
||||||
UserBadge.where(user: user, badge: badge).first.should_not be_present
|
UserBadge.where(user: user, badge: badge).first.should_not be_present
|
||||||
badge.reload.grant_count.should eq(0)
|
badge.reload.grant_count.should eq(0)
|
||||||
|
user.notifications.where(notification_type: Notification.types[:granted_badge]).should be_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user