mirror of
https://github.com/discourse/discourse.git
synced 2025-06-08 00:27:32 +08:00
Mark badge notification as read when the notification is clicked.
This commit is contained in:
@ -16,6 +16,14 @@ class BadgesController < ApplicationController
|
|||||||
def show
|
def show
|
||||||
params.require(:id)
|
params.require(:id)
|
||||||
badge = Badge.find(params[:id])
|
badge = Badge.find(params[:id])
|
||||||
|
|
||||||
|
if current_user
|
||||||
|
user_badge = UserBadge.find_by(user_id: current_user.id, badge_id: badge.id)
|
||||||
|
if user_badge && user_badge.notification
|
||||||
|
user_badge.notification.update_attributes read: true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
serialized = MultiJson.dump(serialize_data(badge, BadgeSerializer, root: "badge"))
|
serialized = MultiJson.dump(serialize_data(badge, BadgeSerializer, root: "badge"))
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html do
|
format.html do
|
||||||
|
@ -7,6 +7,12 @@ class UserBadge < ActiveRecord::Base
|
|||||||
validates :user_id, presence: true
|
validates :user_id, presence: true
|
||||||
validates :granted_at, presence: true
|
validates :granted_at, presence: true
|
||||||
validates :granted_by, presence: true
|
validates :granted_by, presence: true
|
||||||
|
|
||||||
|
# This may be inefficient, but not very easy to optimize unless the data hash
|
||||||
|
# is converted into a hstore.
|
||||||
|
def notification
|
||||||
|
@notification ||= self.user.notifications.where(notification_type: Notification.types[:granted_badge]).where("data LIKE ?", "%" + self.badge_id.to_s + "%").select {|n| n.data_hash["badge_id"] == self.badge_id }.first
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# == Schema Information
|
# == Schema Information
|
||||||
|
@ -49,10 +49,7 @@ class BadgeGranter
|
|||||||
user_badge.user.save!
|
user_badge.user.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
# Delete notification -- This is inefficient, but not very easy to optimize
|
user_badge.notification && user_badge.notification.destroy!
|
||||||
# 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
|
||||||
|
|
||||||
|
@ -2,6 +2,11 @@ require 'spec_helper'
|
|||||||
|
|
||||||
describe BadgesController do
|
describe BadgesController do
|
||||||
let!(:badge) { Fabricate(:badge) }
|
let!(:badge) { Fabricate(:badge) }
|
||||||
|
let(:user) { Fabricate(:user) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
SiteSetting.enable_badges = true
|
||||||
|
end
|
||||||
|
|
||||||
context 'index' do
|
context 'index' do
|
||||||
it 'should return a list of all badges' do
|
it 'should return a list of all badges' do
|
||||||
@ -20,5 +25,13 @@ describe BadgesController do
|
|||||||
parsed = JSON.parse(response.body)
|
parsed = JSON.parse(response.body)
|
||||||
parsed["badge"].should be_present
|
parsed["badge"].should be_present
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should mark the notification as viewed" do
|
||||||
|
log_in_user(user)
|
||||||
|
user_badge = BadgeGranter.grant(badge, user)
|
||||||
|
user_badge.notification.read.should == false
|
||||||
|
get :show, id: badge.id, format: :json
|
||||||
|
user_badge.notification.reload.read.should == true
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user