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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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], :case_sensitive => false
  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. def self.prune_single_user(user, options={})
  53. return unless user.is_a?(User)
  54. pruned = 0
  55. where(:user_id => user.id).each do |watcher|
  56. next if watcher.watchable.nil?
  57. if options.has_key?(:project)
  58. unless watcher.watchable.respond_to?(:project) &&
  59. watcher.watchable.project == options[:project]
  60. next
  61. end
  62. end
  63. if watcher.watchable.respond_to?(:visible?)
  64. unless watcher.watchable.visible?(user)
  65. watcher.destroy
  66. pruned += 1
  67. end
  68. end
  69. end
  70. pruned
  71. end
  72. private_class_method :prune_single_user
  73. end