Insert text representation of hashtags into object["hashtags"]
[akkoma] / test / pleroma / activity / ir / topics_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
7
8 alias Pleroma.Activity
9 alias Pleroma.Activity.Ir.Topics
10 alias Pleroma.Object
11
12 require Pleroma.Constants
13
14 describe "poll answer" do
15 test "produce no topics" do
16 activity = %Activity{object: %Object{data: %{"type" => "Answer"}}}
17
18 assert [] == Topics.get_activity_topics(activity)
19 end
20 end
21
22 describe "non poll answer" do
23 test "always add user and list topics" do
24 activity = %Activity{object: %Object{data: %{"type" => "FooBar"}}}
25 topics = Topics.get_activity_topics(activity)
26
27 assert Enum.member?(topics, "user")
28 assert Enum.member?(topics, "list")
29 end
30 end
31
32 describe "public visibility" do
33 setup do
34 activity = %Activity{
35 object: %Object{data: %{"type" => "Note"}},
36 data: %{"to" => [Pleroma.Constants.as_public()]}
37 }
38
39 {:ok, activity: activity}
40 end
41
42 test "produces public topic", %{activity: activity} do
43 topics = Topics.get_activity_topics(activity)
44
45 assert Enum.member?(topics, "public")
46 end
47
48 test "local action produces public:local topic", %{activity: activity} do
49 activity = %{activity | local: true}
50 topics = Topics.get_activity_topics(activity)
51
52 assert Enum.member?(topics, "public:local")
53 end
54
55 test "non-local action does not produce public:local topic", %{activity: activity} do
56 activity = %{activity | local: false}
57 topics = Topics.get_activity_topics(activity)
58
59 refute Enum.member?(topics, "public:local")
60 end
61 end
62
63 describe "public visibility create events" do
64 setup do
65 activity = %Activity{
66 object: %Object{data: %{"attachment" => []}},
67 data: %{"type" => "Create", "to" => [Pleroma.Constants.as_public()]}
68 }
69
70 {:ok, activity: activity}
71 end
72
73 test "with no attachments doesn't produce public:media topics", %{activity: activity} do
74 topics = Topics.get_activity_topics(activity)
75
76 refute Enum.member?(topics, "public:media")
77 refute Enum.member?(topics, "public:local:media")
78 end
79
80 test "converts tags to hash tags", %{activity: %{object: %{data: data} = object} = activity} do
81 tagged_data = Map.put(data, "hashtags", ["foo", "bar"])
82 activity = %{activity | object: %{object | data: tagged_data}}
83
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
90 test "only converts strings to hash tags", %{
91 activity: %{object: %{data: data} = object} = activity
92 } do
93 tagged_data = Map.put(data, "tag", [2])
94 activity = %{activity | object: %{object | data: tagged_data}}
95
96 topics = Topics.get_activity_topics(activity)
97
98 refute Enum.member?(topics, "hashtag:2")
99 end
100
101 test "non-local action produces public:remote topic", %{activity: activity} do
102 activity = %{activity | local: false, actor: "https://lain.com/users/lain"}
103 topics = Topics.get_activity_topics(activity)
104
105 assert Enum.member?(topics, "public:remote:lain.com")
106 end
107
108 test "local action doesn't produce public:remote topic", %{activity: activity} do
109 activity = %{activity | local: true, actor: "https://lain.com/users/lain"}
110 topics = Topics.get_activity_topics(activity)
111
112 refute Enum.member?(topics, "public:remote:lain.com")
113 end
114 end
115
116 describe "public visibility create events with attachments" do
117 setup do
118 activity = %Activity{
119 object: %Object{data: %{"attachment" => ["foo"]}},
120 data: %{"type" => "Create", "to" => [Pleroma.Constants.as_public()]}
121 }
122
123 {:ok, activity: activity}
124 end
125
126 test "produce public:media topics", %{activity: activity} do
127 topics = Topics.get_activity_topics(activity)
128
129 assert Enum.member?(topics, "public:media")
130 end
131
132 test "local produces public:local:media topics", %{activity: activity} do
133 topics = Topics.get_activity_topics(activity)
134
135 assert Enum.member?(topics, "public:local:media")
136 end
137
138 test "non-local doesn't produce public:local:media topics", %{activity: activity} do
139 activity = %{activity | local: false}
140
141 topics = Topics.get_activity_topics(activity)
142
143 refute Enum.member?(topics, "public:local:media")
144 end
145
146 test "non-local action produces public:remote:media topic", %{activity: activity} do
147 activity = %{activity | local: false, actor: "https://lain.com/users/lain"}
148 topics = Topics.get_activity_topics(activity)
149
150 assert Enum.member?(topics, "public:remote:media:lain.com")
151 end
152 end
153
154 describe "non-public visibility" do
155 test "produces direct topic" do
156 activity = %Activity{object: %Object{data: %{"type" => "Note"}}, data: %{"to" => []}}
157 topics = Topics.get_activity_topics(activity)
158
159 assert Enum.member?(topics, "direct")
160 refute Enum.member?(topics, "public")
161 refute Enum.member?(topics, "public:local")
162 refute Enum.member?(topics, "public:media")
163 refute Enum.member?(topics, "public:local:media")
164 end
165 end
166 end