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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class WatchersController < ApplicationController
  18. before_action :require_login, :find_watchables, :only => [:watch, :unwatch]
  19. def watch
  20. set_watcher(@watchables, User.current, true)
  21. end
  22. def unwatch
  23. set_watcher(@watchables, User.current, false)
  24. end
  25. before_action :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user]
  26. accept_api_auth :create, :destroy
  27. def new
  28. @users = users_for_new_watcher
  29. end
  30. def create
  31. user_ids = []
  32. if params[:watcher]
  33. user_ids << (params[:watcher][:user_ids] || params[:watcher][:user_id])
  34. else
  35. user_ids << params[:user_id]
  36. end
  37. users = User.active.visible.where(:id => user_ids.flatten.compact.uniq)
  38. users.each do |user|
  39. @watchables.each do |watchable|
  40. Watcher.create(:watchable => watchable, :user => user)
  41. end
  42. end
  43. respond_to do |format|
  44. format.html { redirect_to_referer_or {render :html => 'Watcher added.', :status => 200, :layout => true}}
  45. format.js { @users = users_for_new_watcher }
  46. format.api { render_api_ok }
  47. end
  48. end
  49. def append
  50. if params[:watcher]
  51. user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
  52. @users = User.active.visible.where(:id => user_ids).to_a
  53. end
  54. if @users.blank?
  55. head 200
  56. end
  57. end
  58. def destroy
  59. user = User.find(params[:user_id])
  60. @watchables.each do |watchable|
  61. watchable.set_watcher(user, false)
  62. end
  63. respond_to do |format|
  64. format.html { redirect_to_referer_or {render :html => 'Watcher removed.', :status => 200, :layout => true} }
  65. format.js
  66. format.api { render_api_ok }
  67. end
  68. rescue ActiveRecord::RecordNotFound
  69. render_404
  70. end
  71. def autocomplete_for_user
  72. @users = users_for_new_watcher
  73. render :layout => false
  74. end
  75. private
  76. def find_project
  77. if params[:object_type] && params[:object_id]
  78. @watchables = find_objets_from_params
  79. @projects = @watchables.map(&:project).uniq
  80. if @projects.size == 1
  81. @project = @projects.first
  82. end
  83. elsif params[:project_id]
  84. @project = Project.visible.find_by_param(params[:project_id])
  85. end
  86. end
  87. def find_watchables
  88. @watchables = find_objets_from_params
  89. unless @watchables.present?
  90. render_404
  91. end
  92. end
  93. def set_watcher(watchables, user, watching)
  94. watchables.each do |watchable|
  95. watchable.set_watcher(user, watching)
  96. end
  97. respond_to do |format|
  98. format.html {
  99. text = watching ? 'Watcher added.' : 'Watcher removed.'
  100. redirect_to_referer_or {render :html => text, :status => 200, :layout => true}
  101. }
  102. format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} }
  103. end
  104. end
  105. def users_for_new_watcher
  106. scope = nil
  107. if params[:q].blank? && @project.present?
  108. scope = @project.users
  109. else
  110. scope = User.all.limit(100)
  111. end
  112. users = scope.active.visible.sorted.like(params[:q]).to_a
  113. if @watchables && @watchables.size == 1
  114. users -= @watchables.first.watcher_users
  115. end
  116. users
  117. end
  118. def find_objets_from_params
  119. klass = Object.const_get(params[:object_type].camelcase) rescue nil
  120. return unless klass && klass.respond_to?('watched_by')
  121. scope = klass.where(:id => Array.wrap(params[:object_id]))
  122. if klass.reflect_on_association(:project)
  123. scope = scope.preload(:project => :enabled_modules)
  124. end
  125. objects = scope.to_a
  126. raise Unauthorized if objects.any? do |w|
  127. if w.respond_to?(:visible?)
  128. !w.visible?
  129. elsif w.respond_to?(:project) && w.project
  130. !w.project.visible?
  131. end
  132. end
  133. objects
  134. end
  135. end