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 @title = t "diary_entries.new.title"
17 default_lang = current_user.preferences.where(:k => "diary.default_language").first
18 lang_code = default_lang ? default_lang.v : current_user.preferred_language
19 @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
21 render :action => "new"
25 @title = t "diary_entries.new.title"
27 @diary_entry = DiaryEntry.new(entry_params)
28 @diary_entry.user = current_user
31 default_lang = current_user.preferences.where(:k => "diary.default_language").first
33 default_lang.v = @diary_entry.language_code
36 current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
39 # Subscribe user to diary comments
40 @diary_entry.subscriptions.create(:user => current_user)
42 redirect_to :action => "index", :display_name => current_user.display_name
44 render :action => "new"
49 @title = t "diary_entries.edit.title"
50 @diary_entry = DiaryEntry.find(params[:id])
52 redirect_to diary_entry_path(@diary_entry.user, @diary_entry) if current_user != @diary_entry.user
55 rescue ActiveRecord::RecordNotFound
56 render :action => "no_such_entry", :status => :not_found
60 @title = t "diary_entries.edit.title"
61 @diary_entry = DiaryEntry.find(params[:id])
63 if current_user != @diary_entry.user
64 redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
65 elsif params[:diary_entry] && @diary_entry.update(entry_params)
66 redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
69 render :action => "edit"
71 rescue ActiveRecord::RecordNotFound
72 render :action => "no_such_entry", :status => :not_found
76 @entry = DiaryEntry.find(params[:id])
77 @comments = @entry.visible_comments
78 @diary_comment = @entry.comments.build(comment_params)
79 @diary_comment.user = current_user
80 if @diary_comment.save
82 # Notify current subscribers of the new comment
83 @entry.subscribers.visible.each do |user|
84 Notifier.diary_comment_notification(@diary_comment, user).deliver_later if current_user != user
87 # Add the commenter to the subscribers if necessary
88 @entry.subscriptions.create(:user => current_user) unless @entry.subscribers.exists?(current_user.id)
90 redirect_to diary_entry_path(@entry.user, @entry)
92 render :action => "show"
94 rescue ActiveRecord::RecordNotFound
95 render :action => "no_such_entry", :status => :not_found
99 diary_entry = DiaryEntry.find(params[:id])
101 diary_entry.subscriptions.create(:user => current_user) unless diary_entry.subscribers.exists?(current_user.id)
103 redirect_to diary_entry_path(diary_entry.user, diary_entry)
104 rescue ActiveRecord::RecordNotFound
105 render :action => "no_such_entry", :status => :not_found
109 diary_entry = DiaryEntry.find(params[:id])
111 diary_entry.subscriptions.where(:user => current_user).delete_all if diary_entry.subscribers.exists?(current_user.id)
113 redirect_to diary_entry_path(diary_entry.user, diary_entry)
114 rescue ActiveRecord::RecordNotFound
115 render :action => "no_such_entry", :status => :not_found
119 if params[:display_name]
120 @user = User.active.find_by(:display_name => params[:display_name])
123 @title = t "diary_entries.index.user_title", :user => @user.display_name
124 @entries = @user.diary_entries
126 render_unknown_user params[:display_name]
129 elsif params[:friends]
131 @title = t "diary_entries.index.title_friends"
132 @entries = DiaryEntry.where(:user_id => current_user.friend_users)
137 elsif params[:nearby]
139 @title = t "diary_entries.index.title_nearby"
140 @entries = DiaryEntry.where(:user_id => current_user.nearby)
146 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
149 @title = t "diary_entries.index.in_language_title", :language => Language.find(params[:language]).english_name
150 @entries = @entries.where(:language_code => params[:language])
152 @title = t "diary_entries.index.title"
156 @params = params.permit(:display_name, :friends, :nearby, :language)
158 @page = (params[:page] || 1).to_i
161 @entries = @entries.visible unless current_user&.administrator?
162 @entries = @entries.order("created_at DESC")
163 @entries = @entries.offset((@page - 1) * @page_size)
164 @entries = @entries.limit(@page_size)
165 @entries = @entries.includes(:user, :language)
169 if params[:display_name]
170 user = User.active.find_by(:display_name => params[:display_name])
173 @entries = user.diary_entries
174 @title = t("diary_entries.feed.user.title", :user => user.display_name)
175 @description = t("diary_entries.feed.user.description", :user => user.display_name)
176 @link = url_for :action => "index", :display_name => user.display_name, :host => Settings.server_url, :protocol => Settings.server_protocol
182 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
184 # Items can't be flagged as deleted in the RSS format.
185 # For the general feeds, allow a delay before publishing, to help spam fighting
186 @entries = @entries.where("created_at < :time", :time => Settings.diary_feed_delay.hours.ago)
189 @entries = @entries.where(:language_code => params[:language])
190 @title = t("diary_entries.feed.language.title", :language_name => Language.find(params[:language]).english_name)
191 @description = t("diary_entries.feed.language.description", :language_name => Language.find(params[:language]).english_name)
192 @link = url_for :action => "index", :language => params[:language], :host => Settings.server_url, :protocol => Settings.server_protocol
194 @title = t("diary_entries.feed.all.title")
195 @description = t("diary_entries.feed.all.description")
196 @link = url_for :action => "index", :host => Settings.server_url, :protocol => Settings.server_protocol
199 @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
203 @entry = @user.diary_entries.visible.where(:id => params[:id]).first
205 @title = t "diary_entries.show.title", :user => params[:display_name], :title => @entry.title
206 @comments = current_user&.administrator? ? @entry.comments : @entry.visible_comments
208 @title = t "diary_entries.no_such_entry.title", :id => params[:id]
209 render :action => "no_such_entry", :status => :not_found
214 entry = DiaryEntry.find(params[:id])
215 entry.update(:visible => false)
216 redirect_to :action => "index", :display_name => entry.user.display_name
220 entry = DiaryEntry.find(params[:id])
221 entry.update(:visible => true)
222 redirect_to :action => "index", :display_name => entry.user.display_name
226 comment = DiaryComment.find(params[:comment])
227 comment.update(:visible => false)
228 redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
232 comment = DiaryComment.find(params[:comment])
233 comment.update(:visible => true)
234 redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
238 @comment_pages, @comments = paginate(:diary_comments,
243 :order => "created_at DESC",
245 @page = (params[:page] || 1).to_i
251 # return permitted diary entry parameters
253 params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
254 rescue ActionController::ParameterMissing
255 ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
259 # return permitted diary comment parameters
261 params.require(:diary_comment).permit(:body)
265 # decide on a location for the diary entry map
267 if @diary_entry.latitude && @diary_entry.longitude
268 @lon = @diary_entry.longitude
269 @lat = @diary_entry.latitude
271 elsif current_user.home_lat.nil? || current_user.home_lon.nil?
272 @lon = params[:lon] || -0.1
273 @lat = params[:lat] || 51.5
274 @zoom = params[:zoom] || 4
276 @lon = current_user.home_lon
277 @lat = current_user.home_lat