From 202396a93db4e1f995672fdcd44417fd393160b9 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Mon, 2 Dec 2024 17:34:16 +0100 Subject: [PATCH] Implement photos request for both immich and photoprism in single service class --- app/controllers/api/v1/photos_controller.rb | 6 +-- app/models/user.rb | 8 ++++ app/services/photoprism/request_photos.rb | 3 +- app/services/photos/request.rb | 38 +++++++++++++++++++ .../photoprism/request_photos_spec.rb | 9 +++-- 5 files changed, 55 insertions(+), 9 deletions(-) create mode 100644 app/services/photos/request.rb diff --git a/app/controllers/api/v1/photos_controller.rb b/app/controllers/api/v1/photos_controller.rb index 88baf2d7..c023a2d6 100644 --- a/app/controllers/api/v1/photos_controller.rb +++ b/app/controllers/api/v1/photos_controller.rb @@ -3,11 +3,7 @@ class Api::V1::PhotosController < ApiController def index @photos = Rails.cache.fetch("photos_#{params[:start_date]}_#{params[:end_date]}", expires_in: 1.day) do - Immich::RequestPhotos.new( - current_api_user, - start_date: params[:start_date], - end_date: params[:end_date] - ).call.reject { |asset| asset['type'].downcase == 'video' } + Photos::Request.new(current_api_user, start_date: params[:start_date], end_date: params[:end_date]).call end render json: @photos, status: :ok diff --git a/app/models/user.rb b/app/models/user.rb index b7edcb26..53adfa2d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -54,6 +54,14 @@ class User < ApplicationRecord tracked_points.select(:id).where.not(geodata: {}).count end + def immich_integration_configured? + settings['immich_url'].present? && settings['immich_api_key'].present? + end + + def photoprism_integration_configured? + settings['photoprism_url'].present? && settings['photoprism_api_key'].present? + end + private def create_api_key diff --git a/app/services/photoprism/request_photos.rb b/app/services/photoprism/request_photos.rb index a819c9a8..2eb89bab 100644 --- a/app/services/photoprism/request_photos.rb +++ b/app/services/photoprism/request_photos.rb @@ -71,7 +71,8 @@ class Photoprism::RequestPhotos public: true, quality: 3, after: start_date, - count: 1000 + count: 1000, + photo: 'yes' } end diff --git a/app/services/photos/request.rb b/app/services/photos/request.rb new file mode 100644 index 00000000..7c490651 --- /dev/null +++ b/app/services/photos/request.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +class Photos::Request + attr_reader :user, :start_date, :end_date + + def initialize(user, start_date: '1970-01-01', end_date: nil) + @user = user + @start_date = start_date + @end_date = end_date + end + + def call + photos = [] + + photos << request_immich if user.immich_integration_configured? + photos << request_photoprism if user.photoprism_integration_configured? + + photos + end + + private + + def request_immich + Immich::RequestPhotos.new( + user, + start_date: start_date, + end_date: end_date + ).call.reject { |asset| asset['type'].downcase == 'video' } + end + + def request_photoprism + Photoprism::RequestPhotos.new( + user, + start_date: start_date, + end_date: end_date + ).call.select { |asset| asset['Type'].downcase == 'image' } + end +end diff --git a/spec/services/photoprism/request_photos_spec.rb b/spec/services/photoprism/request_photos_spec.rb index 6dc79e6d..fb09fd51 100644 --- a/spec/services/photoprism/request_photos_spec.rb +++ b/spec/services/photoprism/request_photos_spec.rb @@ -231,7 +231,8 @@ RSpec.describe Photoprism::RequestPhotos do count: '1000', public: 'true', q: '', - quality: '3' + quality: '3', + photo: 'yes' } ) .to_return(status: 200, body: first_page.to_json) @@ -247,7 +248,8 @@ RSpec.describe Photoprism::RequestPhotos do public: 'true', q: '', quality: '3', - offset: '1000' + offset: '1000', + photo: 'yes' } ) .to_return(status: 200, body: second_page.to_json) @@ -263,7 +265,8 @@ RSpec.describe Photoprism::RequestPhotos do public: 'true', q: '', quality: '3', - offset: '2000' + offset: '2000', + photo: 'yes' } ) .to_return(status: 200, body: empty_page.to_json)