Class: UserCardSerializer

Inherits:
BasicUserSerializer show all
Defined in:
app/serializers/user_card_serializer.rb

Direct Known Subclasses

UserSerializer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicUserSerializer

#avatar_template, #categories_with_notification_level, #category_user_notification_levels, #include_name?, #name, #user, #user_is_current_user

Methods inherited from ApplicationSerializer

expire_cache_fragment!, fragment_cache

Methods inherited from ActiveModel::Serializer

#include!

Instance Attribute Details

#topic_post_countObject

Returns the value of attribute topic_post_count.



4
5
6
# File 'app/serializers/user_card_serializer.rb', line 4

def topic_post_count
  @topic_post_count
end

Class Method Details

.private_attributes(*attrs) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/serializers/user_card_serializer.rb', line 15

def self.private_attributes(*attrs)
  attributes(*attrs)
  attrs.each do |attr|
    define_method "include_#{attr}?" do
      if defined?(super)
        super() && can_edit
      else
        can_edit
      end
    end
  end
end

.staff_attributes(*attrs) ⇒ Object



6
7
8
9
10
11
12
13
# File 'app/serializers/user_card_serializer.rb', line 6

def self.staff_attributes(*attrs)
  attributes(*attrs)
  attrs.each do |attr|
    define_method "include_#{attr}?" do
      scope.is_staff?
    end
  end
end

.untrusted_attributes(*attrs) ⇒ Object

attributes that are hidden for TL0 users when seen by anonymous



29
30
31
32
33
34
35
36
37
38
# File 'app/serializers/user_card_serializer.rb', line 29

def self.untrusted_attributes(*attrs)
  attributes(*attrs)
  attrs.each do |attr|
    method_name = "include_#{attr}?"
    define_method(method_name) do
      return false if scope.restrict_user_fields?(object)
      public_send(attr).present?
    end
  end
end

Instance Method Details

#bio_excerptObject



96
97
98
# File 'app/serializers/user_card_serializer.rb', line 96

def bio_excerpt
  object..bio_excerpt(350, keep_newlines: true, keep_emoji_images: true)
end

#can_ignore_userObject



133
134
135
# File 'app/serializers/user_card_serializer.rb', line 133

def can_ignore_user
  scope.can_ignore_user?(object)
end

#can_mute_userObject



129
130
131
# File 'app/serializers/user_card_serializer.rb', line 129

def can_mute_user
  scope.can_mute_user?(object)
end

#can_send_private_message_to_userObject



143
144
145
# File 'app/serializers/user_card_serializer.rb', line 143

def can_send_private_message_to_user
  scope.can_send_private_message?(object) && scope.current_user != object
end

#can_send_private_messagesObject

Needed because ‘send_private_message_to_user’ will always return false when the current user is being serialized



139
140
141
# File 'app/serializers/user_card_serializer.rb', line 139

def can_send_private_messages
  scope.can_send_private_messages?
end

#card_background_upload_urlObject



222
223
224
# File 'app/serializers/user_card_serializer.rb', line 222

def card_background_upload_url
  object.card_background_upload&.url
end

#custom_fieldsObject



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/serializers/user_card_serializer.rb', line 164

def custom_fields
  fields = custom_field_keys

  if fields.present?
    if object.custom_fields_preloaded?
      {}.tap { |h| fields.each { |f| h[f] = object.custom_fields[f] } }
    else
      User.custom_fields_for_ids([object.id], fields)[object.id] || {}
    end
  else
    {}
  end
end


210
211
212
# File 'app/serializers/user_card_serializer.rb', line 210

def featured_topic
  object..featured_topic
end

#flair_bg_colorObject



202
203
204
# File 'app/serializers/user_card_serializer.rb', line 202

def flair_bg_color
  object.flair_group&.flair_bg_color
end

#flair_colorObject



206
207
208
# File 'app/serializers/user_card_serializer.rb', line 206

def flair_color
  object.flair_group&.flair_color
