2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-24 13:50:20 +08:00
|
|
|
module DiscourseNarrativeBot
|
|
|
|
module Actions
|
2017-06-06 14:38:26 +08:00
|
|
|
TIMEOUT_DURATION = 900 # 15 mins
|
|
|
|
|
2017-05-26 10:09:42 +08:00
|
|
|
def discobot_user
|
|
|
|
@discobot ||= User.find(-2)
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|
|
|
|
|
2020-06-25 19:29:55 +08:00
|
|
|
def discobot_username
|
|
|
|
self.discobot_user.username_lower
|
|
|
|
end
|
|
|
|
|
2017-05-24 13:50:20 +08:00
|
|
|
private
|
|
|
|
|
2017-06-12 15:41:39 +08:00
|
|
|
def reply_to(post, raw, opts = {}, post_alert_options = {})
|
|
|
|
defaut_post_alert_opts = { skip_send_email: true }.merge(post_alert_options)
|
|
|
|
|
2017-05-24 13:50:20 +08:00
|
|
|
if post
|
|
|
|
default_opts = {
|
|
|
|
raw: raw,
|
|
|
|
topic_id: post.topic_id,
|
2017-06-12 15:41:39 +08:00
|
|
|
reply_to_post_number: post.post_number,
|
2019-11-20 23:29:06 +08:00
|
|
|
post_alert_options: defaut_post_alert_opts,
|
|
|
|
skip_validations: true,
|
2017-05-24 13:50:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-26 10:09:42 +08:00
|
|
|
new_post = PostCreator.create!(self.discobot_user, default_opts.merge(opts))
|
2017-05-24 13:50:20 +08:00
|
|
|
reset_rate_limits(post) if new_post
|
|
|
|
new_post
|
|
|
|
else
|
2017-06-12 15:41:39 +08:00
|
|
|
PostCreator.create!(
|
|
|
|
self.discobot_user,
|
|
|
|
{ post_alert_options: defaut_post_alert_opts, raw: raw, skip_validations: true }.merge(
|
|
|
|
opts,
|
2023-01-07 04:42:16 +08:00
|
|
|
),
|
2017-06-12 15:41:39 +08:00
|
|
|
)
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_rate_limits(post)
|
|
|
|
user = post.user
|
|
|
|
|
2018-05-25 16:01:37 +08:00
|
|
|
duration =
|
|
|
|
if user && user.new_user?
|
|
|
|
SiteSetting.rate_limit_new_user_create_post
|
|
|
|
else
|
|
|
|
SiteSetting.rate_limit_create_post
|
|
|
|
end
|
|
|
|
|
2023-02-16 17:40:11 +08:00
|
|
|
return if duration <= 0
|
2018-05-25 16:01:37 +08:00
|
|
|
|
|
|
|
data = DiscourseNarrativeBot::Store.get(user.id.to_s)
|
2017-05-24 13:50:20 +08:00
|
|
|
return unless data
|
|
|
|
|
|
|
|
key =
|
|
|
|
"#{DiscourseNarrativeBot::PLUGIN_NAME}:reset-rate-limit:#{post.topic_id}:#{data["state"]}"
|
|
|
|
|
2019-12-03 17:05:53 +08:00
|
|
|
if !(count = Discourse.redis.get(key))
|
2017-05-24 13:50:20 +08:00
|
|
|
count = 0
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.setex(key, duration, count)
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if count.to_i < 2
|
|
|
|
post.default_rate_limiter.rollback!
|
|
|
|
post.limit_posts_per_day&.rollback!
|
2019-12-03 17:05:53 +08:00
|
|
|
Discourse.redis.incr(key)
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fake_delay
|
2020-07-13 09:08:31 +08:00
|
|
|
sleep(rand(1.0..2.0)) if Rails.env.production?
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def bot_mentioned?(post)
|
2020-05-05 11:46:57 +08:00
|
|
|
doc = Nokogiri::HTML5.fragment(post.cooked)
|
2017-05-24 13:50:20 +08:00
|
|
|
|
|
|
|
valid = false
|
|
|
|
|
|
|
|
doc
|
|
|
|
.css(".mention")
|
|
|
|
.each do |mention|
|
2020-06-25 19:29:55 +08:00
|
|
|
if User.normalize_username(mention.text) == "@#{self.discobot_username}"
|
2019-05-07 10:53:51 +08:00
|
|
|
valid = true
|
|
|
|
break
|
2023-01-07 04:42:16 +08:00
|
|
|
end
|
2019-05-07 10:53:51 +08:00
|
|
|
end
|
2017-05-24 13:50:20 +08:00
|
|
|
|
|
|
|
valid
|
|
|
|
end
|
|
|
|
|
|
|
|
def reply_to_bot_post?(post)
|
|
|
|
post&.reply_to_post && post.reply_to_post.user_id == -2
|
|
|
|
end
|
|
|
|
|
|
|
|
def pm_to_bot?(post)
|
|
|
|
topic = post.topic
|
|
|
|
return false if !topic
|
|
|
|
|
|
|
|
topic.pm_with_non_human_user? && topic.topic_allowed_users.where(user_id: -2).exists?
|
|
|
|
end
|
2017-06-05 15:23:25 +08:00
|
|
|
|
|
|
|
def cancel_timeout_job(user)
|
|
|
|
Jobs.cancel_scheduled_job(:narrative_timeout, user_id: user.id, klass: self.class.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def enqueue_timeout_job(user)
|
|
|
|
return if Rails.env.test?
|
|
|
|
|
|
|
|
cancel_timeout_job(user)
|
|
|
|
|
|
|
|
Jobs.enqueue_in(
|
|
|
|
TIMEOUT_DURATION,
|
|
|
|
:narrative_timeout,
|
|
|
|
user_id: user.id,
|
|
|
|
klass: self.class.to_s,
|
|
|
|
)
|
|
|
|
end
|
2017-05-24 13:50:20 +08:00
|
|
|
end
|
|
|
|
end
|