1 class DiaryEntryController < ApplicationController
2 layout "site", :except => :rss
4 before_action :authorize_web
5 before_action :set_locale
9 before_action :lookup_user, :only => [:view, :comments]
10 before_action :check_database_readable
11 before_action :check_database_writable, :only => [:new, :edit, :comment, :hide, :hidecomment, :subscribe, :unsubscribe]
12 before_action :allow_thirdparty_images, :only => [:new, :edit, :list, :view, :comments]
16 @title = t "diary_entry.new.title"
19 @diary_entry = DiaryEntry.new(entry_params)
20 @diary_entry.user = current_user
23 default_lang = current_user.preferences.where(:k => "diary.default_language").first
25 default_lang.v = @diary_entry.language_code
28 current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
31 # Subscribe user to diary comments
32 @diary_entry.subscriptions.create(:user => current_user)
34 redirect_to :action => "list", :display_name => current_user.display_name
36 render :action => "edit"
39 default_lang = current_user.preferences.where(:k => "diary.default_language").first
40 lang_code = default_lang ? default_lang.v : current_user.preferred_language
41 @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
43 render :action => "edit"
48 @title = t "diary_entry.edit.title"
49 @diary_entry = DiaryEntry.find(params[:id])
51 if current_user != @diary_entry.user
52 redirect_to :action => "view", :id => params[:id]
53 elsif params[:diary_entry] && @diary_entry.update(entry_params)
54 redirect_to :action => "view", :id => params[:id]
58 rescue ActiveRecord::RecordNotFound
59 render :action => "no_such_entry", :status => :not_found
63 @entry = DiaryEntry.find(params[:id])
64 @diary_comment = @entry.comments.build(comment_params)
65 @diary_comment.user = current_user
66 if @diary_comment.save
68 # Notify current subscribers of the new comment
69 @entry.subscribers.visible.each do |user|
70 Notifier.diary_comment_notification(@diary_comment, user).deliver_now if current_user != user
73 # Add the commenter to the subscribers if necessary
74 @entry.subscriptions.create(:user => current_user) unless @entry.subscribers.exists?(current_user.id)
76 redirect_to :action => "view", :display_name => @entry.user.display_name, :id => @entry.id
78 render :action => "view"
80 rescue ActiveRecord::RecordNotFound
81 render :action => "no_such_entry", :status => :not_found
85 diary_entry = DiaryEntry.find(params[:id])
87 diary_entry.subscriptions.create(:user => current_user) unless diary_entry.subscribers.exists?(current_user.id)
89 redirect_to :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
90 rescue ActiveRecord::RecordNotFound
91 render :action => "no_such_entry", :status => :not_found
95 diary_entry = DiaryEntry.find(params[:id])
97 diary_entry.subscriptions.where(:user => current_user).delete_all if diary_entry.subscribers.exists?(current_user.id)
99 redirect_to :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
100 rescue ActiveRecord::RecordNotFound
101 render :action => "no_such_entry", :status => :not_found
105 if params[:display_name]
106 @user = User.active.find_by(:display_name => params[:display_name])
109 @title = t "diary_entry.list.user_title", :user => @user.display_name
110 @entries = @user.diary_entries
112 render_unknown_user params[:display_name]
115 elsif params[:friends]
117 @title = t "diary_entry.list.title_friends"
118 @entries = DiaryEntry.where(:user_id => current_user.friend_users)
123 elsif params[:nearby]
125 @title = t "diary_entry.list.title_nearby"
126 @entries = DiaryEntry.where(:user_id => current_user.nearby)
132 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
135 @title = t "diary_entry.list.in_language_title", :language => Language.find(params[:language]).english_name
136 @entries = @entries.where(:language_code => params[:language])
138 @title = t "diary_entry.list.title"
142 @params = params.permit(:display_name, :friends, :nearby, :language)
144 @page = (params[:page] || 1).to_i
147 @entries = @entries.visible
148 @entries = @entries.order("created_at DESC")
149 @entries = @entries.offset((@page - 1) * @page_size)
150 @entries = @entries.limit(@page_size)
151 @entries = @entries.includes(:user, :language)
155 if params[:display_name]
156 user = User.active.find_by(:display_name => params[:display_name])
159 @entries = user.diary_entries
160 @title = t("diary_entry.feed.user.title", :user => user.display_name)
161 @description = t("diary_entry.feed.user.description", :user => user.display_name)
162 @link = url_for :controller => "diary_entry", :action => "list", :display_name => user.display_name, :host => SERVER_URL, :protocol => SERVER_PROTOCOL
168 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
171 @entries = @entries.where(:language_code => params[:language])
172 @title = t("diary_entry.feed.language.title", :language_name => Language.find(params[:language]).english_name)
173 @description = t("diary_entry.feed.language.description", :language_name => Language.find(params[:language]).english_name)
174 @link = url_for :controller => "diary_entry", :action => "list", :language => params[:language], :host => SERVER_URL, :protocol => SERVER_PROTOCOL
176 @title = t("diary_entry.feed.all.title")
177 @description = t("diary_entry.feed.all.description")
178 @link = url_for :controller => "diary_entry", :action => "list", :host => SERVER_URL, :protocol => SERVER_PROTOCOL
182 @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
186 @entry = @user.diary_entries.visible.where(:id => params[:id]).first
188 @title = t "diary_entry.view.title", :user => params[:display_name], :title => @entry.title
190 @title = t "diary_entry.no_such_entry.title", :id => params[:id]
191 render :action => "no_such_entry", :status => :not_found
196 entry = DiaryEntry.find(params[:id])
197 entry.update(:visible => false)
198 redirect_to :action => "list", :display_name => entry.user.display_name
202 comment = DiaryComment.find(params[:comment])
203 comment.update(:visible => false)
204 redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
208 @comment_pages, @comments = paginate(:diary_comments,
213 :order => "created_at DESC",
215 @page = (params[:page] || 1).to_i
220 # This is required because, being a default-deny system, cancancan
221 # _cannot_ tell you the reason you were denied access; and so
222 # the "nice" feedback presenting next steps can't be gleaned from
225 # for the hide actions, require that the user is a administrator, or fill out
226 # a helpful error message and return them to the user page.
227 def deny_access(exception)
228 if current_user && exception.action.in?([:hide, :hidecomment])
229 flash[:error] = t("user.filter.not_an_administrator")
230 redirect_to :action => "view"
237 # return permitted diary entry parameters
239 params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
240 rescue ActionController::ParameterMissing
241 ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
245 # return permitted diary comment parameters
247 params.require(:diary_comment).permit(:body)
252 # decide on a location for the diary entry map
254 if @diary_entry.latitude && @diary_entry.longitude
255 @lon = @diary_entry.longitude
256 @lat = @diary_entry.latitude
258 elsif current_user.home_lat.nil? || current_user.home_lon.nil?
259 @lon = params[:lon] || -0.1
260 @lat = params[:lat] || 51.5
261 @zoom = params[:zoom] || 4
263 @lon = current_user.home_lon
264 @lat = current_user.home_lat