Merge branch 'develop' into feature/database-compaction
[akkoma] / lib / pleroma / activity.ex
index 3dfabe9f35a7926ac7671006e19c0021876c1ac3..99cc9c077cb131472711e3e26a98ad880e0709ee 100644 (file)
@@ -10,6 +10,7 @@ defmodule Pleroma.Activity do
   alias Pleroma.Object
   alias Pleroma.Repo
 
+  import Ecto.Changeset
   import Ecto.Query
 
   @type t :: %__MODULE__{}
@@ -31,7 +32,7 @@ defmodule Pleroma.Activity do
     field(:data, :map)
     field(:local, :boolean, default: true)
     field(:actor, :string)
-    field(:recipients, {:array, :string})
+    field(:recipients, {:array, :string}, default: [])
     has_many(:notifications, Notification, on_delete: :delete_all)
 
     # Attention: this is a fake relation, don't try to preload it blindly and expect it to work!
@@ -79,6 +80,13 @@ defmodule Pleroma.Activity do
     )
   end
 
+  def change(struct, params \\ %{}) do
+    struct
+    |> cast(params, [:data])
+    |> validate_required([:data])
+    |> unique_constraint(:ap_id, name: :activities_unique_apid_index)
+  end
+
   def get_by_ap_id_with_object(ap_id) do
     Repo.one(
       from(
@@ -202,15 +210,19 @@ defmodule Pleroma.Activity do
     |> Repo.one()
   end
 
-  def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
-  def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
-  def normalize(_), do: nil
+  defp get_in_reply_to_activity_from_object(%Object{data: %{"inReplyTo" => ap_id}}) do
+    get_create_by_object_ap_id_with_object(ap_id)
+  end
 
-  def get_in_reply_to_activity(%Activity{data: %{"object" => %{"inReplyTo" => ap_id}}}) do
-    get_create_by_object_ap_id(ap_id)
+  defp get_in_reply_to_activity_from_object(_), do: nil
+
+  def get_in_reply_to_activity(%Activity{data: %{"object" => object}}) do
+    get_in_reply_to_activity_from_object(Object.normalize(object))
   end
 
-  def get_in_reply_to_activity(_), do: nil
+  def normalize(obj) when is_map(obj), do: get_by_ap_id_with_object(obj["id"])
+  def normalize(ap_id) when is_binary(ap_id), do: get_by_ap_id_with_object(ap_id)
+  def normalize(_), do: nil
 
   def delete_by_ap_id(id) when is_binary(id) do
     by_object_ap_id(id)
@@ -245,4 +257,54 @@ defmodule Pleroma.Activity do
     |> where([s], s.actor == ^actor)
     |> Repo.all()
   end
+
+  def increase_replies_count(nil), do: nil
+
+  def increase_replies_count(object_ap_id) do
+    from(a in create_by_object_ap_id(object_ap_id),
+      update: [
+        set: [
+          data:
+            fragment(
+              """
+              jsonb_set(?, '{object, repliesCount}',
+                (coalesce((?->'object'->>'repliesCount')::int, 0) + 1)::varchar::jsonb, true)
+              """,
+              a.data,
+              a.data
+            )
+        ]
+      ]
+    )
+    |> Repo.update_all([])
+    |> case do
+      {1, [activity]} -> activity
+      _ -> {:error, "Not found"}
+    end
+  end
+
+  def decrease_replies_count(nil), do: nil
+
+  def decrease_replies_count(object_ap_id) do
+    from(a in create_by_object_ap_id(object_ap_id),
+      update: [
+        set: [
+          data:
+            fragment(
+              """
+              jsonb_set(?, '{object, repliesCount}',
+                (greatest(0, (?->'object'->>'repliesCount')::int - 1))::varchar::jsonb, true)
+              """,
+              a.data,
+              a.data
+            )
+        ]
+      ]
+    )
+    |> Repo.update_all([])
+    |> case do
+      {1, [activity]} -> activity
+      _ -> {:error, "Not found"}
+    end
+  end
 end