diff options
author | Starfall <us@starfall.systems> | 2022-12-13 09:37:37 -0600 |
---|---|---|
committer | Starfall <us@starfall.systems> | 2022-12-13 09:37:37 -0600 |
commit | 9a99643ecd1e11c8763bbcb5c10bd3dfac3dd638 (patch) | |
tree | db93273e38a54683bd41d19f166fe39787b58b67 /app/lib | |
parent | dc9a63381b3498e915d8334728f0951b231527e6 (diff) | |
parent | b0ef980aa17868f18089233060900aa5d5632863 (diff) |
Merge remote-tracking branch 'glitch/main'
Diffstat (limited to 'app/lib')
-rw-r--r-- | app/lib/activitypub/activity/create.rb | 2 | ||||
-rw-r--r-- | app/lib/activitypub/activity/update.rb | 4 | ||||
-rw-r--r-- | app/lib/advanced_text_formatter.rb | 80 | ||||
-rw-r--r-- | app/lib/delivery_failure_tracker.rb | 9 |
4 files changed, 51 insertions, 44 deletions
diff --git a/app/lib/activitypub/activity/create.rb b/app/lib/activitypub/activity/create.rb index ebae12973..4dfbfc665 100644 --- a/app/lib/activitypub/activity/create.rb +++ b/app/lib/activitypub/activity/create.rb @@ -222,7 +222,7 @@ class ActivityPub::Activity::Create < ActivityPub::Activity return if tag['href'].blank? account = account_from_uri(tag['href']) - account = ActivityPub::FetchRemoteAccountService.new.call(tag['href']) if account.nil? + account = ActivityPub::FetchRemoteAccountService.new.call(tag['href'], request_id: @options[:request_id]) if account.nil? return if account.nil? diff --git a/app/lib/activitypub/activity/update.rb b/app/lib/activitypub/activity/update.rb index 5b3238ece..e7c3bc9bf 100644 --- a/app/lib/activitypub/activity/update.rb +++ b/app/lib/activitypub/activity/update.rb @@ -18,7 +18,7 @@ class ActivityPub::Activity::Update < ActivityPub::Activity def update_account return reject_payload! if @account.uri != object_uri - ActivityPub::ProcessAccountService.new.call(@account.username, @account.domain, @object, signed_with_known_key: true) + ActivityPub::ProcessAccountService.new.call(@account.username, @account.domain, @object, signed_with_known_key: true, request_id: @options[:request_id]) end def update_status @@ -28,6 +28,6 @@ class ActivityPub::Activity::Update < ActivityPub::Activity return if @status.nil? - ActivityPub::ProcessStatusUpdateService.new.call(@status, @object) + ActivityPub::ProcessStatusUpdateService.new.call(@status, @object, request_id: @options[:request_id]) end end diff --git a/app/lib/advanced_text_formatter.rb b/app/lib/advanced_text_formatter.rb index dcaf34b91..21e81d4d1 100644 --- a/app/lib/advanced_text_formatter.rb +++ b/app/lib/advanced_text_formatter.rb @@ -19,6 +19,8 @@ class AdvancedTextFormatter < TextFormatter end end + attr_reader :content_type + # @param [String] text # @param [Hash] options # @option options [Boolean] :multiline @@ -27,7 +29,7 @@ class AdvancedTextFormatter < TextFormatter # @option options [Array<Account>] :preloaded_accounts # @option options [String] :content_type def initialize(text, options = {}) - content_type = options.delete(:content_type) + @content_type = options.delete(:content_type) super(text, options) @text = format_markdown(text) if content_type == 'text/markdown' @@ -50,50 +52,50 @@ class AdvancedTextFormatter < TextFormatter html.html_safe # rubocop:disable Rails/OutputSafety end - # Differs from `TextFormatter` by skipping HTML tags and entities - def entities - @entities ||= begin - gaps = [] - total_offset = 0 - - escaped = text.gsub(/<[^>]*>|&#[0-9]+;/) do |match| - total_offset += match.length - 1 - end_offset = Regexp.last_match.end(0) - gaps << [end_offset - total_offset, total_offset] - ' ' - end - - Extractor.extract_entities_with_indices(escaped, extract_url_without_protocol: false).map do |entity| - start_pos, end_pos = entity[:indices] - offset_idx = gaps.rindex { |gap| gap.first <= start_pos } - offset = offset_idx.nil? ? 0 : gaps[offset_idx].last - entity.merge(indices: [start_pos + offset, end_pos + offset]) + # Differs from TextFormatter by operating on the parsed HTML tree + def rewrite + if @tree.nil? + src = text.gsub(Sanitize::REGEX_UNSUITABLE_CHARS, '') + @tree = Nokogiri::HTML5.fragment(src) + document = @tree.document + + @tree.xpath('.//text()[not(ancestor::a | ancestor::code)]').each do |text_node| + # Iterate over text elements and build up their replacements. + content = text_node.content + replacement = Nokogiri::XML::NodeSet.new(document) + processed_index = 0 + Extractor.extract_entities_with_indices( + content, + extract_url_without_protocol: false + ) do |entity| + # Iterate over entities in this text node. + advance = entity[:indices].first - processed_index + if advance.positive? + # Text node for content which precedes entity. + replacement << Nokogiri::XML::Text.new( + content[processed_index, advance], + document + ) + end + replacement << Nokogiri::HTML5.fragment(yield(entity)) + processed_index = entity[:indices].last + end + if processed_index < content.size + # Text node for remaining content. + replacement << Nokogiri::XML::Text.new( + content[processed_index, content.size - processed_index], + document + ) + end + text_node.replace(replacement) end end + + Sanitize.node!(@tree, Sanitize::Config::MASTODON_OUTGOING).to_html end private - # Differs from `TextFormatter` in that it keeps HTML; but it sanitizes at the end to remain safe - def rewrite - entities.sort_by! do |entity| - entity[:indices].first - end - - result = ''.dup - - last_index = entities.reduce(0) do |index, entity| - indices = entity[:indices] - result << text[index...indices.first] - result << yield(entity) - indices.last - end - - result << text[last_index..-1] - - Sanitize.fragment(result, Sanitize::Config::MASTODON_OUTGOING) - end - def format_markdown(html) html = markdown_formatter.render(html) html.delete("\r").delete("\n") diff --git a/app/lib/delivery_failure_tracker.rb b/app/lib/delivery_failure_tracker.rb index 7c4e28eb7..66c1fd8c0 100644 --- a/app/lib/delivery_failure_tracker.rb +++ b/app/lib/delivery_failure_tracker.rb @@ -65,8 +65,13 @@ class DeliveryFailureTracker domains - UnavailableDomain.all.pluck(:domain) end - def warning_domains_map - warning_domains.index_with { |domain| redis.scard(exhausted_deliveries_key_by(domain)) } + def warning_domains_map(domains = nil) + if domains.nil? + warning_domains.index_with { |domain| redis.scard(exhausted_deliveries_key_by(domain)) } + else + domains -= UnavailableDomain.where(domain: domains).pluck(:domain) + domains.index_with { |domain| redis.scard(exhausted_deliveries_key_by(domain)) }.filter { |_, days| days.positive? } + end end private |