1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Activity.Ir.TopicsTest do
6 use Pleroma.DataCase, async: true
9 alias Pleroma.Activity.Ir.Topics
12 require Pleroma.Constants
16 describe "poll answer" do
17 test "produce no topics" do
18 activity = %Activity{object: %Object{data: %{"type" => "Answer"}}}
20 assert [] == Topics.get_activity_topics(activity)
24 describe "non poll answer" do
25 test "always add user and list topics" do
26 activity = %Activity{object: %Object{data: %{"type" => "FooBar"}}}
27 topics = Topics.get_activity_topics(activity)
29 assert Enum.member?(topics, "user")
30 assert Enum.member?(topics, "list")
34 describe "public visibility" do
37 object: %Object{data: %{"type" => "Note"}},
38 data: %{"to" => [Pleroma.Constants.as_public()]}
41 {:ok, activity: activity}
44 test "produces public topic", %{activity: activity} do
45 topics = Topics.get_activity_topics(activity)
47 assert Enum.member?(topics, "public")
50 test "local action produces public:local topic", %{activity: activity} do
51 activity = %{activity | local: true}
52 topics = Topics.get_activity_topics(activity)
54 assert Enum.member?(topics, "public:local")
57 test "non-local action does not produce public:local topic", %{activity: activity} do
58 activity = %{activity | local: false}
59 topics = Topics.get_activity_topics(activity)
61 refute Enum.member?(topics, "public:local")
65 describe "public visibility create events" do
68 object: %Object{data: %{"attachment" => []}},
69 data: %{"type" => "Create", "to" => [Pleroma.Constants.as_public()]}
72 {:ok, activity: activity}
75 test "with no attachments doesn't produce public:media topics", %{activity: activity} do
76 topics = Topics.get_activity_topics(activity)
78 refute Enum.member?(topics, "public:media")
79 refute Enum.member?(topics, "public:local:media")
82 test "converts tags to hash tags", %{activity: activity} do
83 with_mock(Object, [:passthrough], hashtags: fn _ -> ["foo", "bar"] end) do
84 topics = Topics.get_activity_topics(activity)
86 assert Enum.member?(topics, "hashtag:foo")
87 assert Enum.member?(topics, "hashtag:bar")
91 test "only converts strings to hash tags", %{
92 activity: %{object: %{data: data} = object} = activity
94 tagged_data = Map.put(data, "tag", [2])
95 activity = %{activity | object: %{object | data: tagged_data}}
97 topics = Topics.get_activity_topics(activity)
99 refute Enum.member?(topics, "hashtag:2")
102 test "non-local action produces public:remote topic", %{activity: activity} do
103 activity = %{activity | local: false, actor: "https://lain.com/users/lain"}
104 topics = Topics.get_activity_topics(activity)
106 assert Enum.member?(topics, "public:remote:lain.com")
109 test "local action doesn't produce public:remote topic", %{activity: activity} do
110 activity = %{activity | local: true, actor: "https://lain.com/users/lain"}
111 topics = Topics.get_activity_topics(activity)
113 refute Enum.member?(topics, "public:remote:lain.com")
117 describe "public visibility create events with attachments" do
119 activity = %Activity{
120 object: %Object{data: %{"attachment" => ["foo"]}},
121 data: %{"type" => "Create", "to" => [Pleroma.Constants.as_public()]}
124 {:ok, activity: activity}
127 test "produce public:media topics", %{activity: activity} do
128 topics = Topics.get_activity_topics(activity)
130 assert Enum.member?(topics, "public:media")
133 test "local produces public:local:media topics", %{activity: activity} do
134 topics = Topics.get_activity_topics(activity)
136 assert Enum.member?(topics, "public:local:media")
139 test "non-local doesn't produce public:local:media topics", %{activity: activity} do
140 activity = %{activity | local: false}
142 topics = Topics.get_activity_topics(activity)
144 refute Enum.member?(topics, "public:local:media")
147 test "non-local action produces public:remote:media topic", %{activity: activity} do
148 activity = %{activity | local: false, actor: "https://lain.com/users/lain"}
149 topics = Topics.get_activity_topics(activity)
151 assert Enum.member?(topics, "public:remote:media:lain.com")
155 describe "non-public visibility" do
156 test "produces direct topic" do
157 activity = %Activity{object: %Object{data: %{"type" => "Note"}}, data: %{"to" => []}}
158 topics = Topics.get_activity_topics(activity)
160 assert Enum.member?(topics, "direct")
161 refute Enum.member?(topics, "public")
162 refute Enum.member?(topics, "public:local")
163 refute Enum.member?(topics, "public:media")
164 refute Enum.member?(topics, "public:local:media")