Skip to content

Commit

Permalink
Add "tootctl emoji refresh"
Browse files Browse the repository at this point in the history
This allows refreshing remote custom emoji images with optional filtering by shortcode/domain. This does not currently handle refreshing `Emoji` ActivityPub objects, so if a remote URL in the DB is dead then a request to refresh it will fail.
  • Loading branch information
TheEssem committed Jan 6, 2025
1 parent 3b908dc commit 0074055
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lib/mastodon/cli/emoji.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

module Mastodon::CLI
class Emoji < Base
include ActionView::Helpers::NumberHelper

option :prefix
option :suffix
option :overwrite, type: :boolean
Expand Down Expand Up @@ -121,6 +123,53 @@ def purge
say('OK', :green)
end

option :shortcode, type: :string
option :domain, type: :string
option :concurrency, type: :numeric, default: 5, aliases: [:c]
option :verbose, type: :boolean, default: false, aliases: [:v]
option :dry_run, type: :boolean, default: false
option :force, type: :boolean, default: false
desc 'refresh', 'Fetch remote emojis'
long_desc <<-DESC
Re-downloads custom emojis from other servers.
Use the --domain option to download emojis from a specific domain.
Use the --shortcode option to download emojis with a specific shortcode,
using the shortcode@domain syntax.
By default, emojis that are believed to be already downloaded will
not be re-downloaded. To force re-download of every URL, use --force.
DESC
def refresh
if options[:shortcode]
shortcode, domain = options[:shortcode].split('@')
emoji = CustomEmoji.where(domain: domain).find_by(shortcode: shortcode)

fail_with_message 'No such emoji' if emoji.nil?

scope = CustomEmoji.where(id: emoji.id)
elsif options[:domain]
scope = CustomEmoji.where(domain: options[:domain])
else
scope = CustomEmoji.remote
end

processed, aggregate = parallelize_with_progress(scope) do |custom_emoji|
next if custom_emoji.image_remote_url.blank? || (!options[:force] && custom_emoji.image_file_name.present?)
next if DomainBlock.reject_media?(custom_emoji.domain)

unless dry_run?
custom_emoji.reset_image!
custom_emoji.save
end

custom_emoji.image_file_size
end

say("Downloaded #{processed} custom emojis (approx. #{number_to_human_size(aggregate)})#{dry_run_mode_suffix}", :green, true)
end

private

def color(green, _yellow, red)
Expand Down

0 comments on commit 0074055

Please sign in to comment.