From 434ca1ac77521ca852c14a161fbe5541ec8e9f37 Mon Sep 17 00:00:00 2001 From: Tim Cowlishaw Date: Mon, 14 Oct 2024 08:56:40 +0200 Subject: [PATCH 1/2] slim world map representation and add gzip middleware --- app/models/device.rb | 11 ++++---- app/views/v0/devices/_device.jbuilder | 6 ++-- .../v0/devices/_world_map_device.jbuilder | 28 +++++++++++++++++++ app/views/v0/devices/_world_map_list.jbuilder | 2 +- config/application.rb | 1 + 5 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 app/views/v0/devices/_world_map_device.jbuilder diff --git a/app/models/device.rb b/app/models/device.rb index 79f1aa50..503997c0 100644 --- a/app/models/device.rb +++ b/app/models/device.rb @@ -203,18 +203,17 @@ def soft_state end end - def formatted_location + def formatted_location(slim = false) { - ip: nil, exposure: exposure, - elevation: elevation.try(:to_i) , + elevation: slim ? nil : elevation.try(:to_i), latitude: latitude, longitude: longitude, - geohash: geohash, + geohash: slim ? nil : geohash, city: city, - country_code: country_code, + country_code: slim ? nil : country_code, country: country_name - } + }.compact end def formatted_data diff --git a/app/views/v0/devices/_device.jbuilder b/app/views/v0/devices/_device.jbuilder index e3158b31..ec9d72e4 100644 --- a/app/views/v0/devices/_device.jbuilder +++ b/app/views/v0/devices/_device.jbuilder @@ -1,9 +1,7 @@ local_assigns[:with_owner] = true unless local_assigns.has_key?(:with_owner) local_assigns[:with_data] = true unless local_assigns.has_key?(:with_data) -local_assigns[:with_postprocessing] = true unless local_assigns.has_key?(:with_postprocessing) local_assigns[:with_location] = true unless local_assigns.has_key?(:with_location) local_assigns[:slim_owner] = false unless local_assigns.has_key?(:slim_owner) -local_assigns[:never_authorized] = false unless local_assigns.has_key?(:never_authorized) json.( device, @@ -24,7 +22,7 @@ json.( low_battery: device.notify_low_battery }) -authorized = !local_assigns[:never_authorized] && (current_user && (current_user.is_admin? || (device.owner_id && current_user.id == device.owner_id))) +authorized = (current_user && (current_user.is_admin? || (device.owner_id && current_user.id == device.owner_id))) if authorized json.merge! device_token: device.device_token @@ -32,7 +30,7 @@ if authorized else json.merge! device_token: '[FILTERED]' end -json.merge!(postprocessing: device.postprocessing) if local_assigns[:with_postprocessing] +json.merge!(postprocessing: device.postprocessing) json.merge!(location: device.formatted_location) if local_assigns[:with_location] json.merge!(data_policy: device.data_policy(authorized)) json.merge!(hardware: device.hardware(authorized)) diff --git a/app/views/v0/devices/_world_map_device.jbuilder b/app/views/v0/devices/_world_map_device.jbuilder new file mode 100644 index 00000000..10514a97 --- /dev/null +++ b/app/views/v0/devices/_world_map_device.jbuilder @@ -0,0 +1,28 @@ +local_assigns[:never_authorized] = false unless local_assigns.has_key?(:never_authorized) + +json.( + device, + :id, + :name, + :description, + :state, + :system_tags, + :user_tags, + :last_reading_at, +) + + +authorized = !local_assigns[:never_authorized] && (current_user && (current_user.is_admin? || (device.owner_id && current_user.id == device.owner_id))) + +json.merge!(location: device.formatted_location(true)) +json.merge!(hardware: device.hardware(authorized)) + +if device.owner + json.owner do + json.id device.owner.id + json.username device.owner.username + json.url device.owner.url + end +end + + diff --git a/app/views/v0/devices/_world_map_list.jbuilder b/app/views/v0/devices/_world_map_list.jbuilder index d5e580bd..01bcfe38 100644 --- a/app/views/v0/devices/_world_map_list.jbuilder +++ b/app/views/v0/devices/_world_map_list.jbuilder @@ -1 +1 @@ -json.array! Device.for_world_map(never_authorized ? nil : current_user), partial: 'device', as: :device, local_assigns: { with_data: false, with_postprocessing: false, slim_owner: true, never_authorized: never_authorized } +json.array! Device.for_world_map(never_authorized ? nil : current_user), partial: 'world_map_device', as: :device, local_assigns: { never_authorized: never_authorized } diff --git a/config/application.rb b/config/application.rb index c23b78ca..caab0290 100644 --- a/config/application.rb +++ b/config/application.rb @@ -73,5 +73,6 @@ class Application < Rails::Application config.api_only = true config.active_storage.service_urls_expire_in = 30.minutes + config.middleware.use Rack::Deflater end end From ba9acbc4b90fe5b6930b100e4aa972c9a6b53d96 Mon Sep 17 00:00:00 2001 From: Tim Cowlishaw Date: Mon, 14 Oct 2024 11:41:45 +0200 Subject: [PATCH 2/2] remove owner from world map view --- app/views/v0/devices/_world_map_device.jbuilder | 16 +--------------- app/views/v0/devices/_world_map_list.jbuilder | 2 +- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/app/views/v0/devices/_world_map_device.jbuilder b/app/views/v0/devices/_world_map_device.jbuilder index 10514a97..bd634745 100644 --- a/app/views/v0/devices/_world_map_device.jbuilder +++ b/app/views/v0/devices/_world_map_device.jbuilder @@ -1,5 +1,3 @@ -local_assigns[:never_authorized] = false unless local_assigns.has_key?(:never_authorized) - json.( device, :id, @@ -11,18 +9,6 @@ json.( :last_reading_at, ) - -authorized = !local_assigns[:never_authorized] && (current_user && (current_user.is_admin? || (device.owner_id && current_user.id == device.owner_id))) - json.merge!(location: device.formatted_location(true)) -json.merge!(hardware: device.hardware(authorized)) - -if device.owner - json.owner do - json.id device.owner.id - json.username device.owner.username - json.url device.owner.url - end -end - +json.merge!(hardware: device.hardware(false)) diff --git a/app/views/v0/devices/_world_map_list.jbuilder b/app/views/v0/devices/_world_map_list.jbuilder index 01bcfe38..d2652dab 100644 --- a/app/views/v0/devices/_world_map_list.jbuilder +++ b/app/views/v0/devices/_world_map_list.jbuilder @@ -1 +1 @@ -json.array! Device.for_world_map(never_authorized ? nil : current_user), partial: 'world_map_device', as: :device, local_assigns: { never_authorized: never_authorized } +json.array! Device.for_world_map(never_authorized ? nil : current_user), partial: 'world_map_device', as: :device