aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrinpatch <rinpatch@sdf.org>2020-01-24 11:32:25 +0000
committerrinpatch <rinpatch@sdf.org>2020-01-24 11:32:25 +0000
commit0fceeb44b3797e2a3c5100edbfd6fdb9e2bf7f28 (patch)
tree684b36d9c5afa0017fe0ca13a2117f62a3dadf9d
parent72edc3e30fa3ad9ca2a592186c64e01ef439cdc8 (diff)
parent34fc0ca05362d2dfd69352e8f4004b26d39315ac (diff)
downloadpleroma-0fceeb44b3797e2a3c5100edbfd6fdb9e2bf7f28.tar.gz
Merge branch 'emoji-reaction-sanity-check' into 'develop'
Emoji reactions: Add sanity checks for the cache See merge request pleroma/pleroma!2132
-rw-r--r--lib/pleroma/web/activity_pub/utils.ex12
-rw-r--r--test/web/activity_pub/utils_test.exs13
2 files changed, 23 insertions, 2 deletions
diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex
index 4def431f1..4f7fdaf38 100644
--- a/lib/pleroma/web/activity_pub/utils.ex
+++ b/lib/pleroma/web/activity_pub/utils.ex
@@ -337,7 +337,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
%Activity{data: %{"content" => emoji, "actor" => actor}},
object
) do
- reactions = object.data["reactions"] || []
+ reactions = get_cached_emoji_reactions(object)
new_reactions =
case Enum.find_index(reactions, fn [candidate, _] -> emoji == candidate end) do
@@ -365,7 +365,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
%Activity{data: %{"content" => emoji, "actor" => actor}},
object
) do
- reactions = object.data["reactions"] || []
+ reactions = get_cached_emoji_reactions(object)
new_reactions =
case Enum.find_index(reactions, fn [candidate, _] -> emoji == candidate end) do
@@ -385,6 +385,14 @@ defmodule Pleroma.Web.ActivityPub.Utils do
update_element_in_object("reaction", new_reactions, object, count)
end
+ def get_cached_emoji_reactions(object) do
+ if is_list(object.data["reactions"]) do
+ object.data["reactions"]
+ else
+ []
+ end
+ end
+
@spec add_like_to_object(Activity.t(), Object.t()) ::
{:ok, Object.t()} | {:error, Ecto.Changeset.t()}
def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
diff --git a/test/web/activity_pub/utils_test.exs b/test/web/activity_pub/utils_test.exs
index 586eb1d2f..211fa6c95 100644
--- a/test/web/activity_pub/utils_test.exs
+++ b/test/web/activity_pub/utils_test.exs
@@ -636,4 +636,17 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do
assert updated_object.data["announcement_count"] == 1
end
end
+
+ describe "get_cached_emoji_reactions/1" do
+ test "returns the data or an emtpy list" do
+ object = insert(:note)
+ assert Utils.get_cached_emoji_reactions(object) == []
+
+ object = insert(:note, data: %{"reactions" => [["x", ["lain"]]]})
+ assert Utils.get_cached_emoji_reactions(object) == [["x", ["lain"]]]
+
+ object = insert(:note, data: %{"reactions" => %{}})
+ assert Utils.get_cached_emoji_reactions(object) == []
+ end
+ end
end