FIX: Allow users to rename discobot without having to rebuild.
This commit is contained in:
parent
8268c98cf9
commit
7028bdceed
|
@ -1,12 +1,8 @@
|
||||||
module DiscourseNarrativeBot
|
module DiscourseNarrativeBot
|
||||||
module Actions
|
module Actions
|
||||||
extend ActiveSupport::Concern
|
def discobot_user
|
||||||
|
|
||||||
included do
|
|
||||||
def self.discobot_user
|
|
||||||
@discobot ||= User.find(-2)
|
@discobot ||= User.find(-2)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
@ -18,11 +14,11 @@ module DiscourseNarrativeBot
|
||||||
reply_to_post_number: post.post_number
|
reply_to_post_number: post.post_number
|
||||||
}
|
}
|
||||||
|
|
||||||
new_post = PostCreator.create!(self.class.discobot_user, default_opts.merge(opts))
|
new_post = PostCreator.create!(self.discobot_user, default_opts.merge(opts))
|
||||||
reset_rate_limits(post) if new_post
|
reset_rate_limits(post) if new_post
|
||||||
new_post
|
new_post
|
||||||
else
|
else
|
||||||
PostCreator.create!(self.class.discobot_user, { raw: raw }.merge(opts))
|
PostCreator.create!(self.discobot_user, { raw: raw }.merge(opts))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -64,7 +60,7 @@ module DiscourseNarrativeBot
|
||||||
valid = false
|
valid = false
|
||||||
|
|
||||||
doc.css(".mention").each do |mention|
|
doc.css(".mention").each do |mention|
|
||||||
valid = true if mention.text == "@#{self.class.discobot_user.username}"
|
valid = true if mention.text == "@#{self.discobot_user.username}"
|
||||||
end
|
end
|
||||||
|
|
||||||
valid
|
valid
|
||||||
|
|
|
@ -124,7 +124,7 @@ module DiscourseNarrativeBot
|
||||||
post = PostCreator.create!(@user, {
|
post = PostCreator.create!(@user, {
|
||||||
raw: I18n.t(
|
raw: I18n.t(
|
||||||
"#{I18N_KEY}.edit.bot_created_post_raw",
|
"#{I18N_KEY}.edit.bot_created_post_raw",
|
||||||
discobot_username: self.class.discobot_user.username
|
discobot_username: self.discobot_user.username
|
||||||
),
|
),
|
||||||
topic_id: data[:topic_id],
|
topic_id: data[:topic_id],
|
||||||
skip_bot: true
|
skip_bot: true
|
||||||
|
@ -140,7 +140,7 @@ module DiscourseNarrativeBot
|
||||||
post = PostCreator.create!(@user, {
|
post = PostCreator.create!(@user, {
|
||||||
raw: I18n.t(
|
raw: I18n.t(
|
||||||
"#{I18N_KEY}.recover.deleted_post_raw",
|
"#{I18N_KEY}.recover.deleted_post_raw",
|
||||||
discobot_username: self.class.discobot_user.username
|
discobot_username: self.discobot_user.username
|
||||||
),
|
),
|
||||||
topic_id: data[:topic_id],
|
topic_id: data[:topic_id],
|
||||||
skip_bot: true
|
skip_bot: true
|
||||||
|
@ -223,7 +223,7 @@ module DiscourseNarrativeBot
|
||||||
#{instance_eval(&@next_instructions)}
|
#{instance_eval(&@next_instructions)}
|
||||||
RAW
|
RAW
|
||||||
|
|
||||||
PostCreator.create!(self.class.discobot_user,
|
PostCreator.create!(self.discobot_user,
|
||||||
raw: raw,
|
raw: raw,
|
||||||
topic_id: @topic_id
|
topic_id: @topic_id
|
||||||
)
|
)
|
||||||
|
@ -248,7 +248,7 @@ module DiscourseNarrativeBot
|
||||||
#{instance_eval(&@next_instructions)}
|
#{instance_eval(&@next_instructions)}
|
||||||
RAW
|
RAW
|
||||||
|
|
||||||
PostCreator.create!(self.class.discobot_user,
|
PostCreator.create!(self.discobot_user,
|
||||||
raw: raw,
|
raw: raw,
|
||||||
topic_id: @post.topic_id
|
topic_id: @post.topic_id
|
||||||
)
|
)
|
||||||
|
@ -306,7 +306,7 @@ module DiscourseNarrativeBot
|
||||||
|
|
||||||
fake_delay
|
fake_delay
|
||||||
|
|
||||||
post = PostCreator.create!(self.class.discobot_user,
|
post = PostCreator.create!(self.discobot_user,
|
||||||
raw: raw,
|
raw: raw,
|
||||||
topic_id: @topic_id
|
topic_id: @topic_id
|
||||||
)
|
)
|
||||||
|
|
|
@ -40,7 +40,7 @@ module DiscourseNarrativeBot
|
||||||
tutorial_emoji: {
|
tutorial_emoji: {
|
||||||
next_state: :tutorial_mention,
|
next_state: :tutorial_mention,
|
||||||
next_instructions: Proc.new {
|
next_instructions: Proc.new {
|
||||||
I18n.t("#{I18N_KEY}.mention.instructions", discobot_username: self.class.discobot_user.username)
|
I18n.t("#{I18N_KEY}.mention.instructions", discobot_username: self.discobot_user.username)
|
||||||
},
|
},
|
||||||
reply: {
|
reply: {
|
||||||
action: :reply_to_emoji
|
action: :reply_to_emoji
|
||||||
|
@ -148,7 +148,7 @@ module DiscourseNarrativeBot
|
||||||
RAW
|
RAW
|
||||||
|
|
||||||
PostRevisor.new(post, topic).revise!(
|
PostRevisor.new(post, topic).revise!(
|
||||||
self.class.discobot_user,
|
self.discobot_user,
|
||||||
{ raw: raw },
|
{ raw: raw },
|
||||||
{ skip_validations: true, force_new_version: true }
|
{ skip_validations: true, force_new_version: true }
|
||||||
)
|
)
|
||||||
|
@ -201,7 +201,7 @@ module DiscourseNarrativeBot
|
||||||
|
|
||||||
def missing_bookmark
|
def missing_bookmark
|
||||||
return unless valid_topic?(@post.topic_id)
|
return unless valid_topic?(@post.topic_id)
|
||||||
return if @post.user_id == self.class.discobot_user.id
|
return if @post.user_id == self.discobot_user.id
|
||||||
|
|
||||||
fake_delay
|
fake_delay
|
||||||
enqueue_timeout_job(@user)
|
enqueue_timeout_job(@user)
|
||||||
|
@ -211,7 +211,7 @@ module DiscourseNarrativeBot
|
||||||
|
|
||||||
def reply_to_bookmark
|
def reply_to_bookmark
|
||||||
return unless valid_topic?(@post.topic_id)
|
return unless valid_topic?(@post.topic_id)
|
||||||
return unless @post.user_id == self.class.discobot_user.id
|
return unless @post.user_id == self.discobot_user.id
|
||||||
|
|
||||||
raw = <<~RAW
|
raw = <<~RAW
|
||||||
#{I18n.t("#{I18N_KEY}.bookmark.reply", profile_page_url: url_helpers(:user_url, username: @user.username))}
|
#{I18n.t("#{I18N_KEY}.bookmark.reply", profile_page_url: url_helpers(:user_url, username: @user.username))}
|
||||||
|
@ -434,7 +434,7 @@ module DiscourseNarrativeBot
|
||||||
reply_to(@post, I18n.t(
|
reply_to(@post, I18n.t(
|
||||||
"#{I18N_KEY}.mention.not_found",
|
"#{I18N_KEY}.mention.not_found",
|
||||||
username: @user.username,
|
username: @user.username,
|
||||||
discobot_username: self.class.discobot_user.username
|
discobot_username: self.discobot_user.username
|
||||||
))
|
))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -496,7 +496,7 @@ module DiscourseNarrativeBot
|
||||||
username: @user.username,
|
username: @user.username,
|
||||||
base_url: Discourse.base_url,
|
base_url: Discourse.base_url,
|
||||||
certificate: certificate,
|
certificate: certificate,
|
||||||
discobot_username: self.class.discobot_user.username,
|
discobot_username: self.discobot_user.username,
|
||||||
advanced_trigger: AdvancedUserNarrative.reset_trigger
|
advanced_trigger: AdvancedUserNarrative.reset_trigger
|
||||||
),
|
),
|
||||||
topic_id: @data[:topic_id]
|
topic_id: @data[:topic_id]
|
||||||
|
@ -504,7 +504,7 @@ module DiscourseNarrativeBot
|
||||||
end
|
end
|
||||||
|
|
||||||
def like_post(post)
|
def like_post(post)
|
||||||
PostAction.act(self.class.discobot_user, post, PostActionType.types[:like])
|
PostAction.act(self.discobot_user, post, PostActionType.types[:like])
|
||||||
end
|
end
|
||||||
|
|
||||||
def welcome_topic
|
def welcome_topic
|
||||||
|
|
|
@ -136,7 +136,7 @@ module DiscourseNarrativeBot
|
||||||
help_message
|
help_message
|
||||||
elsif hint
|
elsif hint
|
||||||
message = I18n.t(self.class.i18n_key('random_mention.reply'),
|
message = I18n.t(self.class.i18n_key('random_mention.reply'),
|
||||||
discobot_username: self.class.discobot_user.username,
|
discobot_username: self.discobot_user.username,
|
||||||
help_trigger: self.class.help_trigger
|
help_trigger: self.class.help_trigger
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -172,7 +172,7 @@ module DiscourseNarrativeBot
|
||||||
tracks << AdvancedUserNarrative.reset_trigger
|
tracks << AdvancedUserNarrative.reset_trigger
|
||||||
end
|
end
|
||||||
|
|
||||||
discobot_username = self.class.discobot_user.username
|
discobot_username = self.discobot_user.username
|
||||||
|
|
||||||
message = I18n.t(
|
message = I18n.t(
|
||||||
self.class.i18n_key('random_mention.tracks'),
|
self.class.i18n_key('random_mention.tracks'),
|
||||||
|
@ -227,7 +227,7 @@ module DiscourseNarrativeBot
|
||||||
if pm_to_bot?(@post)
|
if pm_to_bot?(@post)
|
||||||
post_raw = @post.raw
|
post_raw = @post.raw
|
||||||
|
|
||||||
post_raw.match(/^@#{self.class.discobot_user.username} #{self.class.skip_trigger}/i) ||
|
post_raw.match(/^@#{self.discobot_user.username} #{self.class.skip_trigger}/i) ||
|
||||||
post_raw.strip == self.class.skip_trigger
|
post_raw.strip == self.class.skip_trigger
|
||||||
else
|
else
|
||||||
false
|
false
|
||||||
|
@ -235,7 +235,7 @@ module DiscourseNarrativeBot
|
||||||
end
|
end
|
||||||
|
|
||||||
def match_trigger?(trigger)
|
def match_trigger?(trigger)
|
||||||
discobot_username = self.class.discobot_user.username
|
discobot_username = self.discobot_user.username
|
||||||
regexp = Regexp.new("<a class=\"mention\".*>@#{discobot_username}</a> #{trigger}", 'i')
|
regexp = Regexp.new("<a class=\"mention\".*>@#{discobot_username}</a> #{trigger}", 'i')
|
||||||
match = @post.cooked.match(regexp)
|
match = @post.cooked.match(regexp)
|
||||||
|
|
||||||
|
@ -248,13 +248,13 @@ module DiscourseNarrativeBot
|
||||||
|
|
||||||
def like_user_post
|
def like_user_post
|
||||||
if @post.raw.match(/thank/i)
|
if @post.raw.match(/thank/i)
|
||||||
PostAction.act(self.class.discobot_user, @post, PostActionType.types[:like])
|
PostAction.act(self.discobot_user, @post, PostActionType.types[:like])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def bot_mentioned?
|
def bot_mentioned?
|
||||||
@bot_mentioned ||= PostAnalyzer.new(@post.raw, @post.topic_id).raw_mentions.include?(
|
@bot_mentioned ||= PostAnalyzer.new(@post.raw, @post.topic_id).raw_mentions.include?(
|
||||||
self.class.discobot_user.username
|
self.discobot_user.username
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -230,7 +230,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should create the right reply' do
|
it 'should create the right reply' do
|
||||||
post.update!(user: described_class.discobot_user)
|
post.update!(user: discobot_user)
|
||||||
narrative.expects(:enqueue_timeout_job).with(user)
|
narrative.expects(:enqueue_timeout_job).with(user)
|
||||||
|
|
||||||
narrative.input(:bookmark, user, post: post)
|
narrative.input(:bookmark, user, post: post)
|
||||||
|
@ -402,7 +402,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
|
||||||
post_action = PostAction.last
|
post_action = PostAction.last
|
||||||
|
|
||||||
expect(post_action.post_action_type_id).to eq(PostActionType.types[:like])
|
expect(post_action.post_action_type_id).to eq(PostActionType.types[:like])
|
||||||
expect(post_action.user).to eq(described_class.discobot_user)
|
expect(post_action.user).to eq(discobot_user)
|
||||||
expect(post_action.post).to eq(new_post)
|
expect(post_action.post).to eq(new_post)
|
||||||
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_flag)
|
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_flag)
|
||||||
end
|
end
|
||||||
|
@ -456,7 +456,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
|
||||||
post_action = PostAction.last
|
post_action = PostAction.last
|
||||||
|
|
||||||
expect(post_action.post_action_type_id).to eq(PostActionType.types[:like])
|
expect(post_action.post_action_type_id).to eq(PostActionType.types[:like])
|
||||||
expect(post_action.user).to eq(described_class.discobot_user)
|
expect(post_action.user).to eq(discobot_user)
|
||||||
expect(post_action.post).to eq(new_post)
|
expect(post_action.post).to eq(new_post)
|
||||||
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_flag)
|
expect(narrative.get_data(user)[:state].to_sym).to eq(:tutorial_flag)
|
||||||
end
|
end
|
||||||
|
@ -728,7 +728,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'flag tutorial' do
|
describe 'flag tutorial' do
|
||||||
let(:post) { Fabricate(:post, user: described_class.discobot_user, topic: topic) }
|
let(:post) { Fabricate(:post, user: discobot_user, topic: topic) }
|
||||||
let(:flag) { Fabricate(:flag, post: post, user: user) }
|
let(:flag) { Fabricate(:flag, post: post, user: user) }
|
||||||
let(:other_post) { Fabricate(:post, user: user, topic: topic) }
|
let(:other_post) { Fabricate(:post, user: user, topic: topic) }
|
||||||
|
|
||||||
|
@ -833,7 +833,7 @@ describe DiscourseNarrativeBot::NewUserNarrative do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'when post contain the right answer' do
|
describe 'when post contain the right answer' do
|
||||||
let(:post) { Fabricate(:post, user: described_class.discobot_user, topic: topic) }
|
let(:post) { Fabricate(:post, user: discobot_user, topic: topic) }
|
||||||
let(:flag) { Fabricate(:flag, post: post, user: user) }
|
let(:flag) { Fabricate(:flag, post: post, user: user) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
|
|
|
@ -2,7 +2,7 @@ require 'rails_helper'
|
||||||
|
|
||||||
describe DiscourseNarrativeBot::TrackSelector do
|
describe DiscourseNarrativeBot::TrackSelector do
|
||||||
let(:user) { Fabricate(:user) }
|
let(:user) { Fabricate(:user) }
|
||||||
let(:discobot_user) { described_class.discobot_user }
|
let(:discobot_user) { User.find(-2) }
|
||||||
let(:narrative) { DiscourseNarrativeBot::NewUserNarrative.new }
|
let(:narrative) { DiscourseNarrativeBot::NewUserNarrative.new }
|
||||||
|
|
||||||
let(:random_mention_reply) do
|
let(:random_mention_reply) do
|
||||||
|
|
Loading…
Reference in New Issue