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