Class: CategoryUser
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- CategoryUser
- Defined in:
- app/models/category_user.rb
Class Method Summary collapse
- .auto_track(opts = {}) ⇒ Object
- .auto_watch(opts = {}) ⇒ Object
- .batch_set(user, level, category_ids) ⇒ Object
- .create_lookup(category_users) ⇒ Object
- .default_notification_level ⇒ Object
- .ensure_consistency! ⇒ Object
- .indirectly_muted_category_ids(user) ⇒ Object
- .lookup(user, level) ⇒ Object
- .lookup_for(user, category_ids) ⇒ Object
- .muted_category_ids(user) ⇒ Object
- .muted_category_ids_query(user, include_direct: false) ⇒ Object
- .notification_levels ⇒ Object
- .notification_levels_for(user) ⇒ Object
- .set_notification_level_for_category(user, level, category_id) ⇒ Object
- .watching_levels ⇒ Object
Class Method Details
.auto_track(opts = {}) ⇒ Object
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 |
# File 'app/models/category_user.rb', line 87 def self.auto_track(opts = {}) builder = DB.build <<~SQL UPDATE topic_users tu SET notification_level = :tracking, notifications_reason_id = :auto_track_category FROM topics t, category_users cu /*where*/ SQL builder.where( "tu.topic_id = t.id AND cu.category_id = t.category_id AND cu.user_id = tu.user_id AND cu.notification_level = :tracking AND tu.notification_level = :regular", ) if category_id = opts[:category_id] builder.where("t.category_id = :category_id", category_id: category_id) end if topic_id = opts[:topic_id] builder.where("tu.topic_id = :topic_id", topic_id: topic_id) end if user_id = opts[:user_id] builder.where("tu.user_id = :user_id", user_id: user_id) end builder.exec( tracking: notification_levels[:tracking], regular: notification_levels[:regular], auto_track_category: TopicUser.notification_reasons[:auto_track_category], ) end |
.auto_watch(opts = {}) ⇒ Object
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 176 177 178 179 180 |
# File 'app/models/category_user.rb', line 123 def self.auto_watch(opts = {}) builder = DB.build <<~SQL UPDATE topic_users tu SET notification_level = CASE WHEN should_track THEN :tracking WHEN should_watch THEN :watching ELSE notification_level END, notifications_reason_id = CASE WHEN should_track THEN null WHEN should_watch THEN :auto_watch_category ELSE notifications_reason_id END FROM ( SELECT tu1.topic_id, tu1.user_id, CASE WHEN cu.user_id IS NULL AND tu1.notification_level = :watching AND tu1.notifications_reason_id = :auto_watch_category THEN true ELSE false END should_track, CASE WHEN cu.user_id IS NOT NULL AND tu1.notification_level in (:regular, :tracking) THEN true ELSE false END should_watch FROM topic_users tu1 JOIN topics t ON t.id = tu1.topic_id LEFT JOIN category_users cu ON cu.category_id = t.category_id AND cu.user_id = tu1.user_id AND cu.notification_level = :watching /*where2*/ ) as X /*where*/ SQL builder.where("X.topic_id = tu.topic_id AND X.user_id = tu.user_id") builder.where("should_watch OR should_track") if category_id = opts[:category_id] builder.where2("t.category_id = :category_id", category_id: category_id) end if topic_id = opts[:topic_id] builder.where("tu.topic_id = :topic_id", topic_id: topic_id) builder.where2("tu1.topic_id = :topic_id", topic_id: topic_id) end if user_id = opts[:user_id] builder.where("tu.user_id = :user_id", user_id: user_id) builder.where2("tu1.user_id = :user_id", user_id: user_id) end builder.exec( watching: notification_levels[:watching], tracking: notification_levels[:tracking], regular: notification_levels[:regular], auto_watch_category: TopicUser.notification_reasons[:auto_watch_category], ) end |
.batch_set(user, level, category_ids) ⇒ Object
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 |
# File 'app/models/category_user.rb', line 19 def self.batch_set(user, level, category_ids) level_num = notification_levels[level] category_ids = Category.where(id: category_ids).pluck(:id) changed = false # Update pre-existing category users if category_ids.present? && CategoryUser .where(user_id: user.id, category_id: category_ids) .where.not(notification_level: level_num) .update_all(notification_level: level_num) > 0 changed = true end # Remove extraneous category users if CategoryUser .where(user_id: user.id, notification_level: level_num) .where.not(category_id: category_ids) .delete_all > 0 changed = true end if category_ids.present? params = { user_id: user.id, level_num: level_num } sql = <<~SQL INSERT INTO category_users (user_id, category_id, notification_level) SELECT :user_id, :category_id, :level_num ON CONFLICT DO NOTHING SQL # we could use VALUES here but it would introduce a string # into the query, plus it is a bit of a micro optimisation category_ids.each do |category_id| params[:category_id] = category_id changed = true if DB.exec(sql, params) > 0 end end if changed auto_watch(user_id: user.id) auto_track(user_id: user.id) end changed end |
.create_lookup(category_users) ⇒ Object
230 231 232 233 234 |
# File 'app/models/category_user.rb', line 230 def self.create_lookup(category_users) category_users.each_with_object({}) do |category_user, acc| acc[category_user.category_id] = category_user end end |
.default_notification_level ⇒ Object
193 194 195 196 197 198 199 |
# File 'app/models/category_user.rb', line 193 def self.default_notification_level if SiteSetting.mute_all_categories_by_default notification_levels[:muted] else notification_levels[:regular] end end |
.ensure_consistency! ⇒ Object
182 183 184 185 186 187 188 189 190 191 |
# File 'app/models/category_user.rb', line 182 def self.ensure_consistency! DB.exec <<~SQL DELETE FROM category_users WHERE user_id IN ( SELECT cu.user_id FROM category_users cu LEFT JOIN users u ON u.id = cu.user_id WHERE u.id IS NULL ) SQL end |
.indirectly_muted_category_ids(user) ⇒ Object
275 276 277 |
# File 'app/models/category_user.rb', line 275 def self.indirectly_muted_category_ids(user) muted_category_ids_query(user).pluck("categories.id") end |
.lookup(user, level) ⇒ Object
7 8 9 |
# File 'app/models/category_user.rb', line 7 def self.lookup(user, level) self.where(user: user, notification_level: notification_levels[level]) end |
.lookup_for(user, category_ids) ⇒ Object
225 226 227 228 |
# File 'app/models/category_user.rb', line 225 def self.lookup_for(user, category_ids) return {} if user.blank? || category_ids.blank? create_lookup(CategoryUser.where(category_id: category_ids, user_id: user.id)) end |
.muted_category_ids(user) ⇒ Object
271 272 273 |
# File 'app/models/category_user.rb', line 271 def self.muted_category_ids(user) muted_category_ids_query(user, include_direct: true).pluck("categories.id") end |
.muted_category_ids_query(user, include_direct: false) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'app/models/category_user.rb', line 236 def self.muted_category_ids_query(user, include_direct: false) query = Category query = query.where.not(parent_category_id: nil) if !include_direct query = query .joins("LEFT JOIN categories categories2 ON categories2.id = categories.parent_category_id") .joins( "LEFT JOIN category_users ON category_users.category_id = categories.id AND category_users.user_id = #{user.id}", ) .joins( "LEFT JOIN category_users category_users2 ON category_users2.category_id = categories2.id AND category_users2.user_id = #{user.id}", ) direct_category_muted_sql = "COALESCE(category_users.notification_level, #{CategoryUser.default_notification_level}) = #{CategoryUser.notification_levels[:muted]}" parent_category_muted_sql = "(category_users.id IS NULL AND COALESCE(category_users2.notification_level, #{CategoryUser.default_notification_level}) = #{notification_levels[:muted]})" conditions = [parent_category_muted_sql] conditions.push(direct_category_muted_sql) if include_direct if SiteSetting.max_category_nesting === 3 query = query.joins( "LEFT JOIN categories categories3 ON categories3.id = categories2.parent_category_id", ).joins( "LEFT JOIN category_users category_users3 ON category_users3.category_id = categories3.id AND category_users3.user_id = #{user.id}", ) grandparent_category_muted_sql = "(category_users.id IS NULL AND category_users2.id IS NULL AND COALESCE(category_users3.notification_level, #{CategoryUser.default_notification_level}) = #{notification_levels[:muted]})" conditions.push(grandparent_category_muted_sql) end query.where(conditions.join(" OR ")) end |
.notification_levels ⇒ Object
11 12 13 |
# File 'app/models/category_user.rb', line 11 def self.notification_levels NotificationLevels.all end |
.notification_levels_for(user) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'app/models/category_user.rb', line 201 def self.notification_levels_for(user) # Anonymous users have all default categories set to regular tracking, # except for default muted categories which stay muted. if user.blank? notification_levels = [ SiteSetting.default_categories_watching.split("|"), SiteSetting.default_categories_tracking.split("|"), SiteSetting.default_categories_watching_first_post.split("|"), SiteSetting.default_categories_normal.split("|"), ].flatten.map { |id| [id.to_i, self.notification_levels[:regular]] } notification_levels += SiteSetting .default_categories_muted .split("|") .map { |id| [id.to_i, self.notification_levels[:muted]] } else notification_levels = CategoryUser.where(user: user).pluck(:category_id, :notification_level) end Hash[*notification_levels.flatten] end |
.set_notification_level_for_category(user, level, category_id) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'app/models/category_user.rb', line 67 def self.set_notification_level_for_category(user, level, category_id) record = CategoryUser.where(user: user, category_id: category_id).first return if record && record.notification_level == level if record.present? record.notification_level = level record.save! else begin CategoryUser.create!(user: user, category_id: category_id, notification_level: level) rescue ActiveRecord::RecordNotUnique # does not matter end end auto_watch(user_id: user.id) auto_track(user_id: user.id) end |
.watching_levels ⇒ Object
15 16 17 |
# File 'app/models/category_user.rb', line 15 def self.watching_levels [notification_levels[:watching], notification_levels[:watching_first_post]] end |