GitLab steht Mittwoch, den 23. September, zwischen 10:00 und 12:00 Uhr aufgrund von Wartungsarbeiten nicht zur Verfügung.

watchers_controller.rb 4.31 KB
Newer Older
1
# Redmine - project management software
jplang's avatar
jplang committed
2
# Copyright (C) 2006-2016  Jean-Philippe Lang
3 4 5 6 7
#
# 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.
8
#
9 10 11 12
# 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.
13
#
14 15 16 17 18
# 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
19
  before_action :require_login, :find_watchables, :only => [:watch, :unwatch]
20

21
  def watch
22
    set_watcher(@watchables, User.current, true)
23
  end
24

25
  def unwatch
26
    set_watcher(@watchables, User.current, false)
27
  end
28

29
  before_action :find_project, :authorize, :only => [:new, :create, :append, :destroy, :autocomplete_for_user]
30 31
  accept_api_auth :create, :destroy

32
  def new
33
    @users = users_for_new_watcher
34 35 36
  end

  def create
37 38 39 40 41 42
    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
43 44
    users = User.active.visible.where(:id => user_ids.flatten.compact.uniq)
    users.each do |user|
45 46 47
      @watchables.each do |watchable|
        Watcher.create(:watchable => watchable, :user => user)
      end
48
    end
49
    respond_to do |format|
jplang's avatar
jplang committed
50
      format.html { redirect_to_referer_or {render :text => 'Watcher added.', :layout => true}}
51
      format.js { @users = users_for_new_watcher }
52
      format.api { render_api_ok }
53
    end
54
  end
55

56 57 58
  def append
    if params[:watcher].is_a?(Hash)
      user_ids = params[:watcher][:user_ids] || [params[:watcher][:user_id]]
59
      @users = User.active.visible.where(:id => user_ids).to_a
60
    end
61 62 63
    if @users.blank?
      render :nothing => true
    end
64 65
  end

66
  def destroy
67 68 69 70
    user = User.find(params[:user_id])
    @watchables.each do |watchable|
      watchable.set_watcher(user, false)
    end
71 72
    respond_to do |format|
      format.html { redirect_to :back }
73
      format.js
74
      format.api { render_api_ok }
75
    end
76 77
  rescue ActiveRecord::RecordNotFound
    render_404
78
  end
79

80
  def autocomplete_for_user
81
    @users = users_for_new_watcher
82 83 84
    render :layout => false
  end

85 86
  private

87
  def find_project
88
    if params[:object_type] && params[:object_id]
89 90 91 92 93
      @watchables = find_objets_from_params
      @projects = @watchables.map(&:project).uniq
      if @projects.size == 1
        @project = @projects.first
      end
94
    elsif params[:project_id]
jplang's avatar
jplang committed
95
      @project = Project.visible.find_by_param(params[:project_id])
96
    end
97
  end
98

99
  def find_watchables
100 101 102
    @watchables = find_objets_from_params
    unless @watchables.present?
      render_404
103 104 105 106 107 108 109
    end
  end

  def set_watcher(watchables, user, watching)
    watchables.each do |watchable|
      watchable.set_watcher(user, watching)
    end
110
    respond_to do |format|
jplang's avatar
jplang committed
111
      format.html { redirect_to_referer_or {render :text => (watching ? 'Watcher added.' : 'Watcher removed.'), :layout => true}}
112
      format.js { render :partial => 'set_watcher', :locals => {:user => user, :watched => watchables} }
113 114
    end
  end
115 116

  def users_for_new_watcher
117
    scope = nil
118
    if params[:q].blank? && @project.present?
119
      scope = @project.users
120
    else
121
      scope = User.all.limit(100)
122
    end
123
    users = scope.active.visible.sorted.like(params[:q]).to_a
124 125
    if @watchables && @watchables.size == 1
      users -= @watchables.first.watcher_users
126 127 128
    end
    users
  end
129 130 131 132 133

  def find_objets_from_params
    klass = Object.const_get(params[:object_type].camelcase) rescue nil
    return unless klass && klass.respond_to?('watched_by')

134 135 136 137 138 139
    scope = klass.where(:id => Array.wrap(params[:object_id]))
    if klass.reflect_on_association(:project)
      scope = scope.preload(:project => :enabled_modules)
    end
    objects = scope.to_a

140 141 142 143 144 145 146 147 148
    raise Unauthorized if objects.any? do |w|
      if w.respond_to?(:visible?)
        !w.visible?
      elsif w.respond_to?(:project) && w.project
        !w.project.visible?
      end
    end
    objects
  end
149
end