dawarich/app/models/point.rb

109 lines
2.8 KiB
Ruby
Raw Normal View History

2024-05-23 14:12:23 -04:00
# frozen_string_literal: true
2024-03-15 18:27:31 -04:00
class Point < ApplicationRecord
include Nearable
include Distanceable
2024-07-12 15:59:03 -04:00
2024-08-22 16:40:27 -04:00
belongs_to :import, optional: true, counter_cache: true
2024-07-21 14:32:29 -04:00
belongs_to :visit, optional: true
2024-07-12 15:59:03 -04:00
belongs_to :user
2025-06-25 16:23:43 -04:00
belongs_to :country, optional: true
belongs_to :track, optional: true
2024-03-15 18:27:31 -04:00
validates :timestamp, :lonlat, presence: true
validates :lonlat, uniqueness: {
scope: %i[timestamp user_id],
message: 'already has a point at this location and time for this user',
index: true
}
2025-07-27 15:38:51 -04:00
enum :battery_status, { unknown: 0, unplugged: 1, charging: 2, full: 3, connected_not_charging: 4, discharging: 5 }, suffix: true
2024-09-05 15:01:59 -04:00
enum :trigger, {
2024-03-15 18:27:31 -04:00
unknown: 0, background_event: 1, circular_region_event: 2, beacon_event: 3,
report_location_message_event: 4, manual_event: 5, timer_based_event: 6,
settings_monitoring_event: 7
2024-09-05 15:01:59 -04:00
}, suffix: true
enum :connection, { mobile: 0, wifi: 1, offline: 2, unknown: 4 }, suffix: true
2024-03-15 18:27:31 -04:00
scope :reverse_geocoded, -> { where.not(reverse_geocoded_at: nil) }
scope :not_reverse_geocoded, -> { where(reverse_geocoded_at: nil) }
scope :visited, -> { where.not(visit_id: nil) }
scope :not_visited, -> { where(visit_id: nil) }
2025-06-29 05:49:44 -04:00
after_create :async_reverse_geocode, if: -> { DawarichSettings.store_geodata? && !reverse_geocoded? }
2025-05-16 12:51:48 -04:00
after_create :set_country
after_create_commit :broadcast_coordinates
2025-07-22 14:28:46 -04:00
# after_create_commit :trigger_incremental_track_generation, if: -> { import_id.nil? }
2025-07-22 14:25:44 -04:00
# after_commit :recalculate_track, on: :update, if: -> { track.present? }
2024-03-15 18:27:31 -04:00
2024-05-23 14:12:23 -04:00
def self.without_raw_data
select(column_names - ['raw_data'])
end
def recorded_at
2025-03-09 15:07:39 -04:00
@recorded_at ||= Time.zone.at(timestamp)
2024-05-23 14:12:23 -04:00
end
def async_reverse_geocode
return unless DawarichSettings.reverse_geocoding_enabled?
2024-08-05 15:23:08 -04:00
ReverseGeocodingJob.perform_later(self.class.to_s, id)
2024-03-15 18:27:31 -04:00
end
def reverse_geocoded?
reverse_geocoded_at.present?
end
def lon
2025-02-22 17:14:23 -05:00
lonlat.x
end
def lat
2025-02-22 17:14:23 -05:00
lonlat.y
end
2025-05-16 12:51:48 -04:00
def found_in_country
Country.containing_point(lon, lat)
end
def country_name
2025-07-29 15:17:33 -04:00
# TODO: Remove the country column in the future.
read_attribute(:country_name) || self.country&.name || read_attribute(:country) || ''
end
private
2025-02-22 17:14:23 -05:00
# rubocop:disable Metrics/MethodLength Metrics/AbcSize
def broadcast_coordinates
2025-07-26 07:06:06 -04:00
return unless user.safe_settings.live_map_enabled
PointsChannel.broadcast_to(
user,
[
2025-02-22 17:14:23 -05:00
lat,
lon,
battery.to_s,
altitude.to_s,
timestamp.to_s,
velocity.to_s,
id.to_s,
2025-06-28 06:22:56 -04:00
country_name.to_s
]
)
end
2025-02-22 17:14:23 -05:00
# rubocop:enable Metrics/MethodLength
2025-05-16 12:51:48 -04:00
def set_country
self.country_id = found_in_country&.id
save! if changed?
end
2025-06-28 06:22:56 -04:00
2025-07-04 13:49:56 -04:00
def recalculate_track
track.recalculate_path_and_distance!
end
2025-07-07 15:48:07 -04:00
def trigger_incremental_track_generation
2025-07-16 16:22:33 -04:00
Tracks::IncrementalCheckJob.perform_later(user.id, id)
2025-07-07 15:48:07 -04:00
end
2024-03-15 18:27:31 -04:00
end