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 Sep 30, 2024
1 parent 35da815 commit 2c703f3
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions lib/mastodon/cli/preview_cards.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ module Mastodon::CLI
class PreviewCards < Base
include ActionView::Helpers::NumberHelper

class PreviewCardsStatus < ApplicationRecord
self.primary_key = [:status_id, :preview_card_id]
belongs_to :status
belongs_to :preview_card
end

option :days, type: :numeric, default: 180
option :concurrency, type: :numeric, default: 5, aliases: [:c]
option :verbose, type: :boolean, aliases: [:v]
Expand Down Expand Up @@ -47,5 +53,43 @@ 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 = PreviewCardsStatus.joins(:preview_card).left_joins(:status).where(statuses: {id: nil}).where(preview_cards: {updated_at: ...time_ago}).pluck(:preview_card_id)

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

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/SpaceInsideHashLiteralBraces: Space inside { missing. (https://rubystyle.guide#spaces-braces)

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

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/SpaceInsideHashLiteralBraces: Space inside } missing. (https://rubystyle.guide#spaces-braces)

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

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/SpaceInsideHashLiteralBraces: Space inside { missing. (https://rubystyle.guide#spaces-braces)

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

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Layout/SpaceInsideHashLiteralBraces: Space inside } missing. (https://rubystyle.guide#spaces-braces)
total = scope.count

progress = create_progress_bar(total)

scope.each_slice(1000) do |preview_card_ids|
if !progress.total.nil? && progress.progress + 1 > progress.total

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

View workflow job for this annotation

GitHub Actions / lint

[Correctable] Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||. (https://rubystyle.guide#if-as-a-modifier)
progress.total = nil
end

unless dry_run?
PreviewCardTrend.where(preview_card_id: preview_card_ids).delete_all
PreviewCardsStatus.where(preview_card_id: preview_card_ids).delete_all
PreviewCard.where(id: preview_card_ids).delete_all
end

progress.progress += preview_card_ids.count
end

progress.stop
say("Removed #{total} PreviewCard records#{dry_run_mode_suffix}", :green)
end
end
end

0 comments on commit 2c703f3

Please sign in to comment.