1 class DiaryEntriesController < ApplicationController
2 layout "site", :except => :rss
4 before_action :authorize_web
5 before_action :set_locale
6 before_action :check_database_readable
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]
15 if params[:display_name]
16 @user = User.active.find_by(:display_name => params[:display_name])
19 @title = t "diary_entries.index.user_title", :user => @user.display_name
20 @entries = @user.diary_entries
22 render_unknown_user params[:display_name]
25 elsif params[:friends]
27 @title = t "diary_entries.index.title_friends"
28 @entries = DiaryEntry.where(:user_id => current_user.friends)
35 @title = t "diary_entries.index.title_nearby"
36 @entries = DiaryEntry.where(:user_id => current_user.nearby)
42 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
45 @title = t "diary_entries.index.in_language_title", :language => Language.find(params[:language]).english_name
46 @entries = @entries.where(:language_code => params[:language])
48 @title = t "diary_entries.index.title"
52 @params = params.permit(:display_name, :friends, :nearby, :language)
54 @page = (params[:page] || 1).to_i
57 @entries = @entries.visible unless can? :unhide, DiaryEntry
58 @entries = @entries.order("created_at DESC")
59 @entries = @entries.offset((@page - 1) * @page_size)
60 @entries = @entries.limit(@page_size)
61 @entries = @entries.includes(:user, :language)
65 @entry = @user.diary_entries.visible.where(:id => params[:id]).first
67 @title = t "diary_entries.show.title", :user => params[:display_name], :title => @entry.title
68 @comments = can?(:unhidecomment, DiaryEntry) ? @entry.comments : @entry.visible_comments
70 @title = t "diary_entries.no_such_entry.title", :id => params[:id]
71 render :action => "no_such_entry", :status => :not_found
76 @title = t "diary_entries.new.title"
78 default_lang = current_user.preferences.where(:k => "diary.default_language").first
79 lang_code = default_lang ? default_lang.v : current_user.preferred_language
80 @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
82 render :action => "new"
86 @title = t "diary_entries.edit.title"
87 @diary_entry = DiaryEntry.find(params[:id])
89 redirect_to diary_entry_path(@diary_entry.user, @diary_entry) if current_user != @diary_entry.user
92 rescue ActiveRecord::RecordNotFound
93 render :action => "no_such_entry", :status => :not_found
97 @title = t "diary_entries.new.title"
99 @diary_entry = DiaryEntry.new(entry_params)
100 @diary_entry.user = current_user
103 default_lang = current_user.preferences.where(:k => "diary.default_language").first
105 default_lang.v = @diary_entry.language_code
108 current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
111 # Subscribe user to diary comments
112 @diary_entry.subscriptions.create(:user => current_user)
114 redirect_to :action => "index", :display_name => current_user.display_name
116 render :action => "new"
121 @title = t "diary_entries.edit.title"
122 @diary_entry = DiaryEntry.find(params[:id])
124 if current_user != @diary_entry.user ||
125 (params[:diary_entry] && @diary_entry.update(entry_params))
126 redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
129 render :action => "edit"
131 rescue ActiveRecord::RecordNotFound
132 render :action => "no_such_entry", :status => :not_found
136 @entry = DiaryEntry.find(params[:id])
137 @comments = @entry.visible_comments
138 @diary_comment = @entry.comments.build(comment_params)
139 @diary_comment.user = current_user
140 if @diary_comment.save
142 # Notify current subscribers of the new comment
143 @entry.subscribers.visible.each do |user|
144 UserMailer.diary_comment_notification(@diary_comment, user).deliver_later if current_user != user
147 # Add the commenter to the subscribers if necessary
148 @entry.subscriptions.create(:user => current_user) unless @entry.subscribers.exists?(current_user.id)
150 redirect_to diary_entry_path(@entry.user, @entry)
152 render :action => "show"
154 rescue ActiveRecord::RecordNotFound
155 render :action => "no_such_entry", :status => :not_found
159 diary_entry = DiaryEntry.find(params[:id])
161 diary_entry.subscriptions.create(:user => current_user) unless diary_entry.subscribers.exists?(current_user.id)
163 redirect_to diary_entry_path(diary_entry.user, diary_entry)
164 rescue ActiveRecord::RecordNotFound
165 render :action => "no_such_entry", :status => :not_found
169 diary_entry = DiaryEntry.find(params[:id])
171 diary_entry.subscriptions.where(:user => current_user).delete_all if diary_entry.subscribers.exists?(current_user.id)
173 redirect_to diary_entry_path(diary_entry.user, diary_entry)
174 rescue ActiveRecord::RecordNotFound
175 render :action => "no_such_entry", :status => :not_found
179 if params[:display_name]
180 user = User.active.find_by(:display_name => params[:display_name])
183 @entries = user.diary_entries
184 @title = t("diary_entries.feed.user.title", :user => user.display_name)
185 @description = t("diary_entries.feed.user.description", :user => user.display_name)
186 @link = url_for :action => "index", :display_name => user.display_name, :host => Settings.server_url, :protocol => Settings.server_protocol
192 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
194 # Items can't be flagged as deleted in the RSS format.
195 # For the general feeds, allow a delay before publishing, to help spam fighting
196 @entries = @entries.where("created_at < :time", :time => Settings.diary_feed_delay.hours.ago)
199 @entries = @entries.where(:language_code => params[:language])
200 @title = t("diary_entries.feed.language.title", :language_name => Language.find(params[:language]).english_name)
201 @description = t("diary_entries.feed.language.description", :language_name => Language.find(params[:language]).english_name)
202 @link = url_for :action => "index", :language => params[:language], :host => Settings.server_url, :protocol => Settings.server_protocol
204 @title = t("diary_entries.feed.all.title")
205 @description = t("diary_entries.feed.all.description")
206 @link = url_for :action => "index", :host => Settings.server_url, :protocol => Settings.server_protocol
209 @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
213 entry = DiaryEntry.find(params[:id])
214 entry.update(:visible => false)
215 redirect_to :action => "index", :display_name => entry.user.display_name
219 entry = DiaryEntry.find(params[:id])
220 entry.update(:visible => true)
221 redirect_to :action => "index", :display_name => entry.user.display_name
225 comment = DiaryComment.find(params[:comment])
226 comment.update(:visible => false)
227 redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
231 comment = DiaryComment.find(params[:comment])
232 comment.update(:visible => true)
233 redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
237 @title = t ".title", :user => @user.display_name
239 conditions = { :user_id => @user }
241 conditions[:visible] = true unless can? :unhidecomment, DiaryEntry
243 @comment_pages, @comments = paginate(:diary_comments,
244 :conditions => conditions,
245 :order => "created_at DESC",
247 @page = (params[:page] || 1).to_i
253 # return permitted diary entry parameters
255 params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
256 rescue ActionController::ParameterMissing
257 ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
261 # return permitted diary comment parameters
263 params.require(:diary_comment).permit(:body)
267 # decide on a location for the diary entry map
269 if @diary_entry.latitude && @diary_entry.longitude
270 @lon = @diary_entry.longitude
271 @lat = @diary_entry.latitude
273 elsif current_user.home_lat.nil? || current_user.home_lon.nil?
274 @lon = params[:lon] || -0.1
275 @lat = params[:lat] || 51.5
276 @zoom = params[:zoom] || 4
278 @lon = current_user.home_lon
279 @lat = current_user.home_lat