diff --git a/app/controllers/trips_controller.rb b/app/controllers/trips_controller.rb index 417dcebd..30ff3de9 100644 --- a/app/controllers/trips_controller.rb +++ b/app/controllers/trips_controller.rb @@ -13,6 +13,10 @@ class TripsController < ApplicationController :latitude, :longitude, :battery, :altitude, :timestamp, :velocity, :id, :country ).map { [_1.to_f, _2.to_f, _3.to_s, _4.to_s, _5.to_s, _6.to_s, _7.to_s, _8.to_s] } + + @photos = Rails.cache.fetch("trip_photos_#{@trip.id}", expires_in: 1.day) do + @trip.photos + end end def new diff --git a/app/javascript/controllers/trips_controller.js b/app/javascript/controllers/trips_controller.js index 9763d37c..fdbdca69 100644 --- a/app/javascript/controllers/trips_controller.js +++ b/app/javascript/controllers/trips_controller.js @@ -10,7 +10,7 @@ import { esriWorldStreetMapLayer } from "../maps/layers" import { esriWorldTopoMapLayer } from "../maps/layers" import { esriWorldImageryMapLayer } from "../maps/layers" import { esriWorldGrayCanvasMapLayer } from "../maps/layers" -// import { fetchAndDisplayPhotos } from "../helpers/photoFetcher"; +import { fetchAndDisplayPhotos } from '../maps/helpers'; export default class extends Controller { static targets = ["container"] @@ -52,7 +52,28 @@ export default class extends Controller { // Add layer control L.control.layers(this.baseMaps(), overlayMaps).addTo(this.map) - // Add markers for each coordinate + // Add event listener for layer changes + this.map.on('overlayadd', (e) => { + if (e.name === 'Photos' && this.coordinates?.length > 0) { + const firstCoord = this.coordinates[0]; + const lastCoord = this.coordinates[this.coordinates.length - 1]; + + // Convert Unix timestamp to a Date object + const startDate = new Date(firstCoord[4] * 1000).toISOString().split('T')[0]; + const endDate = new Date(lastCoord[4] * 1000).toISOString().split('T')[0]; + + fetchAndDisplayPhotos({ + map: this.map, + photoMarkers: this.photoMarkers, + apiKey: this.apiKey, + startDate: startDate, + endDate: endDate, + userSettings: this.userSettings + }); + } + }); + + // Add markers and route if (this.coordinates?.length > 0) { this.addMarkers() this.addPolyline() @@ -129,11 +150,4 @@ export default class extends Controller { esriWorldGrayCanvas: esriWorldGrayCanvasMapLayer(this.map, selectedLayerName) }; } - - someMethod() { - // Example usage - const startDate = '2023-01-01'; - const endDate = '2023-12-31'; - fetchAndDisplayPhotos(this.map, this.apiKey, this.photoMarkers, startDate, endDate); - } } diff --git a/app/models/trip.rb b/app/models/trip.rb index cae1b1f6..5c3851bd 100644 --- a/app/models/trip.rb +++ b/app/models/trip.rb @@ -12,4 +12,24 @@ class Trip < ApplicationRecord def countries points.pluck(:country).uniq.compact end + + def photos + immich_photos = Immich::RequestPhotos.new( + user, + start_date: started_at.to_date.to_s, + end_date: ended_at.to_date.to_s + ).call + + # 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 end diff --git a/app/services/immich/request_photos.rb b/app/services/immich/request_photos.rb index e67c5c31..3ab8a246 100644 --- a/app/services/immich/request_photos.rb +++ b/app/services/immich/request_photos.rb @@ -27,6 +27,7 @@ class Immich::RequestPhotos data = [] max_pages = 10_000 # Prevent infinite loop + # TODO: Handle pagination using nextPage while page <= max_pages response = JSON.parse( HTTParty.post( @@ -49,7 +50,7 @@ class Immich::RequestPhotos page += 1 end - data.flatten + data.flatten.reject { |asset| asset['type'].downcase == 'video' } end def headers diff --git a/app/views/trips/show.html.erb b/app/views/trips/show.html.erb index 9db616d7..07f2f1e4 100644 --- a/app/views/trips/show.html.erb +++ b/app/views/trips/show.html.erb @@ -48,15 +48,20 @@ - <% (1..12).each_slice(4) do |slice| %> -