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
14 before_action :check_database_writable, :only => [:new, :create, :edit, :update, :hide, :unhide, :subscribe, :unsubscribe]
16 allow_thirdparty_images :only => [:new, :create, :edit, :update, :index, :show]
19 if params[:display_name]
20 @user = User.active.find_by(:display_name => params[:display_name])
23 @title = t ".user_title", :user => @user.display_name
24 entries = @user.diary_entries
26 render_unknown_user params[:display_name]
29 elsif params[:friends]
31 @title = t ".title_friends"
32 entries = DiaryEntry.where(:user => current_user.friends)
39 @title = t ".title_nearby"
40 entries = DiaryEntry.where(:user => current_user.nearby)
46 entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
49 @title = t ".in_language_title", :language => Language.find(params[:language]).english_name
50 entries = entries.where(:language_code => params[:language])
52 candidate_codes = preferred_languages.flat_map(&:candidates).uniq.map(&:to_s)
53 @languages = Language.where(:code => candidate_codes).in_order_of(:code, candidate_codes)
58 entries = entries.visible unless can? :unhide, DiaryEntry
60 @params = params.permit(:display_name, :friends, :nearby, :language)
62 @entries, @newer_entries_id, @older_entries_id = get_page_items(entries, :includes => [:user, :language])
66 entries = @user.diary_entries
67 entries = entries.visible unless can? :unhide, DiaryEntry
68 @entry = entries.find_by(:id => params[:id])
70 @title = t ".title", :user => params[:display_name], :title => @entry.title
71 @opengraph_properties = {
72 "og:image" => @entry.body.image,
73 "og:image:alt" => @entry.body.image_alt,
74 "og:description" => @entry.body.description
76 @comments = can?(:unhide, DiaryComment) ? @entry.comments : @entry.visible_comments
78 @title = t "diary_entries.no_such_entry.title", :id => params[:id]
79 render :action => "no_such_entry", :status => :not_found
86 default_lang = current_user.preferences.find_by(:k => "diary.default_language")
87 lang_code = default_lang ? default_lang.v : current_user.preferred_language
88 @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
90 render :action => "new"
95 @diary_entry = DiaryEntry.find(params[:id])
97 redirect_to diary_entry_path(@diary_entry.user, @diary_entry) if current_user != @diary_entry.user
100 rescue ActiveRecord::RecordNotFound
101 render :action => "no_such_entry", :status => :not_found
105 @title = t "diary_entries.new.title"
107 @diary_entry = DiaryEntry.new(entry_params)
108 @diary_entry.user = current_user
111 default_lang = current_user.preferences.find_by(:k => "diary.default_language")
113 default_lang.v = @diary_entry.language_code
116 current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
119 # Subscribe user to diary comments
120 @diary_entry.subscriptions.create(:user => current_user)
122 redirect_to :action => "index", :display_name => current_user.display_name
124 render :action => "new"
129 @title = t "diary_entries.edit.title"
130 @diary_entry = DiaryEntry.find(params[:id])
132 if cannot?(:update, @diary_entry) ||
133 (params[:diary_entry] && @diary_entry.update(entry_params))
134 redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
137 render :action => "edit"
139 rescue ActiveRecord::RecordNotFound
140 render :action => "no_such_entry", :status => :not_found
144 @diary_entry = DiaryEntry.find(params[:id])
147 @diary_entry.subscriptions.create(:user => current_user) unless @diary_entry.subscribers.exists?(current_user.id)
149 redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
151 rescue ActiveRecord::RecordNotFound
152 render :action => "no_such_entry", :status => :not_found
156 @diary_entry = DiaryEntry.find(params[:id])
159 @diary_entry.subscriptions.where(:user => current_user).delete_all if @diary_entry.subscribers.exists?(current_user.id)
161 redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
163 rescue ActiveRecord::RecordNotFound
164 render :action => "no_such_entry", :status => :not_found
168 if params[:display_name]
169 user = User.active.find_by(:display_name => params[:display_name])
172 @entries = user.diary_entries
173 @title = t("diary_entries.feed.user.title", :user => user.display_name)
174 @description = t("diary_entries.feed.user.description", :user => user.display_name)
175 @link = url_for :action => "index", :display_name => user.display_name, :host => Settings.server_url, :protocol => Settings.server_protocol
181 @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
183 # Items can't be flagged as deleted in the RSS format.
184 # For the general feeds, allow a delay before publishing, to help spam fighting
185 @entries = @entries.where("created_at < :time", :time => Settings.diary_feed_delay.hours.ago)
188 @entries = @entries.where(:language_code => params[:language])
189 @title = t("diary_entries.feed.language.title", :language_name => Language.find(params[:language]).english_name)
190 @description = t("diary_entries.feed.language.description", :language_name => Language.find(params[:language]).english_name)
191 @link = url_for :action => "index", :language => params[:language], :host => Settings.server_url, :protocol => Settings.server_protocol
193 @title = t("diary_entries.feed.all.title")
194 @description = t("diary_entries.feed.all.description")
195 @link = url_for :action => "index", :host => Settings.server_url, :protocol => Settings.server_protocol
198 @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
202 entry = DiaryEntry.find(params[:id])
203 entry.update(:visible => false)
204 redirect_to :action => "index", :display_name => entry.user.display_name
208 entry = DiaryEntry.find(params[:id])
209 entry.update(:visible => true)
210 redirect_to :action => "index", :display_name => entry.user.display_name
216 # return permitted diary entry parameters
218 params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
219 rescue ActionController::ParameterMissing
220 ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
224 # decide on a location for the diary entry map
226 if @diary_entry.latitude && @diary_entry.longitude
227 @lon = @diary_entry.longitude
228 @lat = @diary_entry.latitude
230 elsif !current_user.home_location?
231 @lon = params[:lon] || -0.1
232 @lat = params[:lat] || 51.5
233 @zoom = params[:zoom] || 4
235 @lon = current_user.home_lon
236 @lat = current_user.home_lat