Skip to content

A platform for community discussion. Free, open, simple.

5.6 KB
Raw Blame
# frozen_string_literal: true

require "digest"

module MarkdownEndpoint
  class TopicRenderer
    # Edits create new cache keys, so bound how long obsolete entries linger.
    # Recent posts are more likely to be fetched again, so cache them for longer.
    CACHE_TTL = 1.hour
    RECENT_CACHE_TTL = 1.day
    RECENT_POST_WINDOW = 1.month

    def initialize(topic_view, guardian:, post_number: nil, query: {})
      @topic_view = topic_view
      @topic = topic_view.topic
      @guardian = guardian
      @post_number = post_number&.to_i
      @query = query
      @timestamp = Timestamp.new(guardian.user)
    end

    def render
      posts = visible_posts
      raise Discourse::NotFound if single_post? && posts.empty?

      buffer = String.new(header(posts.length))
      unless single_post?
        DiscoursePluginRegistry
          .apply_modifier(:markdown_topic_header_sections, [], @topic_view, @guardian)
          .each { |section| buffer << "\n\n" << section }
      end
      posts.each_with_index do |post, index|
        buffer << (index.zero? ? "\n\n" : "\n\n---\n\n")
        buffer << render_post(post)
      end
      buffer << "\n\n---\n\n_[View the full topic](#{@topic.url})._" if single_post?
      unless single_post?
        {
          "previous_page" => @topic_view.prev_page,
          "next_page" => @topic_view.next_page,
        }.each do |label, page|
          next unless page

          query = @query.merge("page" => page).to_query
          buffer << "\n\n[#{I18n.t("markdown_endpoints.#{label}")}](#{@topic.url}.md?#{query})"
        end
      end
      buffer << "\n"
    end

    private

    def visible_posts
      posts = @topic_view.posts.to_a
      posts = posts.select { |post| post.post_number == @post_number } if single_post?
      posts =
        posts.reject { |post| post.post_type == Post.types[:whisper] } unless can_see_whispers?
      posts
    end

    def single_post?
      @post_number.present?
    end

    def can_see_whispers?
      user = @guardian.user
      allowed_group_ids = SiteSetting.whispers_allowed_groups_map
      user && allowed_group_ids.present? && user.group_users.exists?(group_id: allowed_group_ids)
    end

    def header(visible_post_count)
      title = ContentLocalization.translated_topic_title(@topic, @guardian) || @topic.title
      lines = ["**URL:** <#{@topic.url}>"]
      lines << "**Category:** #{escape_text(@topic.category.name)}" if @topic.category
      tags = @topic_view.visible_tags.map(&:name)
      lines << "**Tags:** #{tags.map { |tag| escape_text(tag) }.join(", ")}" if tags.present?
      lines << "**Created:** #{@timestamp.render(@topic.created_at, url: @topic.url)}"
      lines << "**Posts on this page:** #{visible_post_count}"
      lines << "**Page:** #{@topic_view.page}" unless single_post?
      lines << "**Showing post:** #{@post_number}" if single_post?
      "# #{escape_text(EmojiConverter.convert(title))}\n\n#{lines.join("\\\n")}"
    end

    def render_post(post)
      serializer = BasicPostSerializer.new(post, scope: @guardian, root: false)
      serializer.topic_view = @topic_view
      cooked = serializer.cooked.to_s
      lines = ['<div class="post-metadata">', ""]
      metadata = []
      if post.user
        avatar_url = UrlHelper.absolute(post.user.avatar_template.gsub("{size}", "32"))
        avatar_url = "#{Discourse.base_protocol}:#{avatar_url}" if avatar_url.start_with?("//")
        avatar_url = URI::DEFAULT_PARSER.escape(avatar_url, /[^\x21-\x7E]|[<>"()\\]/)
        author = escape_text(post.user.username)
        author_link = "![#{author}](#{avatar_url}) [@#{author}](#{post.user.full_url})"
        metadata << I18n.t("markdown_endpoints.post_author", author: author_link)
      end
      metadata << I18n.t(
        "markdown_endpoints.post_date",
        timestamp: @timestamp.render(post.created_at, url: post.full_url),
      )
      lines << metadata.join("\\\n")
      lines.concat(["", "</div>", ""])
      if (description = action_description(post)).present?
        lines.concat([description, ""])
      end
      lines << cached_body(cooked, post)
      lines.join("\n")
    end

    def action_description(post)
      return if post.action_code.blank?

      serializer = PostSerializer.new(post, scope: @guardian, root: false)
      serializer.topic_view = @topic_view
      who = serializer.action_code_who
      description =
        I18n.t(
          "js.action_codes.#{serializer.action_code}",
          who: ERB::Util.html_escape(who.present? ? "@#{who}" : ""),
          when:
            ERB::Util.html_escape(
              I18n.t("js.dates.wrap_on", date: @timestamp.text(post.created_at)),
            ),
          path: ERB::Util.html_escape(serializer.action_code_path || @topic.relative_url),
          default: "",
        )

      escape_text(Nokogiri::HTML5.fragment(description).text.strip)
    end

    def cached_body(cooked, post)
      post_url = post.full_url
      context = [
        CookedProcessor::VERSION,
        Discourse.base_url,
        Discourse.asset_host,
        I18n.locale,
        post_url,
        cooked,
      ].join("\0")
      digest = Digest::SHA256.hexdigest(context)
      Discourse
        .cache
        .fetch("markdown-endpoint:cooked:#{digest}", expires_in: cache_ttl(post)) do
          CookedProcessor.to_markdown(cooked, post_url:)
        end
    end

    def cache_ttl(post)
      post.created_at >= RECENT_POST_WINDOW.ago ? RECENT_CACHE_TTL : CACHE_TTL
    end

    def escape_text(text)
      text
        .to_s
        .gsub(/[\\`*_\[\]<>]/) { |character| "\\#{character}" }
        .sub(/\A[#>-]/) { |character| "\\#{character}" }
    end
  end
end