diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb
index b7c321ed953..4088e7ff09e 100644
--- a/app/controllers/posts_controller.rb
+++ b/app/controllers/posts_controller.rb
@@ -42,7 +42,7 @@ class PostsController < ApplicationController
.limit(50)
# Remove posts the user doesn't have permission to see
# This isn't leaking any information we weren't already through the post ID numbers
- posts = posts.reject { |post| !guardian.can_see?(post) }
+ posts = posts.reject { |post| !guardian.can_see?(post) || post.topic.blank? }
counts = PostAction.counts_for(posts, current_user)
respond_to do |format|
diff --git a/app/views/posts/latest.rss.erb b/app/views/posts/latest.rss.erb
index f0f452a9072..7cfb0a8e1d2 100644
--- a/app/views/posts/latest.rss.erb
+++ b/app/views/posts/latest.rss.erb
@@ -7,7 +7,7 @@
<%= @link %>
<%= @description %>
<% @posts.each do |post| %>
- <% next unless post.user && post.topic %>
+ <% next unless post.user %>
<%= post.topic.title %>]]>
diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb
index 76e76f6f3da..7cc71f84c39 100644
--- a/spec/controllers/posts_controller_spec.rb
+++ b/spec/controllers/posts_controller_spec.rb
@@ -53,6 +53,32 @@ end
describe PostsController do
+ describe 'latest' do
+ let(:user) { log_in }
+ let!(:post) { Fabricate(:post, user: user) }
+ let!(:topicless_post) { Fabricate(:post, user: user, raw: '
Car 54, where are you?
') }
+
+ before do
+ topicless_post.update topic_id: -100
+ end
+
+ it 'does not return posts without a topic for json' do
+ xhr :get, :latest, format: :json
+ expect(response).to be_success
+ json = ::JSON.parse(response.body)
+ post_ids = json['latest_posts'].map { |p| p['id'] }
+ expect(post_ids).to include post.id
+ expect(post_ids).to_not include topicless_post.id
+ end
+
+ it 'does not return posts without a topic for rss' do
+ xhr :get, :latest, format: :rss
+ expect(response).to be_success
+ expect(assigns(:posts)).to include post
+ expect(assigns(:posts)).to_not include topicless_post
+ end
+ end
+
describe 'cooked' do
before do
post = Post.new(cooked: 'wat')