2011-04-05 16:09:15 +04:00
|
|
|
# Redmine - project management software
|
2014-01-30 02:45:39 +04:00
|
|
|
# Copyright (C) 2006-2014 Jean-Philippe Lang
|
2007-04-21 16:08:31 +04:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
2011-08-30 18:09:58 +04:00
|
|
|
#
|
2007-04-21 16:08:31 +04:00
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
2011-08-30 18:09:58 +04:00
|
|
|
#
|
2007-04-21 16:08:31 +04:00
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
class WatchersController < ApplicationController
|
2013-02-10 14:31:12 +04:00
|
|
|
before_filter :require_login, :find_watchables, :only => [:watch, :unwatch]
|
2011-08-30 18:09:58 +04:00
|
|
|
|
2008-08-03 13:14:43 +04:00
|
|
|
def watch
|
2013-02-10 14:31:12 +04:00
|
|
|
set_watcher(@watchables, User.current, true)
|
2008-08-03 13:14:43 +04:00
|
|
|
end
|
2011-08-30 18:09:58 +04:00
|
|
|
|
2008-08-03 13:14:43 +04:00
|
|
|
def unwatch
|
2013-02-10 14:31:12 +04:00
|
|
|
set_watcher(@watchables, User.current, false)
|
2007-04-21 16:08:31 +04:00
|
|
|
end
|
2011-08-30 18:09:58 +04:00
|
|
|
|
2013-02-10 14:31:12 +04:00
|
|
|
before_filter :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user]
|
|
|
|
accept_api_auth :create, :destroy
|
|
|
|
|
2008-08-03 13:14:43 +04:00
|
|
|
def new
|
2014-01-19 14:01:14 +04:00
|
|
|
@users = users_for_new_watcher
|
2012-01-09 22:37:16 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2013-01-31 00:51:21 +04:00
|
|
|
user_ids = []
|
|
|
|
if params[:watcher].is_a?(Hash)
|
|
|
|
user_ids << (params[:watcher][:user_ids] || params[:watcher][:user_id])
|
|
|
|
else
|
|
|
|
user_ids << params[:user_id]
|
|
|
|
end
|
|
|
|
user_ids.flatten.compact.uniq.each do |user_id|
|
|
|
|
Watcher.create(:watchable => @watched, :user_id => user_id)
|
2012-01-09 22:37:16 +04:00
|
|
|
end
|
2007-05-13 23:43:35 +04:00
|
|
|
respond_to do |format|
|
2012-04-06 20:51:10 +04:00
|
|
|
format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}}
|
2014-01-19 14:01:14 +04:00
|
|
|
format.js { @users = users_for_new_watcher }
|
2013-01-31 00:51:21 +04:00
|
|
|
format.api { render_api_ok }
|
2007-05-13 23:43:35 +04:00
|
|
|
end
|
2007-04-21 16:08:31 +04:00
|
|
|
end
|
2011-08-30 18:09:58 +04:00
|
|
|
|
2012-03-24 16:57:28 +04:00
|
|
|
def append
|
|
|
|
if params[:watcher].is_a?(Hash)
|
|
|
|
user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
|
2014-01-10 16:49:52 +04:00
|
|
|
@users = User.active.where(:id => user_ids).all
|
2012-03-24 16:57:28 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-25 15:11:53 +03:00
|
|
|
def destroy
|
2013-01-31 00:51:21 +04:00
|
|
|
@watched.set_watcher(User.find(params[:user_id]), false)
|
2009-10-25 15:11:53 +03:00
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to :back }
|
2012-07-19 21:17:10 +04:00
|
|
|
format.js
|
2013-01-31 00:51:21 +04:00
|
|
|
format.api { render_api_ok }
|
2009-10-25 15:11:53 +03:00
|
|
|
end
|
|
|
|
end
|
2011-08-30 18:09:58 +04:00
|
|
|
|
2012-01-09 22:37:16 +04:00
|
|
|
def autocomplete_for_user
|
2014-01-19 14:01:14 +04:00
|
|
|
@users = users_for_new_watcher
|
2012-01-09 22:37:16 +04:00
|
|
|
render :layout => false
|
|
|
|
end
|
|
|
|
|
2013-02-10 14:31:12 +04:00
|
|
|
private
|
|
|
|
|
2007-04-21 16:08:31 +04:00
|
|
|
def find_project
|
2012-03-24 16:57:28 +04:00
|
|
|
if params[:object_type] && params[:object_id]
|
|
|
|
klass = Object.const_get(params[:object_type].camelcase)
|
|
|
|
return false unless klass.respond_to?('watched_by')
|
|
|
|
@watched = klass.find(params[:object_id])
|
|
|
|
@project = @watched.project
|
|
|
|
elsif params[:project_id]
|
2012-04-25 21:17:49 +04:00
|
|
|
@project = Project.visible.find_by_param(params[:project_id])
|
2012-03-24 16:57:28 +04:00
|
|
|
end
|
2007-05-13 23:43:35 +04:00
|
|
|
rescue
|
|
|
|
render_404
|
2007-04-21 16:08:31 +04:00
|
|
|
end
|
2011-08-30 18:09:58 +04:00
|
|
|
|
2013-02-10 14:31:12 +04:00
|
|
|
def find_watchables
|
|
|
|
klass = Object.const_get(params[:object_type].camelcase) rescue nil
|
|
|
|
if klass && klass.respond_to?('watched_by')
|
2014-01-10 16:49:52 +04:00
|
|
|
@watchables = klass.where(:id => Array.wrap(params[:object_id])).all
|
2013-02-10 14:31:12 +04:00
|
|
|
raise Unauthorized if @watchables.any? {|w| w.respond_to?(:visible?) && !w.visible?}
|
|
|
|
end
|
|
|
|
render_404 unless @watchables.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_watcher(watchables, user, watching)
|
|
|
|
watchables.each do |watchable|
|
|
|
|
watchable.set_watcher(user, watching)
|
|
|
|
end
|
2008-08-03 13:14:43 +04:00
|
|
|
respond_to do |format|
|
2012-04-06 20:51:10 +04:00
|
|
|
format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
|
2013-02-10 14:31:12 +04:00
|
|
|
format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} }
|
2008-08-03 13:14:43 +04:00
|
|
|
end
|
|
|
|
end
|
2014-01-19 14:01:14 +04:00
|
|
|
|
|
|
|
def users_for_new_watcher
|
|
|
|
users = []
|
|
|
|
if params[:q].blank? && @project.present?
|
|
|
|
users = @project.users.sorted
|
|
|
|
else
|
|
|
|
users = User.active.sorted.like(params[:q]).limit(100)
|
|
|
|
end
|
|
|
|
if @watched
|
|
|
|
users -= @watched.watcher_users
|
|
|
|
end
|
|
|
|
users
|
|
|
|
end
|
2007-04-21 16:08:31 +04:00
|
|
|
end
|