diff --git a/app/models/user.rb b/app/models/user.rb index 39344eb28f9..d17a1eb6ec5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -235,6 +235,8 @@ class User < ActiveRecord::Base user.staged = false user.active = false user.custom_fields[FROM_STAGED] = true + + DiscourseEvent.trigger(:user_unstaged, user) end user end @@ -383,7 +385,7 @@ class User < ActiveRecord::Base def read_first_notification? if (trust_level > TrustLevel[1] || - created_at < TRACK_FIRST_NOTIFICATION_READ_DURATION.seconds.ago) + (first_seen_at.present? && first_seen_at < TRACK_FIRST_NOTIFICATION_READ_DURATION.seconds.ago)) return true end diff --git a/plugins/discourse-narrative-bot/plugin.rb b/plugins/discourse-narrative-bot/plugin.rb index cb95d19cba9..22dede0bbc8 100644 --- a/plugins/discourse-narrative-bot/plugin.rb +++ b/plugins/discourse-narrative-bot/plugin.rb @@ -112,6 +112,10 @@ after_initialize do end end + self.on(:user_unstaged) do |user| + user.enqueue_bot_welcome_post + end + self.add_to_class(:user, :enqueue_bot_welcome_post) do return if SiteSetting.disable_discourse_narrative_bot_welcome_post diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index a0bcda79d8f..eb1fbd6fcf9 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1478,7 +1478,7 @@ describe User do describe 'when user is an old user' do it 'should return the right value' do - user.update_attributes!(created_at: 1.year.ago) + user.update_attributes!(first_seen_at: 1.year.ago) expect(user.read_first_notification?).to eq(true) end @@ -1581,9 +1581,10 @@ describe User do end describe "#unstage" do + let!(:staged_user) { Fabricate(:staged, email: 'staged@account.com', active: true, username: 'staged1', name: 'Stage Name') } + let(:params) { { email: 'staged@account.com', active: true, username: 'unstaged1', name: 'Foo Bar' } } + it "correctyl unstages a user" do - staged_user = Fabricate(:staged, email: 'staged@account.com', active: true, username: 'staged1', name: 'Stage Name') - params = { email: 'staged@account.com', active: true, username: 'unstaged1', name: 'Foo Bar' } user = User.unstage(params) expect(user.id).to eq(staged_user.id) @@ -1598,6 +1599,14 @@ describe User do expect(User.unstage(email: 'jeff@somewhere.com')).to be_nil expect(User.unstage(email: 'no@account.com')).to be_nil end + + it "triggers an event" do + unstaged_user = nil + event = DiscourseEvent.track_events { unstaged_user = User.unstage(params) }.first + + expect(event[:event_name]).to eq(:user_unstaged) + expect(event[:params].first).to eq(unstaged_user) + end end end