]> git.openstreetmap.org Git - rails.git/blob - app/controllers/diary_entries_controller.rb
Merge remote-tracking branch 'upstream/pull/5078'
[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         "og:description" => @entry.body.description,
75         "article:published_time" => @entry.created_at.xmlschema
76       }
77       @comments = can?(:unhide, DiaryComment) ? @entry.comments : @entry.visible_comments
78     else
79       @title = t "diary_entries.no_such_entry.title", :id => params[:id]
80       render :action => "no_such_entry", :status => :not_found
81     end
82   end
83
84   def new
85     @title = t ".title"
86
87     default_lang = current_user.preferences.find_by(:k => "diary.default_language")
88     lang_code = default_lang ? default_lang.v : current_user.preferred_language
89     @diary_entry = DiaryEntry.new(entry_params.merge(:language_code => lang_code))
90     set_map_location
91     render :action => "new"
92   end
93
94   def edit
95     @title = t ".title"
96     @diary_entry = DiaryEntry.find(params[:id])
97
98     redirect_to diary_entry_path(@diary_entry.user, @diary_entry) if current_user != @diary_entry.user
99
100     set_map_location
101   rescue ActiveRecord::RecordNotFound
102     render :action => "no_such_entry", :status => :not_found
103   end
104
105   def create
106     @title = t "diary_entries.new.title"
107
108     @diary_entry = DiaryEntry.new(entry_params)
109     @diary_entry.user = current_user
110
111     if @diary_entry.save
112       default_lang = current_user.preferences.find_by(:k => "diary.default_language")
113       if default_lang
114         default_lang.v = @diary_entry.language_code
115         default_lang.save!
116       else
117         current_user.preferences.create(:k => "diary.default_language", :v => @diary_entry.language_code)
118       end
119
120       # Subscribe user to diary comments
121       @diary_entry.subscriptions.create(:user => current_user)
122
123       redirect_to :action => "index", :display_name => current_user.display_name
124     else
125       render :action => "new"
126     end
127   end
128
129   def update
130     @title = t "diary_entries.edit.title"
131     @diary_entry = DiaryEntry.find(params[:id])
132
133     if cannot?(:update, @diary_entry) ||
134        (params[:diary_entry] && @diary_entry.update(entry_params))
135       redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
136     else
137       set_map_location
138       render :action => "edit"
139     end
140   rescue ActiveRecord::RecordNotFound
141     render :action => "no_such_entry", :status => :not_found
142   end
143
144   def subscribe
145     @diary_entry = DiaryEntry.find(params[:id])
146
147     if request.post?
148       @diary_entry.subscriptions.create(:user => current_user) unless @diary_entry.subscribers.exists?(current_user.id)
149
150       redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
151     end
152   rescue ActiveRecord::RecordNotFound
153     render :action => "no_such_entry", :status => :not_found
154   end
155
156   def unsubscribe
157     @diary_entry = DiaryEntry.find(params[:id])
158
159     if request.post?
160       @diary_entry.subscriptions.where(:user => current_user).delete_all if @diary_entry.subscribers.exists?(current_user.id)
161
162       redirect_to diary_entry_path(@diary_entry.user, @diary_entry)
163     end
164   rescue ActiveRecord::RecordNotFound
165     render :action => "no_such_entry", :status => :not_found
166   end
167
168   def rss
169     if params[:display_name]
170       user = User.active.find_by(:display_name => params[:display_name])
171
172       if user
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
177       else
178         head :not_found
179         return
180       end
181     else
182       @entries = DiaryEntry.joins(:user).where(:users => { :status => %w[active confirmed] })
183
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)
187
188       if params[:language]
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
193       else
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
197       end
198     end
199     @entries = @entries.visible.includes(:user).order("created_at DESC").limit(20)
200   end
201
202   def hide
203     entry = DiaryEntry.find(params[:id])
204     entry.update(:visible => false)
205     redirect_to :action => "index", :display_name => entry.user.display_name
206   end
207
208   def unhide
209     entry = DiaryEntry.find(params[:id])
210     entry.update(:visible => true)
211     redirect_to :action => "index", :display_name => entry.user.display_name
212   end
213
214   private
215
216   ##
217   # return permitted diary entry parameters
218   def entry_params
219     params.require(:diary_entry).permit(:title, :body, :language_code, :latitude, :longitude)
220   rescue ActionController::ParameterMissing
221     ActionController::Parameters.new.permit(:title, :body, :language_code, :latitude, :longitude)
222   end
223
224   ##
225   # decide on a location for the diary entry map
226   def set_map_location
227     if @diary_entry.latitude && @diary_entry.longitude
228       @lon = @diary_entry.longitude
229       @lat = @diary_entry.latitude
230       @zoom = 12
231     elsif !current_user.home_location?
232       @lon = params[:lon] || -0.1
233       @lat = params[:lat] || 51.5
234       @zoom = params[:zoom] || 4
235     else
236       @lon = current_user.home_lon
237       @lat = current_user.home_lat
238       @zoom = 12
239     end
240   end
241 end