Skip to content

A platform for community discussion. Free, open, simple.

FIX: Various fixes to support posts with no user (#8877)

Dan Ungureanu0754c7c40479
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'}}
<span>
- {{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}}
</span>
<form>
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 <b>@{{old_user}}</b>"
other: "Please choose a new owner for the {{count}} posts by <b>@{{old_user}}</b>"
+ 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 &&
)
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,