[#3213] Made Object.hashtags/1 work with :hashtags assoc. Adjusted tests.
authorIvan Tashkinov <ivantashkinov@gmail.com>
Sun, 27 Dec 2020 21:08:09 +0000 (00:08 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Sun, 27 Dec 2020 21:08:09 +0000 (00:08 +0300)
lib/pleroma/config.ex
lib/pleroma/object.ex
lib/pleroma/web/activity_pub/activity_pub.ex
test/pleroma/activity/ir/topics_test.exs

index 86d4f6b72969f235168513decc8d80ae219d6b13..ee0167f4ef537138983a9d338b411ee9e0e0e84b 100644 (file)
@@ -96,6 +96,8 @@ defmodule Pleroma.Config do
     end
   end
 
+  def object_embedded_hashtags?, do: !get([:instance, :improved_hashtag_timeline])
+
   def oauth_consumer_strategies, do: get([:auth, :oauth_consumer_strategies], [])
 
   def oauth_consumer_enabled?, do: oauth_consumer_strategies() != []
index 1d756bcd1743af73eb413999cddcfd120f15f62e..08114d4f2c7dd51dfca55b44228579a2fa05b9fb 100644 (file)
@@ -384,7 +384,19 @@ defmodule Pleroma.Object do
 
   def tags(_), do: []
 
-  def hashtags(object), do: embedded_hashtags(object)
+  def hashtags(%Object{} = object) do
+    cond do
+      Config.object_embedded_hashtags?() ->
+        embedded_hashtags(object)
+
+      object.id == "pleroma:fake_object_id" ->
+        []
+
+      true ->
+        hashtag_records = Repo.preload(object, :hashtags).hashtags
+        Enum.map(hashtag_records, & &1.name)
+    end
+  end
 
   defp embedded_hashtags(%Object{data: data}) do
     object_data_hashtags(data)
index 54d1a2350ea670c12cd37752150f09f869d49401..626cad336a246187bbcfe9b0234e2a366a255366 100644 (file)
@@ -1199,16 +1199,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
       |> exclude_invisible_actors(opts)
       |> exclude_visibility(opts)
 
-    if Config.get([:instance, :improved_hashtag_timeline]) do
-      query
-      |> restrict_hashtag_any(opts)
-      |> restrict_hashtag_all(opts)
-      |> restrict_hashtag_reject_any(opts)
-    else
+    if Config.object_embedded_hashtags?() do
       query
       |> restrict_tag(opts)
       |> restrict_tag_reject(opts)
       |> restrict_tag_all(opts)
+    else
+      query
+      |> restrict_hashtag_any(opts)
+      |> restrict_hashtag_all(opts)
+      |> restrict_hashtag_reject_any(opts)
     end
   end
 
index b464822d9c21274cf9afa435c22d2d967861746f..984777bdaf532c405dd6106e87260660c6217756 100644 (file)
@@ -11,6 +11,8 @@ defmodule Pleroma.Activity.Ir.TopicsTest do
 
   require Pleroma.Constants
 
+  import Mock
+
   describe "poll answer" do
     test "produce no topics" do
       activity = %Activity{object: %Object{data: %{"type" => "Answer"}}}
@@ -77,14 +79,13 @@ defmodule Pleroma.Activity.Ir.TopicsTest do
       refute Enum.member?(topics, "public:local:media")
     end
 
-    test "converts tags to hash tags", %{activity: %{object: %{data: data} = object} = activity} do
-      tagged_data = Map.put(data, "tag", ["foo", "bar"])
-      activity = %{activity | object: %{object | data: tagged_data}}
-
-      topics = Topics.get_activity_topics(activity)
+    test "converts tags to hash tags", %{activity: activity} do
+      with_mock(Object, [:passthrough], hashtags: fn _ -> ["foo", "bar"] end) do
+        topics = Topics.get_activity_topics(activity)
 
-      assert Enum.member?(topics, "hashtag:foo")
-      assert Enum.member?(topics, "hashtag:bar")
+        assert Enum.member?(topics, "hashtag:foo")
+        assert Enum.member?(topics, "hashtag:bar")
+      end
     end
 
     test "only converts strings to hash tags", %{