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 remote = insert(:user, local: false)
20 {:ok, following, user} = Pleroma.User.follow(following, user)
21 {:ok, list} = Pleroma.List.create("foo", user)
23 Pleroma.List.follow(list, unrelated)
26 CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "public"})
29 CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "private"})
32 CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "direct"})
35 CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "unlisted"})
37 {:ok, local} = CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "local"})
40 CommonAPI.post(user, %{
41 status: "@#{mentioned.nickname}",
42 visibility: "list:#{list.id}"
68 assert Visibility.is_direct?(direct)
69 refute Visibility.is_direct?(public)
70 refute Visibility.is_direct?(private)
71 refute Visibility.is_direct?(unlisted)
72 assert Visibility.is_direct?(list)
73 refute Visibility.is_direct?(local)
84 refute Visibility.is_public?(direct)
85 assert Visibility.is_public?(public)
86 refute Visibility.is_public?(private)
87 assert Visibility.is_public?(unlisted)
88 assert Visibility.is_public?(local)
89 refute Visibility.is_public?(list)
92 test "is_private?", %{
100 refute Visibility.is_private?(direct)
101 refute Visibility.is_private?(public)
102 assert Visibility.is_private?(private)
103 refute Visibility.is_private?(unlisted)
104 refute Visibility.is_private?(list)
105 refute Visibility.is_private?(local)
116 refute Visibility.is_list?(direct)
117 refute Visibility.is_list?(public)
118 refute Visibility.is_list?(private)
119 refute Visibility.is_list?(unlisted)
120 assert Visibility.is_list?(list)
121 refute Visibility.is_list?(local)
124 test "visible_for_user? Activity", %{
130 mentioned: mentioned,
131 following: following,
132 unrelated: unrelated,
137 # All visible to author
139 assert Visibility.visible_for_user?(public, user)
140 assert Visibility.visible_for_user?(private, user)
141 assert Visibility.visible_for_user?(unlisted, user)
142 assert Visibility.visible_for_user?(direct, user)
143 assert Visibility.visible_for_user?(list, user)
144 assert Visibility.visible_for_user?(local, user)
146 # All visible to a mentioned user
148 assert Visibility.visible_for_user?(public, mentioned)
149 assert Visibility.visible_for_user?(private, mentioned)
150 assert Visibility.visible_for_user?(unlisted, mentioned)
151 assert Visibility.visible_for_user?(direct, mentioned)
152 assert Visibility.visible_for_user?(list, mentioned)
153 assert Visibility.visible_for_user?(local, mentioned)
155 # DM not visible for just follower
157 assert Visibility.visible_for_user?(public, following)
158 assert Visibility.visible_for_user?(private, following)
159 assert Visibility.visible_for_user?(unlisted, following)
160 refute Visibility.visible_for_user?(direct, following)
161 refute Visibility.visible_for_user?(list, following)
162 assert Visibility.visible_for_user?(local, following)
164 # Public and unlisted visible for unrelated user
166 assert Visibility.visible_for_user?(public, unrelated)
167 assert Visibility.visible_for_user?(unlisted, unrelated)
168 refute Visibility.visible_for_user?(private, unrelated)
169 refute Visibility.visible_for_user?(direct, unrelated)
170 assert Visibility.visible_for_user?(local, unrelated)
172 # Public and unlisted visible for unauthenticated
174 assert Visibility.visible_for_user?(public, nil)
175 assert Visibility.visible_for_user?(unlisted, nil)
176 refute Visibility.visible_for_user?(private, nil)
177 refute Visibility.visible_for_user?(direct, nil)
178 refute Visibility.visible_for_user?(local, nil)
180 # Visible for a list member
181 assert Visibility.visible_for_user?(list, unrelated)
183 # Local not visible to remote user
184 refute Visibility.visible_for_user?(local, remote)
187 test "visible_for_user? Object", %{
193 mentioned: mentioned,
194 following: following,
195 unrelated: unrelated,
200 public = Object.normalize(public)
201 private = Object.normalize(private)
202 unlisted = Object.normalize(unlisted)
203 direct = Object.normalize(direct)
204 list = Object.normalize(list)
205 local = Object.normalize(local)
207 # All visible to author
209 assert Visibility.visible_for_user?(public, user)
210 assert Visibility.visible_for_user?(private, user)
211 assert Visibility.visible_for_user?(unlisted, user)
212 assert Visibility.visible_for_user?(direct, user)
213 assert Visibility.visible_for_user?(list, user)
215 # All visible to a mentioned user
217 assert Visibility.visible_for_user?(public, mentioned)
218 assert Visibility.visible_for_user?(private, mentioned)
219 assert Visibility.visible_for_user?(unlisted, mentioned)
220 assert Visibility.visible_for_user?(direct, mentioned)
221 assert Visibility.visible_for_user?(list, mentioned)
223 # DM not visible for just follower
225 assert Visibility.visible_for_user?(public, following)
226 assert Visibility.visible_for_user?(private, following)
227 assert Visibility.visible_for_user?(unlisted, following)
228 refute Visibility.visible_for_user?(direct, following)
229 refute Visibility.visible_for_user?(list, following)
231 # Public and unlisted visible for unrelated user
233 assert Visibility.visible_for_user?(public, unrelated)
234 assert Visibility.visible_for_user?(unlisted, unrelated)
235 refute Visibility.visible_for_user?(private, unrelated)
236 refute Visibility.visible_for_user?(direct, unrelated)
238 # Public and unlisted visible for unauthenticated
240 assert Visibility.visible_for_user?(public, nil)
241 assert Visibility.visible_for_user?(unlisted, nil)
242 refute Visibility.visible_for_user?(private, nil)
243 refute Visibility.visible_for_user?(direct, nil)
244 refute Visibility.visible_for_user?(local, nil)
246 # Local posts to remote
247 refute Visibility.visible_for_user?(local, remote)
248 # Visible for a list member
249 # assert Visibility.visible_for_user?(list, unrelated)
252 test "doesn't die when the user doesn't exist",
258 Pleroma.User.invalidate_cache(user)
259 refute Visibility.is_private?(direct)
262 test "get_visibility", %{
269 assert Visibility.get_visibility(public) == "public"
270 assert Visibility.get_visibility(private) == "private"
271 assert Visibility.get_visibility(direct) == "direct"
272 assert Visibility.get_visibility(unlisted) == "unlisted"
273 assert Visibility.get_visibility(list) == "list"
276 test "get_visibility with directMessage flag" do
277 assert Visibility.get_visibility(%{data: %{"directMessage" => true}}) == "direct"
280 test "get_visibility with listMessage flag" do
281 assert Visibility.get_visibility(%{data: %{"listMessage" => ""}}) == "list"
284 describe "entire_thread_visible_for_user?/2" do
285 test "returns false if not found activity", %{user: user} do
286 refute Visibility.entire_thread_visible_for_user?(%Activity{}, user)
289 test "returns true if activity hasn't 'Create' type", %{user: user} do
290 activity = insert(:like_activity)
291 assert Visibility.entire_thread_visible_for_user?(activity, user)
294 test "returns false when invalid recipients", %{user: user} do
295 author = insert(:user)
298 insert(:note_activity,
302 data: %{"to" => ["test-user"]}
306 refute Visibility.entire_thread_visible_for_user?(activity, user)
309 test "returns true if user following to author" do
310 author = insert(:user)
312 Pleroma.User.follow(user, author)
315 insert(:note_activity,
319 data: %{"to" => [user.ap_id]}
323 assert Visibility.entire_thread_visible_for_user?(activity, user)