]> git.openstreetmap.org Git - rails.git/blob - app/controllers/diary_entries_controller.rb
1f47e25880e38f74149fa48b3036aa6ae423958e
[rails.git] / app / controllers / diary_entries_controller.rb
1 class DiaryEntriesController < ApplicationController
2   include UserMethods
3   include PaginationMethods
4
5   layout "site", :except => :rss
6
7   before_action :authorize_web
8   before_action :set_locale
9   before_action :check_database_readable
10
11   authorize_resource
12
13   before_action :lookup_user, :only => :show
14   before_action :check_database_writable, :only => [:new, :create, :edit, :update, :hide, :unhide, :subscribe, :unsubscribe]
15
16   allow_thirdparty_images :only => [:new, :create, :edit, :update, :index, :show]
17
18   def index
19     if params[:display_name]
20       @user = User.active.find_by(:display_name => params[:display_name])
21
22       if @user
23         @title = t ".user_title", :user => @user.display_name
24         entries = @user.diary_entries
25       else
26         render_unknown_user params[:display_name]
27         return
28       end
29     elsif params[:friends]
30       if current_user
31         @title = t ".title_friends"
32         entries = DiaryEntry.where(:user => current_user.friends)
33       else
34         require_user
35         return
36       end
37     elsif params[:nearby]
38       if current_user
39         @title = t ".title_nearby"
40         entries = DiaryEntry.where(:user => current_user.nearby)
41       else
42         require_user
43         return
44       end
45     else
46       entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
47
48       if params[:language]
49         @title = t ".in_language_title", :language => Language.find(params[:language]).english_name
50         entries = entries.where(:language_code => params[:language])
51       else
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)
54         @title = t ".title"
55       end
56     end
57
58     entries = entries.visible unless can? :unhide, DiaryEntry
59
60     @params = params.permit(:display_name, :friends, :nearby, :language)
61
62     @entries, @newer_entries_id, @older_entries_id = get_page_items(entries, :includes => [:user, :language])
63   end
64
65   def show
66     entries = @user.diary_entries
67     entries = entries.visible unless can? :unhide, DiaryEntry
68     @entry = entries.find_by(:id => params[:id])
69     if @entry
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       }
75       @comments = can?(:unhide, DiaryComment) ? @entry.comments : @entry.visible_comments
76     else
77       @title = t "diary_entries.no_such_entry.title", :id => params[:id]
78       render :action => "no_such_entry", :status => :not_found
79     end
80   end
81
82   def new
83     @title = t ".title"
84
85     default_lang = current_user.preferences.find_by(:k => "diary.default_language")
86     lang_code = default_lang ? default_lang.v : current_user.preferred_language
87     @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
88     set_map_location
89     render :action => "new"
90   end
91
92   def edit
93     @title = t ".title"
94     @diary_entry = DiaryEntry.find(params[:id])
95
96     redirect_to diary_entry_path(@diary_entry.user, @diary_entry) if current_user != @diary_entry.user
97
98     set_map_location
99   rescue ActiveRecord::RecordNotFound
100     render :action => "no_such_entry", :status => :not_found
101   end
102
103   def create
104     @title = t "diary_entries.new.title"
105
106     @diary_entry = DiaryEntry.new(entry_params)
107     @diary_entry.user = current_user
108
109     if @diary_entry.save
110       default_lang = current_user.preferences.find_by(:k => "diary.default_language")
111       if default_lang
112         default_lang.v = @diary_entry.language_code
113         default_lang.save!
114       else
115         current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
116       end
117
118       # Subscribe user to diary comments
119       @diary_entry.subscriptions.create(:user => current_user)
120
121       redirect_to :action => "index", :display_name => current_user.display_name
122     else
123       render :action => "new"
124     end
125   end
126
127   def update
128     @title = t "diary_entries.edit.title"
129     @diary_entry = DiaryEntry.find(params[:id])
130
131     if cannot?(:update, @diary_entry) ||
132        (params[:diary_entry] && @diary_entry.update(entry_params))
133       redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
134     else
135       set_map_location
136       render :action => "edit"
137     end
138   rescue ActiveRecord::RecordNotFound
139     render :action => "no_such_entry", :status => :not_found
140   end
141
142   def subscribe
143     @diary_entry = DiaryEntry.find(params[:id])
144
145     if request.post?
146       @diary_entry.subscriptions.create(:user => current_user) unless @diary_entry.subscribers.exists?(current_user.id)
147
148       redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
149     end
150   rescue ActiveRecord::RecordNotFound
151     render :action => "no_such_entry", :status => :not_found
152   end
153
154   def unsubscribe
155     @diary_entry = DiaryEntry.find(params[:id])
156
157     if request.post?
158       @diary_entry.subscriptions.where(:user => current_user).delete_all if @diary_entry.subscribers.exists?(current_user.id)
159
160       redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
161     end
162   rescue ActiveRecord::RecordNotFound
163     render :action => "no_such_entry", :status => :not_found
164   end
165
166   def rss
167     if params[:display_name]
168       user = User.active.find_by(:display_name => params[:display_name])
169
170       if user
171         @entries = user.diary_entries
172         @title = t("diary_entries.feed.user.title", :user => user.display_name)
173         @description = t("diary_entries.feed.user.description", :user => user.display_name)
174         @link = url_for :action => "index", :display_name => user.display_name, :host => Settings.server_url, :protocol => Settings.server_protocol
175       else
176         head :not_found
177         return
178       end
179     else
180       @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
181
182       # Items can't be flagged as deleted in the RSS format.
183       # For the general feeds, allow a delay before publishing, to help spam fighting
184       @entries = @entries.where("created_at < :time", :time => Settings.diary_feed_delay.hours.ago)
185
186       if params[:language]
187         @entries = @entries.where(:language_code => params[:language])
188         @title = t("diary_entries.feed.language.title", :language_name => Language.find(params[:language]).english_name)
189         @description = t("diary_entries.feed.language.description", :language_name => Language.find(params[:language]).english_name)
190         @link = url_for :action => "index", :language => params[:language], :host => Settings.server_url, :protocol => Settings.server_protocol
191       else
192         @title = t("diary_entries.feed.all.title")
193         @description = t("diary_entries.feed.all.description")
194         @link = url_for :action => "index", :host => Settings.server_url, :protocol => Settings.server_protocol
195       end
196     end
197     @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
198   end
199
200   def hide
201     entry = DiaryEntry.find(params[:id])
202     entry.update(:visible => false)
203     redirect_to :action => "index", :display_name => entry.user.display_name
204   end
205
206   def unhide
207     entry = DiaryEntry.find(params[:id])
208     entry.update(:visible => true)
209     redirect_to :action => "index", :display_name => entry.user.display_name
210   end
211
212   private
213
214   ##
215   # return permitted diary entry parameters
216   def entry_params
217     params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
218   rescue ActionController::ParameterMissing
219     ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
220   end
221
222   ##
223   # decide on a location for the diary entry map
224   def set_map_location
225     if @diary_entry.latitude && @diary_entry.longitude
226       @lon = @diary_entry.longitude
227       @lat = @diary_entry.latitude
228       @zoom = 12
229     elsif !current_user.home_location?
230       @lon = params[:lon] || -0.1
231       @lat = params[:lat] || 51.5
232       @zoom = params[:zoom] || 4
233     else
234       @lon = current_user.home_lon
235       @lat = current_user.home_lat
236       @zoom = 12
237     end
238   end
239 end