person_presenter.rb 3.17 KB
Newer Older
1 2
class PersonPresenter < BasePresenter
  def base_hash
3 4 5 6
    {
      id:          id,
      guid:        guid,
      name:        name,
7 8 9 10 11
      diaspora_id: diaspora_handle
    }
  end

  def full_hash
12
    base_hash_with_contact.merge(
13 14 15 16
      relationship:      relationship,
      block:             is_blocked? ? BlockPresenter.new(current_user_person_block).base_hash : false,
      is_own_profile:    own_profile?,
      show_profile_info: public_details? || own_profile? || person_is_following_current_user
17
    )
18 19
  end

20
  def as_json(_options={})
21
    full_hash_with_profile
22 23
  end

24 25 26 27
  def hovercard
    base_hash_with_contact.merge(profile: ProfilePresenter.new(profile).for_hovercard)
  end

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  def metas_attributes
    {
      keywords:             {name:     "keywords",    content: comma_separated_tags},
      description:          {name:     "description", content: description},
      og_title:             {property: "og:title",    content: title},
      og_description:       {property: "og:title",    content: description},
      og_url:               {property: "og:url",      content: url},
      og_image:             {property: "og:image",    content: image_url},
      og_type:              {property: "og:type",     content: "profile"},
      og_profile_username:  {property: "og:profile:username",   content: name},
      og_profile_firstname: {property: "og:profile:first_name", content: first_name},
      og_profile_lastname:  {property: "og:profile:last_name",  content: last_name}
    }
  end

43 44 45 46
  protected

  def own_profile?
    current_user.try(:person) == @presentable
47 48
  end

49
  def relationship
50
    return false unless current_user
51

52 53
    contact = current_user_person_contact
    return :not_sharing unless contact
54

55
    %i(mutual sharing receiving).find do |status|
56 57
      contact.public_send("#{status}?")
    end || :not_sharing
58
  end
59 60

  def person_is_following_current_user
61 62 63
    return false unless current_user
    contact = current_user_person_contact
    contact && contact.sharing?
64 65
  end

66 67 68 69 70 71
  def base_hash_with_contact
    base_hash.merge(
      contact: (!own_profile? && has_contact?) ? contact_hash : false
    )
  end

72 73 74 75 76 77 78 79 80 81 82 83
  def full_hash_with_profile
    attrs = full_hash

    if attrs[:show_profile_info]
      attrs.merge!(profile: ProfilePresenter.new(profile).private_hash)
    else
      attrs.merge!(profile: ProfilePresenter.new(profile).public_hash)
    end

    attrs
  end

84 85 86 87
  def contact_hash
    ContactPresenter.new(current_user_person_contact).full_hash
  end

88 89 90
  private

  def current_user_person_block
91
    @block ||= (current_user ? current_user.block_for(@presentable) : Block.none)
92 93
  end

94
  def current_user_person_contact
95
    @contact ||= (current_user ? current_user.contact_for(@presentable) : Contact.none)
96 97
  end

98 99 100 101
  def has_contact?
    current_user_person_contact.present?
  end

102 103
  def is_blocked?
    current_user_person_block.present?
104
  end
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

  def title
    name
  end

  def comma_separated_tags
    profile.tags.map(&:name).join(", ") if profile.tags
  end

  def url
    url_for(@presentable)
  end

  def description
    public_details? ? bio : ""
  end

  def image_url
    return AppConfig.url_to @presentable.image_url if @presentable.image_url[0] == "/"
    @presentable.image_url
  end
126
end