2024-05-23 14:12:23 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-03-15 18:27:31 -04:00
|
|
|
class Point < ApplicationRecord
|
2025-02-21 17:45:36 -05:00
|
|
|
include Nearable
|
|
|
|
|
include Distanceable
|
2025-12-14 06:05:59 -05:00
|
|
|
include Archivable
|
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
|
2025-08-21 16:32:29 -04:00
|
|
|
belongs_to :user, counter_cache: true
|
2025-06-25 16:23:43 -04:00
|
|
|
belongs_to :country, optional: true
|
2025-07-03 14:18:18 -04:00
|
|
|
belongs_to :track, optional: true
|
2024-03-15 18:27:31 -04:00
|
|
|
|
2025-02-21 17:45:36 -05:00
|
|
|
validates :timestamp, :lonlat, presence: true
|
|
|
|
|
validates :lonlat, uniqueness: {
|
|
|
|
|
scope: %i[timestamp user_id],
|
2025-01-20 11:59:13 -05:00
|
|
|
message: 'already has a point at this location and time for this user',
|
|
|
|
|
index: true
|
|
|
|
|
}
|
2025-02-21 17:45:36 -05:00
|
|
|
|
2025-09-08 14:46:30 -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
|
|
|
|
2024-12-02 08:44:22 -05:00
|
|
|
scope :reverse_geocoded, -> { where.not(reverse_geocoded_at: nil) }
|
|
|
|
|
scope :not_reverse_geocoded, -> { where(reverse_geocoded_at: nil) }
|
2024-08-13 12:25:48 -04:00
|
|
|
scope :visited, -> { where.not(visit_id: nil) }
|
|
|
|
|
scope :not_visited, -> { where(visit_id: nil) }
|
2024-07-09 17:50:19 -04:00
|
|
|
|
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
|
2024-11-03 10:48:43 -05:00
|
|
|
after_create_commit :broadcast_coordinates
|
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
|
|
|
|
|
|
2025-01-04 15:31:21 -05:00
|
|
|
def async_reverse_geocode
|
2025-01-07 07:41:09 -05:00
|
|
|
return unless DawarichSettings.reverse_geocoding_enabled?
|
2024-03-24 13:05:39 -04:00
|
|
|
|
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
|
2024-11-03 10:48:43 -05:00
|
|
|
|
2024-12-02 08:44:22 -05:00
|
|
|
def reverse_geocoded?
|
|
|
|
|
reverse_geocoded_at.present?
|
|
|
|
|
end
|
|
|
|
|
|
2025-02-21 17:45:36 -05:00
|
|
|
def lon
|
2025-02-22 17:14:23 -05:00
|
|
|
lonlat.x
|
2025-02-21 17:45:36 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def lat
|
2025-02-22 17:14:23 -05:00
|
|
|
lonlat.y
|
2025-02-21 17:45:36 -05:00
|
|
|
end
|
|
|
|
|
|
2025-05-16 12:51:48 -04:00
|
|
|
def found_in_country
|
|
|
|
|
Country.containing_point(lon, lat)
|
|
|
|
|
end
|
|
|
|
|
|
2025-07-28 12:53:19 -04:00
|
|
|
def country_name
|
2025-07-29 15:17:33 -04:00
|
|
|
# TODO: Remove the country column in the future.
|
2025-09-08 14:46:30 -04:00
|
|
|
read_attribute(:country_name) || country&.name || self[:country] || ''
|
2025-07-28 12:53:19 -04:00
|
|
|
end
|
|
|
|
|
|
2024-11-03 10:48:43 -05:00
|
|
|
private
|
|
|
|
|
|
2025-02-22 17:14:23 -05:00
|
|
|
# rubocop:disable Metrics/MethodLength Metrics/AbcSize
|
2024-11-03 10:48:43 -05:00
|
|
|
def broadcast_coordinates
|
2025-10-13 08:10:36 -04:00
|
|
|
if user.safe_settings.live_map_enabled
|
|
|
|
|
PointsChannel.broadcast_to(
|
|
|
|
|
user,
|
|
|
|
|
[
|
|
|
|
|
lat,
|
|
|
|
|
lon,
|
|
|
|
|
battery.to_s,
|
|
|
|
|
altitude.to_s,
|
|
|
|
|
timestamp.to_s,
|
|
|
|
|
velocity.to_s,
|
|
|
|
|
id.to_s,
|
|
|
|
|
country_name.to_s
|
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
broadcast_to_family if should_broadcast_to_family?
|
2024-11-03 10:48:43 -05:00
|
|
|
end
|
2025-02-22 17:14:23 -05:00
|
|
|
# rubocop:enable Metrics/MethodLength
|
2025-05-16 12:51:48 -04:00
|
|
|
|
2025-10-13 08:10:36 -04:00
|
|
|
def should_broadcast_to_family?
|
|
|
|
|
return false unless DawarichSettings.family_feature_enabled?
|
|
|
|
|
return false unless user.in_family?
|
|
|
|
|
return false unless user.family_sharing_enabled?
|
|
|
|
|
|
|
|
|
|
true
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def broadcast_to_family
|
|
|
|
|
FamilyLocationsChannel.broadcast_to(
|
|
|
|
|
user.family,
|
|
|
|
|
{
|
|
|
|
|
user_id: user.id,
|
|
|
|
|
email: user.email,
|
|
|
|
|
email_initial: user.email.first.upcase,
|
|
|
|
|
latitude: lat,
|
|
|
|
|
longitude: lon,
|
|
|
|
|
timestamp: timestamp.to_i,
|
|
|
|
|
updated_at: Time.zone.at(timestamp.to_i).iso8601
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
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
|
2024-03-15 18:27:31 -04:00
|
|
|
end
|