Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slim world map representation and add gzip middleware #369

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions app/models/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions app/views/v0/devices/_device.jbuilder
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -24,15 +22,15 @@ 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
json.merge! mac_address: device.mac_address if device.mac_address
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))
Expand Down
14 changes: 14 additions & 0 deletions app/views/v0/devices/_world_map_device.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
json.(
device,
:id,
:name,
:description,
:state,
:system_tags,
:user_tags,
:last_reading_at,
)

json.merge!(location: device.formatted_location(true))
json.merge!(hardware: device.hardware(false))

2 changes: 1 addition & 1 deletion app/views/v0/devices/_world_map_list.jbuilder
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading