1 class DiaryEntriesController < ApplicationController
3 include PaginationMethods
5 layout "site", :except => :rss
7 before_action :authorize_web
8 before_action :set_locale
9 before_action :check_database_readable
13 before_action :lookup_user, :only => [:show, :comments]
14 before_action :check_database_writable, :only => [:new, :create, :edit, :update, :comment, :hide, :hidecomment, :subscribe, :unsubscribe]
15 before_action :allow_thirdparty_images, :only => [:new, :create, :edit, :update, :index, :show, :comments]
18 if params[:display_name]
19 @user = User.active.find_by(:display_name => params[:display_name])
22 @title = t ".user_title", :user => @user.display_name
23 entries = @user.diary_entries
25 render_unknown_user params[:display_name]
28 elsif params[:friends]
30 @title = t ".title_friends"
31 entries = DiaryEntry.where(:user => current_user.friends)
38 @title = t ".title_nearby"
39 entries = DiaryEntry.where(:user => current_user.nearby)
45 entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
48 @title = t ".in_language_title", :language => Language.find(params[:language]).english_name
49 entries = entries.where(:language_code => params[:language])
51 candidate_codes = preferred_languages.flat_map(&:candidates).uniq.map(&:to_s)
52 @languages = Language.where(:code => candidate_codes).in_order_of(:code, candidate_codes)
57 entries = entries.visible unless can? :unhide, DiaryEntry
59 @params = params.permit(:display_name, :friends, :nearby, :language)
61 @entries, @newer_entries_id, @older_entries_id = get_page_items(entries, [:user, :language])
65 entries = @user.diary_entries
66 entries = entries.visible unless can? :unhide, DiaryEntry
67 @entry = entries.find_by(:id => params[:id])
69 @title = t ".title", :user => params[:display_name], :title => @entry.title
70 @comments = can?(:unhidecomment, DiaryEntry) ? @entry.comments : @entry.visible_comments
72 @title = t "diary_entries.no_such_entry.title", :id => params[:id]
73 render :action => "no_such_entry", :status => :not_found
80 default_lang = current_user.preferences.find_by(:k => "diary.default_language")
81 lang_code = default_lang ? default_lang.v : current_user.preferred_language
82 @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
84 render :action => "new"
89 @diary_entry = DiaryEntry.find(params[:id])
91 redirect_to diary_entry_path(@diary_entry.user, @diary_entry) if current_user != @diary_entry.user
94 rescue ActiveRecord::RecordNotFound
95 render :action => "no_such_entry", :status => :not_found
99 @title = t "diary_entries.new.title"
101 @diary_entry = DiaryEntry.new(entry_params)
102 @diary_entry.user = current_user
105 default_lang = current_user.preferences.find_by(:k => "diary.default_language")
107 default_lang.v = @diary_entry.language_code
110 current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
113 # Subscribe user to diary comments
114 @diary_entry.subscriptions.create(:user => current_user)
116 redirect_to :action => "index", :display_name => current_user.display_name
118 render :action => "new"
123 @title = t "diary_entries.edit.title"
124 @diary_entry = DiaryEntry.find(params[:id])
126 if current_user != @diary_entry.user ||
127 (params[:diary_entry] && @diary_entry.update(entry_params))
128 redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
131 render :action => "edit"
133 rescue ActiveRecord::RecordNotFound
134 render :action => "no_such_entry", :status => :not_found
138 @entry = DiaryEntry.find(params[:id])
139 @comments = @entry.visible_comments
140 @diary_comment = @entry.comments.build(comment_params)
141 @diary_comment.user = current_user
142 if @diary_comment.save
144 # Notify current subscribers of the new comment
145 @entry.subscribers.visible.each do |user|
146 UserMailer.diary_comment_notification(@diary_comment, user).deliver_later if current_user != user
149 # Add the commenter to the subscribers if necessary
150 @entry.subscriptions.create(:user => current_user) unless @entry.subscribers.exists?(current_user.id)
152 redirect_to diary_entry_path(@entry.user, @entry)
154 render :action => "show"
156 rescue ActiveRecord::RecordNotFound
157 render :action => "no_such_entry", :status => :not_found
161 @diary_entry = DiaryEntry.find(params[:id])
164 @diary_entry.subscriptions.create(:user => current_user) unless @diary_entry.subscribers.exists?(current_user.id)
166 redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
168 rescue ActiveRecord::RecordNotFound
169 render :action => "no_such_entry", :status => :not_found
173 @diary_entry = DiaryEntry.find(params[:id])
176 @diary_entry.subscriptions.where(:user => current_user).delete_all if @diary_entry.subscribers.exists?(current_user.id)
178 redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
180 rescue ActiveRecord::RecordNotFound
181 render :action => "no_such_entry", :status => :not_found
185 if params[:display_name]
186 user = User.active.find_by(:display_name => params[:display_name])
189 @entries = user.diary_entries
190 @title = t("diary_entries.feed.user.title", :user => user.display_name)
191 @description = t("diary_entries.feed.user.description", :user => user.display_name)
192 @link = url_for :action => "index", :display_name => user.display_name, :host => Settings.server_url, :protocol => Settings.server_protocol
198 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
200 # Items can't be flagged as deleted in the RSS format.
201 # For the general feeds, allow a delay before publishing, to help spam fighting
202 @entries = @entries.where("created_at < :time", :time => Settings.diary_feed_delay.hours.ago)
205 @entries = @entries.where(:language_code => params[:language])
206 @title = t("diary_entries.feed.language.title", :language_name => Language.find(params[:language]).english_name)
207 @description = t("diary_entries.feed.language.description", :language_name => Language.find(params[:language]).english_name)
208 @link = url_for :action => "index", :language => params[:language], :host => Settings.server_url, :protocol => Settings.server_protocol
210 @title = t("diary_entries.feed.all.title")
211 @description = t("diary_entries.feed.all.description")
212 @link = url_for :action => "index", :host => Settings.server_url, :protocol => Settings.server_protocol
215 @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
219 entry = DiaryEntry.find(params[:id])
220 entry.update(:visible => false)
221 redirect_to :action => "index", :display_name => entry.user.display_name
225 entry = DiaryEntry.find(params[:id])
226 entry.update(:visible => true)
227 redirect_to :action => "index", :display_name => entry.user.display_name
231 comment = DiaryComment.find(params[:comment])
232 comment.update(:visible => false)
233 redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
237 comment = DiaryComment.find(params[:comment])
238 comment.update(:visible => true)
239 redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
243 @title = t ".title", :user => @user.display_name
245 comments = DiaryComment.where(:user => @user)
246 comments = comments.visible unless can? :unhidecomment, DiaryEntry
248 @params = params.permit(:display_name, :before, :after)
250 @comments, @newer_comments_id, @older_comments_id = get_page_items(comments, [:user])
256 # return permitted diary entry parameters
258 params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
259 rescue ActionController::ParameterMissing
260 ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
264 # return permitted diary comment parameters
266 params.require(:diary_comment).permit(:body)
270 # decide on a location for the diary entry map
272 if @diary_entry.latitude && @diary_entry.longitude
273 @lon = @diary_entry.longitude
274 @lat = @diary_entry.latitude
276 elsif !current_user.home_location?
277 @lon = params[:lon] || -0.1
278 @lat = params[:lat] || 51.5
279 @zoom = params[:zoom] || 4
281 @lon = current_user.home_lon
282 @lat = current_user.home_lat