FIX: TL2 promotion message and advance training (#10679)

FIX: TL2 promotion message and advance training (#10679)

This is a little bit of refactoring. Core Discourse should have default promotion message for TL2.

In addition, when the Discobot plugin is enabled, the user is invited to advanced training

diff --git a/app/models/user.rb b/app/models/user.rb
index 432c3b7..75d0769 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -401,6 +401,11 @@ class User < ActiveRecord::Base
     Jobs.enqueue(:send_system_message, user_id: id, message_type: "welcome_tl1_user")
   end
 
+  def enqueue_tl2_promotion_message
+    return unless SiteSetting.send_tl2_promotion_message
+    Jobs.enqueue(:send_system_message, user_id: id, message_type: "tl2_promotion_message")
+  end
+
   def enqueue_staff_welcome_message(role)
     return unless staff?
     return if role == :admin && User.real.where(admin: true).count == 1
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index cd7c5f6..67e6935 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -1574,6 +1574,7 @@ en:
     site_contact_group_name: "A valid group name to be invited to all automated messages."
     send_welcome_message: "Send all new users a welcome message with a quick start guide."
     send_tl1_welcome_message: "Send new trust level 1 users a welcome message."
+    send_tl2_promotion_message: "Send new trust level 2 users a message about promotion."
     suppress_reply_directly_below: "Don't show the expandable reply count on a post when there is only a single reply directly below this post."
     suppress_reply_directly_above: "Don't show the expandable in-reply-to on a post when there is only a single reply directly above this post."
     remove_full_quote: "Automatically remove full quotes on direct replies."
@@ -2847,6 +2848,13 @@ en:
 
         [prefs]: %{user_preferences_url}
 
+    tl2_promotion_message:
+      subject_template: "Congratulations on your trust level promotion!"
+      text_body_template: |
+        We’ve promoted you up a [trust level](https://blog.discourse.org/2018/06/understanding-discourse-trust-levels/)!
+
+        We invite you to keep getting involved – we enjoy having you around.
+
     backup_succeeded:
       title: "Backup Succeeded"
       subject_template: "Backup completed successfully"
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 5a693c7..d1b4641 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -1390,6 +1390,7 @@ trust:
     enum: "TrustLevelSetting"
   allow_flagging_staff: true
   send_tl1_welcome_message: true
+  send_tl2_promotion_message: true
   tl1_requires_topics_entered: 5
   tl1_requires_read_posts:
     default: 30
diff --git a/lib/promotion.rb b/lib/promotion.rb
index d768d22..286494f 100644
--- a/lib/promotion.rb
+++ b/lib/promotion.rb
@@ -33,7 +33,11 @@ class Promotion
   end
 
   def review_tl1
-    Promotion.tl2_met?(@user) && change_trust_level!(TrustLevel[2])
+    if Promotion.tl2_met?(@user) && change_trust_level!(TrustLevel[2])
+      @user.enqueue_tl2_promotion_message
+      return true
+    end
+    false
   end
 
   def review_tl2
diff --git a/lib/system_message.rb b/lib/system_message.rb
index fd6e5af..648eede 100644
--- a/lib/system_message.rb
+++ b/lib/system_message.rb
@@ -43,6 +43,8 @@ class SystemMessage
 
     post = I18n.with_locale(@recipient.effective_locale) { creator.create }
 
+    DiscourseEvent.trigger(:system_message_sent, post: post, message_type: type)
+
     if creator.errors.present?
       raise StandardError, creator.errors.full_messages.join(" ")
     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
deleted file mode 100644
index 91b3479..0000000
--- a/plugins/discourse-narrative-bot/autoload/jobs/send_advanced_tutorial_message.rb
+++ /dev/null
@@ -1,23 +0,0 @@
-# frozen_string_literal: true
-
-module Jobs
-  class SendAdvancedTutorialMessage < ::Jobs::Base
-    def execute(args)
-      user = User.find_by(id: args[:user_id])
-      return if user.nil?
-
-      raw = I18n.t("discourse_narrative_bot.tl2_promotion_message.text_body_template",
-                    discobot_username: ::DiscourseNarrativeBot::Base.new.discobot_username,
-                    reset_trigger: "#{::DiscourseNarrativeBot::TrackSelector.reset_trigger} #{::DiscourseNarrativeBot::AdvancedUserNarrative.reset_trigger}")
-
-      PostCreator.create!(
-        Discourse.system_user,
-        title: I18n.t("discourse_narrative_bot.tl2_promotion_message.subject_template"),
-        raw: raw,
-        archetype: Archetype.private_message,
-        target_usernames: user.username,
-        skip_validations: true
-      )
-    end
-  end
-end
diff --git a/plugins/discourse-narrative-bot/config/locales/server.en.yml b/plugins/discourse-narrative-bot/config/locales/server.en.yml
index 5715ca4..b22a53e 100644
--- a/plugins/discourse-narrative-bot/config/locales/server.en.yml
+++ b/plugins/discourse-narrative-bot/config/locales/server.en.yml
@@ -30,8 +30,6 @@ en:
 
         Reply to this message with `@%{discobot_username} %{reset_trigger}` 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/plugin.rb b/plugins/discourse-narrative-bot/plugin.rb
index b37e0a5..e9ceeee 100644
--- a/plugins/discourse-narrative-bot/plugin.rb
+++ b/plugins/discourse-narrative-bot/plugin.rb
@@ -33,7 +33,6 @@ 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',
@@ -282,25 +281,28 @@ 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
-      # The event 'user_promoted' is sometimes called from inside a transaction.
-      # Use this helper to ensure the job is enqueued after commit to prevent
-      # any race conditions.
-      DB.after_commit do
-        Jobs.enqueue(:send_advanced_tutorial_message, user_id: args[:user_id])
-      end
-    end
-  end
-
   UserAvatar.register_custom_user_gravatar_email_hash(
     DiscourseNarrativeBot::BOT_USER_ID,
     "discobot@discourse.org"
   )
 
+  self.on(:system_message_sent) do |args|
+    return if args[:message_type] != 'tl2_promotion_message'
+    return if !SiteSetting.discourse_narrative_bot_enabled
+
+    raw = I18n.t("discourse_narrative_bot.tl2_promotion_message.text_body_template",
+                 discobot_username: ::DiscourseNarrativeBot::Base.new.discobot_username,
+                 reset_trigger: "#{::DiscourseNarrativeBot::TrackSelector.reset_trigger} #{::DiscourseNarrativeBot::AdvancedUserNarrative.reset_trigger}")
+
+    PostCreator.create!(
+      ::DiscourseNarrativeBot::Base.new.discobot_user,
+      title: I18n.t("discourse_narrative_bot.tl2_promotion_message.subject_template"),
+      raw: raw,
+      topic_id: args[:post].topic_id,
+      skip_validations: true
+    )
+  end
+
   PostGuardian.class_eval do
     alias_method :existing_can_create_post?, :can_create_post?
 
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
deleted file mode 100644
index 06233a0..0000000
--- a/plugins/discourse-narrative-bot/spec/jobs/send_advanced_tutorial_message_spec.rb
+++ /dev/null
@@ -1,30 +0,0 @@
-# frozen_string_literal: true
-
-require 'rails_helper'
-
-RSpec.describe Jobs::SendAdvancedTutorialMessage do
-  before do
-    Jobs.run_immediately!
-    SiteSetting.discourse_narrative_bot_enabled = true
-  end
-

[... diff too long, it was truncated ...]

GitHub sha: 0bb51dcb

This commit appears in #10679 which was approved by tgxworld. It was merged by lis2.