class Comment < ApplicationRecord has_ancestry resourcify include Reactable include Searchable SEARCH_SERIALIZER = Search::CommentSerializer SEARCH_CLASS = Search::FeedContent BODY_MARKDOWN_SIZE_RANGE = (1..25_000).freeze COMMENTABLE_TYPES = %w[Article PodcastEpisode].freeze TITLE_DELETED = "[deleted]".freeze TITLE_HIDDEN = "[hidden by post author]".freeze belongs_to :commentable, polymorphic: true, optional: true counter_culture :commentable belongs_to :user counter_culture :user has_many :mentions, as: :mentionable, inverse_of: :mentionable, dependent: :destroy has_many :notifications, as: :notifiable, inverse_of: :notifiable, dependent: :delete_all has_many :notification_subscriptions, as: :notifiable, inverse_of: :notifiable, dependent: :destroy before_validation :evaluate_markdown, if: -> { body_markdown } before_save :set_markdown_character_count, if: :body_markdown before_create :adjust_comment_parent_based_on_depth after_create :after_create_checks after_create :notify_slack_channel_about_warned_users after_update :update_descendant_notifications, if: :deleted after_update :remove_notifications, if: :deleted before_destroy :before_destroy_actions after_destroy :after_destroy_actions after_save :synchronous_bust after_save :bust_cache validate :permissions, if: :commentable validates :body_markdown, presence: true, length: { in: BODY_MARKDOWN_SIZE_RANGE } validates :body_markdown, uniqueness: { scope: %i[user_id ancestry commentable_id commentable_type] } validates :commentable_id, presence: true validates :commentable_type, inclusion: { in: COMMENTABLE_TYPES } validates :user_id, presence: true after_create_commit :record_field_test_event after_create_commit :send_email_notification, if: :should_send_email_notification? after_create_commit :create_first_reaction after_create_commit :send_to_moderator after_commit :calculate_score, on: %i[create update] after_commit :index_to_elasticsearch, on: %i[create update] after_update_commit :update_notifications, if: proc { |comment| comment.saved_changes.include? "body_markdown" } after_commit :remove_from_elasticsearch, on: [:destroy] scope :eager_load_serialized_data, -> { includes(:user, :commentable) } alias touch_by_reaction save def self.tree_for(commentable, limit = 0) commentable.comments.includes(:user).arrange(order: "score DESC").to_a[0..limit - 1].to_h end def search_id "comment_#{id}" end def path "/#{user.username}/comment/#{id_code_generated}" rescue StandardError "/404.html" end def parent_or_root_article parent || commentable end def parent_user parent_or_root_article.user end def parent_type parent_or_root_article.class.name.downcase .gsub("article", "post") .gsub("podcastepisode", "episode") end def id_code_generated # 26 is the conversion base # eg. 1000.to_s(26) would be "1cc" id.to_s(26) end def custom_css MarkdownParser.new(body_markdown).tags_used.map do |tag| Rails.application.assets["ltags/#{tag}.css"].to_s end.join end def title(length = 80) return TITLE_DELETED if deleted return TITLE_HIDDEN if hidden_by_commentable_user text = ActionController::Base.helpers.strip_tags(processed_html).strip truncated_text = ActionController::Base.helpers.truncate(text, length: length).gsub("'", "'").gsub("&", "&") HTMLEntities.new.decode(truncated_text) end def video nil end def readable_publish_date if created_at.year == Time.current.year created_at.strftime("%b %e") else created_at.strftime("%b %e '%y") end end def remove_notifications Notification.remove_all_without_delay(notifiable_ids: id, notifiable_type: "Comment") end def safe_processed_html processed_html.html_safe # rubocop:disable Rails/OutputSafety end def root_exists? ancestry && Comment.exists?(id: ancestry) end private def update_notifications Notification.update_notifications(self) end def update_descendant_notifications return unless has_children? Comment.where(id: descendant_ids).find_each do |comment| Notification.update_notifications(comment) end end def send_to_moderator return if user && user.comments_count > 2 Notification.send_moderation_notification(self) end def evaluate_markdown fixed_body_markdown = MarkdownFixer.fix_for_comment(body_markdown) parsed_markdown = MarkdownParser.new(fixed_body_markdown, source: self, user: user) self.processed_html = parsed_markdown.finalize(link_attributes: { rel: "nofollow" }) wrap_timestamps_if_video_present! if commentable shorten_urls! end def adjust_comment_parent_based_on_depth self.parent_id = parent.descendant_ids.last if parent_exists? && (parent.depth > 1 && parent.has_children?) end def wrap_timestamps_if_video_present! return unless commentable_type != "PodcastEpisode" && commentable.video.present? self.processed_html = processed_html.gsub(/(([0-9]:)?)(([0-5][0-9]|[0-9])?):[0-5][0-9]/) do |string| "#{string}" end end def shorten_urls! doc = Nokogiri::HTML.fragment(processed_html) doc.css("a").each do |anchor| unless anchor.to_s.include?("