Skip to content

Commit

Permalink
Normalize language code of incoming posts (#30403)
Browse files Browse the repository at this point in the history
  • Loading branch information
ClearlyClaire committed May 23, 2024
1 parent 10ec421 commit 133d98f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
11 changes: 9 additions & 2 deletions app/lib/activitypub/parser/status_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class ActivityPub::Parser::StatusParser
include JsonLdHelper

NORMALIZED_LOCALE_NAMES = LanguagesHelper::SUPPORTED_LOCALES.keys.index_by(&:downcase).freeze

# @param [Hash] json
# @param [Hash] options
# @option options [String] :followers_collection
Expand Down Expand Up @@ -87,6 +89,13 @@ def visibility
end

def language
lang = raw_language_code
lang.presence && NORMALIZED_LOCALE_NAMES.fetch(lang.downcase.to_sym, lang)
end

private

def raw_language_code
if content_language_map?
@object['contentMap'].keys.first
elsif name_language_map?
Expand All @@ -96,8 +105,6 @@ def language
end
end

private

def audience_to
as_array(@object['to'] || @json['to']).map { |x| value_or_id(x) }
end
Expand Down
50 changes: 50 additions & 0 deletions spec/lib/activitypub/parser/status_parser_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe ActivityPub::Parser::StatusParser do
subject { described_class.new(json) }

let(:sender) { Fabricate(:account, followers_url: 'http://example.com/followers', domain: 'example.com', uri: 'https://example.com/actor') }
let(:follower) { Fabricate(:account, username: 'bob') }

let(:json) do
{
'@context': 'https://www.w3.org/ns/activitystreams',
id: [ActivityPub::TagManager.instance.uri_for(sender), '#foo'].join,
type: 'Create',
actor: ActivityPub::TagManager.instance.uri_for(sender),
object: object_json,
}.with_indifferent_access
end

let(:object_json) do
{
id: [ActivityPub::TagManager.instance.uri_for(sender), 'post1'].join('/'),
type: 'Note',
to: [
'https://www.w3.org/ns/activitystreams#Public',
ActivityPub::TagManager.instance.uri_for(follower),
],
content: '@bob lorem ipsum',
contentMap: {
EN: '@bob lorem ipsum',
},
published: 1.hour.ago.utc.iso8601,
updated: 1.hour.ago.utc.iso8601,
tag: {
type: 'Mention',
href: ActivityPub::TagManager.instance.uri_for(follower),
},
}
end

it 'correctly parses status' do
expect(subject).to have_attributes(
text: '@bob lorem ipsum',
uri: [ActivityPub::TagManager.instance.uri_for(sender), 'post1'].join('/'),
reply: false,
language: :en
)
end
end

0 comments on commit 133d98f

Please sign in to comment.