Fix MRF policies to also work with Update
[akkoma] / lib / pleroma / web / common_api.ex
index 92afd5cb61e6533ebfc0359742b8eee21bc1ed14..f1f51acf5c9bcc55e15d08987e11b04c3351f5a9 100644 (file)
@@ -5,7 +5,6 @@
 defmodule Pleroma.Web.CommonAPI do
   alias Pleroma.Activity
   alias Pleroma.Conversation.Participation
-  alias Pleroma.Formatter
   alias Pleroma.Object
   alias Pleroma.ThreadMute
   alias Pleroma.User
@@ -30,57 +29,6 @@ defmodule Pleroma.Web.CommonAPI do
     end
   end
 
-  def post_chat_message(%User{} = user, %User{} = recipient, content, opts \\ []) do
-    with maybe_attachment <- opts[:media_id] && Object.get_by_id(opts[:media_id]),
-         :ok <- validate_chat_content_length(content, !!maybe_attachment),
-         {_, {:ok, chat_message_data, _meta}} <-
-           {:build_object,
-            Builder.chat_message(
-              user,
-              recipient.ap_id,
-              content |> format_chat_content,
-              attachment: maybe_attachment
-            )},
-         {_, {:ok, create_activity_data, _meta}} <-
-           {:build_create_activity, Builder.create(user, chat_message_data, [recipient.ap_id])},
-         {_, {:ok, %Activity{} = activity, _meta}} <-
-           {:common_pipeline,
-            Pipeline.common_pipeline(create_activity_data,
-              local: true,
-              idempotency_key: opts[:idempotency_key]
-            )} do
-      {:ok, activity}
-    else
-      {:common_pipeline, {:reject, _} = e} -> e
-      e -> e
-    end
-  end
-
-  defp format_chat_content(nil), do: nil
-
-  defp format_chat_content(content) do
-    {text, _, _} =
-      content
-      |> Formatter.html_escape("text/plain")
-      |> Formatter.linkify()
-      |> (fn {text, mentions, tags} ->
-            {String.replace(text, ~r/\r?\n/, "<br>"), mentions, tags}
-          end).()
-
-    text
-  end
-
-  defp validate_chat_content_length(_, true), do: :ok
-  defp validate_chat_content_length(nil, false), do: {:error, :no_content}
-
-  defp validate_chat_content_length(content, _) do
-    if String.length(content) <= Pleroma.Config.get([:instance, :chat_limit]) do
-      :ok
-    else
-      {:error, :content_too_long}
-    end
-  end
-
   def unblock(blocker, blocked) do
     with {_, %Activity{} = block} <- {:fetch_block, Utils.fetch_latest_block(blocker, blocked)},
          {:ok, unblock_data, _} <- Builder.undo(blocker, block),
@@ -261,7 +209,8 @@ defmodule Pleroma.Web.CommonAPI do
          {:ok, activity, _} <- Pipeline.common_pipeline(emoji_react, local: true) do
       {:ok, activity}
     else
-      _ -> {:error, dgettext("errors", "Could not add reaction emoji")}
+      _ ->
+        {:error, dgettext("errors", "Could not add reaction emoji")}
     end
   end
 
@@ -371,6 +320,10 @@ defmodule Pleroma.Web.CommonAPI do
     end
   end
 
+  def get_quoted_visibility(nil), do: nil
+
+  def get_quoted_visibility(activity), do: get_replied_to_visibility(activity)
+
   def check_expiry_date({:ok, nil} = res), do: res
 
   def check_expiry_date({:ok, in_seconds}) do
@@ -388,21 +341,44 @@ defmodule Pleroma.Web.CommonAPI do
     |> check_expiry_date()
   end
 
-  def listen(user, data) do
-    with {:ok, draft} <- ActivityDraft.listen(user, data) do
-      ActivityPub.listen(draft.changes)
+  def post(user, %{status: _} = data) do
+    with {:ok, draft} <- ActivityDraft.create(user, data) do
+      ActivityPub.create(draft.changes, draft.preview?)
     end
   end
 
-  def post(user, %{status: _} = data) do
-    with {:ok, draft} <- ActivityDraft.create(user, data) do
-      activity = ActivityPub.create(draft.changes, draft.preview?)
+  def update(user, orig_activity, changes) do
+    with orig_object <- Object.normalize(orig_activity),
+         {:ok, new_object} <- make_update_data(user, orig_object, changes),
+         {:ok, update_data, _} <- Builder.update(user, new_object),
+         {:ok, update, _} <- Pipeline.common_pipeline(update_data, local: true) do
+      {:ok, update}
+    else
+      _ -> {:error, nil}
+    end
+  end
 
-      unless draft.preview? do
-        Pleroma.Elasticsearch.maybe_put_into_elasticsearch(activity)
-      end
+  defp make_update_data(user, orig_object, changes) do
+    kept_params = %{
+      visibility: Visibility.get_visibility(orig_object),
+      in_reply_to_id:
+        with replied_id when is_binary(replied_id) <- orig_object.data["inReplyTo"],
+             %Activity{id: activity_id} <- Activity.get_create_by_object_ap_id(replied_id) do
+          activity_id
+        else
+          _ -> nil
+        end
+    }
+
+    params = Map.merge(changes, kept_params)
 
-      activity
+    with {:ok, draft} <- ActivityDraft.create(user, params) do
+      change =
+        Object.Updater.make_update_object_data(orig_object.data, draft.object, Utils.make_date())
+
+      {:ok, change}
+    else
+      _ -> {:error, nil}
     end
   end