dtext links: allow searching for forum posts linking to specific tag.

This commit is contained in:
evazion
2019-10-27 01:05:54 -05:00
parent d946a84480
commit d617b20b49
7 changed files with 55 additions and 20 deletions

View File

@@ -3,7 +3,10 @@ class DtextLink < ApplicationRecord
enum link_type: [:wiki_link, :external_link]
before_validation :normalize_link_target
validates :link_target, uniqueness: { scope: [:model_type, :model_id] }
# validates :link_target, uniqueness: { scope: [:model_type, :model_id] }
scope :wiki_page, -> { where(model_type: "WikiPage") }
scope :forum_post, -> { where(model_type: "ForumPost") }
def self.new_from_dtext(dtext)
links = []
@@ -21,7 +24,7 @@ class DtextLink < ApplicationRecord
def self.model_matches(params)
return all if params.blank?
where(model_id: WikiPage.search(params).reorder(nil))
where(model_type: "WikiPage", model_id: WikiPage.search(params).reorder(nil))
end
def self.linked_wiki_exists(exists = true)
@@ -63,5 +66,9 @@ class DtextLink < ApplicationRecord
if wiki_link?
self.link_target = WikiPage.normalize_title(link_target)
end
# postgres will raise an error if the link is more than 2712 bytes long
# because it can't index values that take up more than 1/3 of an 8kb page.
self.link_target = self.link_target.truncate(2048, omission: "")
end
end

View File

@@ -5,11 +5,14 @@ class ForumPost < ApplicationRecord
belongs_to_creator
belongs_to_updater
belongs_to :topic, :class_name => "ForumTopic"
has_many :dtext_links, as: :model, dependent: :destroy
has_many :votes, class_name: "ForumPostVote"
has_one :tag_alias
has_one :tag_implication
has_one :bulk_update_request
before_validation :initialize_is_deleted, :on => :create
before_save :update_dtext_links, if: :dtext_links_changed?
after_create :update_topic_updated_at_on_create
after_update :update_topic_updated_at_on_update_for_original_posts
after_destroy :update_topic_updated_at_on_destroy
@@ -50,6 +53,10 @@ class ForumPost < ApplicationRecord
q = q.search_attributes(params, :creator, :updater, :topic_id, :is_deleted, :body)
q = q.text_attribute_matches(:body, params[:body_matches], index_column: :text_index)
if params[:linked_to].present?
q = q.where(id: DtextLink.forum_post.wiki_link.where(link_target: params[:linked_to]).select(:model_id))
end
if params[:topic_title_matches].present?
q = q.topic_title_matches(params[:topic_title_matches])
end
@@ -141,6 +148,14 @@ 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

View File

@@ -77,7 +77,7 @@ class WikiPage < ApplicationRecord
end
if params[:linked_to].present?
q = q.where(id: DtextLink.wiki_link.where(link_target: params[:linked_to]).select(:model_id))
q = q.where(id: DtextLink.wiki_page.wiki_link.where(link_target: params[:linked_to]).select(:model_id))
end
if params[:hide_deleted].to_s.truthy?