Class: UserSummary
- Inherits:
-
Object
- Object
- UserSummary
- Defined in:
- app/models/user_summary.rb
Overview
ViewModel used on Summary tab on User page
Defined Under Namespace
Classes: CategoryWithCounts, UserWithCount
Constant Summary collapse
- MAX_SUMMARY_RESULTS =
6
- MAX_BADGES =
6
- REPLY_ACTIONS =
[UserAction::RESPONSE, UserAction::QUOTE, UserAction::MENTION]
Instance Method Summary collapse
- #badges ⇒ Object
- #bookmark_count ⇒ Object
-
#initialize(user, guardian) ⇒ UserSummary
constructor
A new instance of UserSummary.
- #links ⇒ Object
- #most_liked_by_users ⇒ Object
- #most_liked_users ⇒ Object
- #most_replied_to_users ⇒ Object
- #recent_time_read ⇒ Object
- #replies ⇒ Object
- #top_categories ⇒ Object
- #topics ⇒ Object
- #user ⇒ Object
- #user_id ⇒ Object
- #user_stat ⇒ Object
Constructor Details
#initialize(user, guardian) ⇒ UserSummary
Returns a new instance of UserSummary.
11 12 13 14 |
# File 'app/models/user_summary.rb', line 11 def initialize(user, guardian) @user = user @guardian = guardian end |
Instance Method Details
#badges ⇒ Object
107 108 109 |
# File 'app/models/user_summary.rb', line 107 def badges @user.featured_user_badges(MAX_BADGES) end |
#bookmark_count ⇒ Object
123 124 125 |
# File 'app/models/user_summary.rb', line 123 def bookmark_count Bookmark.where(user: @user).count end |
#links ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/user_summary.rb', line 33 def links TopicLink .joins(:topic, :post) .where(posts: { user_id: @user.id }) .includes(:topic, :post) .where("posts.post_type IN (?)", Topic.visible_post_types(@guardian && @guardian.user)) .merge(Topic.listable_topics.visible.secured(@guardian)) .where(user: @user) .where(internal: false, reflection: false, quote: false) .order("clicks DESC, topic_links.created_at DESC") .limit(MAX_SUMMARY_RESULTS) end |
#most_liked_by_users ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/user_summary.rb', line 50 def most_liked_by_users likers = {} UserAction .joins(:target_topic, :target_post) .merge(Topic.listable_topics.visible.secured(@guardian)) .where(user: @user) .where(action_type: UserAction::WAS_LIKED) .group(:acting_user_id) .order("COUNT(*) DESC") .limit(MAX_SUMMARY_RESULTS) .pluck("acting_user_id, COUNT(*)") .each { |l| likers[l[0]] = l[1] } user_counts(likers) end |
#most_liked_users ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'app/models/user_summary.rb', line 66 def most_liked_users liked_users = {} UserAction .joins(:target_topic, :target_post) .merge(Topic.listable_topics.visible.secured(@guardian)) .where(action_type: UserAction::WAS_LIKED) .where(acting_user_id: @user.id) .group(:user_id) .order("COUNT(*) DESC") .limit(MAX_SUMMARY_RESULTS) .pluck("user_actions.user_id, COUNT(*)") .each { |l| liked_users[l[0]] = l[1] } user_counts(liked_users) end |
#most_replied_to_users ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/models/user_summary.rb', line 84 def most_replied_to_users replied_users = {} post_query .joins( "JOIN posts replies ON posts.topic_id = replies.topic_id AND posts.reply_to_post_number = replies.post_number", ) .joins( "JOIN topics ON replies.topic_id = topics.id AND topics.archetype <> 'private_message'", ) .joins( "AND replies.post_type IN (#{Topic.visible_post_types(@user, include_moderator_actions: false).join(",")})", ) .where("replies.user_id <> posts.user_id") .group("replies.user_id") .order("COUNT(*) DESC") .limit(MAX_SUMMARY_RESULTS) .pluck("replies.user_id, COUNT(*)") .each { |r| replied_users[r[0]] = r[1] } user_counts(replied_users) end |
#recent_time_read ⇒ Object
127 128 129 |
# File 'app/models/user_summary.rb', line 127 def recent_time_read @user.recent_time_read end |
#replies ⇒ Object
26 27 28 29 30 31 |
# File 'app/models/user_summary.rb', line 26 def replies post_query .where("post_number > 1") .order("posts.like_count DESC, posts.created_at DESC") .limit(MAX_SUMMARY_RESULTS) end |
#top_categories ⇒ Object
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 |
# File 'app/models/user_summary.rb', line 136 def top_categories post_count_query = post_query.group("topics.category_id") top_categories = {} Category .where( id: post_count_query.order("count(*) DESC").limit(MAX_SUMMARY_RESULTS).pluck("category_id"), ) .pluck(:id, :name, :color, :text_color, :slug, :read_restricted, :parent_category_id) .each do |c| top_categories[c[0].to_i] = CategoryWithCounts.new( Hash[CategoryWithCounts::KEYS.zip(c)].merge(topic_count: 0, post_count: 0), ) end post_count_query .where("post_number > 1") .where("topics.category_id in (?)", top_categories.keys) .pluck("category_id, COUNT(*)") .each { |r| top_categories[r[0].to_i].post_count = r[1] } Topic .listable_topics .visible .secured(@guardian) .where("topics.category_id in (?)", top_categories.keys) .where(user: @user) .group("topics.category_id") .pluck("category_id, COUNT(*)") .each { |r| top_categories[r[0].to_i].topic_count = r[1] } top_categories.values.sort_by { |r| -(r[:post_count] + r[:topic_count]) } end |
#topics ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'app/models/user_summary.rb', line 16 def topics Topic .secured(@guardian) .listable_topics .visible .where(user: @user) .order("like_count DESC, created_at DESC") .limit(MAX_SUMMARY_RESULTS) end |
#user ⇒ Object
115 116 117 |
# File 'app/models/user_summary.rb', line 115 def user @user end |
#user_id ⇒ Object
111 112 113 |
# File 'app/models/user_summary.rb', line 111 def user_id @user.id end |
#user_stat ⇒ Object
119 120 121 |
# File 'app/models/user_summary.rb', line 119 def user_stat @user.user_stat end |