2019-07-28 12:48:19 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'tty-prompt'
|
2023-05-24 10:55:40 +01:00
|
|
|
require_relative 'base'
|
2019-07-28 12:48:19 +01:00
|
|
|
|
2023-05-23 15:08:26 +01:00
|
|
|
module Mastodon::CLI
|
2023-05-24 10:55:40 +01:00
|
|
|
class PreviewCards < Base
|
2019-07-28 12:48:19 +01:00
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
|
|
|
|
option :days, type: :numeric, default: 180
|
2019-09-10 12:48:48 +01:00
|
|
|
option :concurrency, type: :numeric, default: 5, aliases: [:c]
|
|
|
|
option :verbose, type: :boolean, aliases: [:v]
|
2019-07-28 12:48:19 +01:00
|
|
|
option :dry_run, type: :boolean, default: false
|
|
|
|
option :link, type: :boolean, default: false
|
|
|
|
desc 'remove', 'Remove preview cards'
|
|
|
|
long_desc <<-DESC
|
2019-09-10 12:48:48 +01:00
|
|
|
Removes local thumbnails for preview cards.
|
2019-07-28 12:48:19 +01:00
|
|
|
|
|
|
|
The --days option specifies how old preview cards have to be before
|
2019-09-10 12:48:48 +01:00
|
|
|
they are removed. It defaults to 180 days. Since preview cards will
|
|
|
|
not be re-fetched unless the link is re-posted after 2 weeks from
|
|
|
|
last time, it is not recommended to delete preview cards within the
|
|
|
|
last 14 days.
|
2019-07-28 12:48:19 +01:00
|
|
|
|
2019-09-10 12:48:48 +01:00
|
|
|
With the --link option, only link-type preview cards will be deleted,
|
|
|
|
leaving video and photo cards untouched.
|
2019-07-28 12:48:19 +01:00
|
|
|
DESC
|
|
|
|
def remove
|
2019-09-10 12:48:48 +01:00
|
|
|
time_ago = options[:days].days.ago
|
|
|
|
link = options[:link] ? 'link-type ' : ''
|
|
|
|
scope = PreviewCard.cached
|
|
|
|
scope = scope.where(type: :link) if options[:link]
|
|
|
|
scope = scope.where('updated_at < ?', time_ago)
|
2019-07-28 12:48:19 +01:00
|
|
|
|
2019-09-10 12:48:48 +01:00
|
|
|
processed, aggregate = parallelize_with_progress(scope) do |preview_card|
|
|
|
|
next if preview_card.image.blank?
|
2019-07-28 12:48:19 +01:00
|
|
|
|
2019-09-10 12:48:48 +01:00
|
|
|
size = preview_card.image_file_size
|
2019-07-28 12:48:19 +01:00
|
|
|
|
2023-05-30 15:07:44 +01:00
|
|
|
unless dry_run?
|
2019-09-10 12:48:48 +01:00
|
|
|
preview_card.image.destroy
|
|
|
|
preview_card.save
|
2019-07-28 12:48:19 +01:00
|
|
|
end
|
|
|
|
|
2019-09-10 12:48:48 +01:00
|
|
|
size
|
2019-07-28 12:48:19 +01:00
|
|
|
end
|
|
|
|
|
2023-05-30 15:07:44 +01:00
|
|
|
say("Removed #{processed} #{link}preview cards (approx. #{number_to_human_size(aggregate)})#{dry_run_mode_suffix}", :green, true)
|
2019-07-28 12:48:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|