Allow to specify post language
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
b729a8b140
commit
4745a41393
14 changed files with 279 additions and 25 deletions
1
changelog.d/post-languages.add
Normal file
1
changelog.d/post-languages.add
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
Allow to specify post language
|
||||||
|
|
@ -19,7 +19,8 @@ defmodule Pleroma.Constants do
|
||||||
"context_id",
|
"context_id",
|
||||||
"deleted_activity_id",
|
"deleted_activity_id",
|
||||||
"pleroma_internal",
|
"pleroma_internal",
|
||||||
"generator"
|
"generator",
|
||||||
|
"language"
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -38,7 +39,8 @@ defmodule Pleroma.Constants do
|
||||||
"summary",
|
"summary",
|
||||||
"sensitive",
|
"sensitive",
|
||||||
"attachment",
|
"attachment",
|
||||||
"generator"
|
"generator",
|
||||||
|
"language"
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator do
|
||||||
|> fix_attachments()
|
|> fix_attachments()
|
||||||
|> Transmogrifier.fix_emoji()
|
|> Transmogrifier.fix_emoji()
|
||||||
|> Transmogrifier.fix_content_map()
|
|> Transmogrifier.fix_content_map()
|
||||||
|
|> Transmogrifier.maybe_add_language()
|
||||||
end
|
end
|
||||||
|
|
||||||
def changeset(struct, data) do
|
def changeset(struct, data) do
|
||||||
|
|
|
||||||
|
|
@ -57,6 +57,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonFields do
|
||||||
field(:replies_count, :integer, default: 0)
|
field(:replies_count, :integer, default: 0)
|
||||||
field(:like_count, :integer, default: 0)
|
field(:like_count, :integer, default: 0)
|
||||||
field(:announcement_count, :integer, default: 0)
|
field(:announcement_count, :integer, default: 0)
|
||||||
|
field(:language, :string)
|
||||||
field(:inReplyTo, ObjectValidators.ObjectID)
|
field(:inReplyTo, ObjectValidators.ObjectID)
|
||||||
field(:url, ObjectValidators.BareUri)
|
field(:url, ObjectValidators.BareUri)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|
||||||
alias Pleroma.Web.Federator
|
alias Pleroma.Web.Federator
|
||||||
|
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
import Pleroma.Web.CommonAPI.Utils, only: [is_good_locale_code?: 1]
|
||||||
|
import Pleroma.Web.Utils.Guards, only: [not_empty_string: 1]
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
require Pleroma.Constants
|
require Pleroma.Constants
|
||||||
|
|
@ -42,6 +44,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|
||||||
|> fix_content_map()
|
|> fix_content_map()
|
||||||
|> fix_addressing()
|
|> fix_addressing()
|
||||||
|> fix_summary()
|
|> fix_summary()
|
||||||
|
|> maybe_add_language()
|
||||||
end
|
end
|
||||||
|
|
||||||
def fix_summary(%{"summary" => nil} = object) do
|
def fix_summary(%{"summary" => nil} = object) do
|
||||||
|
|
@ -318,6 +321,8 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|
||||||
|
|
||||||
def fix_tag(object), do: object
|
def fix_tag(object), do: object
|
||||||
|
|
||||||
|
def fix_content_map(%{"content" => content} = object) when not_empty_string(content), do: object
|
||||||
|
|
||||||
# content map usually only has one language so this will do for now.
|
# content map usually only has one language so this will do for now.
|
||||||
def fix_content_map(%{"contentMap" => content_map} = object) do
|
def fix_content_map(%{"contentMap" => content_map} = object) do
|
||||||
content_groups = Map.to_list(content_map)
|
content_groups = Map.to_list(content_map)
|
||||||
|
|
@ -454,6 +459,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|
||||||
|> strip_internal_fields()
|
|> strip_internal_fields()
|
||||||
|> fix_type(fetch_options)
|
|> fix_type(fetch_options)
|
||||||
|> fix_in_reply_to(fetch_options)
|
|> fix_in_reply_to(fetch_options)
|
||||||
|
|> maybe_add_language_from_activity(data)
|
||||||
|
|
||||||
data = Map.put(data, "object", object)
|
data = Map.put(data, "object", object)
|
||||||
options = Keyword.put(options, :local, false)
|
options = Keyword.put(options, :local, false)
|
||||||
|
|
@ -679,6 +685,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|
||||||
|> add_mention_tags
|
|> add_mention_tags
|
||||||
|> add_emoji_tags
|
|> add_emoji_tags
|
||||||
|> add_attributed_to
|
|> add_attributed_to
|
||||||
|
|> maybe_add_content_map
|
||||||
|> prepare_attachments
|
|> prepare_attachments
|
||||||
|> set_conversation
|
|> set_conversation
|
||||||
|> set_reply_to_uri
|
|> set_reply_to_uri
|
||||||
|
|
@ -722,7 +729,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|
||||||
data =
|
data =
|
||||||
data
|
data
|
||||||
|> Map.put("object", object)
|
|> Map.put("object", object)
|
||||||
|> Map.merge(Utils.make_json_ld_header())
|
|> Map.merge(Utils.make_json_ld_header(data))
|
||||||
|> Map.delete("bcc")
|
|> Map.delete("bcc")
|
||||||
|
|
||||||
{:ok, data}
|
{:ok, data}
|
||||||
|
|
@ -737,7 +744,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|
||||||
data =
|
data =
|
||||||
data
|
data
|
||||||
|> Map.put("object", object)
|
|> Map.put("object", object)
|
||||||
|> Map.merge(Utils.make_json_ld_header())
|
|> Map.merge(Utils.make_json_ld_header(data))
|
||||||
|> Map.delete("bcc")
|
|> Map.delete("bcc")
|
||||||
|
|
||||||
{:ok, data}
|
{:ok, data}
|
||||||
|
|
@ -758,7 +765,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|
||||||
data =
|
data =
|
||||||
data
|
data
|
||||||
|> strip_internal_fields
|
|> strip_internal_fields
|
||||||
|> Map.merge(Utils.make_json_ld_header())
|
|> Map.merge(Utils.make_json_ld_header(data))
|
||||||
|> Map.delete("bcc")
|
|> Map.delete("bcc")
|
||||||
|
|
||||||
{:ok, data}
|
{:ok, data}
|
||||||
|
|
@ -778,7 +785,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|
||||||
data =
|
data =
|
||||||
data
|
data
|
||||||
|> Map.put("object", object)
|
|> Map.put("object", object)
|
||||||
|> Map.merge(Utils.make_json_ld_header())
|
|> Map.merge(Utils.make_json_ld_header(data))
|
||||||
|
|
||||||
{:ok, data}
|
{:ok, data}
|
||||||
end
|
end
|
||||||
|
|
@ -796,7 +803,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|
||||||
data =
|
data =
|
||||||
data
|
data
|
||||||
|> Map.put("object", object)
|
|> Map.put("object", object)
|
||||||
|> Map.merge(Utils.make_json_ld_header())
|
|> Map.merge(Utils.make_json_ld_header(data))
|
||||||
|
|
||||||
{:ok, data}
|
{:ok, data}
|
||||||
end
|
end
|
||||||
|
|
@ -807,7 +814,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|
||||||
data
|
data
|
||||||
|> strip_internal_fields
|
|> strip_internal_fields
|
||||||
|> maybe_fix_object_url
|
|> maybe_fix_object_url
|
||||||
|> Map.merge(Utils.make_json_ld_header())
|
|> Map.merge(Utils.make_json_ld_header(data))
|
||||||
|
|
||||||
{:ok, data}
|
{:ok, data}
|
||||||
end
|
end
|
||||||
|
|
@ -952,4 +959,63 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|
||||||
def maybe_fix_user_url(data), do: data
|
def maybe_fix_user_url(data), do: data
|
||||||
|
|
||||||
def maybe_fix_user_object(data), do: maybe_fix_user_url(data)
|
def maybe_fix_user_object(data), do: maybe_fix_user_url(data)
|
||||||
|
|
||||||
|
defp maybe_add_content_map(%{"language" => language, "content" => content} = object)
|
||||||
|
when not_empty_string(language) do
|
||||||
|
Map.put(object, "contentMap", Map.put(%{}, language, content))
|
||||||
|
end
|
||||||
|
|
||||||
|
defp maybe_add_content_map(object), do: object
|
||||||
|
|
||||||
|
def maybe_add_language(object) do
|
||||||
|
language =
|
||||||
|
[
|
||||||
|
get_language_from_context(object),
|
||||||
|
get_language_from_content_map(object),
|
||||||
|
get_language_from_content(object)
|
||||||
|
]
|
||||||
|
|> Enum.find(&is_good_locale_code?(&1))
|
||||||
|
|
||||||
|
if language do
|
||||||
|
Map.put(object, "language", language)
|
||||||
|
else
|
||||||
|
object
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def maybe_add_language_from_activity(object, activity) do
|
||||||
|
language = get_language_from_context(activity)
|
||||||
|
|
||||||
|
if is_good_locale_code?(language) do
|
||||||
|
Map.put(object, "language", language)
|
||||||
|
else
|
||||||
|
object
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp get_language_from_context(%{"@context" => context}) when is_list(context) do
|
||||||
|
case context
|
||||||
|
|> Enum.find(fn
|
||||||
|
%{"@language" => language} -> language != "und"
|
||||||
|
_ -> nil
|
||||||
|
end) do
|
||||||
|
%{"@language" => language} -> language
|
||||||
|
_ -> nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp get_language_from_context(_), do: nil
|
||||||
|
|
||||||
|
defp get_language_from_content_map(%{"contentMap" => content_map, "content" => source_content}) do
|
||||||
|
content_groups = Map.to_list(content_map)
|
||||||
|
|
||||||
|
case Enum.find(content_groups, fn {_, content} -> content == source_content end) do
|
||||||
|
{language, _} -> language
|
||||||
|
_ -> nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp get_language_from_content_map(_), do: nil
|
||||||
|
|
||||||
|
defp get_language_from_content(_), do: nil
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|
||||||
alias Pleroma.Web.Router.Helpers
|
alias Pleroma.Web.Router.Helpers
|
||||||
|
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
import Pleroma.Web.Utils.Guards, only: [not_empty_string: 1]
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
require Pleroma.Constants
|
require Pleroma.Constants
|
||||||
|
|
@ -108,18 +109,24 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def make_json_ld_header do
|
def make_json_ld_header(data \\ %{}) do
|
||||||
%{
|
%{
|
||||||
"@context" => [
|
"@context" => [
|
||||||
"https://www.w3.org/ns/activitystreams",
|
"https://www.w3.org/ns/activitystreams",
|
||||||
"#{Endpoint.url()}/schemas/litepub-0.1.jsonld",
|
"#{Endpoint.url()}/schemas/litepub-0.1.jsonld",
|
||||||
%{
|
%{
|
||||||
"@language" => "und"
|
"@language" => get_language(data)
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp get_language(%{"language" => language}) when not_empty_string(language) do
|
||||||
|
language
|
||||||
|
end
|
||||||
|
|
||||||
|
defp get_language(_), do: "und"
|
||||||
|
|
||||||
def make_date do
|
def make_date do
|
||||||
DateTime.utc_now() |> DateTime.to_iso8601()
|
DateTime.utc_now() |> DateTime.to_iso8601()
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectView do
|
||||||
alias Pleroma.Web.ActivityPub.Transmogrifier
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
||||||
|
|
||||||
def render("object.json", %{object: %Object{} = object}) do
|
def render("object.json", %{object: %Object{} = object}) do
|
||||||
base = Pleroma.Web.ActivityPub.Utils.make_json_ld_header()
|
base = Pleroma.Web.ActivityPub.Utils.make_json_ld_header(object.data)
|
||||||
|
|
||||||
additional = Transmogrifier.prepare_object(object.data)
|
additional = Transmogrifier.prepare_object(object.data)
|
||||||
Map.merge(base, additional)
|
Map.merge(base, additional)
|
||||||
|
|
@ -17,7 +17,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectView do
|
||||||
|
|
||||||
def render("object.json", %{object: %Activity{data: %{"type" => activity_type}} = activity})
|
def render("object.json", %{object: %Activity{data: %{"type" => activity_type}} = activity})
|
||||||
when activity_type in ["Create", "Listen"] do
|
when activity_type in ["Create", "Listen"] do
|
||||||
base = Pleroma.Web.ActivityPub.Utils.make_json_ld_header()
|
base = Pleroma.Web.ActivityPub.Utils.make_json_ld_header(activity.data)
|
||||||
object = Object.normalize(activity, fetch: false)
|
object = Object.normalize(activity, fetch: false)
|
||||||
|
|
||||||
additional =
|
additional =
|
||||||
|
|
@ -28,7 +28,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectView do
|
||||||
end
|
end
|
||||||
|
|
||||||
def render("object.json", %{object: %Activity{} = activity}) do
|
def render("object.json", %{object: %Activity{} = activity}) do
|
||||||
base = Pleroma.Web.ActivityPub.Utils.make_json_ld_header()
|
base = Pleroma.Web.ActivityPub.Utils.make_json_ld_header(activity.data)
|
||||||
object_id = Object.normalize(activity, id_only: true)
|
object_id = Object.normalize(activity, id_only: true)
|
||||||
|
|
||||||
additional =
|
additional =
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
|
||||||
cc: [],
|
cc: [],
|
||||||
context: nil,
|
context: nil,
|
||||||
sensitive: false,
|
sensitive: false,
|
||||||
|
language: nil,
|
||||||
object: nil,
|
object: nil,
|
||||||
preview?: false,
|
preview?: false,
|
||||||
changes: %{}
|
changes: %{}
|
||||||
|
|
@ -57,6 +58,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
|
||||||
|> content()
|
|> content()
|
||||||
|> with_valid(&to_and_cc/1)
|
|> with_valid(&to_and_cc/1)
|
||||||
|> with_valid(&context/1)
|
|> with_valid(&context/1)
|
||||||
|
|> with_valid(&language/1)
|
||||||
|> sensitive()
|
|> sensitive()
|
||||||
|> with_valid(&object/1)
|
|> with_valid(&object/1)
|
||||||
|> preview?()
|
|> preview?()
|
||||||
|
|
@ -190,6 +192,16 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
|
||||||
%__MODULE__{draft | sensitive: sensitive}
|
%__MODULE__{draft | sensitive: sensitive}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp language(draft) do
|
||||||
|
language = draft.params[:language]
|
||||||
|
|
||||||
|
if Utils.is_good_locale_code?(language) do
|
||||||
|
%__MODULE__{draft | language: language}
|
||||||
|
else
|
||||||
|
draft
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp object(draft) do
|
defp object(draft) do
|
||||||
emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
|
emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
|
||||||
|
|
||||||
|
|
@ -229,6 +241,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
|
||||||
"mediaType" => Utils.get_content_type(draft.params[:content_type])
|
"mediaType" => Utils.get_content_type(draft.params[:content_type])
|
||||||
})
|
})
|
||||||
|> Map.put("generator", draft.params[:generator])
|
|> Map.put("generator", draft.params[:generator])
|
||||||
|
|> Map.put("language", draft.language)
|
||||||
|
|
||||||
%__MODULE__{draft | object: object}
|
%__MODULE__{draft | object: object}
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -494,4 +494,20 @@ defmodule Pleroma.Web.CommonAPI.Utils do
|
||||||
{:error, dgettext("errors", "Too many attachments")}
|
{:error, dgettext("errors", "Too many attachments")}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def is_good_locale_code?(code) when is_binary(code) do
|
||||||
|
code
|
||||||
|
|> String.codepoints()
|
||||||
|
|> Enum.all?(&valid_char?/1)
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_good_locale_code?(_code), do: false
|
||||||
|
|
||||||
|
# [a-zA-Z0-9-]
|
||||||
|
defp valid_char?(char) do
|
||||||
|
("a" <= char and char <= "z") or
|
||||||
|
("A" <= char and char <= "Z") or
|
||||||
|
("0" <= char and char <= "9") or
|
||||||
|
char == "-"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -200,7 +200,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
||||||
mentions: mentions,
|
mentions: mentions,
|
||||||
tags: reblogged[:tags] || [],
|
tags: reblogged[:tags] || [],
|
||||||
application: build_application(object.data["generator"]),
|
application: build_application(object.data["generator"]),
|
||||||
language: nil,
|
language: object.data["language"],
|
||||||
emojis: [],
|
emojis: [],
|
||||||
pleroma: %{
|
pleroma: %{
|
||||||
local: activity.local,
|
local: activity.local,
|
||||||
|
|
@ -391,7 +391,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|
||||||
mentions: mentions,
|
mentions: mentions,
|
||||||
tags: build_tags(tags),
|
tags: build_tags(tags),
|
||||||
application: build_application(object.data["generator"]),
|
application: build_application(object.data["generator"]),
|
||||||
language: nil,
|
language: object.data["language"],
|
||||||
emojis: build_emojis(object.data["emoji"]),
|
emojis: build_emojis(object.data["emoji"]),
|
||||||
pleroma: %{
|
pleroma: %{
|
||||||
local: activity.local,
|
local: activity.local,
|
||||||
|
|
|
||||||
|
|
@ -221,6 +221,36 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.NoteHandlingTest do
|
||||||
"<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
|
"<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it only uses contentMap if content is not present" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
message = %{
|
||||||
|
"@context" => "https://www.w3.org/ns/activitystreams",
|
||||||
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||||
|
"cc" => [],
|
||||||
|
"type" => "Create",
|
||||||
|
"object" => %{
|
||||||
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||||
|
"cc" => [],
|
||||||
|
"id" => Utils.generate_object_id(),
|
||||||
|
"type" => "Note",
|
||||||
|
"content" => "Hi",
|
||||||
|
"contentMap" => %{
|
||||||
|
"de" => "Hallo",
|
||||||
|
"uk" => "Привіт"
|
||||||
|
},
|
||||||
|
"inReplyTo" => nil,
|
||||||
|
"attributedTo" => user.ap_id
|
||||||
|
},
|
||||||
|
"actor" => user.ap_id
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(message)
|
||||||
|
object = Object.normalize(data["object"], fetch: false)
|
||||||
|
|
||||||
|
assert object.data["content"] == "Hi"
|
||||||
|
end
|
||||||
|
|
||||||
test "it works for incoming notices with to/cc not being an array (kroeg)" do
|
test "it works for incoming notices with to/cc not being an array (kroeg)" do
|
||||||
data = File.read!("test/fixtures/kroeg-post-activity.json") |> Jason.decode!()
|
data = File.read!("test/fixtures/kroeg-post-activity.json") |> Jason.decode!()
|
||||||
|
|
||||||
|
|
@ -358,6 +388,87 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.NoteHandlingTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it detects language from context" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
message = %{
|
||||||
|
"@context" => ["https://www.w3.org/ns/activitystreams", %{"@language" => "pl"}],
|
||||||
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||||
|
"cc" => [],
|
||||||
|
"type" => "Create",
|
||||||
|
"object" => %{
|
||||||
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||||
|
"cc" => [],
|
||||||
|
"id" => Utils.generate_object_id(),
|
||||||
|
"type" => "Note",
|
||||||
|
"content" => "Szczęść Boże",
|
||||||
|
"attributedTo" => user.ap_id
|
||||||
|
},
|
||||||
|
"actor" => user.ap_id
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(message)
|
||||||
|
object = Object.normalize(data["object"], fetch: false)
|
||||||
|
|
||||||
|
assert object.data["language"] == "pl"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it detects language from contentMap" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
message = %{
|
||||||
|
"@context" => "https://www.w3.org/ns/activitystreams",
|
||||||
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||||
|
"cc" => [],
|
||||||
|
"type" => "Create",
|
||||||
|
"object" => %{
|
||||||
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||||
|
"cc" => [],
|
||||||
|
"id" => Utils.generate_object_id(),
|
||||||
|
"type" => "Note",
|
||||||
|
"content" => "Szczęść Boże",
|
||||||
|
"contentMap" => %{
|
||||||
|
"de" => "Gott segne",
|
||||||
|
"pl" => "Szczęść Boże"
|
||||||
|
},
|
||||||
|
"attributedTo" => user.ap_id
|
||||||
|
},
|
||||||
|
"actor" => user.ap_id
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(message)
|
||||||
|
object = Object.normalize(data["object"], fetch: false)
|
||||||
|
|
||||||
|
assert object.data["language"] == "pl"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "it detects language from content" do
|
||||||
|
clear_config([Pleroma.Language.LanguageDetector, :provider], LanguageDetectorMock)
|
||||||
|
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
message = %{
|
||||||
|
"@context" => ["https://www.w3.org/ns/activitystreams"],
|
||||||
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||||
|
"cc" => [],
|
||||||
|
"type" => "Create",
|
||||||
|
"object" => %{
|
||||||
|
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
|
||||||
|
"cc" => [],
|
||||||
|
"id" => Utils.generate_object_id(),
|
||||||
|
"type" => "Note",
|
||||||
|
"content" => "Dieu vous bénisse, Fédivers.",
|
||||||
|
"attributedTo" => user.ap_id
|
||||||
|
},
|
||||||
|
"actor" => user.ap_id
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(message)
|
||||||
|
object = Object.normalize(data["object"], fetch: false)
|
||||||
|
|
||||||
|
assert object.data["language"] == "fr"
|
||||||
|
end
|
||||||
|
|
||||||
describe "`handle_incoming/2`, Mastodon format `replies` handling" do
|
describe "`handle_incoming/2`, Mastodon format `replies` handling" do
|
||||||
setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
|
setup do: clear_config([:activitypub, :note_replies_output_limit], 5)
|
||||||
setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
|
setup do: clear_config([:instance, :federation_incoming_replies_max_depth])
|
||||||
|
|
|
||||||
|
|
@ -352,6 +352,18 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it adds contentMap if language is specified" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
{:ok, activity} = CommonAPI.post(user, %{status: "тест", language: "uk"})
|
||||||
|
|
||||||
|
{:ok, prepared} = Transmogrifier.prepare_outgoing(activity.data)
|
||||||
|
|
||||||
|
assert prepared["object"]["contentMap"] == %{
|
||||||
|
"uk" => "тест"
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
describe "actor rewriting" do
|
describe "actor rewriting" do
|
||||||
test "it fixes the actor URL property to be a proper URI" do
|
test "it fixes the actor URL property to be a proper URI" do
|
||||||
data = %{
|
data = %{
|
||||||
|
|
|
||||||
|
|
@ -138,16 +138,30 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
test "make_json_ld_header/0" do
|
describe "make_json_ld_header/1" do
|
||||||
assert Utils.make_json_ld_header() == %{
|
test "makes jsonld header" do
|
||||||
"@context" => [
|
assert Utils.make_json_ld_header() == %{
|
||||||
"https://www.w3.org/ns/activitystreams",
|
"@context" => [
|
||||||
"http://localhost:4001/schemas/litepub-0.1.jsonld",
|
"https://www.w3.org/ns/activitystreams",
|
||||||
%{
|
"http://localhost:4001/schemas/litepub-0.1.jsonld",
|
||||||
"@language" => "und"
|
%{
|
||||||
}
|
"@language" => "und"
|
||||||
]
|
}
|
||||||
}
|
]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "includes language if specified" do
|
||||||
|
assert Utils.make_json_ld_header(%{"language" => "pl"}) == %{
|
||||||
|
"@context" => [
|
||||||
|
"https://www.w3.org/ns/activitystreams",
|
||||||
|
"http://localhost:4001/schemas/litepub-0.1.jsonld",
|
||||||
|
%{
|
||||||
|
"@language" => "pl"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "get_existing_votes" do
|
describe "get_existing_votes" do
|
||||||
|
|
|
||||||
|
|
@ -770,6 +770,16 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
|
||||||
assert status.edited_at
|
assert status.edited_at
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it shows post language" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
{:ok, post} = CommonAPI.post(user, %{status: "Szczęść Boże", language: "pl"})
|
||||||
|
|
||||||
|
status = StatusView.render("show.json", activity: post)
|
||||||
|
|
||||||
|
assert status.language == "pl"
|
||||||
|
end
|
||||||
|
|
||||||
test "with a source object" do
|
test "with a source object" do
|
||||||
note =
|
note =
|
||||||
insert(:note,
|
insert(:note,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue