Support locally cached inline images in Articles
This commit is contained in:
parent
615bfe8c16
commit
b3e65978b4
@ -4,8 +4,8 @@ class ActivityPub::Activity
|
||||
include JsonLdHelper
|
||||
include Redisable
|
||||
|
||||
SUPPORTED_TYPES = %w(Note Question).freeze
|
||||
CONVERTED_TYPES = %w(Image Video Article Page).freeze
|
||||
SUPPORTED_TYPES = %w(Note Question Article).freeze
|
||||
CONVERTED_TYPES = %w(Image Video Page).freeze
|
||||
|
||||
def initialize(json, account, **options)
|
||||
@json = json
|
||||
|
@ -30,6 +30,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
|
||||
@mentions = []
|
||||
@params = {}
|
||||
|
||||
process_inline_images if @object['content'].present? && @object['type'] == 'Article'
|
||||
process_status_params
|
||||
process_tags
|
||||
process_audience
|
||||
@ -73,6 +74,56 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
|
||||
end
|
||||
end
|
||||
|
||||
class Handler < ::Ox::Sax
|
||||
attr_reader :srcs
|
||||
attr_reader :alts
|
||||
def initialize(block)
|
||||
@stack = []
|
||||
@srcs = []
|
||||
@alts = {}
|
||||
end
|
||||
|
||||
def start_element(element_name)
|
||||
@stack << [element_name, {}]
|
||||
end
|
||||
|
||||
def end_element(element_name)
|
||||
self_name, self_attributes = @stack[-1]
|
||||
if self_name == :img && !self_attributes[:src].nil?
|
||||
@srcs << self_attributes[:src]
|
||||
@alts[self_attributes[:src]] = self_attributes[:alt]
|
||||
end
|
||||
@stack.pop
|
||||
end
|
||||
|
||||
def attr(attribute_name, attribute_value)
|
||||
_name, attributes = @stack.last
|
||||
attributes[attribute_name] = attribute_value
|
||||
end
|
||||
end
|
||||
|
||||
def process_inline_images
|
||||
proc = Proc.new { |name| puts name }
|
||||
handler = Handler.new(proc)
|
||||
Ox.sax_parse(handler, @object['content'])
|
||||
handler.srcs.each do |src|
|
||||
if skip_download?
|
||||
@object['content'].gsub!(src, '')
|
||||
next
|
||||
end
|
||||
|
||||
media_attachment = MediaAttachment.create(account: @account, remote_url: src, description: handler.alts[src], focus: nil)
|
||||
media_attachment.file_remote_url = src
|
||||
media_attachment.save
|
||||
if unsupported_media_type?(media_attachment.file.content_type)
|
||||
@object['content'].gsub!(src, '')
|
||||
media_attachment.delete
|
||||
else
|
||||
@object['content'].gsub!(src, media_attachment.file.url(:small))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def process_audience
|
||||
(as_array(@object['to']) + as_array(@object['cc'])).uniq.each do |audience|
|
||||
next if audience == ActivityPub::TagManager::COLLECTIONS[:public]
|
||||
@ -310,6 +361,8 @@ class ActivityPub::Activity::Create < ActivityPub::Activity
|
||||
def text_from_content
|
||||
return Formatter.instance.linkify([[text_from_name, text_from_summary.presence].compact.join("\n\n"), object_url || @object['id']].join(' ')) if converted_object_type?
|
||||
|
||||
return Formatter.instance.format_article(@object['content']) if @object['content'].present? && @object['type'] == 'Article'
|
||||
|
||||
if @object['content'].present?
|
||||
@object['content']
|
||||
elsif content_language_map?
|
||||
|
@ -90,6 +90,11 @@ class Formatter
|
||||
html.html_safe # rubocop:disable Rails/OutputSafety
|
||||
end
|
||||
|
||||
def format_article(text)
|
||||
text = text.gsub(/>\n+</, "><")
|
||||
text.html_safe # rubocop:disable Rails/OutputSafety
|
||||
end
|
||||
|
||||
def linkify(text)
|
||||
html = encode_and_link_urls(text)
|
||||
html = simple_format(html, {}, sanitize: false)
|
||||
|
@ -34,13 +34,14 @@ class Sanitize
|
||||
end
|
||||
|
||||
MASTODON_STRICT ||= freeze_config(
|
||||
elements: %w(p br span a abbr del pre blockquote code b strong i em h1 h2 h3 h4 h5 ul ol li),
|
||||
elements: %w(p br span a abbr del pre blockquote code b strong i em h1 h2 h3 h4 h5 ul ol li img),
|
||||
|
||||
attributes: {
|
||||
'a' => %w(href rel class title),
|
||||
'span' => %w(class),
|
||||
'abbr' => %w(title),
|
||||
'blockquote' => %w(cite),
|
||||
'img' => %w(src alt),
|
||||
},
|
||||
|
||||
add_attributes: {
|
||||
|
Loading…
Reference in New Issue
Block a user