Add tootctl emoji misskey-export

This commit is contained in:
noellabo 2023-03-03 01:13:11 +09:00
parent 002741bfa4
commit f3a1763942
5 changed files with 135 additions and 0 deletions

View File

@ -95,6 +95,7 @@ gem 'tzinfo-data', '~> 1.2021'
gem 'webpacker', '~> 5.4'
gem 'webpush', '~> 0.3'
gem 'webauthn', '~> 3.0.0.alpha1'
gem 'rubyzip'
gem 'json-ld'
gem 'json-ld-preloaded', '~> 3.1'

View File

@ -579,6 +579,7 @@ GEM
ruby-saml (1.11.0)
nokogiri (>= 1.5.10)
ruby2_keywords (0.0.5)
rubyzip (2.3.2)
rufus-scheduler (3.7.0)
fugit (~> 1.1, >= 1.1.6)
safety_net_attestation (0.4.0)
@ -808,6 +809,7 @@ DEPENDENCIES
rubocop (~> 1.18)
rubocop-rails (~> 2.11)
ruby-progressbar (~> 1.11)
rubyzip
sanitize (~> 6.0)
scenic (~> 1.5)
sidekiq (~> 6.2)

View File

@ -0,0 +1,69 @@
# frozen_string_literal: true
class Misskey::CustomEmojiSerializer < ActiveModel::Serializer
attributes :fileName, :downloaded
has_one :emoji
def fileName
"#{object.shortcode}#{File.extname(object.image_file_name)}"
end
def downloaded
true
end
def emoji
object
end
class CustomEmojiSerializer < ActiveModel::Serializer
include RoutingHelper
attributes :id, :updateAt, :name, :host, :originalUrl, :publicUrl, :uri, :type, :aliases
attribute :category, if: :category_loaded?
def id
object.id.to_s
end
def updateAt
object.updated_at.iso8601
end
def name
object.shortcode
end
def host
object.domain
end
def category
object.category.name
end
def category_loaded?
object.association(:category).loaded? && object.category.present?
end
def originalUrl
full_asset_url(object.image.url)
end
def publicUrl
full_asset_url(object.image.url)
end
def uri
ActivityPub::TagManager.instance.uri_for(object)
end
def type
object.image_content_type
end
def aliases
['']
end
end
end

View File

@ -0,0 +1,22 @@
# frozen_string_literal: true
class Misskey::MetaSerializer < ActiveModel::Serializer
attributes :metaVersion, :host, :exportedAt
has_many :emojis, serializer: Misskey::CustomEmojiSerializer
def metaVersion
2
end
def host
Rails.configuration.x.local_domain
end
def exportedAt
Time.now.iso8601
end
def emojis
object
end
end

View File

@ -1,6 +1,7 @@
# frozen_string_literal: true
require 'rubygems/package'
require 'zip'
require_relative '../../config/boot'
require_relative '../../config/environment'
require_relative 'cli_helper'
@ -121,6 +122,46 @@ module Mastodon
say("Exported #{exported}")
end
option :category
option :overwrite, type: :boolean
desc 'misskey-export PATH', 'Misskey export emoji to a ZIP archive at PATH'
long_desc <<-LONG_DESC
Exports custom emoji to 'export.zip' at PATH.
The --category option dumps only the specified category.
If this option is not specified, all emoji will be exported.
The --overwrite option will overwrite an existing archive.
LONG_DESC
def misskey_export(path)
exported = 0
category = CustomEmojiCategory.find_by(name: options[:category])
export_file_name = File.join(path, 'export.zip')
if File.file?(export_file_name) && !options[:overwrite]
say("Archive already exists! Use '--overwrite' to overwrite it!")
exit 1
end
if category.nil? && options[:category]
say("Unable to find category '#{options[:category]}'!")
exit 1
end
Zip::OutputStream.open(export_file_name) do |zos|
scope = !options[:category] || category.nil? ? CustomEmoji.local : category.emojis
scope.find_each do |emoji|
say("Adding '#{emoji.shortcode}'...")
zos.put_next_entry(emoji.shortcode + File.extname(emoji.image_file_name))
zos.write Paperclip.io_adapters.for(emoji.image).read
exported += 1
end
say("Adding 'meta.json'...")
zos.put_next_entry('meta.json')
zos.write Oj.dump(Misskey::MetaSerializer.new(scope))
end
say("Exported #{exported}")
end
option :remote_only, type: :boolean
desc 'purge', 'Remove all custom emoji'
long_desc <<-LONG_DESC