You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

watcher.rb 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2019 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class Watcher < ActiveRecord::Base
  19. belongs_to :watchable, :polymorphic => true
  20. belongs_to :user
  21. validates_presence_of :user
  22. validates_uniqueness_of :user_id, :scope => [:watchable_type, :watchable_id]
  23. validate :validate_user
  24. # Returns true if at least one object among objects is watched by user
  25. def self.any_watched?(objects, user)
  26. objects = objects.reject(&:new_record?)
  27. if objects.any?
  28. objects.group_by {|object| object.class.base_class}.each do |base_class, objects|
  29. if Watcher.where(:watchable_type => base_class.name, :watchable_id => objects.map(&:id), :user_id => user.id).exists?
  30. return true
  31. end
  32. end
  33. end
  34. false
  35. end
  36. # Unwatch things that users are no longer allowed to view
  37. def self.prune(options={})
  38. if options.has_key?(:user)
  39. prune_single_user(options[:user], options)
  40. else
  41. pruned = 0
  42. User.where("id IN (SELECT DISTINCT user_id FROM #{table_name})").each do |user|
  43. pruned += prune_single_user(user, options)
  44. end
  45. pruned
  46. end
  47. end
  48. protected
  49. def validate_user
  50. errors.add :user_id, :invalid unless user.nil? || user.active?
  51. end
  52. private
  53. def self.prune_single_user(user, options={})
  54. return unless user.is_a?(User)
  55. pruned = 0
  56. where(:user_id => user.id).each do |watcher|
  57. next if watcher.watchable.nil?
  58. if options.has_key?(:project)
  59. unless watcher.watchable.respond_to?(:project) &&
  60. watcher.watchable.project == options[:project]
  61. next
  62. end
  63. end
  64. if watcher.watchable.respond_to?(:visible?)
  65. unless watcher.watchable.visible?(user)
  66. watcher.destroy
  67. pruned += 1
  68. end
  69. end
  70. end
  71. pruned
  72. end
  73. end