From c63e84dc62c58abc1ead3bec13a2de0de3cb909e Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Wed, 13 Nov 2019 18:31:49 -0300 Subject: [PATCH] FEATURE: Send a message to new TL2 users and point them to the advanced tutorial (#8335) * FEATURE: Send a message to new TL2 users and point them to the advanced tutorial * DEV: Use a method to find the discobot user --- lib/promotion.rb | 2 +- .../jobs/send_advanced_tutorial_message.rb | 19 +++++++++++++++ .../jobs/send_default_welcome_message.rb | 2 +- .../config/locales/server.en.yml | 9 ++++++++ .../certificate_generator.rb | 2 +- plugins/discourse-narrative-bot/plugin.rb | 10 ++++++++ .../advanced_user_narrative_spec.rb | 2 +- .../new_user_narrative_spec.rb | 2 +- .../track_selector_spec.rb | 2 +- .../jobs/onceoff/remap_old_bot_iamges_spec.rb | 4 ++-- .../send_advanced_tutorial_message_spec.rb | 23 +++++++++++++++++++ 11 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 plugins/discourse-narrative-bot/autoload/jobs/send_advanced_tutorial_message.rb create mode 100644 plugins/discourse-narrative-bot/spec/jobs/send_advanced_tutorial_message_spec.rb diff --git a/lib/promotion.rb b/lib/promotion.rb index fe810aebcd4..06977c10fef 100644 --- a/lib/promotion.rb +++ b/lib/promotion.rb @@ -74,6 +74,7 @@ class Promotion @user.save! @user.user_profile.recook_bio @user.user_profile.save! + DiscourseEvent.trigger(:user_promoted, user_id: @user.id, new_trust_level: new_level, old_trust_level: old_level) Group.user_trust_level_change!(@user.id, @user.trust_level) BadgeGranter.queue_badge_grant(Badge::Trigger::TrustLevelChange, user: @user) end @@ -141,5 +142,4 @@ class Promotion user.change_trust_level!(2, log_action_for: performed_by || Discourse.system_user) end end - end diff --git a/plugins/discourse-narrative-bot/autoload/jobs/send_advanced_tutorial_message.rb b/plugins/discourse-narrative-bot/autoload/jobs/send_advanced_tutorial_message.rb new file mode 100644 index 00000000000..b10f416f0a6 --- /dev/null +++ b/plugins/discourse-narrative-bot/autoload/jobs/send_advanced_tutorial_message.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module Jobs + class SendAdvancedTutorialMessage < ::Jobs::Base + def execute(args) + user = User.find_by(id: args[:user_id]) + raise Discourse::InvalidParameters.new(:user_id) if user.nil? + + PostCreator.create!( + Discourse.system_user, + title: I18n.t("discourse_narrative_bot.tl2_promotion_message.subject_template"), + raw: I18n.t("discourse_narrative_bot.tl2_promotion_message.text_body_template"), + archetype: Archetype.private_message, + target_usernames: user.username, + skip_validations: true + ) + end + end +end diff --git a/plugins/discourse-narrative-bot/autoload/jobs/send_default_welcome_message.rb b/plugins/discourse-narrative-bot/autoload/jobs/send_default_welcome_message.rb index 1b2c54aa6cf..4eeda6c0044 100644 --- a/plugins/discourse-narrative-bot/autoload/jobs/send_default_welcome_message.rb +++ b/plugins/discourse-narrative-bot/autoload/jobs/send_default_welcome_message.rb @@ -9,7 +9,7 @@ module Jobs title = I18n.t("system_messages.#{type}.subject_template", params) raw = I18n.t("system_messages.#{type}.text_body_template", params) - discobot_user = User.find(-2) + discobot_user = ::DiscourseNarrativeBot::Base.new.discobot_user post = PostCreator.create!( discobot_user, diff --git a/plugins/discourse-narrative-bot/config/locales/server.en.yml b/plugins/discourse-narrative-bot/config/locales/server.en.yml index f0d54c7f550..00de771a4d5 100644 --- a/plugins/discourse-narrative-bot/config/locales/server.en.yml +++ b/plugins/discourse-narrative-bot/config/locales/server.en.yml @@ -22,6 +22,15 @@ en: discourse_narrative_bot: bio: "Hi, I’m not a real person. I’m a bot that can teach you about this site. To interact with me, send me a message or mention **`@%{discobot_username}`** anywhere." + tl2_promotion_message: + subject_template: "Congratulations on your trust level promotion!" + text_body_template: | + Now that you’ve been promoted, it’s time to learn about some advanced features! + + Reply to this message with `@discobot start advanced tutorial` to find out more about what you can do. + + We invite you to keep getting involved – we enjoy having you around. + timeout: message: |- Hey @%{username}, just checking in because I haven’t heard from you in a while. diff --git a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb index 411cf1c8a27..a66dcba191e 100644 --- a/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb +++ b/plugins/discourse-narrative-bot/lib/discourse_narrative_bot/certificate_generator.rb @@ -18,7 +18,7 @@ module DiscourseNarrativeBot end @date = I18n.l(date, format: :date_only) - @discobot_user = User.find(-2) + @discobot_user = ::DiscourseNarrativeBot::Base.new.discobot_user end def new_user_track diff --git a/plugins/discourse-narrative-bot/plugin.rb b/plugins/discourse-narrative-bot/plugin.rb index 9b598a681f6..b42e3e4c2c9 100644 --- a/plugins/discourse-narrative-bot/plugin.rb +++ b/plugins/discourse-narrative-bot/plugin.rb @@ -33,6 +33,7 @@ after_initialize do '../autoload/jobs/narrative_timeout.rb', '../autoload/jobs/narrative_init.rb', '../autoload/jobs/send_default_welcome_message.rb', + '../autoload/jobs/send_advanced_tutorial_message.rb', '../autoload/jobs/onceoff/grant_badges.rb', '../autoload/jobs/onceoff/remap_old_bot_images.rb', '../lib/discourse_narrative_bot/actions.rb', @@ -245,4 +246,13 @@ after_initialize do ) end end + + self.on(:user_promoted) do |args| + promoted_from_tl1 = args[:new_trust_level] == TrustLevel[2] && + args[:old_trust_level] == TrustLevel[1] + + if SiteSetting.discourse_narrative_bot_enabled && promoted_from_tl1 + Jobs.enqueue(:send_advanced_tutorial_message, user_id: args[:user_id]) + end + end end diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb index 85ff8a5b3c0..d12311eac3f 100644 --- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb +++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/advanced_user_narrative_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do - let(:discobot_user) { User.find(-2) } + let(:discobot_user) { ::DiscourseNarrativeBot::Base.new.discobot_user } let(:first_post) { Fabricate(:post, user: discobot_user) } let(:user) { Fabricate(:user) } diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb index b7c5ec34d4c..bb642a733e1 100644 --- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb +++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/new_user_narrative_spec.rb @@ -4,7 +4,7 @@ require 'rails_helper' describe DiscourseNarrativeBot::NewUserNarrative do let!(:welcome_topic) { Fabricate(:topic, title: 'Welcome to Discourse') } - let(:discobot_user) { User.find(-2) } + let(:discobot_user) { ::DiscourseNarrativeBot::Base.new.discobot_user } let(:first_post) { Fabricate(:post, user: discobot_user) } let(:user) { Fabricate(:user) } diff --git a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb index e2a398a56f1..140e894046b 100644 --- a/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb +++ b/plugins/discourse-narrative-bot/spec/discourse_narrative_bot/track_selector_spec.rb @@ -4,7 +4,7 @@ require 'rails_helper' describe DiscourseNarrativeBot::TrackSelector do let(:user) { Fabricate(:user) } - let(:discobot_user) { User.find(-2) } + let(:discobot_user) { ::DiscourseNarrativeBot::Base.new.discobot_user } let(:narrative) { DiscourseNarrativeBot::NewUserNarrative.new } let(:random_mention_reply) do diff --git a/plugins/discourse-narrative-bot/spec/jobs/onceoff/remap_old_bot_iamges_spec.rb b/plugins/discourse-narrative-bot/spec/jobs/onceoff/remap_old_bot_iamges_spec.rb index d6976828906..6543cbd9549 100644 --- a/plugins/discourse-narrative-bot/spec/jobs/onceoff/remap_old_bot_iamges_spec.rb +++ b/plugins/discourse-narrative-bot/spec/jobs/onceoff/remap_old_bot_iamges_spec.rb @@ -6,7 +6,7 @@ RSpec.describe Jobs::DiscourseNarrativeBot::RemapOldBotImages do context "when bot's post contains an old link" do let(:post) do Fabricate(:post, - user: User.find(-2), + user: ::DiscourseNarrativeBot::Base.new.discobot_user, raw: 'If you’d like to learn more, select below and **bookmark this private message**. If you do, there may be a :gift: in your future!' ) end @@ -27,7 +27,7 @@ RSpec.describe Jobs::DiscourseNarrativeBot::RemapOldBotImages do context 'subfolder' do let(:post) do Fabricate(:post, - user: User.find(-2), + user: ::DiscourseNarrativeBot::Base.new.discobot_user, raw: 'If you’d like to learn more, select below and **bookmark this private message**. If you do, there may be a :gift: in your future!' ) end diff --git a/plugins/discourse-narrative-bot/spec/jobs/send_advanced_tutorial_message_spec.rb b/plugins/discourse-narrative-bot/spec/jobs/send_advanced_tutorial_message_spec.rb new file mode 100644 index 00000000000..2500b2fa817 --- /dev/null +++ b/plugins/discourse-narrative-bot/spec/jobs/send_advanced_tutorial_message_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +RSpec.describe Jobs::SendAdvancedTutorialMessage do + before do + Jobs.run_immediately! + SiteSetting.discourse_narrative_bot_enabled = true + end + + it 'sends a message to the promoted user' do + user = Fabricate(:user) + discobot_user = Discourse.system_user + Jobs.enqueue(:send_advanced_tutorial_message, user_id: user.id) + + topic = Topic.last + + expect(topic).not_to be_nil + expect(topic.user).to eq(discobot_user) + expect(topic.archetype).to eq(Archetype.private_message) + expect(topic.topic_allowed_users.pluck(:user_id)).to contain_exactly( + discobot_user.id, user.id + ) + end +end