end

#flair_nameObject



194
195
196
# File 'app/serializers/user_card_serializer.rb', line 194

def flair_name
  object.flair_group&.name
end

#flair_urlObject



198
199
200
# File 'app/serializers/user_card_serializer.rb', line 198

def flair_url
  object.flair_group&.flair_url
end

#ignoredObject



119
120
121
122
# File 'app/serializers/user_card_serializer.rb', line 119

def ignored
  scope_ignored_user_ids = scope.user&.ignored_user_ids || []
  scope_ignored_user_ids.include?(object.id)
end

#include_email?Boolean Also known as: include_secondary_emails?, include_unconfirmed_emails?

Returns:

  • (Boolean)


89
90
91
# File 'app/serializers/user_card_serializer.rb', line 89

def include_email?
  (object.id && object.id == scope.user.try(:id)) || (scope.is_staff? && object.staged?)
end

#include_pending_posts_count?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'app/serializers/user_card_serializer.rb', line 85

def include_pending_posts_count?
  scope.is_me?(object) || scope.is_staff?
end

#include_status?Boolean

Returns:

  • (Boolean)


226
227
228
# File 'app/serializers/user_card_serializer.rb', line 226

def include_status?
  SiteSetting.enable_user_status && user.has_status?
end

#include_suspend_reason?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'app/serializers/user_card_serializer.rb', line 147

def include_suspend_reason?
  scope.can_see_suspension_reason?(object) && object.suspended?
end

#include_suspended_till?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'app/serializers/user_card_serializer.rb', line 151

def include_suspended_till?
  object.suspended?
end

#include_timezone?Boolean

Returns:

  • (Boolean)


214
215
216
# File 'app/serializers/user_card_serializer.rb', line 214

def include_timezone?
  SiteSetting.display_local_time_in_user_card?
end

#include_topic_post_count?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'app/serializers/user_card_serializer.rb', line 178

def include_topic_post_count?
  topic_post_count.present?
end

#include_user_fields?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'app/serializers/user_card_serializer.rb', line 160

def include_user_fields?
  user_fields.present?
end

#locationObject



100
101
102
# File 'app/serializers/user_card_serializer.rb', line 100

def location
  object..location
end

#mutedObject



124
125
126
127
# File 'app/serializers/user_card_serializer.rb', line 124

def muted
  scope_muted_user_ids = scope.user&.muted_user_ids || []
  scope_muted_user_ids.include?(object.id)
end

#primary_group_nameObject



190
191
192
# File 'app/serializers/user_card_serializer.rb', line 190

def primary_group_name
  object.primary_group&.name
end

#recent_time_readObject



186
187
188
# File 'app/serializers/user_card_serializer.rb', line 186

def recent_time_read
  time = object.recent_time_read
end

#statusObject



230
231
232
# File 'app/serializers/user_card_serializer.rb', line 230

def status
  UserStatusSerializer.new(user.user_status, root: false)
end

#time_readObject



182
183
184
# File 'app/serializers/user_card_serializer.rb', line 182

def time_read
  object.user_stat&.time_read
end

#timezoneObject



218
219
220
# File 'app/serializers/user_card_serializer.rb', line 218

def timezone
  object.user_option.timezone
end

#user_fieldsObject



155
156
157
158
# File 'app/serializers/user_card_serializer.rb', line 155

def user_fields
  allowed_keys = scope.allowed_user_field_ids(object)
  object.user_fields(allowed_keys)
end

#websiteObject



104
105
106
# File 'app/serializers/user_card_serializer.rb', line 104

def website
  object..website
end

#website_nameObject



108
109
110
111
112
113
114
115
116
117
# File 'app/serializers/user_card_serializer.rb', line 108

def website_name
  uri =
    begin
      URI(website.to_s)
    rescue URI::Error
    end

  return if uri.nil? || uri.host.nil?
  uri.host.sub(/\Awww\./, "") + uri.path
end