Welcome to mirror list, hosted at ThFree Co, Russian Federation.

person_presenter.rb « presenters « app - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ee0d495f572780cf4809b59b10bbc1c50d3f4343 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# frozen_string_literal: true

class PersonPresenter < BasePresenter
  def base_hash
    {
      id:          id,
      guid:        guid,
      name:        name,
      diaspora_id: diaspora_handle
    }
  end

  def as_api_json
    {
      guid:        guid,
      diaspora_id: diaspora_handle,
      name:        name,
      avatar:      AvatarPresenter.new(@presentable).medium
    }
  end

  def full_hash
    base_hash_with_contact.merge(
      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
    )
  end

  def profile_hash_as_api_json
    if own_profile?
      ProfilePresenter.new(profile).as_self_api_json.merge(guid: guid)
    else
      show_detailed = @presentable.public_details? || person_is_following_current_user
      ProfilePresenter.new(profile).as_other_api_json(show_detailed).merge(
        guid:         guid,
        blocked:      is_blocked?,
        relationship: relationship_detailed,
        aspects:      aspects_detailed
      )
    end
  end

  def as_json(_options={})
    full_hash_with_profile
  end

  def hovercard
    base_hash_with_contact.merge(profile: ProfilePresenter.new(profile).for_hovercard)
  end

  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

  def self.people_names(people)
    people.map(&:name).join(", ")
  end

  protected

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

  def relationship
    return false unless current_user

    contact = current_user_person_contact
    return :not_sharing unless contact

    %i(mutual sharing receiving).find do |status|
      contact.public_send("#{status}?")
    end || :not_sharing
  end

  def relationship_detailed
    status = {receiving: false, sharing: false}
    return status unless current_user

    contact = current_user_person_contact
    return status unless contact

    status.keys.each do |key|
      status[key] = contact.public_send("#{key}?")
    end
    status
  end

  def aspects_detailed
    return [] unless current_user_person_contact

    aspects_for_person = current_user.aspects_with_person(@presentable)
    aspects_for_person.map {|a| AspectPresenter.new(a).as_api_json(false, with_order: false) }
  end

  def person_is_following_current_user
    return false unless current_user
    contact = current_user_person_contact
    contact && contact.sharing?
  end

  def base_hash_with_contact
    base_hash.merge(
      contact: (!own_profile? && has_contact?) ? contact_hash : false
    )
  end

  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

  def contact_hash
    ContactPresenter.new(current_user_person_contact).full_hash
  end

  private

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

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

  def has_contact?
    current_user_person_contact.present?
  end

  def is_blocked?
    current_user_person_block.present?
  end

  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
end