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