2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-04 01:03:01 +08:00
|
|
|
class Reviewable < ActiveRecord::Base
|
|
|
|
class Conversation
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
|
|
|
class Post
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
attr_reader :id, :user, :excerpt
|
|
|
|
|
|
|
|
def initialize(post)
|
|
|
|
@user = post.user
|
|
|
|
@id = post.id
|
2019-09-13 00:21:33 +08:00
|
|
|
@excerpt = self.class.excerpt(post.cooked)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.excerpt(cooked)
|
2019-10-19 00:31:15 +08:00
|
|
|
excerpt = ::Post.excerpt(cooked, 250, keep_emoji_images: true)
|
2019-09-13 00:21:33 +08:00
|
|
|
# remove the first link if it's the first node
|
|
|
|
fragment = Nokogiri::HTML.fragment(excerpt)
|
|
|
|
if fragment.children.first == fragment.css("a:first").first && fragment.children.first
|
|
|
|
fragment.children.first.remove
|
|
|
|
end
|
|
|
|
fragment.to_html.strip
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :id, :permalink, :has_more, :conversation_posts
|
|
|
|
|
|
|
|
def initialize(meta_topic)
|
|
|
|
@id = meta_topic.id
|
|
|
|
@has_more = false
|
2019-06-24 18:39:34 +08:00
|
|
|
@permalink = "#{Discourse.base_url_no_prefix}#{meta_topic.relative_url}"
|
2019-01-04 01:03:01 +08:00
|
|
|
@posts = []
|
|
|
|
|
|
|
|
meta_posts = meta_topic.ordered_posts.where(post_type: ::Post.types[:regular]).limit(2)
|
|
|
|
|
|
|
|
@conversation_posts = meta_posts.map { |mp| Reviewable::Conversation::Post.new(mp) }
|
|
|
|
@has_more = meta_topic.posts_count > 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|