diff --git a/app/models/dtext_link.rb b/app/models/dtext_link.rb index d091d33e1..f77a844c6 100644 --- a/app/models/dtext_link.rb +++ b/app/models/dtext_link.rb @@ -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 diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index e0aa4ce87..5c37ac0e7 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -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 diff --git a/app/models/wiki_page.rb b/app/models/wiki_page.rb index e508b52b4..50abccd02 100644 --- a/app/models/wiki_page.rb +++ b/app/models/wiki_page.rb @@ -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? diff --git a/app/views/dtext_links/index.html.erb b/app/views/dtext_links/index.html.erb index 812888ebf..029371b40 100644 --- a/app/views/dtext_links/index.html.erb +++ b/app/views/dtext_links/index.html.erb @@ -1,15 +1,8 @@