1 class DiaryEntriesController < ApplicationController
4 layout "site", :except => :rss
6 before_action :authorize_web
7 before_action :set_locale
8 before_action :check_database_readable
12 before_action :lookup_user, :only => [:show, :comments]
13 before_action :check_database_writable, :only => [:new, :create, :edit, :update, :comment, :hide, :hidecomment, :subscribe, :unsubscribe]
14 before_action :allow_thirdparty_images, :only => [:new, :create, :edit, :update, :index, :show, :comments]
17 if params[:display_name]
18 @user = User.active.find_by(:display_name => params[:display_name])
21 @title = t ".user_title", :user => @user.display_name
22 entries = @user.diary_entries
24 render_unknown_user params[:display_name]
27 elsif params[:friends]
29 @title = t ".title_friends"
30 entries = DiaryEntry.where(:user => current_user.friends)
37 @title = t ".title_nearby"
38 entries = DiaryEntry.where(:user => current_user.nearby)
44 entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
47 @title = t ".in_language_title", :language => Language.find(params[:language]).english_name
48 entries = entries.where(:language_code => params[:language])
54 entries = entries.visible unless can? :unhide, DiaryEntry
56 @params = params.permit(:display_name, :friends, :nearby, :language)
58 @entries = if params[:before]
59 entries.where("diary_entries.id < ?", params[:before]).order(:id => :desc)
61 entries.where("diary_entries.id > ?", params[:after]).order(:id => :asc)
63 entries.order(:id => :desc)
66 @entries = @entries.limit(20)
67 @entries = @entries.includes(:user, :language)
68 @entries = @entries.sort.reverse
70 @newer_entries = @entries.count.positive? && entries.exists?(["diary_entries.id > ?", @entries.first.id])
71 @older_entries = @entries.count.positive? && entries.exists?(["diary_entries.id < ?", @entries.last.id])
75 entries = @user.diary_entries
76 entries = entries.visible unless can? :unhide, DiaryEntry
77 @entry = entries.where(:id => params[:id]).first
79 @title = t ".title", :user => params[:display_name], :title => @entry.title
80 @comments = can?(:unhidecomment, DiaryEntry) ? @entry.comments : @entry.visible_comments
82 @title = t "diary_entries.no_such_entry.title", :id => params[:id]
83 render :action => "no_such_entry", :status => :not_found
90 default_lang = current_user.preferences.where(:k => "diary.default_language").first
91 lang_code = default_lang ? default_lang.v : current_user.preferred_language
92 @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
94 render :action => "new"
99 @diary_entry = DiaryEntry.find(params[:id])
101 redirect_to diary_entry_path(@diary_entry.user, @diary_entry) if current_user != @diary_entry.user
104 rescue ActiveRecord::RecordNotFound
105 render :action => "no_such_entry", :status => :not_found
109 @title = t "diary_entries.new.title"
111 @diary_entry = DiaryEntry.new(entry_params)
112 @diary_entry.user = current_user
115 default_lang = current_user.preferences.where(:k => "diary.default_language").first
117 default_lang.v = @diary_entry.language_code
120 current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
123 # Subscribe user to diary comments
124 @diary_entry.subscriptions.create(:user => current_user)
126 redirect_to :action => "index", :display_name => current_user.display_name
128 render :action => "new"
133 @title = t "diary_entries.edit.title"
134 @diary_entry = DiaryEntry.find(params[:id])
136 if current_user != @diary_entry.user ||
137 (params[:diary_entry] && @diary_entry.update(entry_params))
138 redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
141 render :action => "edit"
143 rescue ActiveRecord::RecordNotFound
144 render :action => "no_such_entry", :status => :not_found
148 @entry = DiaryEntry.find(params[:id])
149 @comments = @entry.visible_comments
150 @diary_comment = @entry.comments.build(comment_params)
151 @diary_comment.user = current_user
152 if @diary_comment.save
154 # Notify current subscribers of the new comment
155 @entry.subscribers.visible.each do |user|
156 UserMailer.diary_comment_notification(@diary_comment, user).deliver_later if current_user != user
159 # Add the commenter to the subscribers if necessary
160 @entry.subscriptions.create(:user => current_user) unless @entry.subscribers.exists?(current_user.id)
162 redirect_to diary_entry_path(@entry.user, @entry)
164 render :action => "show"
166 rescue ActiveRecord::RecordNotFound
167 render :action => "no_such_entry", :status => :not_found
171 diary_entry = DiaryEntry.find(params[:id])
173 diary_entry.subscriptions.create(:user => current_user) unless diary_entry.subscribers.exists?(current_user.id)
175 redirect_to diary_entry_path(diary_entry.user, diary_entry)
176 rescue ActiveRecord::RecordNotFound
177 render :action => "no_such_entry", :status => :not_found
181 diary_entry = DiaryEntry.find(params[:id])
183 diary_entry.subscriptions.where(:user => current_user).delete_all if diary_entry.subscribers.exists?(current_user.id)
185 redirect_to diary_entry_path(diary_entry.user, diary_entry)
186 rescue ActiveRecord::RecordNotFound
187 render :action => "no_such_entry", :status => :not_found
191 if params[:display_name]
192 user = User.active.find_by(:display_name => params[:display_name])
195 @entries = user.diary_entries
196 @title = t("diary_entries.feed.user.title", :user => user.display_name)
197 @description = t("diary_entries.feed.user.description", :user => user.display_name)
198 @link = url_for :action => "index", :display_name => user.display_name, :host => Settings.server_url, :protocol => Settings.server_protocol
204 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
206 # Items can't be flagged as deleted in the RSS format.
207 # For the general feeds, allow a delay before publishing, to help spam fighting
208 @entries = @entries.where("created_at < :time", :time => Settings.diary_feed_delay.hours.ago)
211 @entries = @entries.where(:language_code => params[:language])
212 @title = t("diary_entries.feed.language.title", :language_name => Language.find(params[:language]).english_name)
213 @description = t("diary_entries.feed.language.description", :language_name => Language.find(params[:language]).english_name)
214 @link = url_for :action => "index", :language => params[:language], :host => Settings.server_url, :protocol => Settings.server_protocol
216 @title = t("diary_entries.feed.all.title")
217 @description = t("diary_entries.feed.all.description")
218 @link = url_for :action => "index", :host => Settings.server_url, :protocol => Settings.server_protocol
221 @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
225 entry = DiaryEntry.find(params[:id])
226 entry.update(:visible => false)
227 redirect_to :action => "index", :display_name => entry.user.display_name
231 entry = DiaryEntry.find(params[:id])
232 entry.update(:visible => true)
233 redirect_to :action => "index", :display_name => entry.user.display_name
237 comment = DiaryComment.find(params[:comment])
238 comment.update(:visible => false)
239 redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
243 comment = DiaryComment.find(params[:comment])
244 comment.update(:visible => true)
245 redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
249 @title = t ".title", :user => @user.display_name
251 conditions = { :user_id => @user }
253 conditions[:visible] = true unless can? :unhidecomment, DiaryEntry
255 @comment_pages, @comments = paginate(:diary_comments,
256 :conditions => conditions,
257 :order => "created_at DESC",
259 @page = (params[:page] || 1).to_i
265 # return permitted diary entry parameters
267 params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
268 rescue ActionController::ParameterMissing
269 ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
273 # return permitted diary comment parameters
275 params.require(:diary_comment).permit(:body)
279 # decide on a location for the diary entry map
281 if @diary_entry.latitude && @diary_entry.longitude
282 @lon = @diary_entry.longitude
283 @lat = @diary_entry.latitude
285 elsif !current_user.home_location?
286 @lon = params[:lon] || -0.1
287 @lat = params[:lat] || 51.5
288 @zoom = params[:zoom] || 4
290 @lon = current_user.home_lon
291 @lat = current_user.home_lat