]> git.openstreetmap.org Git - rails.git/blob - app/controllers/issues_controller.rb
Issue reopens on Reports coming in after instance update
[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     @issues = Issue.all.order(:status)
11   end
12
13   def show
14     @read_reports = @issue.read_reports
15     @unread_reports = @issue.unread_reports
16     @comments = @issue.comments
17     @related_issues = @issue.user.issues
18   end
19
20   def new
21     unless create_new_issue_params.blank?
22       @issue = Issue.find_or_initialize_by(create_new_issue_params)
23     end
24   end
25
26   def create
27     @issue = Issue.find_by_reportable_id_and_reportable_type(params[:reportable_id],params[:reportable_type])
28
29     # Check if Issue alrwady exists
30     if !@issue 
31       @issue = Issue.find_or_initialize_by(issue_params)
32       @admins = UserRole.where(role: "administrator")
33       @admins.each do |admin|
34         Notifier.new_issue_notification(User.find(admin.user_id)).deliver_now
35       end
36     end
37
38     # Check if details provided are sufficient
39     if params[:report][:details] and (params[:spam] or params[:offensive] or params[:threat] or params[:vandal] or params[:other])
40       @report = @issue.reports.build(report_params)
41       details =  params[:report][:details].to_s + "||" + params[:spam].to_s + "||" + params[:offensive].to_s + "||" + params[:threat].to_s + "||" + params[:vandal].to_s + "||" + params[:other].to_s
42       @report.reporter_user_id = @user.id
43       @report.details = details
44
45       # Checking if instance has been updated since last report
46       @last_report = @issue.reports.order(updated_at: :desc).last
47       if @issue.reportable.updated_at.present? and (@issue.ignored? or @issue.resolved?) and @issue.reportable.updated_at > @last_report.updated_at
48         if @issue.reopen
49           @issue.save!
50         end
51       end
52
53       if @issue.save!
54         redirect_to root_path, notice: t('issues.create.successful_report')
55       end
56     else
57       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')
58     end
59   end
60
61   def update
62     @issue = Issue.find_by(issue_params)
63
64     # Check if details provided are sufficient
65     if params[:report][:details] and (params[:spam] or params[:offensive] or params[:threat] or params[:vandal] or params[:other])
66       @report = @issue.reports.where(reporter_user_id: @user.id).first
67       
68       if @report == nil
69         @report = @issue.reports.build(report_params)
70         @report.reporter_user_id = @user.id
71         notice = t('issues.update.new_report')
72       end
73
74       details =  params[:report][:details].to_s + "||" + params[:spam].to_s + "||" + params[:offensive].to_s + "||" + params[:threat].to_s + "||" + params[:vandal].to_s + "||" + params[:other].to_s
75       @report.details = details    
76
77     # Checking if instance has been updated since last report
78       @last_report = @issue.reports.order(updated_at: :desc).last
79       if @issue.reportable.updated_at.present? and (@issue.ignored? or @issue.resolved?) and @issue.reportable.updated_at > @last_report.updated_at
80         @issue.reopen
81         @issue.save!
82       end
83
84       if notice == nil
85         notice = t('issues.update.successful_update')
86       end
87
88       if @report.save!
89         redirect_to root_path, notice: notice
90       end
91     else
92       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')
93     end  
94   end
95
96   def comment
97     @issue = Issue.find(params[:id])
98     @issue_comment = @issue.comments.build(issue_comment_params)
99     @issue_comment.commenter_user_id = @user.id
100     @issue_comment.save!
101     redirect_to @issue
102   end
103
104   # Status Transistions
105   def resolve
106     if @issue.resolve
107       @issue.save!
108       redirect_to @issue, notice: t('issues.resolved')
109     else
110       render :show
111     end
112   end
113
114   def ignore
115     if @issue.ignore
116       @issue.save!
117       redirect_to @issue, notice: t('issues.ignored')
118     else
119       render :show
120     end
121   end
122
123   def reopen
124     if @issue.reopen
125       @issue.save!
126       redirect_to @issue, notice: t('issues.reopened')
127     else
128       render :show
129     end
130   end
131
132   private
133
134     def find_issue
135       @issue = Issue.find(params[:id])
136     end
137
138     def check_permission
139       unless @user.administrator?
140         flash[:error] = t("application.require_admin.not_an_admin")
141         redirect_to root_path
142       end
143     end
144
145     def create_new_issue_params
146       params.permit(:reportable_id, :reportable_type, :reported_user_id)
147     end
148
149     def issue_params
150       params[:issue].permit(:reportable_id, :reportable_type,:reported_user_id)
151     end
152
153     def report_params
154       params[:report].permit(:details)
155     end
156
157     def issue_comment_params
158       params.require(:issue_comment).permit(:body)
159     end
160 end