post_presenter.rb 2.77 KB
Newer Older
1
class PostPresenter < BasePresenter
2 3
  include PostsHelper

4
  attr_accessor :post
5

theworldbright's avatar
theworldbright committed
6
  def initialize(post, current_user=nil)
7 8
    @post = post
    @current_user = current_user
9 10
  end

theworldbright's avatar
theworldbright committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  def as_json(_options={})
    @post.as_json(only: directly_retrieved_attributes).merge(non_directly_retrieved_attributes)
  end

  private

  def directly_retrieved_attributes
    %i(id guid public created_at interacted_at provider_display_name image_url object_url)
  end

  def non_directly_retrieved_attributes
    {
      text:                         build_text,
      post_type:                    @post.post_type,
      nsfw:                         @post.nsfw,
      author:                       @post.author.as_api_response(:backbone),
      o_embed_cache:                @post.o_embed_cache.try(:as_api_response, :backbone),
      open_graph_cache:             build_open_graph_cache,
      mentioned_people:             build_mentioned_people_json,
      photos:                       build_photos_json,
      root:                         root,
      title:                        title,
33
      location:                     @post.post_location,
theworldbright's avatar
theworldbright committed
34 35 36 37 38 39 40 41 42
      poll:                         @post.poll,
      already_participated_in_poll: already_participated_in_poll,
      participation:                participate?,
      interactions:                 build_interactions_json
    }
  end

  def build_text
    if @post.message
43 44
      @post.message.plain_text_for_json
    else
Benjamin Neff's avatar
Benjamin Neff committed
45
      @post.text
46
    end
47
  end
48

theworldbright's avatar
theworldbright committed
49 50
  def build_open_graph_cache
    @post.open_graph_cache.try(:as_api_response, :backbone)
51
  end
52

theworldbright's avatar
theworldbright committed
53 54
  def build_mentioned_people_json
    @post.mentioned_people.as_api_response(:backbone)
55 56
  end

theworldbright's avatar
theworldbright committed
57 58
  def build_photos_json
    @post.photos.map {|p| p.as_api_response(:backbone) }
59 60
  end

theworldbright's avatar
theworldbright committed
61 62
  def title
    @post.message.present? ? @post.message.title : I18n.t("posts.presenter.title", name: @post.author_name)
63 64
  end

theworldbright's avatar
theworldbright committed
65 66 67 68 69
  def root
    if @post.respond_to?(:absolute_root) && @post.absolute_root.present?
      PostPresenter.new(@post.absolute_root, current_user).as_json
    end
  end
70

theworldbright's avatar
theworldbright committed
71 72 73 74 75 76 77 78
  def build_interactions_json
    {
      likes:          [user_like].compact,
      reshares:       [user_reshare].compact,
      comments_count: @post.comments_count,
      likes_count:    @post.likes_count,
      reshares_count: @post.reshares_count
    }
79 80
  end

theworldbright's avatar
theworldbright committed
81 82
  def user_like
    @post.like_for(current_user).try(:as_api_response, :backbone)
83
  end
84

theworldbright's avatar
theworldbright committed
85 86 87
  def user_reshare
    @post.reshare_for(current_user)
  end
88 89

  def already_participated_in_poll
90
    if @post.poll && user_signed_in?
91 92 93 94
      @post.poll.already_participated?(current_user)
    end
  end

95
  def participate?
theworldbright's avatar
theworldbright committed
96 97 98 99 100
    user_signed_in? && current_user.participations.where(target_id: @post).exists?
  end

  def user_signed_in?
    current_user.present?
101 102
  end

theworldbright's avatar
theworldbright committed
103 104 105
  def person
    current_user.person
  end
106
end