4ddcea1ec04f39a81b2598f3041379ad3d840da5
[akkoma] / test / 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, "tag", ["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 end
101
102 describe "public visibility create events with attachments" do
103 setup do
104 activity = %Activity{
105 object: %Object{data: %{"attachment" => ["foo"]}},
106 data: %{"type" => "Create", "to" => [Pleroma.Constants.as_public()]}
107 }
108
109 {:ok, activity: activity}
110 end
111
112 test "produce public:media topics", %{activity: activity} do
113 topics = Topics.get_activity_topics(activity)
114
115 assert Enum.member?(topics, "public:media")
116 end
117
118 test "local produces public:local:media topics", %{activity: activity} do
119 topics = Topics.get_activity_topics(activity)
120
121 assert Enum.member?(topics, "public:local:media")
122 end
123
124 test "non-local doesn't produce public:local:media topics", %{activity: activity} do
125 activity = %{activity | local: false}
126
127 topics = Topics.get_activity_topics(activity)
128
129 refute Enum.member?(topics, "public:local:media")
130 end
131 end
132
133 describe "non-public visibility" do
134 test "produces direct topic" do
135 activity = %Activity{object: %Object{data: %{"type" => "Note"}}, data: %{"to" => []}}
136 topics = Topics.get_activity_topics(activity)
137
138 assert Enum.member?(topics, "direct")
139 refute Enum.member?(topics, "public")
140 refute Enum.member?(topics, "public:local")
141 refute Enum.member?(topics, "public:media")
142 refute Enum.member?(topics, "public:local:media")
143 end
144 end
145 end