diff --git a/app/logical/concerns/has_dtext_links.rb b/app/logical/concerns/has_dtext_links.rb new file mode 100644 index 000000000..4573717c3 --- /dev/null +++ b/app/logical/concerns/has_dtext_links.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module HasDtextLinks + extend ActiveSupport::Concern + + class_methods do + # Declare a field that has DText links. Any wiki links or external links + # contained in this field will be saved in the dtext_links table whenever + # the field is updated. This allows finding wiki pages, forum posts, and + # pool descriptions linking to a given tag. + # + # @param attribute [Symbol] the name of the DText field. + def has_dtext_links(attribute) + has_many :dtext_links, as: :model, dependent: :destroy + before_save :update_dtext_links, if: :dtext_links_changed? + + define_method(:dtext_links_changed?) do + attribute_changed?(attribute) && DText.dtext_links_differ?(self[attribute], attribute_was(attribute)) + end + + define_method(:update_dtext_links) do + self.dtext_links = DtextLink.new_from_dtext(self[attribute]) + end + end + + # Return pages (e.g. wikis, forum posts, pool descriptions) that link to the given wiki page. + def linked_to(title) + where(dtext_links: DtextLink.where(model_type: name).wiki_link.where(link_target: WikiPage.normalize_title(title))) + end + + # Return pages that don't link to the given wiki page. + def not_linked_to(title) + where.not(dtext_links: DtextLink.where(model_type: name).wiki_link.where(link_target: WikiPage.normalize_title(title))) + end + end +end diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 1c8b94d6d..97b653f6a 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -7,6 +7,7 @@ class ApplicationRecord < ActiveRecord::Base include Mentionable include Normalizable include ArrayAttribute + include HasDtextLinks extend HasBitFlags extend Searchable diff --git a/app/models/dtext_link.rb b/app/models/dtext_link.rb index 87af5dcf7..d353291d5 100644 --- a/app/models/dtext_link.rb +++ b/app/models/dtext_link.rb @@ -12,15 +12,16 @@ class DtextLink < ApplicationRecord scope :wiki_page, -> { where(model_type: "WikiPage") } scope :forum_post, -> { where(model_type: "ForumPost") } + scope :pool, -> { where(model_type: "Pool") } 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))) + wiki_page.or(forum_post.where.not(model_id: ForumPost.not_visible(user))).or(pool) end def self.model_types - %w[WikiPage ForumPost] + %w[WikiPage ForumPost Pool] end def self.new_from_dtext(dtext) diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 50ca429ad..d9b4ce309 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -7,7 +7,6 @@ class ForumPost < ApplicationRecord belongs_to_updater belongs_to :topic, class_name: "ForumTopic", inverse_of: :forum_posts - has_many :dtext_links, as: :model, dependent: :destroy has_many :moderation_reports, as: :model has_many :votes, class_name: "ForumPostVote" has_one :tag_alias @@ -16,7 +15,6 @@ class ForumPost < ApplicationRecord validates :body, presence: true, length: { maximum: 200_000 }, if: :body_changed? - before_save :update_dtext_links, if: :dtext_links_changed? before_create :autoreport_spam after_create :update_topic_updated_at_on_create after_update :update_topic_updated_at_on_update_for_original_posts @@ -31,6 +29,7 @@ class ForumPost < ApplicationRecord after_create_commit :async_send_discord_notification deletable + has_dtext_links :body mentionable( message_field: :body, title: ->(_user_name) {%{#{creator.name} mentioned you in topic ##{topic_id} (#{topic.title})}}, @@ -112,14 +111,6 @@ class ForumPost < ApplicationRecord update_topic_updated_at_on_undelete end - def dtext_links_changed? - body_changed? && DText.dtext_links_differ?(body, body_was) - end - - def update_dtext_links - self.dtext_links = DtextLink.new_from_dtext(body) - end - def update_topic_updated_at_on_delete max = ForumPost.where(:topic_id => topic.id, :is_deleted => false).order("updated_at desc").first if max diff --git a/app/models/pool.rb b/app/models/pool.rb index dc5e61d94..cee883fe9 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -15,6 +15,7 @@ class Pool < ApplicationRecord after_save :create_version deletable + has_dtext_links :description scope :series, -> { where(category: "series") } scope :collection, -> { where(category: "collection") } @@ -37,7 +38,7 @@ class Pool < ApplicationRecord end def search(params) - q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :name, :description, :post_ids) + q = search_attributes(params, :id, :created_at, :updated_at, :is_deleted, :name, :description, :post_ids, :dtext_links) q = q.text_attribute_matches(:description, params[:description_matches]) if params[:post_tags_match] @@ -48,6 +49,14 @@ class Pool < ApplicationRecord q = q.name_matches(params[:name_matches]) end + if params[:linked_to].present? + q = q.linked_to(params[:linked_to]) + end + + if params[:not_linked_to].present? + q = q.not_linked_to(params[:not_linked_to]) + end + case params[:category] when "series" q = q.series diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index d8e7fe620..bd77d3416 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -5,7 +5,6 @@ class WikiPage < ApplicationRecord META_WIKIS = ["list_of_", "tag_group:", "pool_group:", "howto:", "about:", "help:", "template:"] - before_save :update_dtext_links, if: :dtext_links_changed? after_save :create_version normalize :title, :normalize_title @@ -22,9 +21,9 @@ class WikiPage < ApplicationRecord has_one :tag, :foreign_key => "name", :primary_key => "title" has_one :artist, -> { active }, foreign_key: "name", primary_key: "title" has_many :versions, -> {order("wiki_page_versions.id ASC")}, :class_name => "WikiPageVersion", :dependent => :destroy - has_many :dtext_links, as: :model, dependent: :destroy deletable + has_dtext_links :body module SearchMethods def find_by_id_or_title(id) @@ -58,14 +57,6 @@ class WikiPage < ApplicationRecord end end - def linked_to(title) - where(dtext_links: DtextLink.wiki_page.wiki_link.where(link_target: normalize_title(title))) - end - - def not_linked_to(title) - where.not(dtext_links: DtextLink.wiki_page.wiki_link.where(link_target: normalize_title(title))) - end - def default_order order(updated_at: :desc) end @@ -213,14 +204,6 @@ class WikiPage < ApplicationRecord end end - def dtext_links_changed? - body_changed? && DText.dtext_links_differ?(body, body_was) - end - - def update_dtext_links - self.dtext_links = DtextLink.new_from_dtext(body) - end - def tags titles = DText.parse_wiki_titles(body).uniq tags = Tag.nonempty.where(name: titles).pluck(:name) diff --git a/app/views/dtext_links/index.html.erb b/app/views/dtext_links/index.html.erb index 8ec40e974..266d8820a 100644 --- a/app/views/dtext_links/index.html.erb +++ b/app/views/dtext_links/index.html.erb @@ -2,7 +2,7 @@