2024-11-27 14:14:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class Trip < ApplicationRecord
|
2024-11-28 09:29:17 -05:00
|
|
|
has_rich_text :notes
|
2024-11-28 06:00:54 -05:00
|
|
|
|
2024-11-27 14:14:17 -05:00
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
|
|
validates :name, :started_at, :ended_at, presence: true
|
2024-11-27 15:37:21 -05:00
|
|
|
|
2024-11-28 09:29:17 -05:00
|
|
|
before_save :calculate_distance
|
|
|
|
|
|
2024-11-27 15:37:21 -05:00
|
|
|
def points
|
2024-11-28 09:29:17 -05:00
|
|
|
user.tracked_points.where(timestamp: started_at.to_i..ended_at.to_i).order(:timestamp)
|
2024-11-27 15:37:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def countries
|
|
|
|
|
points.pluck(:country).uniq.compact
|
|
|
|
|
end
|
2024-11-28 04:40:08 -05:00
|
|
|
|
|
|
|
|
def photos
|
|
|
|
|
immich_photos = Immich::RequestPhotos.new(
|
|
|
|
|
user,
|
|
|
|
|
start_date: started_at.to_date.to_s,
|
|
|
|
|
end_date: ended_at.to_date.to_s
|
2024-11-28 10:11:04 -05:00
|
|
|
).call.reject { |asset| asset['type'].downcase == 'video' }
|
2024-11-28 04:40:08 -05:00
|
|
|
|
|
|
|
|
# let's count what photos are more: vertical or horizontal and select the ones that are more
|
|
|
|
|
vertical_photos = immich_photos.select { _1['exifInfo']['orientation'] == '6' }
|
|
|
|
|
horizontal_photos = immich_photos.select { _1['exifInfo']['orientation'] == '3' }
|
|
|
|
|
|
|
|
|
|
# this is ridiculous, but I couldn't find my way around frontend
|
|
|
|
|
# to show all photos in the same height
|
|
|
|
|
photos = vertical_photos.count > horizontal_photos.count ? vertical_photos : horizontal_photos
|
|
|
|
|
|
|
|
|
|
photos.sample(12).map do |asset|
|
|
|
|
|
{ url: "/api/v1/photos/#{asset['id']}/thumbnail.jpg?api_key=#{user.api_key}" }
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-11-28 09:29:17 -05:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
def calculate_distance
|
|
|
|
|
distance = 0
|
|
|
|
|
|
|
|
|
|
points.each_cons(2) do |point1, point2|
|
|
|
|
|
distance_between = Geocoder::Calculations.distance_between(
|
|
|
|
|
point1.to_coordinates, point2.to_coordinates, units: ::DISTANCE_UNIT
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
distance += distance_between
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
self.distance = distance.round
|
|
|
|
|
end
|
2024-11-27 14:14:17 -05:00
|
|
|
end
|