diff --git a/app/models/user_action.rb b/app/models/user_action.rb index 6a240c86558..123857fceec 100644 --- a/app/models/user_action.rb +++ b/app/models/user_action.rb @@ -162,20 +162,15 @@ ORDER BY p.created_at desc end action.save! - action_type = hash[:action_type] user_id = hash[:user_id] - if action_type == LIKE - User.update_all('likes_given = likes_given + 1', id: user_id) - elsif action_type == WAS_LIKED - User.update_all('likes_received = likes_received + 1', id: user_id) - end + update_like_count(user_id, hash[:action_type], 1) topic = Topic.includes(:category).where(id: hash[:target_topic_id]).first # move into Topic perhaps group_ids = nil if topic && topic.category && topic.category.secure - group_ids = topic.category.groups.select("groups.id").map{|g| g.id} + group_ids = topic.category.groups.pluck("groups.id") end MessageBus.publish("/users/#{action.user.username.downcase}", @@ -197,17 +192,19 @@ ORDER BY p.created_at desc MessageBus.publish("/user/#{hash[:user_id]}", {user_action_id: action.id, remove: true}) end - action_type = hash[:action_type] - user_id = hash[:user_id] - if action_type == LIKE - User.update_all('likes_given = likes_given - 1', id: user_id) - elsif action_type == WAS_LIKED - User.update_all('likes_received = likes_received - 1', id: user_id) - end + update_like_count(hash[:user_id], hash[:action_type], -1) end protected + def self.update_like_count(user_id, action_type, delta) + if action_type == LIKE + User.update_all("likes_given = likes_given + #{delta.to_i}", id: user_id) + elsif action_type == WAS_LIKED + User.update_all("likes_received = likes_received + #{delta.to_i}", id: user_id) + end + end + def self.apply_common_filters(builder,user_id,guardian,ignore_private_messages=false) unless guardian.can_see_deleted_posts? diff --git a/lib/post_creator.rb b/lib/post_creator.rb index 7541a90b24e..1bd96235de0 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -252,31 +252,25 @@ class PostCreator def add_users(topic, usernames) return unless usernames - usernames = usernames.split(',') - User.where(username: usernames).each do |u| - - unless guardian.can_send_private_message?(u) - topic.errors.add(:archetype, :cant_send_pm) - @errors = topic.errors - raise ActiveRecord::Rollback.new - end - - topic.topic_allowed_users.build(user_id: u.id) + User.where(username: usernames.split(',')).each do |user| + check_can_send_permission!(topic,user) + topic.topic_allowed_users.build(user_id: user.id) end end def add_groups(topic, groups) return unless groups - groups = groups.split(',') - Group.where(name: groups).each do |g| + Group.where(name: groups.split(',')).each do |group| + check_can_send_permission!(topic,group) + topic.topic_allowed_groups.build(group_id: group.id) + end + end - unless guardian.can_send_private_message?(g) - topic.errors.add(:archetype, :cant_send_pm) - @errors = topic.errors - raise ActiveRecord::Rollback.new - end - - topic.topic_allowed_groups.build(group_id: g.id) + def check_can_send_permission!(topic,item) + unless guardian.can_send_private_message?(item) + topic.errors.add(:archetype, :cant_send_pm) + @errors = topic.errors + raise ActiveRecord::Rollback.new end end end