forked from paviliondev/discourse-locations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.rb
323 lines (269 loc) · 10 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# frozen_string_literal: true
# name: discourse-locations
# about: Tools for handling locations in Discourse
# version: 6.5.3
# authors: Angus McLeod, Robert Barrow
# contact_emails: [email protected]
# url: https://github.com/angusmcleod/discourse-locations
enabled_site_setting :location_enabled
register_asset 'stylesheets/common/locations.scss'
register_asset 'stylesheets/desktop/locations.scss', :desktop
register_asset 'stylesheets/mobile/locations.scss', :mobile
register_asset 'lib/leaflet/leaflet.css'
register_asset 'lib/leaflet/leaflet.js'
register_asset 'lib/leaflet/leaflet.markercluster.js'
register_asset 'lib/leaflet/MarkerCluster.css'
register_asset 'lib/leaflet/MarkerCluster.Default.css'
Discourse.top_menu_items.push(:map)
Discourse.anonymous_top_menu_items.push(:map)
Discourse.filters.push(:map)
Discourse.anonymous_filters.push(:map)
gem 'geocoder', '1.8.2'
load File.expand_path('../app/models/location_country_default_site_setting.rb', __FILE__)
load File.expand_path('../app/models/location_geocoding_language_site_setting.rb', __FILE__)
load File.expand_path('../app/models/user_location.rb', __FILE__)
load File.expand_path('../app/models/topic_location.rb', __FILE__)
load File.expand_path('../lib/user_location_process.rb', __FILE__)
load File.expand_path('../lib/topic_location_process.rb', __FILE__)
if respond_to?(:register_svg_icon)
register_svg_icon "far-map"
register_svg_icon "info"
register_svg_icon "expand"
end
after_initialize do
Category.register_custom_field_type('location', :json)
Category.register_custom_field_type('location_enabled', :boolean)
Category.register_custom_field_type('location_topic_status', :boolean)
Category.register_custom_field_type('location_map_filter_closed', :boolean)
add_to_class(:category, :location) do
if self.custom_fields['location']
if self.custom_fields['location'].is_a?(String)
begin
JSON.parse(self.custom_fields['location'])
rescue JSON::ParserError => e
puts e.message
end
elsif self.custom_fields['location'].is_a?(Hash)
self.custom_fields['location']
else
nil
end
else
nil
end
end
module LocationsSiteSettingExtension
def type_hash(name)
if name == :top_menu
@choices[name].push("map") if @choices[name].exclude?("map")
end
super(name)
end
end
require_dependency 'site_settings/type_supervisor'
class SiteSettings::TypeSupervisor
prepend LocationsSiteSettingExtension
end
[
"location",
"location_enabled",
"location_topic_status",
"location_map_filter_closed"
].each do |key|
Site.preloaded_category_custom_fields << key if Site.respond_to? :preloaded_category_custom_fields
end
Topic.register_custom_field_type('location', :json)
Topic.register_custom_field_type('has_geo_location', :boolean)
add_to_class(:topic, :location) { self.custom_fields['location'] }
add_to_serializer(:topic_view, :location, include_condition: -> { object.topic.location.present? }) do
object.topic.location
end
TopicList.preloaded_custom_fields << 'location' if TopicList.respond_to? :preloaded_custom_fields
add_to_serializer(:topic_list_item, :location, include_condition: -> { object.location.present? }) do
object.location
end
User.register_custom_field_type('geo_location', :json)
register_editable_user_custom_field [:geo_location, geo_location: {}] if defined? register_editable_user_custom_field
add_to_serializer(:user, :geo_location, respect_plugin_enabled: false) do
object.custom_fields['geo_location']
end
add_to_serializer(
:user_card,
:geo_location,
include_condition: -> do
object.custom_fields['geo_location'].present? && object.custom_fields['geo_location'] != "{}"
end,
) do
object.custom_fields['geo_location']
end
require_dependency 'directory_item_serializer'
class ::DirectoryItemSerializer::UserSerializer
attributes :geo_location
def geo_location
object.custom_fields['geo_location']
end
def include_geo_location?
SiteSetting.location_users_map
end
end
public_user_custom_fields = SiteSetting.public_user_custom_fields.split('|')
public_user_custom_fields.push('geo_location') unless public_user_custom_fields.include?('geo_location')
SiteSetting.public_user_custom_fields = public_user_custom_fields.join('|')
PostRevisor.track_topic_field(:location) do |tc, location|
if location.present? &&
location = Locations::Helper.parse_location(location.to_unsafe_hash)
tc.record_change('location', tc.topic.custom_fields['location'], location)
tc.topic.custom_fields['location'] = location
tc.topic.custom_fields['has_geo_location'] = location['geo_location'].present?
Locations::TopicLocationProcess.upsert(tc.topic.id)
else
tc.topic.custom_fields['location'] = {}
tc.topic.custom_fields['has_geo_location'] = false
end
end
DiscourseEvent.on(:post_created) do |post, opts, user|
if post.is_first_post? &&
opts[:location].present? &&
location = Locations::Helper.parse_location(opts[:location])
topic = post.topic
topic.custom_fields['location'] = location
topic.custom_fields['has_geo_location'] = location['geo_location'].present?
topic.save!
Locations::TopicLocationProcess.upsert(topic.id)
end
end
require_dependency 'application_controller'
module ::Locations
class Engine < ::Rails::Engine
engine_name 'locations'
isolate_namespace Locations
end
end
Locations::Engine.routes.draw do
get 'search' => 'geo#search'
get 'validate' => 'geo#validate'
get 'countries' => 'geo#countries'
end
Discourse::Application.routes.append do
mount ::Locations::Engine, at: 'location'
end
Discourse::Application.routes.prepend do
get 'u/user-map' => 'users#index'
get 'users/user-map' => 'users#index'
get '/map_feed' => 'list#map_feed'
end
load File.expand_path('../serializers/geo_location.rb', __FILE__)
load File.expand_path('../lib/country.rb', __FILE__)
load File.expand_path('../lib/geocode.rb', __FILE__)
load File.expand_path('../lib/locations.rb', __FILE__)
load File.expand_path('../lib/map.rb', __FILE__)
load File.expand_path('../lib/users_map.rb', __FILE__)
load File.expand_path('../controllers/geocode.rb', __FILE__)
# check latitude and longitude are included when updating users location or raise and error
register_modifier(:users_controller_update_user_params) do |result, current_user, params|
if params &&
params[:custom_fields] &&
params[:custom_fields][:geo_location] &&
params[:custom_fields][:geo_location] != "{}" &&
(!params[:custom_fields][:geo_location]['lat'] ||
!params[:custom_fields][:geo_location]['lon'])
raise Discourse::InvalidParameters.new, I18n.t('location.errors.invalid')
end
if params &&
params[:custom_fields] &&
params[:custom_fields][:geo_location]
result[:custom_fields][:geo_location] = params[:custom_fields][:geo_location]
end
result
end
DiscourseEvent.on(:user_updated) do |*params|
user_id = params[0].id
if SiteSetting.location_enabled
Locations::UserLocationProcess.upsert(user_id)
end
end
DiscourseEvent.on(:user_destroyed) do |*params|
user_id = params[0].id
Locations::UserLocationProcess.delete(user_id)
end
class ::Jobs::AnonymizeUser
module LocationsEdits
def make_anonymous
super
::Locations::UserLocationProcess.delete(@user_id)
end
end
prepend LocationsEdits
end
unless Rails.env.test?
begin
Locations::Geocode.set_config
rescue
Locations::Geocode.revert_to_default_provider
end
# To be removed
if SiteSetting.location_geocoding_provider == 'mapzen'
Locations::Geocode.revert_to_default_provider
end
end
add_model_callback(SiteSetting, :before_save) do
if name == 'location_geocoding_provider'
Locations::Geocode.set_config(provider: value)
end
if name == 'location_geocoding_timeout'
Locations::Geocode.set_config(timeout: value)
end
end
add_to_class(:site, :country_codes) do
@country_codes ||= Locations::Country.codes
end
add_to_serializer(:site, :country_codes, respect_plugin_enabled: false) { object.country_codes }
require_dependency 'topic_query'
class ::TopicQuery
def list_map
@options[:per_page] = SiteSetting.location_map_max_topics
create_list(:map) do |topics|
topics = topics.joins("INNER JOIN locations_topic
ON locations_topic.topic_id = topics.id")
Locations::Map.sorted_list_filters.each do |filter|
topics = filter[:block].call(topics, @options)
end
topics
end
end
end
Locations::Map.add_list_filter do |topics, options|
if options[:category_id]
category = Category.find(options[:category_id])
end
if SiteSetting.location_map_filter_closed || (options[:category_id] && category.custom_fields['location_map_filter_closed'])
topics = topics.where(closed: false)
end
topics
end
DiscourseEvent.trigger(:locations_ready)
end
on(:custom_wizard_ready) do
if defined?(CustomWizard) == 'constant' && CustomWizard.class == Module
CustomWizard::Field.register('location', 'discourse-locations')
CustomWizard::Action.register_callback(:before_create_topic) do |params, wizard, action, submission|
if action['add_location']
location = CustomWizard::Mapper.new(
inputs: action['add_location'],
data: submission&.fields_and_meta,
user: wizard.user
).perform
if location.present?
location = Locations::Helper.parse_location(location)
location_params = {}
location_params['location'] = location
location_params['has_geo_location'] = location['geo_location'].present?
params[:topic_opts] ||= {}
params[:topic_opts][:custom_fields] ||= {}
params[:topic_opts][:custom_fields].merge!(location_params)
end
end
params
end
end
end