]> git.openstreetmap.org Git - rails.git/blob - app/controllers/issues_controller.rb
Added report icon + changes to admin UI + changes to report form + few other minor...
[rails.git] / app / controllers / issues_controller.rb
1 class IssuesController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :require_user
6   before_action :check_permission, only: [:index, :show, :resolve,:open,:ignore,:comment]
7   before_action :find_issue, only: [:show, :resolve, :reopen, :ignore]
8   before_action :get_user_role, only: [:show, :index]
9
10   def index
11     # If search
12     if params[:search_by_user]
13       @find_user = User.find_by_display_name(params[:search_by_user])
14       if @find_user
15         @issues = Issue.where(reported_user_id: @find_user.id, issue_type: @user_role).order(:status)
16       else 
17         @issues = Issue.where(issue_type: @user_role).order(:status)
18         notice = t('issues.index.search.user_not_found') 
19       end
20
21       if @find_user !=nil and @issues.first == nil
22         @issues = Issue.where(issue_type: @user_role).order(:status)
23         notice = t('issues.index.search.issues_not_found')
24       end
25
26       if notice
27         redirect_to issues_path, notice: notice
28       end 
29     
30     else
31       @issues = Issue.where(issue_type: @user_role).order(:status)
32     end
33   end
34
35   def show
36     @read_reports = @issue.read_reports
37     @unread_reports = @issue.unread_reports
38     @comments = @issue.comments
39     @related_issues = @issue.user.issues.where(issue_type: @user_role)
40     if @issue.updated_by
41       @updated_by_admin = User.find(@issue.updated_by)
42     end
43   end
44
45   def new
46     unless create_new_issue_params.blank?
47       @issue = Issue.find_or_initialize_by(create_new_issue_params)
48       path = 'issues.report_strings.' + @issue.reportable.class.name.to_s
49       @report_strings_yaml = t(path)
50     end
51   end
52
53   def create
54
55     # TODO: Find better place to add these
56     admin_issues = [ 'DiaryEntry', 'DiaryComment', 'User']
57     moderator_issues = [ 'Changeset' ]
58
59     
60     @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id],params[:reportable_type])
61     # Check if Issue alrwady exists
62     if !@issue 
63       @issue = Issue.find_or_initialize_by(issue_params)
64       @issue.updated_by = nil
65
66       # Reassign to moderators if it is a moderator issue
67       @issue.issue_type = "administrator"
68       if moderator_issues.include? @issue.reportable.class.name
69         reassign_issue
70       end
71
72       @admins_or_mods = UserRole.where(role: @issue.issue_type)
73       @admins_or_mods.each do |user|
74         Notifier.new_issue_notification(User.find(user.user_id)).deliver_now
75       end
76
77     end
78
79     # Check if details provided are sufficient
80     if check_report_params
81       @report = @issue.reports.build(report_params)
82       details =  get_report_details
83       @report.reporter_user_id = @user.id
84       @report.details = details
85
86       # Checking if instance has been updated since last report
87       @last_report = @issue.reports.order(updated_at: :desc).last
88       if check_if_updated
89         if @issue.reopen
90           @issue.save!
91         end
92       end
93
94       if @issue.save!
95         redirect_to root_path, notice: t('issues.create.successful_report')
96       end
97     else
98       redirect_to new_issue_path(reportable_type: @issue.reportable_type,reportable_id: @issue.reportable_id, reported_user_id: @issue.reported_user_id), notice: t('issues.create.provide_details')
99     end
100   end
101
102   def update
103     @issue = Issue.find_by(issue_params)
104     # Check if details provided are sufficient
105     if check_report_params
106       @report = @issue.reports.where(reporter_user_id: @user.id).first
107       
108       if @report == nil
109         @report = @issue.reports.build(report_params)
110         @report.reporter_user_id = @user.id
111         notice = t('issues.update.new_report')
112       end
113
114       details =  get_report_details
115       @report.details = details    
116
117     # Checking if instance has been updated since last report
118       @last_report = @issue.reports.order(updated_at: :desc).last
119       if check_if_updated
120         @issue.reopen
121         @issue.save!
122       end
123
124       if notice == nil
125         notice = t('issues.update.successful_update')
126       end
127
128       if @report.save!
129         redirect_to root_path, notice: notice
130       end
131     else
132       redirect_to new_issue_path(reportable_type: @issue.reportable_type,reportable_id: @issue.reportable_id, reported_user_id: @issue.reported_user_id), notice: t('issues.update.provide_details')
133     end  
134   end
135
136   def comment
137     @issue = Issue.find(params[:id])
138     if issue_comment_params.blank?
139       notice = t('issues.comment.provide_details')
140     else
141       @issue_comment = @issue.comments.build(issue_comment_params)
142       @issue_comment.commenter_user_id = @user.id
143       if params[:reassign]
144         reassign_issue
145         @issue_comment.reassign = true
146       end
147       @issue_comment.save!
148       @issue.updated_by = @user.id
149       @issue.save!
150       notice = t('issues.comment.comment_created')
151     end
152     redirect_to @issue, notice: notice
153   end
154
155   # Status Transistions
156   def resolve
157     if @issue.resolve
158       @issue.save!
159       redirect_to @issue, notice: t('issues.resolved')
160     else
161       render :show
162     end
163   end
164
165   def ignore
166     if @issue.ignore
167       @issue.updated_by = @user.id
168       @issue.save!
169       redirect_to @issue, notice: t('issues.ignored')
170     else
171       render :show
172     end
173   end
174
175   def reopen
176     if @issue.reopen
177       @issue.updated_by = @user.id      
178       @issue.save!
179       redirect_to @issue, notice: t('issues.reopened')
180     else
181       render :show
182     end
183   end
184
185   # Reassign Issues between Administrators and Moderators
186   def reassign_issue
187     if @issue.issue_type == "moderator"
188       @issue.issue_type = "administrator"
189     else
190       @issue.issue_type = "moderator"
191     end
192     @issue.save!
193   end
194
195   private
196
197     def get_user_role
198       # Get user role
199       if @user.administrator?
200         @user_role = "administrator"
201       else
202         @user_role = "moderator"
203       end      
204     end
205     
206     def check_if_updated
207       if @issue.reportable and (@issue.ignored? or @issue.resolved?) and @issue.reportable.updated_at > @last_report.updated_at
208         return true
209       else
210         return false
211       end
212     end
213  
214     def get_report_details
215       details = params[:report][:details] + "--||--"
216       details = details + params[:report_type].to_s + "--||--"
217       return details
218     end
219
220     def check_report_params
221       if params[:report] and params[:report][:details] and params[:report_type]
222         return true
223       end
224       return false
225     end
226
227     def find_issue
228       @issue = Issue.find(params[:id])
229     end
230
231     def check_permission
232       unless @user.administrator? or @user.moderator?
233         flash[:error] = t('application.require_admin.not_an_admin')
234         redirect_to root_path
235       end
236     end
237
238     def create_new_issue_params
239       params.permit(:reportable_id, :reportable_type, :reported_user_id)
240     end
241
242     def issue_params
243       params[:issue].permit(:reportable_id, :reportable_type,:reported_user_id)
244     end
245
246     def report_params
247       params[:report].permit(:details)
248     end
249
250     def issue_comment_params
251       params.require(:issue_comment).permit(:body)
252     end
253
254 end