diff --git a/Changelog.md b/Changelog.md index ffc4bb5f3..1f18982b5 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,6 +5,7 @@ ## Bug fixes ## Features +* Add cronjob to cleanup pending photos which were never posted [#8041](https://github.com/diaspora/diaspora/pull/8041) # 0.7.12.0 diff --git a/app/workers/cleanup_pending_photos.rb b/app/workers/cleanup_pending_photos.rb new file mode 100644 index 000000000..e7cabf54b --- /dev/null +++ b/app/workers/cleanup_pending_photos.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +module Workers + class CleanupPendingPhotos < Base + sidekiq_options queue: :low + + def perform + Photo.where(pending: true).where("created_at < ?", 1.day.ago).destroy_all + end + end +end diff --git a/config/initializers/sidekiq_scheduled.rb b/config/initializers/sidekiq_scheduled.rb index ac9140e38..46fc92adb 100644 --- a/config/initializers/sidekiq_scheduled.rb +++ b/config/initializers/sidekiq_scheduled.rb @@ -29,6 +29,11 @@ def default_job_config "class": "Workers::CleanupOldExports" }, + cleanup_pending_photos: { + "cron": "#{random_minute.call} #{random_hour.call} * * *", + "class": "Workers::CleanupPendingPhotos" + }, + queue_users_for_removal: { "cron": "#{random_minute.call} #{random_hour.call} * * *", "class": "Workers::QueueUsersForRemoval" diff --git a/spec/workers/cleanup_pending_photos_spec.rb b/spec/workers/cleanup_pending_photos_spec.rb new file mode 100644 index 000000000..1a92cac96 --- /dev/null +++ b/spec/workers/cleanup_pending_photos_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +describe Workers::CleanupPendingPhotos do + let!(:photo) { FactoryGirl.create(:photo, author: alice.person, pending: true) } + + it "removes pending photos" do + Timecop.travel(25.hours) do + Workers::CleanupPendingPhotos.new.perform + expect(Photo).not_to exist(photo.id) + end + end + + it "does not remove pending photos newer than one day" do + Timecop.travel(1.hour) do + Workers::CleanupPendingPhotos.new.perform + expect(Photo).to exist(photo.id) + end + end + + it "does not remove posted photos" do + StatusMessageCreationService.new(alice).create( + status_message: {text: "Post with photo"}, + public: true, + photos: [photo.id] + ) + Timecop.travel(25.hours) do + Workers::CleanupPendingPhotos.new.perform + expect(Photo).to exist(photo.id) + end + end +end