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 ".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 ".title_friends"
28 entries = DiaryEntry.where(:user_id => current_user.friends)
35 @title = t ".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 ".in_language_title", :language => Language.find(params[:language]).english_name
46 entries = entries.where(:language_code => params[:language])
52 entries = entries.visible unless can? :unhide, DiaryEntry
54 @params = params.permit(:display_name, :friends, :nearby, :language)
56 @entries = if params[:before]
57 entries.where("diary_entries.id < ?", params[:before]).order(:id => :desc)
59 entries.where("diary_entries.id > ?", params[:after]).order(:id => :asc)
61 entries.order(:id => :desc)
64 @entries = @entries.limit(20)
65 @entries = @entries.includes(:user, :language)
66 @entries = @entries.sort.reverse
68 @newer_entries = @entries.count.positive? && entries.exists?(["diary_entries.id > ?", @entries.first.id])
69 @older_entries = @entries.count.positive? && entries.exists?(["diary_entries.id < ?", @entries.last.id])
73 entries = @user.diary_entries
74 entries = entries.visible unless can? :unhide, DiaryEntry
75 @entry = entries.where(:id => params[:id]).first
77 @title = t ".title", :user => params[:display_name], :title => @entry.title
78 @comments = can?(:unhidecomment, DiaryEntry) ? @entry.comments : @entry.visible_comments
80 @title = t "diary_entries.no_such_entry.title", :id => params[:id]
81 render :action => "no_such_entry", :status => :not_found
88 default_lang = current_user.preferences.where(:k => "diary.default_language").first
89 lang_code = default_lang ? default_lang.v : current_user.preferred_language
90 @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
92 render :action => "new"
97 @diary_entry = DiaryEntry.find(params[:id])
99 redirect_to diary_entry_path(@diary_entry.user, @diary_entry) if current_user != @diary_entry.user
102 rescue ActiveRecord::RecordNotFound
103 render :action => "no_such_entry", :status => :not_found
107 @title = t "diary_entries.new.title"
109 @diary_entry = DiaryEntry.new(entry_params)
110 @diary_entry.user = current_user
113 default_lang = current_user.preferences.where(:k => "diary.default_language").first
115 default_lang.v = @diary_entry.language_code
118 current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
121 # Subscribe user to diary comments
122 @diary_entry.subscriptions.create(:user => current_user)
124 redirect_to :action => "index", :display_name => current_user.display_name
126 render :action => "new"
131 @title = t "diary_entries.edit.title"
132 @diary_entry = DiaryEntry.find(params[:id])
134 if current_user != @diary_entry.user ||
135 (params[:diary_entry] && @diary_entry.update(entry_params))
136 redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
139 render :action => "edit"
141 rescue ActiveRecord::RecordNotFound
142 render :action => "no_such_entry", :status => :not_found
146 @entry = DiaryEntry.find(params[:id])
147 @comments = @entry.visible_comments
148 @diary_comment = @entry.comments.build(comment_params)
149 @diary_comment.user = current_user
150 if @diary_comment.save
152 # Notify current subscribers of the new comment
153 @entry.subscribers.visible.each do |user|
154 UserMailer.diary_comment_notification(@diary_comment, user).deliver_later if current_user != user
157 # Add the commenter to the subscribers if necessary
158 @entry.subscriptions.create(:user => current_user) unless @entry.subscribers.exists?(current_user.id)
160 redirect_to diary_entry_path(@entry.user, @entry)
162 render :action => "show"
164 rescue ActiveRecord::RecordNotFound
165 render :action => "no_such_entry", :status => :not_found
169 diary_entry = DiaryEntry.find(params[:id])
171 diary_entry.subscriptions.create(:user => current_user) unless 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 diary_entry = DiaryEntry.find(params[:id])
181 diary_entry.subscriptions.where(:user => current_user).delete_all if diary_entry.subscribers.exists?(current_user.id)
183 redirect_to diary_entry_path(diary_entry.user, diary_entry)
184 rescue ActiveRecord::RecordNotFound
185 render :action => "no_such_entry", :status => :not_found
189 if params[:display_name]
190 user = User.active.find_by(:display_name => params[:display_name])
193 @entries = user.diary_entries
194 @title = t("diary_entries.feed.user.title", :user => user.display_name)
195 @description = t("diary_entries.feed.user.description", :user => user.display_name)
196 @link = url_for :action => "index", :display_name => user.display_name, :host => Settings.server_url, :protocol => Settings.server_protocol
202 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
204 # Items can't be flagged as deleted in the RSS format.
205 # For the general feeds, allow a delay before publishing, to help spam fighting
206 @entries = @entries.where("created_at < :time", :time => Settings.diary_feed_delay.hours.ago)
209 @entries = @entries.where(:language_code => params[:language])
210 @title = t("diary_entries.feed.language.title", :language_name => Language.find(params[:language]).english_name)
211 @description = t("diary_entries.feed.language.description", :language_name => Language.find(params[:language]).english_name)
212 @link = url_for :action => "index", :language => params[:language], :host => Settings.server_url, :protocol => Settings.server_protocol
214 @title = t("diary_entries.feed.all.title")
215 @description = t("diary_entries.feed.all.description")
216 @link = url_for :action => "index", :host => Settings.server_url, :protocol => Settings.server_protocol
219 @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
223 entry = DiaryEntry.find(params[:id])
224 entry.update(:visible => false)
225 redirect_to :action => "index", :display_name => entry.user.display_name
229 entry = DiaryEntry.find(params[:id])
230 entry.update(:visible => true)
231 redirect_to :action => "index", :display_name => entry.user.display_name
235 comment = DiaryComment.find(params[:comment])
236 comment.update(:visible => false)
237 redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
241 comment = DiaryComment.find(params[:comment])
242 comment.update(:visible => true)
243 redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
247 @title = t ".title", :user => @user.display_name
249 conditions = { :user_id => @user }
251 conditions[:visible] = true unless can? :unhidecomment, DiaryEntry
253 @comment_pages, @comments = paginate(:diary_comments,
254 :conditions => conditions,
255 :order => "created_at DESC",
257 @page = (params[:page] || 1).to_i
263 # return permitted diary entry parameters
265 params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
266 rescue ActionController::ParameterMissing
267 ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
271 # return permitted diary comment parameters
273 params.require(:diary_comment).permit(:body)
277 # decide on a location for the diary entry map
279 if @diary_entry.latitude && @diary_entry.longitude
280 @lon = @diary_entry.longitude
281 @lat = @diary_entry.latitude
283 elsif !current_user.home_location?
284 @lon = params[:lon] || -0.1
285 @lat = params[:lat] || 51.5
286 @zoom = params[:zoom] || 4
288 @lon = current_user.home_lon
289 @lat = current_user.home_lat