]> git.openstreetmap.org Git - rails.git/blob - app/controllers/messages_controller.rb
Remove outdated comments
[rails.git] / app / controllers / messages_controller.rb
1 class MessagesController < ApplicationController
2   layout "site"
3
4   before_action :authorize_web
5   before_action :set_locale
6
7   authorize_resource
8
9   before_action :lookup_user, :only => [:new, :create]
10   before_action :check_database_readable
11   before_action :check_database_writable, :only => [:new, :create, :reply, :mark, :destroy]
12   before_action :allow_thirdparty_images, :only => [:new, :create, :show]
13
14   # Show a message
15   def show
16     @title = t ".title"
17     @message = Message.find(params[:id])
18
19     if @message.recipient == current_user || @message.sender == current_user
20       @message.message_read = true if @message.recipient == current_user
21       @message.save
22     else
23       flash[:notice] = t ".wrong_user", :user => current_user.display_name
24       redirect_to login_path(:referer => request.fullpath)
25     end
26   rescue ActiveRecord::RecordNotFound
27     @title = t "messages.no_such_message.title"
28     render :action => "no_such_message", :status => :not_found
29   end
30
31   # Allow the user to write a new message to another user.
32   def new
33     @message = Message.new(message_params.merge(:recipient => @user))
34     @title = t ".title"
35   end
36
37   def create
38     @message = Message.new(message_params)
39     @message.recipient = @user
40     @message.sender = current_user
41     @message.sent_on = Time.now.utc
42
43     if current_user.sent_messages.where("sent_on >= ?", Time.now.utc - 1.hour).count >= current_user.max_messages_per_hour
44       flash.now[:error] = t ".limit_exceeded"
45       render :action => "new"
46     elsif @message.save
47       flash[:notice] = t ".message_sent"
48       UserMailer.message_notification(@message).deliver_later
49       redirect_to :action => :inbox
50     else
51       @title = t "messages.new.title"
52       render :action => "new"
53     end
54   end
55
56   # Destroy the message.
57   def destroy
58     @message = Message.where("to_user_id = ? OR from_user_id = ?", current_user.id, current_user.id).find(params[:id])
59     @message.from_user_visible = false if @message.sender == current_user
60     @message.to_user_visible = false if @message.recipient == current_user
61     if @message.save && !request.xhr?
62       flash[:notice] = t ".destroyed"
63
64       referer = safe_referer(params[:referer]) if params[:referer]
65
66       redirect_to referer || { :action => :inbox }
67     end
68   rescue ActiveRecord::RecordNotFound
69     @title = t "messages.no_such_message.title"
70     render :action => "no_such_message", :status => :not_found
71   end
72
73   # Allow the user to reply to another message.
74   def reply
75     message = Message.find(params[:message_id])
76
77     if message.recipient == current_user
78       message.update(:message_read => true)
79
80       @message = Message.new(
81         :recipient => message.sender,
82         :title => "Re: #{message.title.sub(/^Re:\s*/, '')}",
83         :body => "On #{message.sent_on} #{message.sender.display_name} wrote:\n\n#{message.body.gsub(/^/, '> ')}"
84       )
85
86       @title = @message.title
87
88       render :action => "new"
89     else
90       flash[:notice] = t ".wrong_user", :user => current_user.display_name
91       redirect_to login_path(:referer => request.fullpath)
92     end
93   rescue ActiveRecord::RecordNotFound
94     @title = t "messages.no_such_message.title"
95     render :action => "no_such_message", :status => :not_found
96   end
97
98   # Display the list of messages that have been sent to the user.
99   def inbox
100     @title = t ".title"
101   end
102
103   # Display the list of messages that the user has sent to other users.
104   def outbox
105     @title = t ".title"
106   end
107
108   # Set the message as being read or unread.
109   def mark
110     @message = Message.where("to_user_id = ? OR from_user_id = ?", current_user.id, current_user.id).find(params[:message_id])
111     if params[:mark] == "unread"
112       message_read = false
113       notice = t ".as_unread"
114     else
115       message_read = true
116       notice = t ".as_read"
117     end
118     @message.message_read = message_read
119     if @message.save && !request.xhr?
120       flash[:notice] = notice
121       redirect_to :action => :inbox
122     end
123   rescue ActiveRecord::RecordNotFound
124     @title = t "messages.no_such_message.title"
125     render :action => "no_such_message", :status => :not_found
126   end
127
128   private
129
130   ##
131   # return permitted message parameters
132   def message_params
133     params.require(:message).permit(:title, :body)
134   rescue ActionController::ParameterMissing
135     ActionController::Parameters.new.permit(:title, :body)
136   end
137 end