FIX: Discobot exception when site_contact_username is promoted (#11666)

Because the site admin is sending a message to themselves, there is only one user in the topic_allowed_users, and `.last` returns nil.
Attempt to recognize this situation and continue, or bail without doing anything if this somehow happens another way.
This commit is contained in:
Kane York 2021-01-11 13:07:36 -08:00 committed by GitHub
parent b1f32f2f57
commit a88a246c6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 1 deletions

View File

@ -302,7 +302,9 @@ after_initialize do
discobot_username: ::DiscourseNarrativeBot::Base.new.discobot_username,
reset_trigger: "#{::DiscourseNarrativeBot::TrackSelector.reset_trigger} #{::DiscourseNarrativeBot::AdvancedUserNarrative.reset_trigger}")
recipient = args[:post].topic.topic_users.where.not(user_id: args[:post].user_id).last.user
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
return if recipient.nil?
PostCreator.create!(
::DiscourseNarrativeBot::Base.new.discobot_user,

View File

@ -741,4 +741,14 @@ RSpec.describe DiscourseNarrativeBot::AdvancedUserNarrative do
expect(Topic.last.title).to eq(I18n.t("discourse_narrative_bot.tl2_promotion_message.subject_template"))
expect(Topic.last.topic_users.map(&:user_id).sort).to eq([DiscourseNarrativeBot::Base.new.discobot_user.id, recipient.id])
end
it 'invites the site_contact_username to advanced training fine as well' do
recipient = Post.last.user
SiteSetting.site_contact_username = recipient.username
expect {
DiscourseEvent.trigger(:system_message_sent, post: Post.last, message_type: 'tl2_promotion_message')
}.to change { Topic.count }
expect(Topic.last.title).to eq(I18n.t("discourse_narrative_bot.tl2_promotion_message.subject_template"))
expect(Topic.last.topic_users.map(&:user_id).sort).to eq([DiscourseNarrativeBot::Base.new.discobot_user.id, recipient.id])
end
end