1 class DiaryEntryController < ApplicationController
2 layout "site", :except => :rss
4 before_action :authorize_web
5 before_action :set_locale
6 before_action :require_user, :only => [:new, :edit, :comment, :hide, :hidecomment, :subscribe, :unsubscribe]
7 before_action :lookup_this_user, :only => [:view, :comments]
8 before_action :check_database_readable
9 before_action :check_database_writable, :only => [:new, :edit, :comment, :hide, :hidecomment, :subscribe, :unsubscribe]
10 before_action :require_administrator, :only => [:hide, :hidecomment]
13 @title = t "diary_entry.new.title"
15 if params[:diary_entry]
16 @diary_entry = DiaryEntry.new(entry_params)
17 @diary_entry.user = @user
20 default_lang = @user.preferences.where(:k => "diary.default_language").first
22 default_lang.v = @diary_entry.language_code
25 @user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
28 # Subscribe user to diary comments
29 @diary_entry.subscriptions.create(:user => @user)
31 redirect_to :action => "list", :display_name => @user.display_name
33 render :action => "edit"
36 default_lang = @user.preferences.where(:k => "diary.default_language").first
37 lang_code = default_lang ? default_lang.v : @user.preferred_language
38 @diary_entry = DiaryEntry.new(:language_code => lang_code)
40 render :action => "edit"
45 @title = t "diary_entry.edit.title"
46 @diary_entry = DiaryEntry.find(params[:id])
48 if @user != @diary_entry.user
49 redirect_to :action => "view", :id => params[:id]
50 elsif params[:diary_entry] && @diary_entry.update_attributes(entry_params)
51 redirect_to :action => "view", :id => params[:id]
55 rescue ActiveRecord::RecordNotFound
56 render :action => "no_such_entry", :status => :not_found
60 @entry = DiaryEntry.find(params[:id])
61 @diary_comment = @entry.comments.build(comment_params)
62 @diary_comment.user = @user
63 if @diary_comment.save
65 # Notify current subscribers of the new comment
66 @entry.subscribers.visible.each do |user|
68 Notifier.diary_comment_notification(@diary_comment, user).deliver_now
72 # Add the commenter to the subscribers if necessary
73 @entry.subscriptions.create(:user => @user) unless @entry.subscribers.exists?(@user.id)
75 redirect_to :action => "view", :display_name => @entry.user.display_name, :id => @entry.id
77 render :action => "view"
79 rescue ActiveRecord::RecordNotFound
80 render :action => "no_such_entry", :status => :not_found
84 diary_entry = DiaryEntry.find(params[:id])
86 diary_entry.subscriptions.create(:user => @user) unless diary_entry.subscribers.exists?(@user.id)
88 redirect_to :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
89 rescue ActiveRecord::RecordNotFound
90 render :action => "no_such_entry", :status => :not_found
94 diary_entry = DiaryEntry.find(params[:id])
96 diary_entry.subscriptions.where(:user => @user).delete_all if diary_entry.subscribers.exists?(@user.id)
98 redirect_to :action => "view", :display_name => diary_entry.user.display_name, :id => diary_entry.id
99 rescue ActiveRecord::RecordNotFound
100 render :action => "no_such_entry", :status => :not_found
104 if params[:display_name]
105 @this_user = User.active.find_by(:display_name => params[:display_name])
108 @title = t "diary_entry.list.user_title", :user => @this_user.display_name
109 @entries = @this_user.diary_entries
111 render_unknown_user params[:display_name]
114 elsif params[:friends]
116 @title = t "diary_entry.list.title_friends"
117 @entries = DiaryEntry.where(:user_id => @user.friend_users)
122 elsif params[:nearby]
124 @title = t "diary_entry.list.title_nearby"
125 @entries = DiaryEntry.where(:user_id => @user.nearby)
131 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w(active confirmed) })
134 @title = t "diary_entry.list.in_language_title", :language => Language.find(params[:language]).english_name
135 @entries = @entries.where(:language_code => params[:language])
137 @title = t "diary_entry.list.title"
141 @page = (params[:page] || 1).to_i
144 @entries = @entries.visible
145 @entries = @entries.order("created_at DESC")
146 @entries = @entries.offset((@page - 1) * @page_size)
147 @entries = @entries.limit(@page_size)
148 @entries = @entries.includes(:user, :language)
152 if params[:display_name]
153 user = User.active.find_by(:display_name => params[:display_name])
156 @entries = user.diary_entries
157 @title = I18n.t("diary_entry.feed.user.title", :user => user.display_name)
158 @description = I18n.t("diary_entry.feed.user.description", :user => user.display_name)
159 @link = "http://#{SERVER_URL}/user/#{user.display_name}/diary"
161 render :text => "", :status => :not_found
165 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w(active confirmed) })
168 @entries = @entries.where(:language_code => params[:language])
169 @title = I18n.t("diary_entry.feed.language.title", :language_name => Language.find(params[:language]).english_name)
170 @description = I18n.t("diary_entry.feed.language.description", :language_name => Language.find(params[:language]).english_name)
171 @link = "http://#{SERVER_URL}/diary/#{params[:language]}"
173 @title = I18n.t("diary_entry.feed.all.title")
174 @description = I18n.t("diary_entry.feed.all.description")
175 @link = "http://#{SERVER_URL}/diary"
179 @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
183 @entry = @this_user.diary_entries.visible.where(:id => params[:id]).first
185 @title = t "diary_entry.view.title", :user => params[:display_name], :title => @entry.title
187 @title = t "diary_entry.no_such_entry.title", :id => params[:id]
188 render :action => "no_such_entry", :status => :not_found
193 entry = DiaryEntry.find(params[:id])
194 entry.update_attributes(:visible => false)
195 redirect_to :action => "list", :display_name => entry.user.display_name
199 comment = DiaryComment.find(params[:comment])
200 comment.update_attributes(:visible => false)
201 redirect_to :action => "view", :display_name => comment.diary_entry.user.display_name, :id => comment.diary_entry.id
205 @comment_pages, @comments = paginate(:diary_comments,
207 :user_id => @this_user,
210 :order => "created_at DESC",
212 @page = (params[:page] || 1).to_i
218 # return permitted diary entry parameters
220 params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
224 # return permitted diary comment parameters
226 params.require(:diary_comment).permit(:body)
230 # require that the user is a administrator, or fill out a helpful error message
231 # and return them to the user page.
232 def require_administrator
233 unless @user.administrator?
234 flash[:error] = t("user.filter.not_an_administrator")
235 redirect_to :action => "view"
240 # decide on a location for the diary entry map
242 if @diary_entry.latitude && @diary_entry.longitude
243 @lon = @diary_entry.longitude
244 @lat = @diary_entry.latitude
246 elsif @user.home_lat.nil? || @user.home_lon.nil?
247 @lon = params[:lon] || -0.1
248 @lat = params[:lat] || 51.5
249 @zoom = params[:zoom] || 4
251 @lon = @user.home_lon
252 @lat = @user.home_lat