2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-24 01:50:20 -04:00
|
|
|
# name: discourse-narrative-bot
|
|
|
|
# about: Introduces staff to Discourse
|
2018-03-22 21:57:53 -04:00
|
|
|
# version: 1.0
|
|
|
|
# authors: Nick Sahler, Alan Tan
|
2021-07-19 11:35:47 -04:00
|
|
|
# url: https://github.com/discourse/discourse/tree/main/plugins/discourse-narrative-bot
|
2017-05-24 01:50:20 -04:00
|
|
|
|
|
|
|
enabled_site_setting :discourse_narrative_bot_enabled
|
2023-08-30 20:01:01 -04:00
|
|
|
hide_plugin
|
2017-05-24 01:50:20 -04:00
|
|
|
|
2024-02-01 11:28:10 -05:00
|
|
|
require_relative "lib/discourse_narrative_bot/welcome_post_type_site_setting"
|
2021-02-02 18:05:26 -05:00
|
|
|
register_asset "stylesheets/discourse-narrative-bot.scss"
|
2017-05-24 01:50:20 -04:00
|
|
|
|
2024-03-05 22:14:53 -05:00
|
|
|
module ::DiscourseNarrativeBot
|
|
|
|
PLUGIN_NAME = "discourse-narrative-bot".freeze
|
|
|
|
BOT_USER_ID = -2
|
|
|
|
end
|
|
|
|
|
|
|
|
require_relative "lib/discourse_narrative_bot/engine"
|
|
|
|
|
2017-05-24 01:50:20 -04:00
|
|
|
after_initialize do
|
2024-03-05 22:14:53 -05:00
|
|
|
if Rails.env.test?
|
|
|
|
::SiteSetting.defaults.tap do |s|
|
|
|
|
# disable plugins
|
|
|
|
if ENV["LOAD_PLUGINS"] == "1"
|
|
|
|
s.set_regardless_of_locale(:discourse_narrative_bot_enabled, false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-07 06:10:52 -04:00
|
|
|
SeedFu.fixture_paths << Rails
|
|
|
|
.root
|
|
|
|
.join("plugins", "discourse-narrative-bot", "db", "fixtures")
|
|
|
|
.to_s
|
2017-05-24 01:50:20 -04:00
|
|
|
|
|
|
|
Mime::Type.register "image/svg+xml", :svg
|
|
|
|
|
2021-03-19 06:07:39 -04:00
|
|
|
RailsMultisite::ConnectionManagement.safe_each_connection do
|
2021-03-17 11:41:31 -04:00
|
|
|
if SiteSetting.discourse_narrative_bot_enabled
|
|
|
|
# Disable welcome message because that is what the bot is supposed to replace.
|
|
|
|
SiteSetting.send_welcome_message = false
|
|
|
|
|
|
|
|
certificate_path = "#{Discourse.base_url}/discobot/certificate.svg"
|
|
|
|
if !SiteSetting.allowed_iframes.include?(certificate_path)
|
|
|
|
SiteSetting.allowed_iframes =
|
|
|
|
SiteSetting.allowed_iframes.split("|").append(certificate_path).join("|")
|
|
|
|
end
|
|
|
|
end
|
2021-02-01 18:49:32 -05:00
|
|
|
end
|
|
|
|
|
2017-05-24 01:50:20 -04:00
|
|
|
self.add_model_callback(User, :after_destroy) { DiscourseNarrativeBot::Store.remove(self.id) }
|
|
|
|
|
2019-10-04 01:18:49 -04:00
|
|
|
self.on(:user_created) do |user|
|
|
|
|
if SiteSetting.discourse_narrative_bot_welcome_post_delay == 0 && !user.staged
|
|
|
|
user.enqueue_bot_welcome_post
|
2017-06-01 04:20:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.on(:user_first_logged_in) do |user|
|
|
|
|
user.enqueue_bot_welcome_post if SiteSetting.discourse_narrative_bot_welcome_post_delay > 0
|
|
|
|
end
|
|
|
|
|
2018-01-19 11:56:24 -05:00
|
|
|
self.on(:user_unstaged) { |user| user.enqueue_bot_welcome_post }
|
|
|
|
|
2017-06-01 04:20:16 -04:00
|
|
|
self.add_to_class(:user, :enqueue_bot_welcome_post) do
|
2017-05-24 01:50:20 -04:00
|
|
|
return if SiteSetting.disable_discourse_narrative_bot_welcome_post
|
|
|
|
|
|
|
|
delay = SiteSetting.discourse_narrative_bot_welcome_post_delay
|
|
|
|
|
|
|
|
case SiteSetting.discourse_narrative_bot_welcome_post_type
|
|
|
|
when "new_user_track"
|
2021-03-29 13:21:30 -04:00
|
|
|
if enqueue_narrative_bot_job? && !manually_disabled_discobot?
|
2017-05-24 01:50:20 -04:00
|
|
|
Jobs.enqueue_in(
|
|
|
|
delay,
|
|
|
|
:narrative_init,
|
|
|
|
user_id: self.id,
|
|
|
|
klass: DiscourseNarrativeBot::NewUserNarrative.to_s,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
when "welcome_message"
|
|
|
|
Jobs.enqueue_in(delay, :send_default_welcome_message, user_id: self.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-03-29 13:21:30 -04:00
|
|
|
self.add_to_class(:user, :manually_disabled_discobot?) { user_option&.skip_new_user_tips }
|
|
|
|
|
2017-05-31 23:06:25 -04:00
|
|
|
self.add_to_class(:user, :enqueue_narrative_bot_job?) do
|
|
|
|
SiteSetting.discourse_narrative_bot_enabled && self.human? && !self.anonymous? &&
|
|
|
|
!self.staged &&
|
|
|
|
!SiteSetting
|
|
|
|
.discourse_narrative_bot_ignored_usernames
|
|
|
|
.split("|".freeze)
|
|
|
|
.include?(self.username)
|
2017-05-24 01:50:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
self.on(:post_created) do |post, options|
|
|
|
|
user = post.user
|
|
|
|
|
2020-03-11 08:03:20 -04:00
|
|
|
if user&.enqueue_narrative_bot_job? && !options[:skip_bot]
|
2017-05-24 01:50:20 -04:00
|
|
|
Jobs.enqueue(:bot_input, user_id: user.id, post_id: post.id, input: "reply")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.on(:post_edited) do |post|
|
2020-03-11 08:03:20 -04:00
|
|
|
if post.user&.enqueue_narrative_bot_job?
|
2017-05-24 01:50:20 -04:00
|
|
|
Jobs.enqueue(:bot_input, user_id: post.user.id, post_id: post.id, input: "edit")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.on(:post_destroyed) do |post, options, user|
|
2020-03-11 08:03:20 -04:00
|
|
|
if user&.enqueue_narrative_bot_job? && !options[:skip_bot]
|
2017-05-24 01:50:20 -04:00
|
|
|
Jobs.enqueue(
|
|
|
|
:bot_input,
|
|
|
|
user_id: user.id,
|
|
|
|
post_id: post.id,
|
|
|
|
topic_id: post.topic_id,
|
2022-02-04 14:58:34 -05:00
|
|
|
input: "delete",
|
2017-05-24 01:50:20 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.on(:post_recovered) do |post, _, user|
|
2020-03-11 08:03:20 -04:00
|
|
|
if user&.enqueue_narrative_bot_job?
|
2017-05-24 01:50:20 -04:00
|
|
|
Jobs.enqueue(:bot_input, user_id: user.id, post_id: post.id, input: "recover")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.add_model_callback(PostAction, :after_commit, on: :create) do
|
2018-12-21 09:09:27 -05:00
|
|
|
if self.post && self.user.enqueue_narrative_bot_job?
|
2017-05-24 01:50:20 -04:00
|
|
|
input =
|
|
|
|
case self.post_action_type_id
|
2020-08-03 21:38:04 -04:00
|
|
|
when *PostActionType.flag_types.values
|
2022-02-04 14:58:34 -05:00
|
|
|
self.post_action_type_id == PostActionType.types[:inappropriate] ? "flag" : "reply"
|
2017-05-24 01:50:20 -04:00
|
|
|
when PostActionType.types[:like]
|
2022-02-04 14:58:34 -05:00
|
|
|
"like"
|
2017-05-24 01:50:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
Jobs.enqueue(:bot_input, user_id: self.user.id, post_id: self.post.id, input: input) if input
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-26 22:17:18 -04:00
|
|
|
self.add_model_callback(Bookmark, :after_commit, on: :create) do
|
2022-05-09 02:19:18 -04:00
|
|
|
if self.user.enqueue_narrative_bot_job?
|
2024-05-30 08:58:51 -04:00
|
|
|
if self.bookmarkable_type == "Post" || self.bookmarkable_type == "Topic"
|
|
|
|
is_topic = self.bookmarkable_type == "Topic"
|
|
|
|
first_post_id =
|
|
|
|
Post.where(topic_id: self.bookmarkable_id, post_number: 1).pick(:id) if is_topic
|
|
|
|
|
2022-05-09 02:19:18 -04:00
|
|
|
Jobs.enqueue(
|
|
|
|
:bot_input,
|
|
|
|
user_id: self.user_id,
|
2024-05-30 08:58:51 -04:00
|
|
|
post_id: is_topic ? first_post_id : self.bookmarkable_id,
|
2022-05-09 02:19:18 -04:00
|
|
|
input: "bookmark",
|
|
|
|
)
|
|
|
|
end
|
2020-03-26 22:17:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-24 01:50:20 -04:00
|
|
|
self.on(:topic_notification_level_changed) do |_, user_id, topic_id|
|
|
|
|
user = User.find_by(id: user_id)
|
|
|
|
|
|
|
|
if user && user.enqueue_narrative_bot_job?
|
|
|
|
Jobs.enqueue(
|
|
|
|
:bot_input,
|
|
|
|
user_id: user_id,
|
|
|
|
topic_id: topic_id,
|
2022-02-04 14:58:34 -05:00
|
|
|
input: "topic_notification_level_changed",
|
2017-05-24 01:50:20 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2019-11-13 16:31:49 -05:00
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
UserAvatar.register_custom_user_gravatar_email_hash(
|
|
|
|
DiscourseNarrativeBot::BOT_USER_ID,
|
|
|
|
"discobot@discourse.org",
|
|
|
|
)
|
2020-08-21 13:13:13 -04:00
|
|
|
|
2020-09-21 20:17:52 -04:00
|
|
|
self.on(:system_message_sent) do |args|
|
2021-05-30 16:07:45 -04:00
|
|
|
next if !SiteSetting.discourse_narrative_bot_enabled
|
|
|
|
next if args[:message_type] != "tl2_promotion_message"
|
2020-09-22 12:56:21 -04:00
|
|
|
|
2023-06-07 15:51:24 -04:00
|
|
|
recipient = args[:recipient]
|
2021-05-30 16:07:45 -04:00
|
|
|
next if recipient.nil?
|
2020-09-22 12:56:21 -04:00
|
|
|
|
2021-05-30 16:07:45 -04:00
|
|
|
I18n.with_locale(recipient.effective_locale) do
|
|
|
|
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}",
|
|
|
|
)
|
2020-09-27 23:52:33 -04:00
|
|
|
|
2020-09-22 12:56:21 -04:00
|
|
|
PostCreator.create!(
|
|
|
|
::DiscourseNarrativeBot::Base.new.discobot_user,
|
|
|
|
title: I18n.t("discourse_narrative_bot.tl2_promotion_message.subject_template"),
|
|
|
|
raw: raw,
|
2020-09-27 23:52:33 -04:00
|
|
|
skip_validations: true,
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: recipient.username,
|
2020-09-22 12:56:21 -04:00
|
|
|
)
|
|
|
|
end
|
2020-09-21 20:17:52 -04:00
|
|
|
end
|
|
|
|
|
2024-03-05 22:42:20 -05:00
|
|
|
self.on(:site_setting_changed) do |name, old_value, new_value|
|
|
|
|
next if name.to_s != "default_locale"
|
|
|
|
next if !SiteSetting.discourse_narrative_bot_enabled
|
|
|
|
|
|
|
|
profile = UserProfile.find_by(user_id: DiscourseNarrativeBot::BOT_USER_ID)
|
|
|
|
|
|
|
|
next if profile.blank?
|
|
|
|
|
|
|
|
new_bio = I18n.with_locale(new_value) { I18n.t("discourse_narrative_bot.bio") }
|
|
|
|
profile.update!(bio_raw: new_bio)
|
|
|
|
end
|
|
|
|
|
2024-02-01 11:28:10 -05:00
|
|
|
PostGuardian.prepend(DiscourseNarrativeBot::PostGuardianExtension)
|
2017-05-24 01:50:20 -04:00
|
|
|
end
|