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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 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. users = User.active.visible.where(:id => user_ids.flatten.compact.uniq)
  39. users.each do |user|
  40. @watchables.each do |watchable|
  41. Watcher.create(:watchable => watchable, :user => user)
  42. end
  43. end
  44. respond_to do |format|
  45. format.html { redirect_to_referer_or {render :html => 'Watcher added.', :status => 200, :layout => true}}
  46. format.js { @users = users_for_new_watcher }
  47. format.api { render_api_ok }
  48. end
  49. end
  50. def append
  51. if params[:watcher]
  52. user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
  53. @users = User.active.visible.where(:id => user_ids).to_a
  54. end
  55. if @users.blank?
  56. head 200
  57. end
  58. end
  59. def destroy
  60. user = User.find(params[:user_id])
  61. @watchables.each do |watchable|
  62. watchable.set_watcher(user, false)
  63. end
  64. respond_to do |format|
  65. format.html { redirect_to_referer_or {render :html => 'Watcher removed.', :status => 200, :layout => true} }
  66. format.js
  67. format.api { render_api_ok }
  68. end
  69. rescue ActiveRecord::RecordNotFound
  70. render_404
  71. end
  72. def autocomplete_for_user
  73. @users = users_for_new_watcher
  74. render :layout => false
  75. end
  76. private
  77. def find_project
  78. if params[:object_type] && params[:object_id]
  79. @watchables = find_objets_from_params
  80. @projects = @watchables.map(&:project).uniq
  81. if @projects.size == 1
  82. @project = @projects.first
  83. end
  84. elsif params[:project_id]
  85. @project = Project.visible.find_by_param(params[:project_id])
  86. end
  87. end
  88. def find_watchables
  89. @watchables = find_objets_from_params
  90. unless @watchables.present?
  91. render_404
  92. end
  93. end
  94. def set_watcher(watchables, user, watching)
  95. watchables.each do |watchable|
  96. watchable.set_watcher(user, watching)
  97. end
  98. respond_to do |format|
  99. format.html {
  100. text = watching ? 'Watcher added.' : 'Watcher removed.'
  101. redirect_to_referer_or {render :html => text, :status => 200, :layout => true}
  102. }
  103. format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} }
  104. end
  105. end
  106. def users_for_new_watcher
  107. scope = nil
  108. if params[:q].blank? && @project.present?
  109. scope = @project.users
  110. else
  111. scope = User.all.limit(100)
  112. end
  113. users = scope.active.visible.sorted.like(params[:q]).to_a
  114. if @watchables && @watchables.size == 1
  115. users -= @watchables.first.watcher_users
  116. end
  117. users
  118. end
  119. def find_objets_from_params
  120. klass = Object.const_get(params[:object_type].camelcase) rescue nil
  121. return unless klass && klass.respond_to?('watched_by')
  122. scope = klass.where(:id => Array.wrap(params[:object_id]))
  123. if klass.reflect_on_association(:project)
  124. scope = scope.preload(:project => :enabled_modules)
  125. end
  126. objects = scope.to_a
  127. raise Unauthorized if objects.any? do |w|
  128. if w.respond_to?(:visible?)
  129. !w.visible?
  130. elsif w.respond_to?(:project) && w.project
  131. !w.project.visible?
  132. end
  133. end
  134. objects
  135. end
  136. end