/app/serializers/user_serializer.rb

https://gitlab.com/mba811/discourse · Ruby · 257 lines · 199 code · 47 blank · 11 comment · 9 complexity · f2eef8760ba4aeb5d940d2b0ab8916ad MD5 · raw file

  1. class UserSerializer < BasicUserSerializer
  2. def self.staff_attributes(*attrs)
  3. attributes(*attrs)
  4. attrs.each do |attr|
  5. define_method "include_#{attr}?" do
  6. scope.is_staff?
  7. end
  8. end
  9. end
  10. def self.private_attributes(*attrs)
  11. attributes(*attrs)
  12. attrs.each do |attr|
  13. define_method "include_#{attr}?" do
  14. can_edit
  15. end
  16. end
  17. end
  18. attributes :name,
  19. :email,
  20. :last_posted_at,
  21. :last_seen_at,
  22. :bio_raw,
  23. :bio_cooked,
  24. :created_at,
  25. :website,
  26. :profile_background,
  27. :location,
  28. :can_edit,
  29. :can_edit_username,
  30. :can_edit_email,
  31. :can_edit_name,
  32. :stats,
  33. :can_send_private_message_to_user,
  34. :bio_excerpt,
  35. :trust_level,
  36. :moderator,
  37. :admin,
  38. :title,
  39. :suspend_reason,
  40. :suspended_till,
  41. :uploaded_avatar_id,
  42. :badge_count,
  43. :has_title_badges,
  44. :edit_history_public,
  45. :custom_fields
  46. has_one :invited_by, embed: :object, serializer: BasicUserSerializer
  47. has_many :custom_groups, embed: :object, serializer: BasicGroupSerializer
  48. has_many :featured_user_badges, embed: :ids, serializer: UserBadgeSerializer, root: :user_badges
  49. staff_attributes :number_of_deleted_posts,
  50. :number_of_flagged_posts,
  51. :number_of_flags_given,
  52. :number_of_suspensions
  53. private_attributes :email,
  54. :locale,
  55. :email_digests,
  56. :email_private_messages,
  57. :email_direct,
  58. :email_always,
  59. :digest_after_days,
  60. :mailing_list_mode,
  61. :auto_track_topics_after_msecs,
  62. :new_topic_duration_minutes,
  63. :external_links_in_new_tab,
  64. :dynamic_favicon,
  65. :enable_quoting,
  66. :muted_category_ids,
  67. :tracked_category_ids,
  68. :watched_category_ids,
  69. :private_messages_stats,
  70. :disable_jump_reply,
  71. :gravatar_avatar_upload_id,
  72. :custom_avatar_upload_id,
  73. :has_title_badges
  74. ###
  75. ### ATTRIBUTES
  76. ###
  77. def bio_raw
  78. object.user_profile.bio_raw
  79. end
  80. def include_bio_raw?
  81. bio_raw.present?
  82. end
  83. def bio_cooked
  84. object.user_profile.bio_processed
  85. end
  86. def website
  87. object.user_profile.website
  88. end
  89. def include_website?
  90. website.present?
  91. end
  92. def profile_background
  93. object.user_profile.profile_background
  94. end
  95. def include_profile_background?
  96. profile_background.present?
  97. end
  98. def location
  99. object.user_profile.location
  100. end
  101. def include_location?
  102. location.present?
  103. end
  104. def can_edit
  105. scope.can_edit?(object)
  106. end
  107. def can_edit_username
  108. scope.can_edit_username?(object)
  109. end
  110. def can_edit_email
  111. scope.can_edit_email?(object)
  112. end
  113. def can_edit_name
  114. scope.can_edit_name?(object)
  115. end
  116. def stats
  117. UserAction.stats(object.id, scope)
  118. end
  119. def can_send_private_message_to_user
  120. scope.can_send_private_message?(object)
  121. end
  122. def bio_excerpt
  123. # If they have a bio return it
  124. excerpt = object.user_profile.bio_excerpt
  125. return excerpt if excerpt.present?
  126. # Without a bio, determine what message to show
  127. if scope.user && scope.user.id == object.id
  128. I18n.t('user_profile.no_info_me', username_lower: object.username_lower)
  129. else
  130. I18n.t('user_profile.no_info_other', name: object.name)
  131. end
  132. end
  133. def include_suspend_reason?
  134. object.suspended?
  135. end
  136. def include_suspended_till?
  137. object.suspended?
  138. end
  139. ###
  140. ### STAFF ATTRIBUTES
  141. ###
  142. def number_of_deleted_posts
  143. Post.with_deleted
  144. .where(user_id: object.id)
  145. .where(user_deleted: false)
  146. .where.not(deleted_by_id: object.id)
  147. .count
  148. end
  149. def number_of_flagged_posts
  150. Post.with_deleted
  151. .where(user_id: object.id)
  152. .where(id: PostAction.with_deleted
  153. .where(post_action_type_id: PostActionType.notify_flag_type_ids)
  154. .select(:post_id))
  155. .count
  156. end
  157. def number_of_flags_given
  158. PostAction.with_deleted
  159. .where(user_id: object.id)
  160. .where(post_action_type_id: PostActionType.notify_flag_type_ids)
  161. .count
  162. end
  163. def number_of_suspensions
  164. UserHistory.for(object, :suspend_user).count
  165. end
  166. ###
  167. ### PRIVATE ATTRIBUTES
  168. ###
  169. def auto_track_topics_after_msecs
  170. object.auto_track_topics_after_msecs || SiteSetting.auto_track_topics_after
  171. end
  172. def new_topic_duration_minutes
  173. object.new_topic_duration_minutes || SiteSetting.new_topic_duration_minutes
  174. end
  175. def muted_category_ids
  176. CategoryUser.lookup(object, :muted).pluck(:category_id)
  177. end
  178. def tracked_category_ids
  179. CategoryUser.lookup(object, :tracking).pluck(:category_id)
  180. end
  181. def watched_category_ids
  182. CategoryUser.lookup(object, :watching).pluck(:category_id)
  183. end
  184. def private_messages_stats
  185. UserAction.private_messages_stats(object.id, scope)
  186. end
  187. def gravatar_avatar_upload_id
  188. object.user_avatar.try(:gravatar_upload_id)
  189. end
  190. def custom_avatar_upload_id
  191. object.user_avatar.try(:custom_upload_id)
  192. end
  193. def has_title_badges
  194. object.badges.where(allow_title: true).count > 0
  195. end
  196. def include_edit_history_public?
  197. can_edit && !SiteSetting.edit_history_visible_to_public
  198. end
  199. def custom_fields
  200. fields = nil
  201. if SiteSetting.public_user_custom_fields.present?
  202. fields = SiteSetting.public_user_custom_fields.split('|')
  203. end
  204. if fields.present?
  205. User.custom_fields_for_ids([object.id], fields)[object.id]
  206. else
  207. {}
  208. end
  209. end
  210. end