Skip to content

Commit

Permalink
Add tootctl preview_cards clean
Browse files Browse the repository at this point in the history
  • Loading branch information
noellabo committed Aug 22, 2024
1 parent c0db580 commit 1a74e80
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/mastodon/cli/preview_cards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,39 @@ def remove

say("Removed #{processed} #{link}preview cards (approx. #{number_to_human_size(aggregate)})#{dry_run_mode_suffix}", :green, true)
end

option :days, type: :numeric, default: 14
option :verbose, type: :boolean, aliases: [:v]
option :dry_run, type: :boolean, default: false
desc 'clean', 'Clean unused preview card records'
long_desc <<-DESC
Clean unused preview card records.
The --days option specifies the number of days after which unused
preview cards are deleted. The default is 14 days. Preview cards are
reused if the link is reposted within two weeks of the last time,
so deleting them too early can result in additional overhead for
refetching.
DESC
def clean
time_ago = options[:days].days.ago
scope = ActiveRecord::Base.connection.select_values(ActiveRecord::Base.sanitize_sql_array([
"select preview_card_id from preview_cards_statuses ps join preview_cards p on ps.preview_card_id = p.id left join statuses s on ps.status_id = s.id where s.id is null and p.updated_at < :time_ago",

Check failure on line 67 in lib/mastodon/cli/preview_cards.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/FirstArrayElementIndentation: Use 2 spaces for indentation in an array, relative to the first position after the preceding left parenthesis.

Check failure on line 67 in lib/mastodon/cli/preview_cards.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. (https://rubystyle.guide#consistent-string-literals)
{ time_ago: time_ago }]))

Check failure on line 68 in lib/mastodon/cli/preview_cards.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/MultilineArrayBraceLayout: The closing array brace must be on the line after the last array element when the opening brace is on a separate line from the first array element.

processed = 0
scope.each_slice(1000) do |preview_card_ids|
unless dry_run?
PreviewCard.where(id: preview_card_ids).destroy_all
ActiveRecord::Base.connection.execute(ActiveRecord::Base.sanitize_sql_array([
"delete from preview_cards_statuses where preview_card_id in (:preview_card_ids)",

Check failure on line 75 in lib/mastodon/cli/preview_cards.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/FirstArrayElementIndentation: Use 2 spaces for indentation in an array, relative to the first position after the preceding left parenthesis.

Check failure on line 75 in lib/mastodon/cli/preview_cards.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/StringLiterals: Prefer single-quoted strings when you don't need string interpolation or special symbols. (https://rubystyle.guide#consistent-string-literals)
{ preview_card_ids: preview_card_ids }]))

Check failure on line 76 in lib/mastodon/cli/preview_cards.rb

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/MultilineArrayBraceLayout: The closing array brace must be on the line after the last array element when the opening brace is on a separate line from the first array element.
end

processed += preview_card_ids.count
end

say("Removed #{processed} PreviewCard records#{dry_run_mode_suffix}", :green)
end
end
end

0 comments on commit 1a74e80

Please sign in to comment.