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.Web.ActivityPub.VisibilityTest do
6 use Pleroma.DataCase, async: true
10 alias Pleroma.Web.ActivityPub.Visibility
11 alias Pleroma.Web.CommonAPI
12 import Pleroma.Factory
16 mentioned = insert(:user)
17 following = insert(:user)
18 unrelated = insert(:user)
19 {:ok, following, user} = Pleroma.User.follow(following, user)
20 {:ok, list} = Pleroma.List.create("foo", user)
22 Pleroma.List.follow(list, unrelated)
25 CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "public"})
28 CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "private"})
31 CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "direct"})
34 CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "unlisted"})
37 CommonAPI.post(user, %{
38 status: "@#{mentioned.nickname}",
39 visibility: "list:#{list.id}"
62 assert Visibility.is_direct?(direct)
63 refute Visibility.is_direct?(public)
64 refute Visibility.is_direct?(private)
65 refute Visibility.is_direct?(unlisted)
66 assert Visibility.is_direct?(list)
76 refute Visibility.is_public?(direct)
77 assert Visibility.is_public?(public)
78 refute Visibility.is_public?(private)
79 assert Visibility.is_public?(unlisted)
80 refute Visibility.is_public?(list)
83 test "is_private?", %{
90 refute Visibility.is_private?(direct)
91 refute Visibility.is_private?(public)
92 assert Visibility.is_private?(private)
93 refute Visibility.is_private?(unlisted)
94 refute Visibility.is_private?(list)
104 refute Visibility.is_list?(direct)
105 refute Visibility.is_list?(public)
106 refute Visibility.is_list?(private)
107 refute Visibility.is_list?(unlisted)
108 assert Visibility.is_list?(list)
111 test "visible_for_user? Activity", %{
117 mentioned: mentioned,
118 following: following,
119 unrelated: unrelated,
122 # All visible to author
124 assert Visibility.visible_for_user?(public, user)
125 assert Visibility.visible_for_user?(private, user)
126 assert Visibility.visible_for_user?(unlisted, user)
127 assert Visibility.visible_for_user?(direct, user)
128 assert Visibility.visible_for_user?(list, user)
130 # All visible to a mentioned user
132 assert Visibility.visible_for_user?(public, mentioned)
133 assert Visibility.visible_for_user?(private, mentioned)
134 assert Visibility.visible_for_user?(unlisted, mentioned)
135 assert Visibility.visible_for_user?(direct, mentioned)
136 assert Visibility.visible_for_user?(list, mentioned)
138 # DM not visible for just follower
140 assert Visibility.visible_for_user?(public, following)
141 assert Visibility.visible_for_user?(private, following)
142 assert Visibility.visible_for_user?(unlisted, following)
143 refute Visibility.visible_for_user?(direct, following)
144 refute Visibility.visible_for_user?(list, following)
146 # Public and unlisted visible for unrelated user
148 assert Visibility.visible_for_user?(public, unrelated)
149 assert Visibility.visible_for_user?(unlisted, unrelated)
150 refute Visibility.visible_for_user?(private, unrelated)
151 refute Visibility.visible_for_user?(direct, unrelated)
153 # Public and unlisted visible for unauthenticated
155 assert Visibility.visible_for_user?(public, nil)
156 assert Visibility.visible_for_user?(unlisted, nil)
157 refute Visibility.visible_for_user?(private, nil)
158 refute Visibility.visible_for_user?(direct, nil)
160 # Visible for a list member
161 assert Visibility.visible_for_user?(list, unrelated)
164 test "visible_for_user? Object", %{
170 mentioned: mentioned,
171 following: following,
172 unrelated: unrelated,
175 public = Object.normalize(public)
176 private = Object.normalize(private)
177 unlisted = Object.normalize(unlisted)
178 direct = Object.normalize(direct)
179 list = Object.normalize(list)
181 # All visible to author
183 assert Visibility.visible_for_user?(public, user)
184 assert Visibility.visible_for_user?(private, user)
185 assert Visibility.visible_for_user?(unlisted, user)
186 assert Visibility.visible_for_user?(direct, user)
187 assert Visibility.visible_for_user?(list, user)
189 # All visible to a mentioned user
191 assert Visibility.visible_for_user?(public, mentioned)
192 assert Visibility.visible_for_user?(private, mentioned)
193 assert Visibility.visible_for_user?(unlisted, mentioned)
194 assert Visibility.visible_for_user?(direct, mentioned)
195 assert Visibility.visible_for_user?(list, mentioned)
197 # DM not visible for just follower
199 assert Visibility.visible_for_user?(public, following)
200 assert Visibility.visible_for_user?(private, following)
201 assert Visibility.visible_for_user?(unlisted, following)
202 refute Visibility.visible_for_user?(direct, following)
203 refute Visibility.visible_for_user?(list, following)
205 # Public and unlisted visible for unrelated user
207 assert Visibility.visible_for_user?(public, unrelated)
208 assert Visibility.visible_for_user?(unlisted, unrelated)
209 refute Visibility.visible_for_user?(private, unrelated)
210 refute Visibility.visible_for_user?(direct, unrelated)
212 # Public and unlisted visible for unauthenticated
214 assert Visibility.visible_for_user?(public, nil)
215 assert Visibility.visible_for_user?(unlisted, nil)
216 refute Visibility.visible_for_user?(private, nil)
217 refute Visibility.visible_for_user?(direct, nil)
219 # Visible for a list member
220 # assert Visibility.visible_for_user?(list, unrelated)
223 test "doesn't die when the user doesn't exist",
229 Pleroma.User.invalidate_cache(user)
230 refute Visibility.is_private?(direct)
233 test "get_visibility", %{
240 assert Visibility.get_visibility(public) == "public"
241 assert Visibility.get_visibility(private) == "private"
242 assert Visibility.get_visibility(direct) == "direct"
243 assert Visibility.get_visibility(unlisted) == "unlisted"
244 assert Visibility.get_visibility(list) == "list"
247 test "get_visibility with directMessage flag" do
248 assert Visibility.get_visibility(%{data: %{"directMessage" => true}}) == "direct"
251 test "get_visibility with listMessage flag" do
252 assert Visibility.get_visibility(%{data: %{"listMessage" => ""}}) == "list"
255 describe "entire_thread_visible_for_user?/2" do
256 test "returns false if not found activity", %{user: user} do
257 refute Visibility.entire_thread_visible_for_user?(%Activity{}, user)
260 test "returns true if activity hasn't 'Create' type", %{user: user} do
261 activity = insert(:like_activity)
262 assert Visibility.entire_thread_visible_for_user?(activity, user)
265 test "returns false when invalid recipients", %{user: user} do
266 author = insert(:user)
269 insert(:note_activity,
273 data: %{"to" => ["test-user"]}
277 refute Visibility.entire_thread_visible_for_user?(activity, user)
280 test "returns true if user following to author" do
281 author = insert(:user)
283 Pleroma.User.follow(user, author)
286 insert(:note_activity,
290 data: %{"to" => [user.ap_id]}
294 assert Visibility.entire_thread_visible_for_user?(activity, user)