summaryrefslogtreecommitdiff
path: root/test/pleroma/activity/ir/topics_test.exs
blob: d299fea633f52949ce677712245bccf850c2fba8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Activity.Ir.TopicsTest do
  use Pleroma.DataCase, async: true

  alias Pleroma.Activity
  alias Pleroma.Activity.Ir.Topics
  alias Pleroma.Object

  require Pleroma.Constants

  import Mock

  describe "chat message" do
    test "Create produces no topics" do
      activity = %Activity{
        object: %Object{data: %{"type" => "ChatMessage"}},
        data: %{"type" => "Create"}
      }

      assert [] == Topics.get_activity_topics(activity)
    end

    test "Delete produces user and user:pleroma_chat" do
      activity = %Activity{
        object: %Object{data: %{"type" => "ChatMessage"}},
        data: %{"type" => "Delete"}
      }

      topics = Topics.get_activity_topics(activity)
      assert [_, _] = topics
      assert "user" in topics
      assert "user:pleroma_chat" in topics
    end
  end

  describe "poll answer" do
    test "produce no topics" do
      activity = %Activity{object: %Object{data: %{"type" => "Answer"}}}

      assert [] == Topics.get_activity_topics(activity)
    end
  end

  describe "non poll answer" do
    test "always add user and list topics" do
      activity = %Activity{object: %Object{data: %{"type" => "FooBar"}}}
      topics = Topics.get_activity_topics(activity)

      assert Enum.member?(topics, "user")
      assert Enum.member?(topics, "list")
    end
  end

  describe "public visibility" do
    setup do
      activity = %Activity{
        object: %Object{data: %{"type" => "Note"}},
        data: %{"to" => [Pleroma.Constants.as_public()], "type" => "Create"}
      }

      {:ok, activity: activity}
    end

    test "produces public topic", %{activity: activity} do
      topics = Topics.get_activity_topics(activity)

      assert Enum.member?(topics, "public")
    end

    test "local action produces public:local topic", %{activity: activity} do
      activity = %{activity | local: true}
      topics = Topics.get_activity_topics(activity)

      assert Enum.member?(topics, "public:local")
    end

    test "non-local action does not produce public:local topic", %{activity: activity} do
      activity = %{activity | local: false}
      topics = Topics.get_activity_topics(activity)

      refute Enum.member?(topics, "public:local")
    end
  end

  describe "public visibility create events" do
    setup do
      activity = %Activity{
        object: %Object{data: %{"attachment" => []}},
        data: %{"type" => "Create", "to" => [Pleroma.Constants.as_public()]}
      }

      {:ok, activity: activity}
    end

    test "with no attachments doesn't produce public:media topics", %{activity: activity} do
      topics = Topics.get_activity_topics(activity)

      refute Enum.member?(topics, "public:media")
      refute Enum.member?(topics, "public:local:media")
    end

    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")
      end
    end

    test "only converts strings to hash tags", %{
      activity: %{object: %{data: data} = object} = activity
    } do
      tagged_data = Map.put(data, "tag", [2])
      activity = %{activity | object: %{object | data: tagged_data}}

      topics = Topics.get_activity_topics(activity)

      refute Enum.member?(topics, "hashtag:2")
    end

    test "non-local action produces public:remote topic", %{activity: activity} do
      activity = %{activity | local: false, actor: "https://lain.com/users/lain"}
      topics = Topics.get_activity_topics(activity)

      assert Enum.member?(topics, "public:remote:lain.com")
    end

    test "local action doesn't produce public:remote topic", %{activity: activity} do
      activity = %{activity | local: true, actor: "https://lain.com/users/lain"}
      topics = Topics.get_activity_topics(activity)

      refute Enum.member?(topics, "public:remote:lain.com")
    end
  end

  describe "public visibility Announces" do
    setup do
      activity = %Activity{
        object: %Object{data: %{"attachment" => []}},
        data: %{"type" => "Announce", "to" => [Pleroma.Constants.as_public()]}
      }

      {:ok, activity: activity}
    end

    test "does not generate public topics", %{activity: activity} do
      topics = Topics.get_activity_topics(activity)

      refute "public" in topics
      refute "public:remote" in topics
      refute "public:local" in topics
    end
  end

  describe "local-public visibility create events" do
    setup do
      activity = %Activity{
        object: %Object{data: %{"attachment" => []}},
        data: %{"type" => "Create", "to" => [Pleroma.Web.ActivityPub.Utils.as_local_public()]}
      }

      {:ok, activity: activity}
    end

    test "doesn't produce public topics", %{activity: activity} do
      topics = Topics.get_activity_topics(activity)

      refute Enum.member?(topics, "public")
    end

    test "produces public:local topics", %{activity: activity} do
      topics = Topics.get_activity_topics(activity)

      assert Enum.member?(topics, "public:local")
    end

    test "with no attachments doesn't produce public:media topics", %{activity: activity} do
      topics = Topics.get_activity_topics(activity)

      refute Enum.member?(topics, "public:media")
      refute Enum.member?(topics, "public:local:media")
    end
  end

  describe "public visibility create events with attachments" do
    setup do
      activity = %Activity{
        object: %Object{data: %{"attachment" => ["foo"]}},
        data: %{"type" => "Create", "to" => [Pleroma.Constants.as_public()]}
      }

      {:ok, activity: activity}
    end

    test "produce public:media topics", %{activity: activity} do
      topics = Topics.get_activity_topics(activity)

      assert Enum.member?(topics, "public:media")
    end

    test "local produces public:local:media topics", %{activity: activity} do
      topics = Topics.get_activity_topics(activity)

      assert Enum.member?(topics, "public:local:media")
    end

    test "non-local doesn't produce public:local:media topics", %{activity: activity} do
      activity = %{activity | local: false}

      topics = Topics.get_activity_topics(activity)

      refute Enum.member?(topics, "public:local:media")
    end

    test "non-local action produces public:remote:media topic", %{activity: activity} do
      activity = %{activity | local: false, actor: "https://lain.com/users/lain"}
      topics = Topics.get_activity_topics(activity)

      assert Enum.member?(topics, "public:remote:media:lain.com")
    end
  end

  describe "local-public visibility create events with attachments" do
    setup do
      activity = %Activity{
        object: %Object{data: %{"attachment" => ["foo"]}},
        data: %{"type" => "Create", "to" => [Pleroma.Web.ActivityPub.Utils.as_local_public()]}
      }

      {:ok, activity: activity}
    end

    test "do not produce public:media topics", %{activity: activity} do
      topics = Topics.get_activity_topics(activity)

      refute Enum.member?(topics, "public:media")
    end

    test "produces public:local:media topics", %{activity: activity} do
      topics = Topics.get_activity_topics(activity)

      assert Enum.member?(topics, "public:local:media")
    end
  end

  describe "non-public visibility" do
    test "produces direct topic" do
      activity = %Activity{
        object: %Object{data: %{"type" => "Note"}},
        data: %{"to" => [], "type" => "Create"}
      }

      topics = Topics.get_activity_topics(activity)

      assert Enum.member?(topics, "direct")
      refute Enum.member?(topics, "public")
      refute Enum.member?(topics, "public:local")
      refute Enum.member?(topics, "public:media")
      refute Enum.member?(topics, "public:local:media")
    end
  end
end