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.

watchers_controller.rb 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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 WatchersController < ApplicationController
  19. before_action :require_login, :find_watchables, :only => [:watch, :unwatch]
  20. def watch
  21. set_watcher(@watchables, User.current, true)
  22. end
  23. def unwatch
  24. set_watcher(@watchables, User.current, false)
  25. end
  26. before_action :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user]
  27. accept_api_auth :create, :destroy
  28. def new
  29. @users = users_for_new_watcher
  30. end
  31. def create
  32. user_ids = []
  33. if params[:watcher]
  34. user_ids << (params[:watcher][:user_ids] || params[:watcher][:user_id])
  35. else
  36. user_ids << params[:user_id]
  37. end
  38. user_ids = user_ids.flatten.compact.uniq
  39. users = Principal.assignable_watchers.where(:id => user_ids).to_a
  40. users.each do |user|
  41. @watchables.each do |watchable|
  42. Watcher.create(:watchable => watchable, :user => user)
  43. end
  44. end
  45. respond_to do |format|
  46. format.html do
  47. redirect_to_referer_or do
  48. render(:html => 'Watcher added.', :status => 200, :layout => true)
  49. end
  50. end
  51. format.js {@users = users_for_new_watcher}
  52. format.api {render_api_ok}
  53. end
  54. end
  55. def append
  56. if params[:watcher]
  57. user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
  58. @users = Principal.assignable_watchers.where(:id => user_ids).to_a
  59. end
  60. if @users.blank?
  61. head 200
  62. end
  63. end
  64. def destroy
  65. user = Principal.find(params[:user_id])
  66. @watchables.each do |watchable|
  67. watchable.set_watcher(user, false)
  68. end
  69. respond_to do |format|
  70. format.html do
  71. redirect_to_referer_or do
  72. render(:html => 'Watcher removed.', :status => 200, :layout => true)
  73. end
  74. end
  75. format.js
  76. format.api {render_api_ok}
  77. end
  78. rescue ActiveRecord::RecordNotFound
  79. render_404
  80. end
  81. def autocomplete_for_user
  82. @users = users_for_new_watcher
  83. render :layout => false
  84. end
  85. private
  86. def find_project
  87. if params[:object_type] && params[:object_id]
  88. @watchables = find_objects_from_params
  89. @projects = @watchables.map(&:project).uniq
  90. if @projects.size == 1
  91. @project = @projects.first
  92. end
  93. elsif params[:project_id]
  94. @project = Project.visible.find_by_param(params[:project_id])
  95. end
  96. end
  97. def find_watchables
  98. @watchables = find_objects_from_params
  99. unless @watchables.present?
  100. render_404
  101. end
  102. end
  103. def set_watcher(watchables, user, watching)
  104. watchables.each do |watchable|
  105. watchable.set_watcher(user, watching)
  106. end
  107. respond_to do |format|
  108. format.html do
  109. text = watching ? 'Watcher added.' : 'Watcher removed.'
  110. redirect_to_referer_or do
  111. render(:html => text, :status => 200, :layout => true)
  112. end
  113. end
  114. format.js do
  115. render(:partial => 'set_watcher',
  116. :locals => {:user => user, :watched => watchables})
  117. end
  118. end
  119. end
  120. def users_for_new_watcher
  121. scope = nil
  122. if params[:q].blank? && @project.present?
  123. scope = @project.principals.assignable_watchers
  124. else
  125. scope = Principal.assignable_watchers.limit(100)
  126. end
  127. users = scope.sorted.like(params[:q]).to_a
  128. if @watchables && @watchables.size == 1
  129. watchable_object = @watchables.first
  130. users -= watchable_object.watcher_users
  131. if watchable_object.respond_to?(:visible?)
  132. users.reject! {|user| user.is_a?(User) && !watchable_object.visible?(user)}
  133. end
  134. end
  135. users
  136. end
  137. def find_objects_from_params
  138. klass =
  139. begin
  140. Object.const_get(params[:object_type].camelcase)
  141. rescue
  142. nil
  143. end
  144. return unless klass && klass.respond_to?('watched_by')
  145. scope = klass.where(:id => Array.wrap(params[:object_id]))
  146. if klass.reflect_on_association(:project)
  147. scope = scope.preload(:project => :enabled_modules)
  148. end
  149. objects = scope.to_a
  150. raise Unauthorized if objects.any? do |w|
  151. if w.respond_to?(:visible?)
  152. !w.visible?
  153. elsif w.respond_to?(:project) && w.project
  154. !w.project.visible?
  155. end
  156. end
  157. objects
  158. end
  159. end