move to 2.5.5
[anni] / test / pleroma / activity / ir / topics_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Activity.Ir.TopicsTest do
6   use Pleroma.DataCase, async: true
7
8   alias Pleroma.Activity
9   alias Pleroma.Activity.Ir.Topics
10   alias Pleroma.Object
11
12   require Pleroma.Constants
13
14   import Mock
15
16   describe "chat message" do
17     test "Create produces no topics" do
18       activity = %Activity{
19         object: %Object{data: %{"type" => "ChatMessage"}},
20         data: %{"type" => "Create"}
21       }
22
23       assert [] == Topics.get_activity_topics(activity)
24     end
25
26     test "Delete produces user and user:pleroma_chat" do
27       activity = %Activity{
28         object: %Object{data: %{"type" => "ChatMessage"}},
29         data: %{"type" => "Delete"}
30       }
31
32       topics = Topics.get_activity_topics(activity)
33       assert [_, _] = topics
34       assert "user" in topics
35       assert "user:pleroma_chat" in topics
36     end
37   end
38
39   describe "poll answer" do
40     test "produce no topics" do
41       activity = %Activity{object: %Object{data: %{"type" => "Answer"}}}
42
43       assert [] == Topics.get_activity_topics(activity)
44     end
45   end
46
47   describe "non poll answer" do
48     test "always add user and list topics" do
49       activity = %Activity{object: %Object{data: %{"type" => "FooBar"}}}
50       topics = Topics.get_activity_topics(activity)
51
52       assert Enum.member?(topics, "user")
53       assert Enum.member?(topics, "list")
54     end
55   end
56
57   describe "public visibility" do
58     setup do
59       activity = %Activity{
60         object: %Object{data: %{"type" => "Note"}},
61         data: %{"to" => [Pleroma.Constants.as_public()], "type" => "Create"}
62       }
63
64       {:ok, activity: activity}
65     end
66
67     test "produces public topic", %{activity: activity} do
68       topics = Topics.get_activity_topics(activity)
69
70       assert Enum.member?(topics, "public")
71     end
72
73     test "local action produces public:local topic", %{activity: activity} do
74       activity = %{activity | local: true}
75       topics = Topics.get_activity_topics(activity)
76
77       assert Enum.member?(topics, "public:local")
78     end
79
80     test "non-local action does not produce public:local topic", %{activity: activity} do
81       activity = %{activity | local: false}
82       topics = Topics.get_activity_topics(activity)
83
84       refute Enum.member?(topics, "public:local")
85     end
86   end
87
88   describe "public visibility create events" do
89     setup do
90       activity = %Activity{
91         object: %Object{data: %{"attachment" => []}},
92         data: %{"type" => "Create", "to" => [Pleroma.Constants.as_public()]}
93       }
94
95       {:ok, activity: activity}
96     end
97
98     test "with no attachments doesn't produce public:media topics", %{activity: activity} do
99       topics = Topics.get_activity_topics(activity)
100
101       refute Enum.member?(topics, "public:media")
102       refute Enum.member?(topics, "public:local:media")
103     end
104
105     test "converts tags to hash tags", %{activity: activity} do
106       with_mock(Object, [:passthrough], hashtags: fn _ -> ["foo", "bar"] end) do
107         topics = Topics.get_activity_topics(activity)
108
109         assert Enum.member?(topics, "hashtag:foo")
110         assert Enum.member?(topics, "hashtag:bar")
111       end
112     end
113
114     test "only converts strings to hash tags", %{
115       activity: %{object: %{data: data} = object} = activity
116     } do
117       tagged_data = Map.put(data, "tag", [2])
118       activity = %{activity | object: %{object | data: tagged_data}}
119
120       topics = Topics.get_activity_topics(activity)
121
122       refute Enum.member?(topics, "hashtag:2")
123     end
124
125     test "non-local action produces public:remote topic", %{activity: activity} do
126       activity = %{activity | local: false, actor: "https://lain.com/users/lain"}
127       topics = Topics.get_activity_topics(activity)
128
129       assert Enum.member?(topics, "public:remote:lain.com")
130     end
131
132     test "local action doesn't produce public:remote topic", %{activity: activity} do
133       activity = %{activity | local: true, actor: "https://lain.com/users/lain"}
134       topics = Topics.get_activity_topics(activity)
135
136       refute Enum.member?(topics, "public:remote:lain.com")
137     end
138   end
139
140   describe "public visibility Announces" do
141     setup do
142       activity = %Activity{
143         object: %Object{data: %{"attachment" => []}},
144         data: %{"type" => "Announce", "to" => [Pleroma.Constants.as_public()]}
145       }
146
147       {:ok, activity: activity}
148     end
149
150     test "does not generate public topics", %{activity: activity} do
151       topics = Topics.get_activity_topics(activity)
152
153       refute "public" in topics
154       refute "public:remote" in topics
155       refute "public:local" in topics
156     end
157   end
158
159   describe "local-public visibility create events" do
160     setup do
161       activity = %Activity{
162         object: %Object{data: %{"attachment" => []}},
163         data: %{"type" => "Create", "to" => [Pleroma.Web.ActivityPub.Utils.as_local_public()]}
164       }
165
166       {:ok, activity: activity}
167     end
168
169     test "doesn't produce public topics", %{activity: activity} do
170       topics = Topics.get_activity_topics(activity)
171
172       refute Enum.member?(topics, "public")
173     end
174
175     test "produces public:local topics", %{activity: activity} do
176       topics = Topics.get_activity_topics(activity)
177
178       assert Enum.member?(topics, "public:local")
179     end
180
181     test "with no attachments doesn't produce public:media topics", %{activity: activity} do
182       topics = Topics.get_activity_topics(activity)
183
184       refute Enum.member?(topics, "public:media")
185       refute Enum.member?(topics, "public:local:media")
186     end
187   end
188
189   describe "public visibility create events with attachments" do
190     setup do
191       activity = %Activity{
192         object: %Object{data: %{"attachment" => ["foo"]}},
193         data: %{"type" => "Create", "to" => [Pleroma.Constants.as_public()]}
194       }
195
196       {:ok, activity: activity}
197     end
198
199     test "produce public:media topics", %{activity: activity} do
200       topics = Topics.get_activity_topics(activity)
201
202       assert Enum.member?(topics, "public:media")
203     end
204
205     test "local produces public:local:media topics", %{activity: activity} do
206       topics = Topics.get_activity_topics(activity)
207
208       assert Enum.member?(topics, "public:local:media")
209     end
210
211     test "non-local doesn't produce public:local:media topics", %{activity: activity} do
212       activity = %{activity | local: false}
213
214       topics = Topics.get_activity_topics(activity)
215
216       refute Enum.member?(topics, "public:local:media")
217     end
218
219     test "non-local action produces public:remote:media topic", %{activity: activity} do
220       activity = %{activity | local: false, actor: "https://lain.com/users/lain"}
221       topics = Topics.get_activity_topics(activity)
222
223       assert Enum.member?(topics, "public:remote:media:lain.com")
224     end
225   end
226
227   describe "local-public visibility create events with attachments" do
228     setup do
229       activity = %Activity{
230         object: %Object{data: %{"attachment" => ["foo"]}},
231         data: %{"type" => "Create", "to" => [Pleroma.Web.ActivityPub.Utils.as_local_public()]}
232       }
233
234       {:ok, activity: activity}
235     end
236
237     test "do not produce public:media topics", %{activity: activity} do
238       topics = Topics.get_activity_topics(activity)
239
240       refute Enum.member?(topics, "public:media")
241     end
242
243     test "produces public:local:media topics", %{activity: activity} do
244       topics = Topics.get_activity_topics(activity)
245
246       assert Enum.member?(topics, "public:local:media")
247     end
248   end
249
250   describe "non-public visibility" do
251     test "produces direct topic" do
252       activity = %Activity{
253         object: %Object{data: %{"type" => "Note"}},
254         data: %{"to" => [], "type" => "Create"}
255       }
256
257       topics = Topics.get_activity_topics(activity)
258
259       assert Enum.member?(topics, "direct")
260       refute Enum.member?(topics, "public")
261       refute Enum.member?(topics, "public:local")
262       refute Enum.member?(topics, "public:media")
263       refute Enum.member?(topics, "public:local:media")
264     end
265   end
266 end