From 0754c7c40479b95bb62db631008cc6dce6b6f708 Mon Sep 17 00:00:00 2001 From: Dan Ungureanu Date: Wed, 11 Mar 2020 14:03:20 +0200 Subject: [PATCH] FIX: Various fixes to support posts with no user (#8877) * Do not grant badges for posts with no user * Ensure instructions are correct in Change Owner modal * Hide user-dependent actions from posts with no user * Make PostRevisor work with posts with no user * Ensure posts with no user can be deleted * discourse-narrative-bot should ignore posts with no user * Skip TopicLink creation for posts with no user --- .../discourse/templates/modal/change-owner.hbs | 4 +++- .../javascripts/discourse/widgets/post-admin-menu.js.es6 | 2 +- app/models/topic_link.rb | 2 +- config/locales/client.en.yml | 3 +++ lib/cooked_post_processor.rb | 2 +- lib/guardian/post_guardian.rb | 4 ++-- lib/post_destroyer.rb | 2 ++ lib/post_revisor.rb | 4 ++-- plugins/discourse-narrative-bot/plugin.rb | 8 ++++---- 9 files changed, 19 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/discourse/templates/modal/change-owner.hbs b/app/assets/javascripts/discourse/templates/modal/change-owner.hbs index 1069c0d2fac..a4e0a35c9b0 100644 --- a/app/assets/javascripts/discourse/templates/modal/change-owner.hbs +++ b/app/assets/javascripts/discourse/templates/modal/change-owner.hbs @@ -1,6 +1,8 @@ {{#d-modal-body class='change-ownership'}} - {{i18n 'topic.change_owner.instructions' count=selectedPostsCount old_user=selectedPostsUsername}} + {{i18n (if selectedPostsUsername 'topic.change_owner.instructions' 'topic.change_owner.instructions_without_old_user') + count=selectedPostsCount + old_user=selectedPostsUsername}}
diff --git a/app/assets/javascripts/discourse/widgets/post-admin-menu.js.es6 b/app/assets/javascripts/discourse/widgets/post-admin-menu.js.es6 index 83e77347576..40dc1d3c85d 100644 --- a/app/assets/javascripts/discourse/widgets/post-admin-menu.js.es6 +++ b/app/assets/javascripts/discourse/widgets/post-admin-menu.js.es6 @@ -73,7 +73,7 @@ export function buildManageButtons(attrs, currentUser, siteSettings) { }); } - if (currentUser.staff) { + if (attrs.user_id && currentUser.staff) { if (siteSettings.enable_badges) { contents.push({ icon: "certificate", diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb index ed0633ecd75..2fc9fd0b84e 100644 --- a/app/models/topic_link.rb +++ b/app/models/topic_link.rb @@ -108,7 +108,7 @@ class TopicLink < ActiveRecord::Base end def self.extract_from(post) - return if post.blank? || post.whisper? + return if post.blank? || post.whisper? || post.user_id.blank? current_urls = [] reflected_ids = [] diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index dfee7675147..ea18a5b17a8 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2420,6 +2420,9 @@ en: instructions: one: "Please choose a new owner for the post by @{{old_user}}" other: "Please choose a new owner for the {{count}} posts by @{{old_user}}" + instructions_without_old_user: + one: "Please choose a new owner for the post" + other: "Please choose a new owner for the {{count}} posts" change_timestamp: title: "Change Timestamp..." diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rb index 3d85718fb56..bd56c12ca04 100644 --- a/lib/cooked_post_processor.rb +++ b/lib/cooked_post_processor.rb @@ -56,7 +56,7 @@ class CookedPostProcessor end def grant_badges - return unless Guardian.new.can_see?(@post) + return if @post.user.blank? || !Guardian.new.can_see?(@post) BadgeGranter.grant(Badge.find(Badge::FirstEmoji), @post.user, post_id: @post.id) if has_emoji? BadgeGranter.grant(Badge.find(Badge::FirstOnebox), @post.user, post_id: @post.id) if @has_oneboxes diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rb index 60936e7480b..2bbc48f88f3 100644 --- a/lib/guardian/post_guardian.rb +++ b/lib/guardian/post_guardian.rb @@ -30,7 +30,7 @@ module PostGuardian return false unless (can_see_post.nil? && can_see_post?(post)) || can_see_post # no warnings except for staff - return false if (action_key == :notify_user && !is_staff? && opts[:is_warning].present? && opts[:is_warning] == 'true') + return false if action_key == :notify_user && (post.user.blank? || (!is_staff? && opts[:is_warning].present? && opts[:is_warning] == 'true')) taken = opts[:taken_actions].try(:keys).to_a is_flag = PostActionType.notify_flag_types[action_key] @@ -71,7 +71,7 @@ module PostGuardian not(post.trashed?) && # don't like your own stuff - not(action_key == :like && is_my_own?(post)) + not(action_key == :like && (post.user.blank? || is_my_own?(post))) end !!result diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index 1247346f265..578722543fb 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -276,6 +276,8 @@ class PostDestroyer end def notify_deletion(reviewable) + return if @post.user.blank? + allowed_user = @user.human? && @user.staff? return unless allowed_user && rs = reviewable.reviewable_scores.order('created_at DESC').first diff --git a/lib/post_revisor.rb b/lib/post_revisor.rb index 981ff45f369..2685292e69c 100644 --- a/lib/post_revisor.rb +++ b/lib/post_revisor.rb @@ -180,13 +180,13 @@ class PostRevisor @fields.has_key?('raw') && @editor.staff? && @editor != Discourse.system_user && - !@post.user.staff? + !@post.user&.staff? ) PostLocker.new(@post, @editor).lock end # We log staff edits to posts - if @editor.staff? && @editor.id != @post.user.id && @fields.has_key?('raw') && !@opts[:skip_staff_log] + if @editor.staff? && @editor.id != @post.user_id && @fields.has_key?('raw') && !@opts[:skip_staff_log] StaffActionLogger.new(@editor).log_post_edit( @post, old_raw: old_raw diff --git a/plugins/discourse-narrative-bot/plugin.rb b/plugins/discourse-narrative-bot/plugin.rb index 5ea07c9090a..ea145c48736 100644 --- a/plugins/discourse-narrative-bot/plugin.rb +++ b/plugins/discourse-narrative-bot/plugin.rb @@ -178,7 +178,7 @@ after_initialize do self.on(:post_created) do |post, options| user = post.user - if user.enqueue_narrative_bot_job? && !options[:skip_bot] + if user&.enqueue_narrative_bot_job? && !options[:skip_bot] Jobs.enqueue(:bot_input, user_id: user.id, post_id: post.id, @@ -188,7 +188,7 @@ after_initialize do end self.on(:post_edited) do |post| - if post.user.enqueue_narrative_bot_job? + if post.user&.enqueue_narrative_bot_job? Jobs.enqueue(:bot_input, user_id: post.user.id, post_id: post.id, @@ -198,7 +198,7 @@ after_initialize do end self.on(:post_destroyed) do |post, options, user| - if user.enqueue_narrative_bot_job? && !options[:skip_bot] + if user&.enqueue_narrative_bot_job? && !options[:skip_bot] Jobs.enqueue(:bot_input, user_id: user.id, post_id: post.id, @@ -209,7 +209,7 @@ after_initialize do end self.on(:post_recovered) do |post, _, user| - if user.enqueue_narrative_bot_job? + if user&.enqueue_narrative_bot_job? Jobs.enqueue(:bot_input, user_id: user.id, post_id: post.id,