2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-24 13:50:20 +08:00
|
|
|
# name: discourse-narrative-bot
|
|
|
|
# about: Introduces staff to Discourse
|
2018-03-23 09:57:53 +08:00
|
|
|
# version: 1.0
|
|
|
|
# authors: Nick Sahler, Alan Tan
|
2021-07-19 23:35:47 +08:00
|
|
|
# url: https://github.com/discourse/discourse/tree/main/plugins/discourse-narrative-bot
|
2017-05-24 13:50:20 +08:00
|
|
|
|
|
|
|
enabled_site_setting :discourse_narrative_bot_enabled
|
2018-05-16 06:43:09 +08:00
|
|
|
hide_plugin if self.respond_to?(:hide_plugin)
|
2017-05-24 13:50:20 +08:00
|
|
|
|
2018-03-23 09:57:53 +08:00
|
|
|
if Rails.env == "development"
|
|
|
|
# workaround, teach reloader to reload jobs
|
|
|
|
# if we do not do this then
|
|
|
|
#
|
|
|
|
# 1. on reload rails goes and undefines Jobs::Base
|
|
|
|
# 2. as a side effect this undefines Jobs::BotInput
|
|
|
|
# 3. we have a post_edited hook that queues a job for bot input
|
|
|
|
# 4. if you are not running sidekiq in dev every time you save a post it will trigger it
|
|
|
|
# 5. but the constant can not be autoloaded
|
2022-05-26 20:19:55 +08:00
|
|
|
Rails.configuration.autoload_paths << File.expand_path('../autoload/jobs', __FILE__)
|
2018-03-23 09:57:53 +08:00
|
|
|
end
|
|
|
|
|
2017-05-24 13:50:20 +08:00
|
|
|
require_relative 'lib/discourse_narrative_bot/welcome_post_type_site_setting.rb'
|
2021-02-03 07:05:26 +08:00
|
|
|
register_asset 'stylesheets/discourse-narrative-bot.scss'
|
2017-05-24 13:50:20 +08:00
|
|
|
|
|
|
|
after_initialize do
|
2018-06-07 18:10:52 +08:00
|
|
|
SeedFu.fixture_paths << Rails.root.join("plugins", "discourse-narrative-bot", "db", "fixtures").to_s
|
2017-05-24 13:50:20 +08:00
|
|
|
|
|
|
|
Mime::Type.register "image/svg+xml", :svg
|
|
|
|
|
|
|
|
[
|
2022-05-26 20:19:55 +08:00
|
|
|
'../autoload/jobs/regular/bot_input.rb',
|
|
|
|
'../autoload/jobs/regular/narrative_timeout.rb',
|
|
|
|
'../autoload/jobs/regular/narrative_init.rb',
|
|
|
|
'../autoload/jobs/regular/send_default_welcome_message.rb',
|
|
|
|
'../autoload/jobs/onceoff/discourse_narrative_bot/grant_badges.rb',
|
|
|
|
'../autoload/jobs/onceoff/discourse_narrative_bot/remap_old_bot_images.rb',
|
2017-05-24 13:50:20 +08:00
|
|
|
'../lib/discourse_narrative_bot/actions.rb',
|
|
|
|
'../lib/discourse_narrative_bot/base.rb',
|
|
|
|
'../lib/discourse_narrative_bot/new_user_narrative.rb',
|
|
|
|
'../lib/discourse_narrative_bot/advanced_user_narrative.rb',
|
|
|
|
'../lib/discourse_narrative_bot/track_selector.rb',
|
|
|
|
'../lib/discourse_narrative_bot/certificate_generator.rb',
|
|
|
|
'../lib/discourse_narrative_bot/dice.rb',
|
|
|
|
'../lib/discourse_narrative_bot/quote_generator.rb',
|
|
|
|
'../lib/discourse_narrative_bot/magic_8_ball.rb',
|
|
|
|
'../lib/discourse_narrative_bot/welcome_post_type_site_setting.rb'
|
|
|
|
].each { |path| load File.expand_path(path, __FILE__) }
|
|
|
|
|
2021-03-19 18:07:39 +08:00
|
|
|
RailsMultisite::ConnectionManagement.safe_each_connection do
|
2021-03-17 23:41:31 +08: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-02 07:49:32 +08:00
|
|
|
end
|
|
|
|
|
2017-09-15 13:02:11 +08:00
|
|
|
require_dependency 'plugin_store'
|
|
|
|
|
2017-05-24 13:50:20 +08:00
|
|
|
module ::DiscourseNarrativeBot
|
|
|
|
PLUGIN_NAME = "discourse-narrative-bot".freeze
|
2020-05-23 12:56:13 +08:00
|
|
|
BOT_USER_ID = -2
|
2017-05-24 13:50:20 +08:00
|
|
|
|
|
|
|
class Engine < ::Rails::Engine
|
|
|
|
engine_name PLUGIN_NAME
|
|
|
|
isolate_namespace DiscourseNarrativeBot
|
|
|
|
end
|
|
|
|
|
|
|
|
class Store
|
|
|
|
def self.set(key, value)
|
|
|
|
::PluginStore.set(PLUGIN_NAME, key, value)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.get(key)
|
|
|
|
::PluginStore.get(PLUGIN_NAME, key)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.remove(key)
|
|
|
|
::PluginStore.remove(PLUGIN_NAME, key)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class CertificatesController < ::ApplicationController
|
|
|
|
layout :false
|
2017-08-31 12:06:56 +08:00
|
|
|
skip_before_action :check_xhr
|
2018-08-20 11:21:54 +08:00
|
|
|
requires_login
|
2017-05-24 13:50:20 +08:00
|
|
|
|
|
|
|
def generate
|
2019-10-22 00:14:15 +08:00
|
|
|
immutable_for(24.hours)
|
|
|
|
|
|
|
|
%i[date user_id].each do |key|
|
|
|
|
raise Discourse::InvalidParameters.new("#{key} must be present") unless params[key]&.present?
|
2018-08-20 11:21:54 +08:00
|
|
|
end
|
2017-05-24 13:50:20 +08:00
|
|
|
|
2021-07-06 08:23:57 +08:00
|
|
|
if params[:user_id].to_i != current_user.id
|
|
|
|
rate_limiter = RateLimiter.new(current_user, 'svg_certificate', 3, 1.minute)
|
|
|
|
else
|
|
|
|
rate_limiter = RateLimiter.new(current_user, 'svg_certificate_self', 30, 10.minutes)
|
|
|
|
end
|
2019-10-22 00:14:15 +08:00
|
|
|
rate_limiter.performed! unless current_user.staff?
|
|
|
|
|
2017-05-24 13:50:20 +08:00
|
|
|
user = User.find_by(id: params[:user_id])
|
|
|
|
raise Discourse::NotFound if user.blank?
|
|
|
|
|
2019-10-22 00:14:15 +08:00
|
|
|
hijack do
|
2021-02-02 07:49:32 +08:00
|
|
|
generator = CertificateGenerator.new(user, params[:date], avatar_url(user))
|
2017-05-24 13:50:20 +08:00
|
|
|
|
2019-10-22 00:14:15 +08:00
|
|
|
svg = params[:type] == 'advanced' ? generator.advanced_user_track : generator.new_user_track
|
2017-05-24 13:50:20 +08:00
|
|
|
|
2019-10-22 00:14:15 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.svg { render inline: svg }
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-22 00:14:15 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-02-02 07:49:32 +08:00
|
|
|
def avatar_url(user)
|
|
|
|
UrlHelper.absolute(Discourse.base_path + user.avatar_template.gsub('{size}', '250'))
|
2019-10-22 00:14:15 +08:00
|
|
|
end
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
DiscourseNarrativeBot::Engine.routes.draw do
|
|
|
|
get "/certificate" => "certificates#generate", format: :svg
|
|
|
|
end
|
|
|
|
|
|
|
|
Discourse::Application.routes.append do
|
|
|
|
mount ::DiscourseNarrativeBot::Engine, at: "/discobot"
|
|
|
|
end
|
|
|
|
|
|
|
|
self.add_model_callback(User, :after_destroy) do
|
|
|
|
DiscourseNarrativeBot::Store.remove(self.id)
|
|
|
|
end
|
|
|
|
|
2019-10-04 13:18:49 +08: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 16:20:16 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.on(:user_first_logged_in) do |user|
|
|
|
|
if SiteSetting.discourse_narrative_bot_welcome_post_delay > 0
|
|
|
|
user.enqueue_bot_welcome_post
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-20 00:56:24 +08:00
|
|
|
self.on(:user_unstaged) do |user|
|
|
|
|
user.enqueue_bot_welcome_post
|
|
|
|
end
|
|
|
|
|
2020-08-18 16:13:40 +08:00
|
|
|
self.add_model_callback(UserOption, :after_save) do
|
|
|
|
if saved_change_to_skip_new_user_tips? && self.skip_new_user_tips
|
|
|
|
user.delete_bot_welcome_post
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-01 16:20:16 +08:00
|
|
|
self.add_to_class(:user, :enqueue_bot_welcome_post) do
|
2017-05-24 13:50:20 +08: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-30 01:21:30 +08:00
|
|
|
if enqueue_narrative_bot_job? && !manually_disabled_discobot?
|
2017-05-24 13:50:20 +08: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-30 01:21:30 +08:00
|
|
|
self.add_to_class(:user, :manually_disabled_discobot?) do
|
|
|
|
user_option&.skip_new_user_tips
|
|
|
|
end
|
|
|
|
|
2017-06-01 11:06:25 +08:00
|
|
|
self.add_to_class(:user, :enqueue_narrative_bot_job?) do
|
|
|
|
SiteSetting.discourse_narrative_bot_enabled &&
|
2019-02-09 02:34:54 +08:00
|
|
|
self.human? &&
|
2017-06-01 11:06:25 +08:00
|
|
|
!self.anonymous? &&
|
|
|
|
!self.staged &&
|
|
|
|
!SiteSetting.discourse_narrative_bot_ignored_usernames.split('|'.freeze).include?(self.username)
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|
|
|
|
|
2020-08-18 16:13:40 +08:00
|
|
|
self.add_to_class(:user, :delete_bot_welcome_post) do
|
|
|
|
data = DiscourseNarrativeBot::Store.get(self.id) || {}
|
|
|
|
topic_id = data[:topic_id]
|
|
|
|
return if topic_id.blank? || data[:track] != DiscourseNarrativeBot::NewUserNarrative.to_s
|
|
|
|
|
|
|
|
topic_user = topic_users.find_by(topic_id: topic_id)
|
2021-07-05 14:17:31 +08:00
|
|
|
return if topic_user.present? && topic_user.last_read_post_number.present?
|
2020-08-18 16:13:40 +08:00
|
|
|
|
|
|
|
topic = Topic.find_by(id: topic_id)
|
|
|
|
return if topic.blank?
|
|
|
|
|
|
|
|
first_post = topic.ordered_posts.first
|
2020-10-02 08:32:45 +08:00
|
|
|
|
|
|
|
notification = Notification.where(topic_id: topic.id, post_number: first_post.post_number).first
|
|
|
|
if notification.present?
|
|
|
|
Notification.read(self, notification.id)
|
|
|
|
self.saw_notification_id(notification.id)
|
|
|
|
self.reload
|
|
|
|
self.publish_notifications_state
|
|
|
|
end
|
|
|
|
|
2020-09-23 14:55:39 +08:00
|
|
|
PostDestroyer.new(Discourse.system_user, first_post, skip_staff_log: true).destroy
|
2020-08-18 16:13:40 +08:00
|
|
|
DiscourseNarrativeBot::Store.remove(self.id)
|
|
|
|
end
|
|
|
|
|
2017-05-24 13:50:20 +08:00
|
|
|
self.on(:post_created) do |post, options|
|
|
|
|
user = post.user
|
|
|
|
|
2020-03-11 20:03:20 +08:00
|
|
|
if user&.enqueue_narrative_bot_job? && !options[:skip_bot]
|
2017-05-24 13:50:20 +08:00
|
|
|
Jobs.enqueue(:bot_input,
|
|
|
|
user_id: user.id,
|
|
|
|
post_id: post.id,
|
2022-02-05 03:58:34 +08:00
|
|
|
input: "reply"
|
2017-05-24 13:50:20 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.on(:post_edited) do |post|
|
2020-03-11 20:03:20 +08:00
|
|
|
if post.user&.enqueue_narrative_bot_job?
|
2017-05-24 13:50:20 +08:00
|
|
|
Jobs.enqueue(:bot_input,
|
|
|
|
user_id: post.user.id,
|
|
|
|
post_id: post.id,
|
2022-02-05 03:58:34 +08:00
|
|
|
input: "edit"
|
2017-05-24 13:50:20 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.on(:post_destroyed) do |post, options, user|
|
2020-03-11 20:03:20 +08:00
|
|
|
if user&.enqueue_narrative_bot_job? && !options[:skip_bot]
|
2017-05-24 13:50:20 +08:00
|
|
|
Jobs.enqueue(:bot_input,
|
|
|
|
user_id: user.id,
|
|
|
|
post_id: post.id,
|
|
|
|
topic_id: post.topic_id,
|
2022-02-05 03:58:34 +08:00
|
|
|
input: "delete"
|
2017-05-24 13:50:20 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.on(:post_recovered) do |post, _, user|
|
2020-03-11 20:03:20 +08:00
|
|
|
if user&.enqueue_narrative_bot_job?
|
2017-05-24 13:50:20 +08:00
|
|
|
Jobs.enqueue(:bot_input,
|
|
|
|
user_id: user.id,
|
|
|
|
post_id: post.id,
|
2022-02-05 03:58:34 +08:00
|
|
|
input: "recover"
|
2017-05-24 13:50:20 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.add_model_callback(PostAction, :after_commit, on: :create) do
|
2018-12-21 22:09:27 +08:00
|
|
|
if self.post && self.user.enqueue_narrative_bot_job?
|
2017-05-24 13:50:20 +08:00
|
|
|
input =
|
|
|
|
case self.post_action_type_id
|
2020-08-04 09:38:04 +08:00
|
|
|
when *PostActionType.flag_types.values
|
2022-02-05 03:58:34 +08:00
|
|
|
self.post_action_type_id == PostActionType.types[:inappropriate] ? "flag" : "reply"
|
2017-05-24 13:50:20 +08:00
|
|
|
when PostActionType.types[:like]
|
2022-02-05 03:58:34 +08:00
|
|
|
"like"
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if input
|
|
|
|
Jobs.enqueue(:bot_input,
|
|
|
|
user_id: self.user.id,
|
|
|
|
post_id: self.post.id,
|
|
|
|
input: input
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-27 10:17:18 +08:00
|
|
|
self.add_model_callback(Bookmark, :after_commit, on: :create) do
|
2022-05-09 14:19:18 +08:00
|
|
|
if self.user.enqueue_narrative_bot_job?
|
2022-05-23 08:07:15 +08:00
|
|
|
if self.bookmarkable_type == "Post"
|
2022-05-09 14:19:18 +08:00
|
|
|
Jobs.enqueue(:bot_input, user_id: self.user_id, post_id: self.bookmarkable_id, input: "bookmark")
|
|
|
|
end
|
2020-03-27 10:17:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-24 13:50:20 +08: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-05 03:58:34 +08:00
|
|
|
input: "topic_notification_level_changed"
|
2017-05-24 13:50:20 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2019-11-14 05:31:49 +08:00
|
|
|
|
2020-05-23 12:56:13 +08:00
|
|
|
UserAvatar.register_custom_user_gravatar_email_hash(
|
|
|
|
DiscourseNarrativeBot::BOT_USER_ID,
|
|
|
|
"discobot@discourse.org"
|
|
|
|
)
|
2020-08-22 01:13:13 +08:00
|
|
|
|
2020-09-22 08:17:52 +08:00
|
|
|
self.on(:system_message_sent) do |args|
|
2021-05-31 04:07:45 +08:00
|
|
|
next if !SiteSetting.discourse_narrative_bot_enabled
|
|
|
|
next if args[:message_type] != 'tl2_promotion_message'
|
2020-09-23 00:56:21 +08:00
|
|
|
|
2021-05-31 04:07:45 +08:00
|
|
|
recipient = args[:post].topic.topic_users.where.not(user_id: args[:post].user_id).last&.user
|
|
|
|
recipient ||= Discourse.site_contact_user if args[:post].user == Discourse.site_contact_user
|
|
|
|
next if recipient.nil?
|
2020-09-23 00:56:21 +08:00
|
|
|
|
2021-05-31 04:07:45 +08: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-28 11:52:33 +08:00
|
|
|
|
2020-09-23 00:56:21 +08:00
|
|
|
PostCreator.create!(
|
|
|
|
::DiscourseNarrativeBot::Base.new.discobot_user,
|
|
|
|
title: I18n.t("discourse_narrative_bot.tl2_promotion_message.subject_template"),
|
|
|
|
raw: raw,
|
2020-09-28 11:52:33 +08:00
|
|
|
skip_validations: true,
|
|
|
|
archetype: Archetype.private_message,
|
|
|
|
target_usernames: recipient.username
|
2020-09-23 00:56:21 +08:00
|
|
|
)
|
|
|
|
end
|
2020-09-22 08:17:52 +08:00
|
|
|
end
|
|
|
|
|
2020-08-22 01:13:13 +08:00
|
|
|
PostGuardian.class_eval do
|
|
|
|
alias_method :existing_can_create_post?, :can_create_post?
|
|
|
|
|
|
|
|
def can_create_post?(parent)
|
|
|
|
return true if SiteSetting.discourse_narrative_bot_enabled &&
|
|
|
|
parent.try(:subtype) == "system_message" &&
|
|
|
|
parent.try(:user) == ::DiscourseNarrativeBot::Base.new.discobot_user
|
|
|
|
|
|
|
|
existing_can_create_post?(parent)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|