Language code on mastoapi statuses (#433)
[akkoma] / lib / pleroma / web / common_api / activity_draft.ex
index 8668b600ebd5b69f75bbf2556986c89fe1dae21f..adddea669f59595415255f73cb5cec0854b39da9 100644 (file)
@@ -5,6 +5,8 @@
 defmodule Pleroma.Web.CommonAPI.ActivityDraft do
   alias Pleroma.Activity
   alias Pleroma.Conversation.Participation
+  alias Pleroma.Object
+  alias Pleroma.Web.ActivityPub.Builder
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.CommonAPI.Utils
 
@@ -20,6 +22,10 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
             attachments: [],
             in_reply_to: nil,
             in_reply_to_conversation: nil,
+            language: nil,
+            content_map: %{},
+            quote_id: nil,
+            quote: nil,
             visibility: nil,
             expires_at: nil,
             extra: nil,
@@ -52,7 +58,9 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
     |> with_valid(&in_reply_to/1)
     |> with_valid(&in_reply_to_conversation/1)
     |> with_valid(&visibility/1)
+    |> with_valid(&quote_id/1)
     |> content()
+    |> with_valid(&language/1)
     |> with_valid(&to_and_cc/1)
     |> with_valid(&context/1)
     |> sensitive()
@@ -62,30 +70,6 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
     |> validate()
   end
 
-  def listen(user, params) do
-    user
-    |> new(params)
-    |> visibility()
-    |> to_and_cc()
-    |> context()
-    |> listen_object()
-    |> with_valid(&changes/1)
-    |> validate()
-  end
-
-  defp listen_object(draft) do
-    object =
-      draft.params
-      |> Map.take([:album, :artist, :title, :length])
-      |> Map.new(fn {key, value} -> {to_string(key), value} end)
-      |> Map.put("type", "Audio")
-      |> Map.put("to", draft.to)
-      |> Map.put("cc", draft.cc)
-      |> Map.put("actor", draft.user.ap_id)
-
-    %__MODULE__{draft | object: object}
-  end
-
   defp put_params(draft, params) do
     params = Map.put_new(params, :in_reply_to_status_id, params[:in_reply_to_id])
     %__MODULE__{draft | params: params}
@@ -130,6 +114,39 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
     %__MODULE__{draft | in_reply_to_conversation: in_reply_to_conversation}
   end
 
+  defp quote_id(%{params: %{quote_id: ""}} = draft), do: draft
+
+  defp quote_id(%{params: %{quote_id: id}} = draft) when is_binary(id) do
+    with {:activity, %Activity{} = quote} <- {:activity, Activity.get_by_id(id)},
+         visibility <- CommonAPI.get_quoted_visibility(quote),
+         {:visibility, true} <- {:visibility, visibility in ["public", "unlisted"]} do
+      %__MODULE__{draft | quote: Activity.get_by_id(id)}
+    else
+      {:activity, _} ->
+        add_error(draft, dgettext("errors", "You can't quote a status that doesn't exist"))
+
+      {:visibility, false} ->
+        add_error(draft, dgettext("errors", "You can only quote public or unlisted statuses"))
+    end
+  end
+
+  defp quote_id(%{params: %{quote_id: %Activity{} = quote}} = draft) do
+    %__MODULE__{draft | quote: quote}
+  end
+
+  defp quote_id(draft), do: draft
+
+  defp language(%{params: %{language: language}, content_html: content} = draft)
+       when is_binary(language) do
+    if Pleroma.ISO639.valid_alpha2?(language) do
+      %__MODULE__{draft | content_map: %{language => content}}
+    else
+      add_error(draft, dgettext("errors", "Invalid language"))
+    end
+  end
+
+  defp language(draft), do: draft
+
   defp visibility(%{params: params} = draft) do
     case CommonAPI.get_visibility(params, draft.in_reply_to, draft.in_reply_to_conversation) do
       {visibility, "direct"} when visibility != "direct" ->
@@ -174,7 +191,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
   end
 
   defp context(draft) do
-    context = Utils.make_context(draft.in_reply_to, draft.in_reply_to_conversation)
+    context = Utils.make_context(draft)
     %__MODULE__{draft | context: context}
   end
 
@@ -186,17 +203,48 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
   defp object(draft) do
     emoji = Map.merge(Pleroma.Emoji.Formatter.get_emoji_map(draft.full_payload), draft.emoji)
 
+    # Sometimes people create posts with subject containing emoji,
+    # since subjects are usually copied this will result in a broken
+    # subject when someone replies from an instance that does not have
+    # the emoji or has it under different shortcode. This is an attempt
+    # to mitigate this by copying emoji from inReplyTo if they are present
+    # in the subject.
+    summary_emoji =
+      with %Activity{} <- draft.in_reply_to,
+           %Object{data: %{"tag" => [_ | _] = tag}} <- Object.normalize(draft.in_reply_to) do
+        Enum.reduce(tag, %{}, fn
+          %{"type" => "Emoji", "name" => name, "icon" => %{"url" => url}}, acc ->
+            if String.contains?(draft.summary, name) do
+              Map.put(acc, name, url)
+            else
+              acc
+            end
+
+          _, acc ->
+            acc
+        end)
+      else
+        _ -> %{}
+      end
+
+    emoji = Map.merge(emoji, summary_emoji)
+    {:ok, note_data, _meta} = Builder.note(draft)
+
     object =
-      Utils.make_note_data(draft)
+      note_data
       |> Map.put("emoji", emoji)
-      |> Map.put("source", draft.status)
+      |> Map.put("source", %{
+        "content" => draft.status,
+        "mediaType" => Utils.get_content_type(draft.params[:content_type])
+      })
       |> Map.put("generator", draft.params[:generator])
+      |> Map.put("contentMap", draft.content_map)
 
     %__MODULE__{draft | object: object}
   end
 
   defp preview?(draft) do
-    preview? = Pleroma.Web.ControllerHelper.truthy_param?(draft.params[:preview])
+    preview? = Pleroma.Web.Utils.Params.truthy_param?(draft.params[:preview])
     %__MODULE__{draft | preview?: preview?}
   end