Files
jcoffey-dev 319ed76dbd
Crowdin / Upload translations / upload-translations (push) Skipped
Historical data migration test / test (14-alpine) (push) Canceled after 0s
Historical data migration test / test (15-alpine) (push) Canceled after 0s
Historical data migration test / test (16-alpine) (push) Canceled after 0s
Historical data migration test / test (17-alpine) (push) Canceled after 0s
Bundler Audit / security (push) Canceled after 0s
Haml Linting / lint (push) Canceled after 0s
JavaScript Linting / lint (push) Canceled after 0s
Ruby Linting / lint (push) Canceled after 0s
JavaScript Testing / test (push) Canceled after 0s
Ruby Testing / build (production) (push) Canceled after 0s
Ruby Testing / test (.ruby-version) (push) Canceled after 0s
Ruby Testing / test (3.3) (push) Canceled after 0s
Ruby Testing / test (3.4) (push) Canceled after 0s
Ruby Testing / End to End testing (.ruby-version) (push) Canceled after 0s
Ruby Testing / End to End testing (3.3) (push) Canceled after 0s
Ruby Testing / End to End testing (3.4) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check i18n / check-i18n (push) Canceled after 0s
CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (javascript) (push) Canceled after 0s
CodeQL / Analyze (ruby) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, opensearchproject/opensearch:2) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.3, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (3.4, docker.elastic.co/elasticsearch/elasticsearch:7.17.29) (push) Canceled after 0s
Check formatting / lint (push) Canceled after 0s
CSS Linting / lint (push) Canceled after 0s
Ruby Testing / Elastic Search integration testing (.ruby-version, docker.elastic.co/elasticsearch/elasticsearch:8.19.2) (push) Canceled after 0s
Chromatic / Check for relevant changes (push) Canceled after 0s
Chromatic / Run Chromatic (push) Canceled after 0s
Import Mastodon v4.7.3
Snapshot of upstream tag v4.7.3 (commit 8ff11c2bb7b9f0714e947b10fa3629c6542c7121) from
https://github.com/mastodon/mastodon, unmodified.
2026-10-05 22:03:13 -07:00

68 lines
1.6 KiB
Ruby

# frozen_string_literal: true
class Webhooks::PayloadRenderer
class DocumentTraverser
INT_REGEX = /[0-9]+/
def initialize(document)
@document = document.with_indifferent_access
end
def get(path)
value = @document.dig(*parse_path(path))
string = JSON.generate(value)
# We want to make sure people can use the variable inside
# other strings, so it can't be wrapped in quotes.
if value.is_a?(String)
string[1...-1]
else
string
end
end
private
def parse_path(path)
path.split('.').filter_map do |segment|
if segment.match(INT_REGEX)
segment.to_i
else
segment.presence
end
end
end
end
class TemplateParser < Parslet::Parser
rule(:dot) { str('.') }
rule(:digit) { match('[0-9]') }
rule(:property_name) { match('[a-z_]').repeat(1) }
rule(:array_index) { digit.repeat(1) }
rule(:segment) { property_name | array_index }
rule(:path) { property_name >> (dot >> segment).repeat }
rule(:variable) { (str('}}').absent? >> path).repeat.as(:variable) }
rule(:expression) { str('{{') >> variable >> str('}}') }
rule(:text) { (str('{{').absent? >> any).repeat(1) }
rule(:text_with_expressions) { (text.as(:text) | expression).repeat.as(:text) }
root(:text_with_expressions)
end
EXPRESSION_REGEXP = /
\{\{
[a-z_]+
(\.
([a-z_]+|[0-9]+)
)*
\}\}
/iox
def initialize(json)
@document = DocumentTraverser.new(JSON.parse(json))
end
def render(template)
template.gsub(EXPRESSION_REGEXP) { |match| @document.get(match[2...-2]) }
end
end