Refactor diary entries to use create and update methods
[rails.git] / app / controllers / diary_entries_controller.rb
1 class DiaryEntriesController < ApplicationController
2   layout "site", :except => :rss
3
4   before_action :authorize_web
5   before_action :set_locale
6   before_action :check_database_readable
7
8   authorize_resource
9
10   before_action :lookup_user, :only => [:show, :comments]
11   before_action :check_database_writable, :only => [:new, :create, :edit, :update, :comment, :hide, :hidecomment, :subscribe, :unsubscribe]
12   before_action :allow_thirdparty_images, :only => [:new, :create, :edit, :update, :index, :show, :comments]
13
14   def new
15     @title = t "diary_entries.new.title"
16
17     default_lang = current_user.preferences.where(:k => "diary.default_language").first
18     lang_code = default_lang ? default_lang.v : current_user.preferred_language
19     @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
20     set_map_location
21     render :action => "new"
22   end
23
24   def create
25     @title = t "diary_entries.new.title"
26
27     @diary_entry = DiaryEntry.new(entry_params)
28     @diary_entry.user = current_user
29
30     if @diary_entry.save
31       default_lang = current_user.preferences.where(:k => "diary.default_language").first
32       if default_lang
33         default_lang.v = @diary_entry.language_code
34         default_lang.save!
35       else
36         current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
37       end
38
39       # Subscribe user to diary comments
40       @diary_entry.subscriptions.create(:user => current_user)
41
42       redirect_to :action => "index", :display_name => current_user.display_name
43     else
44       render :action => "new"
45     end
46   end
47
48   def edit
49     @title = t "diary_entries.edit.title"
50     @diary_entry = DiaryEntry.find(params[:id])
51
52     redirect_to diary_entry_path(@diary_entry.user, @diary_entry) if current_user != @diary_entry.user
53
54     set_map_location
55   rescue ActiveRecord::RecordNotFound
56     render :action => "no_such_entry", :status => :not_found
57   end
58
59   def update
60     @title = t "diary_entries.edit.title"
61     @diary_entry = DiaryEntry.find(params[:id])
62
63     if current_user != @diary_entry.user
64       redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
65     elsif params[:diary_entry] && @diary_entry.update(entry_params)
66       redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
67     else
68       set_map_location
69       render :action => "edit"
70     end
71   rescue ActiveRecord::RecordNotFound
72     render :action => "no_such_entry", :status => :not_found
73   end
74
75   def comment
76     @entry = DiaryEntry.find(params[:id])
77     @diary_comment = @entry.comments.build(comment_params)
78     @diary_comment.user = current_user
79     if @diary_comment.save
80
81       # Notify current subscribers of the new comment
82       @entry.subscribers.visible.each do |user|
83         Notifier.diary_comment_notification(@diary_comment, user).deliver_later if current_user != user
84       end
85
86       # Add the commenter to the subscribers if necessary
87       @entry.subscriptions.create(:user => current_user) unless @entry.subscribers.exists?(current_user.id)
88
89       redirect_to diary_entry_path(@entry.user, @entry)
90     else
91       render :action => "show"
92     end
93   rescue ActiveRecord::RecordNotFound
94     render :action => "no_such_entry", :status => :not_found
95   end
96
97   def subscribe
98     diary_entry = DiaryEntry.find(params[:id])
99
100     diary_entry.subscriptions.create(:user => current_user) unless diary_entry.subscribers.exists?(current_user.id)
101
102     redirect_to diary_entry_path(diary_entry.user, diary_entry)
103   rescue ActiveRecord::RecordNotFound
104     render :action => "no_such_entry", :status => :not_found
105   end
106
107   def unsubscribe
108     diary_entry = DiaryEntry.find(params[:id])
109
110     diary_entry.subscriptions.where(:user => current_user).delete_all if diary_entry.subscribers.exists?(current_user.id)
111
112     redirect_to diary_entry_path(diary_entry.user, diary_entry)
113   rescue ActiveRecord::RecordNotFound
114     render :action => "no_such_entry", :status => :not_found
115   end
116
117   def index
118     if params[:display_name]
119       @user = User.active.find_by(:display_name => params[:display_name])
120
121       if @user
122         @title = t "diary_entries.index.user_title", :user => @user.display_name
123         @entries = @user.diary_entries
124       else
125         render_unknown_user params[:display_name]
126         return
127       end
128     elsif params[:friends]
129       if current_user
130         @title = t "diary_entries.index.title_friends"
131         @entries = DiaryEntry.where(:user_id => current_user.friend_users)
132       else
133         require_user
134         return
135       end
136     elsif params[:nearby]
137       if current_user
138         @title = t "diary_entries.index.title_nearby"
139         @entries = DiaryEntry.where(:user_id => current_user.nearby)
140       else
141         require_user
142         return
143       end
144     else
145       @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
146
147       if params[:language]
148         @title = t "diary_entries.index.in_language_title", :language => Language.find(params[:language]).english_name
149         @entries = @entries.where(:language_code => params[:language])
150       else
151         @title = t "diary_entries.index.title"
152       end
153     end
154
155     @params = params.permit(:display_name, :friends, :nearby, :language)
156
157     @page = (params[:page] || 1).to_i
158     @page_size = 20
159
160     @entries = @entries.visible
161     @entries = @entries.order("created_at DESC")
162     @entries = @entries.offset((@page - 1) * @page_size)
163     @entries = @entries.limit(@page_size)
164     @entries = @entries.includes(:user, :language)
165   end
166
167   def rss
168     if params[:display_name]
169       user = User.active.find_by(:display_name => params[:display_name])
170
171       if user
172         @entries = user.diary_entries
173         @title = t("diary_entries.feed.user.title", :user => user.display_name)
174         @description = t("diary_entries.feed.user.description", :user => user.display_name)
175         @link = url_for :action => "index", :display_name => user.display_name, :host => Settings.server_url, :protocol => Settings.server_protocol
176       else
177         head :not_found
178         return
179       end
180     else
181       @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
182
183       if params[:language]
184         @entries = @entries.where(:language_code => params[:language])
185         @title = t("diary_entries.feed.language.title", :language_name => Language.find(params[:language]).english_name)
186         @description = t("diary_entries.feed.language.description", :language_name => Language.find(params[:language]).english_name)
187         @link = url_for :action => "index", :language => params[:language], :host => Settings.server_url, :protocol => Settings.server_protocol
188       else
189         @title = t("diary_entries.feed.all.title")
190         @description = t("diary_entries.feed.all.description")
191         @link = url_for :action => "index", :host => Settings.server_url, :protocol => Settings.server_protocol
192       end
193     end
194
195     @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
196   end
197
198   def show
199     @entry = @user.diary_entries.visible.where(:id => params[:id]).first
200     if @entry
201       @title = t "diary_entries.show.title", :user => params[:display_name], :title => @entry.title
202     else
203       @title = t "diary_entries.no_such_entry.title", :id => params[:id]
204       render :action => "no_such_entry", :status => :not_found
205     end
206   end
207
208   def hide
209     entry = DiaryEntry.find(params[:id])
210     entry.update(:visible => false)
211     redirect_to :action => "index", :display_name => entry.user.display_name
212   end
213
214   def hidecomment
215     comment = DiaryComment.find(params[:comment])
216     comment.update(:visible => false)
217     redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
218   end
219
220   def comments
221     @comment_pages, @comments = paginate(:diary_comments,
222                                          :conditions => {
223                                            :user_id => @user,
224                                            :visible => true
225                                          },
226                                          :order => "created_at DESC",
227                                          :per_page => 20)
228     @page = (params[:page] || 1).to_i
229   end
230
231   private
232
233   ##
234   # return permitted diary entry parameters
235   def entry_params
236     params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
237   rescue ActionController::ParameterMissing
238     ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
239   end
240
241   ##
242   # return permitted diary comment parameters
243   def comment_params
244     params.require(:diary_comment).permit(:body)
245   end
246
247   ##
248   # decide on a location for the diary entry map
249   def set_map_location
250     if @diary_entry.latitude && @diary_entry.longitude
251       @lon = @diary_entry.longitude
252       @lat = @diary_entry.latitude
253       @zoom = 12
254     elsif current_user.home_lat.nil? || current_user.home_lon.nil?
255       @lon = params[:lon] || -0.1
256       @lat = params[:lat] || 51.5
257       @zoom = params[:zoom] || 4
258     else
259       @lon = current_user.home_lon
260       @lat = current_user.home_lat
261       @zoom = 12
262     end
263   end
264 end