Merge remote-tracking branch 'pleroma/develop' into cycles-router-api-routes
[akkoma] / test / pleroma / activity / ir / topics_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 "poll answer" do
17 test "produce no topics" do
18 activity = %Activity{object: %Object{data: %{"type" => "Answer"}}}
19
20 assert [] == Topics.get_activity_topics(activity)
21 end
22 end
23
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)
28
29 assert Enum.member?(topics, "user")
30 assert Enum.member?(topics, "list")
31 end
32 end
33
34 describe "public visibility" do
35 setup do
36 activity = %Activity{
37 object: %Object{data: %{"type" => "Note"}},
38 data: %{"to" => [Pleroma.Constants.as_public()]}
39 }
40
41 {:ok, activity: activity}
42 end
43
44 test "produces public topic", %{activity: activity} do
45 topics = Topics.get_activity_topics(activity)
46
47 assert Enum.member?(topics, "public")
48 end
49
50 test "local action produces public:local topic", %{activity: activity} do
51 activity = %{activity | local: true}
52 topics = Topics.get_activity_topics(activity)
53
54 assert Enum.member?(topics, "public:local")
55 end
56
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)
60
61 refute Enum.member?(topics, "public:local")
62 end
63 end
64
65 describe "public visibility create events" do
66 setup do
67 activity = %Activity{
68 object: %Object{data: %{"attachment" => []}},
69 data: %{"type" => "Create", "to" => [Pleroma.Constants.as_public()]}
70 }
71
72 {:ok, activity: activity}
73 end
74
75 test "with no attachments doesn't produce public:media topics", %{activity: activity} do
76 topics = Topics.get_activity_topics(activity)
77
78 refute Enum.member?(topics, "public:media")
79 refute Enum.member?(topics, "public:local:media")
80 end
81
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)
85
86 assert Enum.member?(topics, "hashtag:foo")
87 assert Enum.member?(topics, "hashtag:bar")
88 end
89 end
90
91 test "only converts strings to hash tags", %{
92 activity: %{object: %{data: data} = object} = activity
93 } do
94 tagged_data = Map.put(data, "tag", [2])
95 activity = %{activity | object: %{object | data: tagged_data}}
96
97 topics = Topics.get_activity_topics(activity)
98
99 refute Enum.member?(topics, "hashtag:2")
100 end
101
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)
105
106 assert Enum.member?(topics, "public:remote:lain.com")
107 end
108
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)
112
113 refute Enum.member?(topics, "public:remote:lain.com")
114 end
115 end
116
117 describe "public visibility create events with attachments" do
118 setup do
119 activity = %Activity{
120 object: %Object{data: %{"attachment" => ["foo"]}},
121 data: %{"type" => "Create", "to" => [Pleroma.Constants.as_public()]}
122 }
123
124 {:ok, activity: activity}
125 end
126
127 test "produce public:media topics", %{activity: activity} do
128 topics = Topics.get_activity_topics(activity)
129
130 assert Enum.member?(topics, "public:media")
131 end
132
133 test "local produces public:local:media topics", %{activity: activity} do
134 topics = Topics.get_activity_topics(activity)
135
136 assert Enum.member?(topics, "public:local:media")
137 end
138
139 test "non-local doesn't produce public:local:media topics", %{activity: activity} do
140 activity = %{activity | local: false}
141
142 topics = Topics.get_activity_topics(activity)
143
144 refute Enum.member?(topics, "public:local:media")
145 end
146
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)
150
151 assert Enum.member?(topics, "public:remote:media:lain.com")
152 end
153 end
154
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)
159
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")
165 end
166 end
167 end