diff --git a/app/models/dtext_link.rb b/app/models/dtext_link.rb index 3d3f8d62b..6eff459f6 100644 --- a/app/models/dtext_link.rb +++ b/app/models/dtext_link.rb @@ -11,6 +11,12 @@ class DtextLink < ApplicationRecord scope :wiki_page, -> { where(model_type: "WikiPage") } scope :forum_post, -> { where(model_type: "ForumPost") } + def self.visible(user) + # XXX the double negation is to prevent postgres from choosing a bad query + # plan (it doesn't know that most forum posts aren't mod-only posts). + wiki_page.or(forum_post.where.not(model_id: ForumPost.not_visible(user))) + end + def self.model_types %w[WikiPage ForumPost] end diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index d195ad40b..73097a3af 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -40,6 +40,10 @@ class ForumPost < ApplicationRecord where(topic_id: ForumTopic.visible(user)) end + def not_visible(user) + where.not(topic_id: ForumTopic.visible(user)) + end + def wiki_link_matches(title) where(id: DtextLink.forum_post.wiki_link.where(link_target: WikiPage.normalize_title(title)).select(:model_id)) end