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
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,