Allow to specify post language
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
0ff7f6d03c
commit
ff35b13b52
8 changed files with 120 additions and 23 deletions
|
@ -22,7 +22,8 @@ defmodule Pleroma.Constants do
|
|||
"generator",
|
||||
"assigned_account",
|
||||
"rules",
|
||||
"content_type"
|
||||
"content_type",
|
||||
"language"
|
||||
]
|
||||
)
|
||||
|
||||
|
@ -41,7 +42,8 @@ defmodule Pleroma.Constants do
|
|||
"summary",
|
||||
"sensitive",
|
||||
"attachment",
|
||||
"generator"
|
||||
"generator",
|
||||
"language"
|
||||
]
|
||||
)
|
||||
|
||||
|
|
|
@ -772,6 +772,7 @@ def prepare_object(object) do
|
|||
|> add_mention_tags
|
||||
|> add_emoji_tags
|
||||
|> add_attributed_to
|
||||
|> maybe_add_content_map
|
||||
|> prepare_attachments
|
||||
|> set_conversation
|
||||
|> set_reply_to_uri
|
||||
|
@ -817,7 +818,7 @@ def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
|
|||
data =
|
||||
data
|
||||
|> Map.put("object", object)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
|> Map.merge(Utils.make_json_ld_header(data))
|
||||
|> Map.delete("bcc")
|
||||
|
||||
{:ok, data}
|
||||
|
@ -832,7 +833,7 @@ def prepare_outgoing(%{"type" => "Update", "object" => %{"type" => objtype} = ob
|
|||
data =
|
||||
data
|
||||
|> Map.put("object", object)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
|> Map.merge(Utils.make_json_ld_header(data))
|
||||
|> Map.delete("bcc")
|
||||
|
||||
{:ok, data}
|
||||
|
@ -853,7 +854,7 @@ def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => objec
|
|||
data =
|
||||
data
|
||||
|> strip_internal_fields
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
|> Map.merge(Utils.make_json_ld_header(data))
|
||||
|> Map.delete("bcc")
|
||||
|
||||
{:ok, data}
|
||||
|
@ -873,7 +874,7 @@ def prepare_outgoing(%{"type" => "Accept"} = data) do
|
|||
data =
|
||||
data
|
||||
|> Map.put("object", object)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
|> Map.merge(Utils.make_json_ld_header(data))
|
||||
|
||||
{:ok, data}
|
||||
end
|
||||
|
@ -891,7 +892,7 @@ def prepare_outgoing(%{"type" => "Reject"} = data) do
|
|||
data =
|
||||
data
|
||||
|> Map.put("object", object)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
|> Map.merge(Utils.make_json_ld_header(data))
|
||||
|
||||
{:ok, data}
|
||||
end
|
||||
|
@ -902,7 +903,7 @@ def prepare_outgoing(%{"type" => _type} = data) do
|
|||
data
|
||||
|> strip_internal_fields
|
||||
|> maybe_fix_object_url
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
|> Map.merge(Utils.make_json_ld_header(data))
|
||||
|
||||
{:ok, data}
|
||||
end
|
||||
|
@ -1088,4 +1089,11 @@ def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
|
|||
def maybe_fix_user_url(data), do: 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
|
||||
end
|
||||
|
|
|
@ -19,6 +19,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|
|||
alias Pleroma.Web.Router.Helpers
|
||||
|
||||
import Ecto.Query
|
||||
import Pleroma.Web.Utils.Guards, only: [not_empty_string: 1]
|
||||
|
||||
require Logger
|
||||
require Pleroma.Constants
|
||||
|
@ -107,18 +108,24 @@ def maybe_splice_recipient(ap_id, params) do
|
|||
end
|
||||
end
|
||||
|
||||
def make_json_ld_header do
|
||||
def make_json_ld_header(data \\ %{}) do
|
||||
%{
|
||||
"@context" => [
|
||||
"https://www.w3.org/ns/activitystreams",
|
||||
"#{Endpoint.url()}/schemas/litepub-0.1.jsonld",
|
||||
%{
|
||||
"@language" => "und"
|
||||
"@language" => get_language(data)
|
||||
}
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
defp get_language(%{"language" => language}) when not_empty_string(language) do
|
||||
language
|
||||
end
|
||||
|
||||
defp get_language(_), do: "und"
|
||||
|
||||
def make_date do
|
||||
DateTime.utc_now() |> DateTime.to_iso8601()
|
||||
end
|
||||
|
|
|
@ -9,7 +9,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectView do
|
|||
alias Pleroma.Web.ActivityPub.Transmogrifier
|
||||
|
||||
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)
|
||||
Map.merge(base, additional)
|
||||
|
@ -17,7 +17,7 @@ def render("object.json", %{object: %Object{} = object}) do
|
|||
|
||||
def render("object.json", %{object: %Activity{data: %{"type" => activity_type}} = activity})
|
||||
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)
|
||||
|
||||
additional =
|
||||
|
@ -28,7 +28,7 @@ def render("object.json", %{object: %Activity{data: %{"type" => activity_type}}
|
|||
end
|
||||
|
||||
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)
|
||||
|
||||
additional =
|
||||
|
|
|
@ -14,6 +14,15 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
|
|||
import Pleroma.Web.Gettext
|
||||
import Pleroma.Web.Utils.Guards, only: [not_empty_string: 1]
|
||||
|
||||
@supported_locales ~w(
|
||||
aa ab ae af ak am an ar as av ay az ba be bg bh bi bm bn bo br bs ca ce ch co cr cs cu cv cy da
|
||||
de dv dz ee el en eo es et eu fa ff fi fj fo fr fy ga gd gl gu gv ha he hi ho hr ht hu hy hz ia
|
||||
id ie ig ii ik io is it iu ja jv ka kg ki kj kk kl km kn ko kr ks ku kv kw ky la lb lg li ln lo
|
||||
lt lu lv mg mh mi mk ml mn mr ms mt my na nb nd ne ng nl nn no nr nv ny oc oj om or os pa pi pl
|
||||
ps pt qu rm rn ro ru rw sa sc sd se sg si sk sl sn so sq sr ss st su sv sw ta te tg th ti tk tl
|
||||
tn to tr ts tt tw ty ug uk ur uz ve vi vo wa wo xh yi yo za zh zu ast ckb kab kmr zgh
|
||||
)
|
||||
|
||||
defstruct valid?: true,
|
||||
errors: [],
|
||||
user: nil,
|
||||
|
@ -36,6 +45,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
|
|||
cc: [],
|
||||
context: nil,
|
||||
sensitive: false,
|
||||
language: nil,
|
||||
object: nil,
|
||||
preview?: false,
|
||||
changes: %{}
|
||||
|
@ -62,6 +72,7 @@ def create(user, params) do
|
|||
|> content()
|
||||
|> with_valid(&to_and_cc/1)
|
||||
|> with_valid(&context/1)
|
||||
|> with_valid(&language/1)
|
||||
|> sensitive()
|
||||
|> with_valid(&object/1)
|
||||
|> preview?()
|
||||
|
@ -224,6 +235,18 @@ defp sensitive(draft) do
|
|||
%__MODULE__{draft | sensitive: sensitive}
|
||||
end
|
||||
|
||||
defp language(%{params: %{language: language}} = draft) when not_empty_string(language) do
|
||||
case language |> String.split("_") |> Enum.at(0) do
|
||||
locale when locale in @supported_locales ->
|
||||
%__MODULE__{draft | language: locale}
|
||||
|
||||
_ ->
|
||||
draft
|
||||
end
|
||||
end
|
||||
|
||||
defp language(draft), do: draft
|
||||
|
||||
defp object(draft) do
|
||||
emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
|
||||
|
||||
|
@ -264,6 +287,7 @@ defp object(draft) do
|
|||
})
|
||||
|> Map.put("generator", draft.params[:generator])
|
||||
|> Map.put("content_type", draft.params[:content_type])
|
||||
|> Map.put("language", draft.language)
|
||||
|
||||
%__MODULE__{draft | object: object}
|
||||
end
|
||||
|
|
|
@ -220,6 +220,36 @@ test "it works for incoming notices with contentMap" do
|
|||
"<p><span class=\"h-card\"><a href=\"http://localtesting.pleroma.lol/users/lain\" class=\"u-url mention\">@<span>lain</span></a></span></p>"
|
||||
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
|
||||
data = File.read!("test/fixtures/kroeg-post-activity.json") |> Jason.decode!()
|
||||
|
||||
|
|
|
@ -392,6 +392,18 @@ test "it prepares a quote post" do
|
|||
assert modified["object"]["quoteUrl"] == quote_id
|
||||
assert modified["object"]["quoteUri"] == quote_id
|
||||
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
|
||||
end
|
||||
|
||||
describe "user upgrade" do
|
||||
|
|
|
@ -138,7 +138,8 @@ test "does not adress actor's follower address if the activity is not public", %
|
|||
end
|
||||
end
|
||||
|
||||
test "make_json_ld_header/0" do
|
||||
describe "make_json_ld_header/1" do
|
||||
test "makes jsonld header" do
|
||||
assert Utils.make_json_ld_header() == %{
|
||||
"@context" => [
|
||||
"https://www.w3.org/ns/activitystreams",
|
||||
|
@ -150,6 +151,19 @@ test "make_json_ld_header/0" do
|
|||
}
|
||||
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
|
||||
|
||||
describe "get_existing_votes" do
|
||||
test "fetches existing votes" do
|
||||
user = insert(:user)
|
||||
|
|
Loading…
Reference in a new issue