Still do caching in tests.
authorlain <lain@soykaf.club>
Sun, 3 Feb 2019 17:28:14 +0000 (18:28 +0100)
committerlain <lain@soykaf.club>
Sun, 3 Feb 2019 17:28:14 +0000 (18:28 +0100)
lib/pleroma/object.ex
lib/pleroma/web/activity_pub/utils.ex
test/web/twitter_api/twitter_api_test.exs

index 1088bb5e4f5f20116424f2b84bd961478fb50c7d..7b46a3b05266fd47d8a3fa928975c4d9cc54e018 100644 (file)
@@ -42,24 +42,18 @@ defmodule Pleroma.Object do
   # Legacy objects can be mutated by anybody
   def authorize_mutation(%Object{}, %User{}), do: true
 
-  if Mix.env() == :test do
-    def get_cached_by_ap_id(ap_id) do
-      get_by_ap_id(ap_id)
-    end
-  else
-    def get_cached_by_ap_id(ap_id) do
-      key = "object:#{ap_id}"
-
-      Cachex.fetch!(:object_cache, key, fn _ ->
-        object = get_by_ap_id(ap_id)
-
-        if object do
-          {:commit, object}
-        else
-          {:ignore, object}
-        end
-      end)
-    end
+  def get_cached_by_ap_id(ap_id) do
+    key = "object:#{ap_id}"
+
+    Cachex.fetch!(:object_cache, key, fn _ ->
+      object = get_by_ap_id(ap_id)
+
+      if object do
+        {:commit, object}
+      else
+        {:ignore, object}
+      end
+    end)
   end
 
   def context_mapping(context) do
@@ -90,4 +84,17 @@ defmodule Pleroma.Object do
       {:ok, object}
     end
   end
+
+  def set_cache(%Object{data: %{"id" => ap_id}} = object) do
+    Cachex.put(:object_cache, "object:#{ap_id}", object)
+    {:ok, object}
+  end
+
+  def update_and_set_cache(changeset) do
+    with {:ok, object} <- Repo.update(changeset) do
+      set_cache(object)
+    else
+      e -> e
+    end
+  end
 end
index 3b0cdfe711baa33dad6f56331d9514614930a83b..4a2cc67384836a31f69c68531a552905a74b6a15 100644 (file)
@@ -285,7 +285,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
            |> Map.put("#{property}_count", length(element))
            |> Map.put("#{property}s", element),
          changeset <- Changeset.change(object, data: new_data),
-         {:ok, object} <- Repo.update(changeset),
+         {:ok, object} <- Object.update_and_set_cache(changeset),
          _ <- update_object_in_activities(object) do
       {:ok, object}
     end
index f94e2b873cb281741ccd5f6235e704b70ad50514..48ddbcf506ecd76ef79b1853df6ea95c08b784a8 100644 (file)
@@ -200,12 +200,27 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
 
   test "it favorites a status, returns the updated activity" do
     user = insert(:user)
+    other_user = insert(:user)
     note_activity = insert(:note_activity)
 
     {:ok, status} = TwitterAPI.fav(user, note_activity.id)
     updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
+    assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 1
+
+    object = Object.normalize(note_activity.data["object"])
+
+    assert object.data["like_count"] == 1
 
     assert status == updated_activity
+
+    {:ok, _status} = TwitterAPI.fav(other_user, note_activity.id)
+
+    object = Object.normalize(note_activity.data["object"])
+
+    assert object.data["like_count"] == 2
+
+    updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
+    assert ActivityView.render("activity.json", %{activity: updated_activity})["fave_num"] == 2
   end
 
   test "it unfavorites a status, returns the updated activity" do