1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Activity.Ir.TopicsTest do
6 use Pleroma.DataCase, async: true
9 alias Pleroma.Activity.Ir.Topics
12 require Pleroma.Constants
14 describe "poll answer" do
15 test "produce no topics" do
16 activity = %Activity{object: %Object{data: %{"type" => "Answer"}}}
18 assert [] == Topics.get_activity_topics(activity)
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)
27 assert Enum.member?(topics, "user")
28 assert Enum.member?(topics, "list")
32 describe "public visibility" do
35 object: %Object{data: %{"type" => "Note"}},
36 data: %{"to" => [Pleroma.Constants.as_public()]}
39 {:ok, activity: activity}
42 test "produces public topic", %{activity: activity} do
43 topics = Topics.get_activity_topics(activity)
45 assert Enum.member?(topics, "public")
48 test "local action produces public:local topic", %{activity: activity} do
49 activity = %{activity | local: true}
50 topics = Topics.get_activity_topics(activity)
52 assert Enum.member?(topics, "public:local")
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)
59 refute Enum.member?(topics, "public:local")
63 describe "public visibility create events" do
66 object: %Object{data: %{"attachment" => []}},
67 data: %{"type" => "Create", "to" => [Pleroma.Constants.as_public()]}
70 {:ok, activity: activity}
73 test "with no attachments doesn't produce public:media topics", %{activity: activity} do
74 topics = Topics.get_activity_topics(activity)
76 refute Enum.member?(topics, "public:media")
77 refute Enum.member?(topics, "public:local:media")
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}}
84 topics = Topics.get_activity_topics(activity)
86 assert Enum.member?(topics, "hashtag:foo")
87 assert Enum.member?(topics, "hashtag:bar")
90 test "only converts strings to hash tags", %{
91 activity: %{object: %{data: data} = object} = activity
93 tagged_data = Map.put(data, "tag", [2])
94 activity = %{activity | object: %{object | data: tagged_data}}
96 topics = Topics.get_activity_topics(activity)
98 refute Enum.member?(topics, "hashtag:2")
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)
105 assert Enum.member?(topics, "public:remote:lain.com")
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)
112 refute Enum.member?(topics, "public:remote:lain.com")
116 describe "public visibility create events with attachments" do
118 activity = %Activity{
119 object: %Object{data: %{"attachment" => ["foo"]}},
120 data: %{"type" => "Create", "to" => [Pleroma.Constants.as_public()]}
123 {:ok, activity: activity}
126 test "produce public:media topics", %{activity: activity} do
127 topics = Topics.get_activity_topics(activity)
129 assert Enum.member?(topics, "public:media")
132 test "local produces public:local:media topics", %{activity: activity} do
133 topics = Topics.get_activity_topics(activity)
135 assert Enum.member?(topics, "public:local:media")
138 test "non-local doesn't produce public:local:media topics", %{activity: activity} do
139 activity = %{activity | local: false}
141 topics = Topics.get_activity_topics(activity)
143 refute Enum.member?(topics, "public:local:media")
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)
150 assert Enum.member?(topics, "public:remote:media:lain.com")
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)
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")