Quote posting (#113)
[akkoma] / lib / pleroma / web / common_api / activity_draft.ex
index 9bcb9f5875d31403898b5d616a24d0b05cd52efa..767b2bf0feaeb5d75b0bb972122315ce898052cd 100644 (file)
@@ -1,10 +1,12 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 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,9 +22,11 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
             attachments: [],
             in_reply_to: nil,
             in_reply_to_conversation: nil,
+            quote_id: nil,
+            quote: nil,
             visibility: nil,
             expires_at: nil,
-            poll: nil,
+            extra: nil,
             emoji: %{},
             content_html: nil,
             mentions: [],
@@ -35,9 +39,14 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
             preview?: false,
             changes: %{}
 
-  def create(user, params) do
+  def new(user, params) do
     %__MODULE__{user: user}
     |> put_params(params)
+  end
+
+  def create(user, params) do
+    user
+    |> new(params)
     |> status()
     |> summary()
     |> with_valid(&attachments/1)
@@ -47,6 +56,7 @@ 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(&to_and_cc/1)
     |> with_valid(&context/1)
@@ -101,6 +111,28 @@ 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 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" ->
@@ -121,7 +153,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
   defp poll(draft) do
     case Utils.make_poll_data(draft.params) do
       {:ok, {poll, poll_emoji}} ->
-        %__MODULE__{draft | poll: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
+        %__MODULE__{draft | extra: poll, emoji: Map.merge(draft.emoji, poll_emoji)}
 
       {:error, message} ->
         add_error(draft, message)
@@ -129,32 +161,18 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
   end
 
   defp content(draft) do
-    {content_html, mentions, tags} =
-      Utils.make_content_html(
-        draft.status,
-        draft.attachments,
-        draft.params,
-        draft.visibility
-      )
-
-    %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
-  end
+    {content_html, mentioned_users, tags} = Utils.make_content_html(draft)
 
-  defp to_and_cc(draft) do
-    addressed_users =
-      draft.mentions
+    mentions =
+      mentioned_users
       |> Enum.map(fn {_, mentioned_user} -> mentioned_user.ap_id end)
       |> Utils.get_addressed_users(draft.params[:to])
 
-    {to, cc} =
-      Utils.get_to_and_cc(
-        draft.user,
-        addressed_users,
-        draft.in_reply_to,
-        draft.visibility,
-        draft.in_reply_to_conversation
-      )
+    %__MODULE__{draft | content_html: content_html, mentions: mentions, tags: tags}
+  end
 
+  defp to_and_cc(draft) do
+    {to, cc} = Utils.get_to_and_cc(draft)
     %__MODULE__{draft | to: to, cc: cc}
   end
 
@@ -164,34 +182,54 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
   end
 
   defp sensitive(draft) do
-    sensitive = draft.params[:sensitive] || Enum.member?(draft.tags, {"#nsfw", "nsfw"})
+    sensitive = draft.params[:sensitive]
     %__MODULE__{draft | sensitive: sensitive}
   end
 
   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.user.ap_id,
-        draft.to,
-        draft.context,
-        draft.content_html,
-        draft.attachments,
-        draft.in_reply_to,
-        draft.tags,
-        draft.summary,
-        draft.cc,
-        draft.sensitive,
-        draft.poll
-      )
+      note_data
       |> Map.put("emoji", emoji)
+      |> Map.put("source", %{
+        "content" => draft.status,
+        "mediaType" => draft.params[:content_type]
+      })
+      |> Map.put("generator", draft.params[:generator])
 
     %__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
 
@@ -201,7 +239,7 @@ defmodule Pleroma.Web.CommonAPI.ActivityDraft do
 
     additional =
       case draft.expires_at do
-        %NaiveDateTime{} = expires_at -> Map.put(additional, "expires_at", expires_at)
+        %DateTime{} = expires_at -> Map.put(additional, "expires_at", expires_at)
         _ -> additional
       end