2019-07-09 23:13:23 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 13:38:50 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-07-09 23:13:23 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-02-22 05:29:52 -07:00
|
|
|
defmodule Pleroma.Web.ActivityPub.VisibilityTest do
|
2020-12-21 04:21:40 -07:00
|
|
|
use Pleroma.DataCase, async: true
|
2019-02-22 05:29:52 -07:00
|
|
|
|
2019-06-03 07:04:39 -06:00
|
|
|
alias Pleroma.Activity
|
2021-01-21 09:45:42 -07:00
|
|
|
alias Pleroma.Object
|
2019-02-22 05:29:52 -07:00
|
|
|
alias Pleroma.Web.ActivityPub.Visibility
|
2019-03-04 19:52:23 -07:00
|
|
|
alias Pleroma.Web.CommonAPI
|
2019-02-22 05:29:52 -07:00
|
|
|
import Pleroma.Factory
|
|
|
|
|
|
|
|
setup do
|
|
|
|
user = insert(:user)
|
|
|
|
mentioned = insert(:user)
|
|
|
|
following = insert(:user)
|
|
|
|
unrelated = insert(:user)
|
2020-12-01 13:17:52 -07:00
|
|
|
{:ok, following, user} = Pleroma.User.follow(following, user)
|
2019-07-11 06:29:24 -06:00
|
|
|
{:ok, list} = Pleroma.List.create("foo", user)
|
|
|
|
|
|
|
|
Pleroma.List.follow(list, unrelated)
|
2019-02-22 05:29:52 -07:00
|
|
|
|
|
|
|
{:ok, public} =
|
2020-05-12 13:59:26 -06:00
|
|
|
CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "public"})
|
2019-02-22 05:29:52 -07:00
|
|
|
|
|
|
|
{:ok, private} =
|
2020-05-12 13:59:26 -06:00
|
|
|
CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "private"})
|
2019-02-22 05:29:52 -07:00
|
|
|
|
|
|
|
{:ok, direct} =
|
2020-05-12 13:59:26 -06:00
|
|
|
CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "direct"})
|
2019-02-22 05:29:52 -07:00
|
|
|
|
|
|
|
{:ok, unlisted} =
|
2020-05-12 13:59:26 -06:00
|
|
|
CommonAPI.post(user, %{status: "@#{mentioned.nickname}", visibility: "unlisted"})
|
2019-02-22 05:29:52 -07:00
|
|
|
|
2019-07-11 06:29:24 -06:00
|
|
|
{:ok, list} =
|
|
|
|
CommonAPI.post(user, %{
|
2020-05-12 13:59:26 -06:00
|
|
|
status: "@#{mentioned.nickname}",
|
|
|
|
visibility: "list:#{list.id}"
|
2019-07-11 06:29:24 -06:00
|
|
|
})
|
|
|
|
|
2019-02-22 05:29:52 -07:00
|
|
|
%{
|
|
|
|
public: public,
|
|
|
|
private: private,
|
|
|
|
direct: direct,
|
|
|
|
unlisted: unlisted,
|
|
|
|
user: user,
|
|
|
|
mentioned: mentioned,
|
|
|
|
following: following,
|
2019-07-11 06:29:24 -06:00
|
|
|
unrelated: unrelated,
|
|
|
|
list: list
|
2019-02-22 05:29:52 -07:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-07-11 06:29:24 -06:00
|
|
|
test "is_direct?", %{
|
|
|
|
public: public,
|
|
|
|
private: private,
|
|
|
|
direct: direct,
|
|
|
|
unlisted: unlisted,
|
|
|
|
list: list
|
|
|
|
} do
|
2019-02-22 05:29:52 -07:00
|
|
|
assert Visibility.is_direct?(direct)
|
|
|
|
refute Visibility.is_direct?(public)
|
|
|
|
refute Visibility.is_direct?(private)
|
|
|
|
refute Visibility.is_direct?(unlisted)
|
2019-07-11 06:29:24 -06:00
|
|
|
assert Visibility.is_direct?(list)
|
2019-02-22 05:29:52 -07:00
|
|
|
end
|
|
|
|
|
2019-07-11 06:29:24 -06:00
|
|
|
test "is_public?", %{
|
|
|
|
public: public,
|
|
|
|
private: private,
|
|
|
|
direct: direct,
|
|
|
|
unlisted: unlisted,
|
|
|
|
list: list
|
|
|
|
} do
|
2019-02-22 05:29:52 -07:00
|
|
|
refute Visibility.is_public?(direct)
|
|
|
|
assert Visibility.is_public?(public)
|
|
|
|
refute Visibility.is_public?(private)
|
|
|
|
assert Visibility.is_public?(unlisted)
|
2019-07-11 06:29:24 -06:00
|
|
|
refute Visibility.is_public?(list)
|
2019-02-22 05:29:52 -07:00
|
|
|
end
|
|
|
|
|
2019-07-11 06:29:24 -06:00
|
|
|
test "is_private?", %{
|
|
|
|
public: public,
|
|
|
|
private: private,
|
|
|
|
direct: direct,
|
|
|
|
unlisted: unlisted,
|
|
|
|
list: list
|
|
|
|
} do
|
2019-02-22 05:29:52 -07:00
|
|
|
refute Visibility.is_private?(direct)
|
|
|
|
refute Visibility.is_private?(public)
|
|
|
|
assert Visibility.is_private?(private)
|
|
|
|
refute Visibility.is_private?(unlisted)
|
2019-07-11 06:29:24 -06:00
|
|
|
refute Visibility.is_private?(list)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "is_list?", %{
|
|
|
|
public: public,
|
|
|
|
private: private,
|
|
|
|
direct: direct,
|
|
|
|
unlisted: unlisted,
|
|
|
|
list: list
|
|
|
|
} do
|
|
|
|
refute Visibility.is_list?(direct)
|
|
|
|
refute Visibility.is_list?(public)
|
|
|
|
refute Visibility.is_list?(private)
|
|
|
|
refute Visibility.is_list?(unlisted)
|
|
|
|
assert Visibility.is_list?(list)
|
2019-02-22 05:29:52 -07:00
|
|
|
end
|
|
|
|
|
2021-01-21 09:45:42 -07:00
|
|
|
test "visible_for_user? Activity", %{
|
2019-02-22 05:29:52 -07:00
|
|
|
public: public,
|
|
|
|
private: private,
|
|
|
|
direct: direct,
|
|
|
|
unlisted: unlisted,
|
|
|
|
user: user,
|
|
|
|
mentioned: mentioned,
|
|
|
|
following: following,
|
2019-07-11 06:29:24 -06:00
|
|
|
unrelated: unrelated,
|
|
|
|
list: list
|
2019-02-22 05:29:52 -07:00
|
|
|
} do
|
|
|
|
# All visible to author
|
|
|
|
|
|
|
|
assert Visibility.visible_for_user?(public, user)
|
|
|
|
assert Visibility.visible_for_user?(private, user)
|
|
|
|
assert Visibility.visible_for_user?(unlisted, user)
|
|
|
|
assert Visibility.visible_for_user?(direct, user)
|
2019-07-11 06:29:24 -06:00
|
|
|
assert Visibility.visible_for_user?(list, user)
|
2019-02-22 05:29:52 -07:00
|
|
|
|
2019-07-15 01:54:40 -06:00
|
|
|
# All visible to a mentioned user
|
2019-02-22 05:29:52 -07:00
|
|
|
|
|
|
|
assert Visibility.visible_for_user?(public, mentioned)
|
|
|
|
assert Visibility.visible_for_user?(private, mentioned)
|
|
|
|
assert Visibility.visible_for_user?(unlisted, mentioned)
|
|
|
|
assert Visibility.visible_for_user?(direct, mentioned)
|
2019-07-15 01:54:40 -06:00
|
|
|
assert Visibility.visible_for_user?(list, mentioned)
|
2019-02-22 05:29:52 -07:00
|
|
|
|
|
|
|
# DM not visible for just follower
|
|
|
|
|
|
|
|
assert Visibility.visible_for_user?(public, following)
|
|
|
|
assert Visibility.visible_for_user?(private, following)
|
|
|
|
assert Visibility.visible_for_user?(unlisted, following)
|
|
|
|
refute Visibility.visible_for_user?(direct, following)
|
2019-07-11 06:29:24 -06:00
|
|
|
refute Visibility.visible_for_user?(list, following)
|
2019-02-22 05:29:52 -07:00
|
|
|
|
|
|
|
# Public and unlisted visible for unrelated user
|
|
|
|
|
|
|
|
assert Visibility.visible_for_user?(public, unrelated)
|
|
|
|
assert Visibility.visible_for_user?(unlisted, unrelated)
|
|
|
|
refute Visibility.visible_for_user?(private, unrelated)
|
|
|
|
refute Visibility.visible_for_user?(direct, unrelated)
|
2019-07-11 06:29:24 -06:00
|
|
|
|
2021-01-21 09:45:42 -07:00
|
|
|
# Public and unlisted visible for unauthenticated
|
|
|
|
|
|
|
|
assert Visibility.visible_for_user?(public, nil)
|
|
|
|
assert Visibility.visible_for_user?(unlisted, nil)
|
|
|
|
refute Visibility.visible_for_user?(private, nil)
|
|
|
|
refute Visibility.visible_for_user?(direct, nil)
|
|
|
|
|
2019-07-11 06:29:24 -06:00
|
|
|
# Visible for a list member
|
|
|
|
assert Visibility.visible_for_user?(list, unrelated)
|
2019-02-22 05:29:52 -07:00
|
|
|
end
|
2019-05-08 10:08:50 -06:00
|
|
|
|
2021-01-21 09:45:42 -07:00
|
|
|
test "visible_for_user? Object", %{
|
|
|
|
public: public,
|
|
|
|
private: private,
|
|
|
|
direct: direct,
|
|
|
|
unlisted: unlisted,
|
|
|
|
user: user,
|
|
|
|
mentioned: mentioned,
|
|
|
|
following: following,
|
|
|
|
unrelated: unrelated,
|
|
|
|
list: list
|
|
|
|
} do
|
|
|
|
public = Object.normalize(public)
|
|
|
|
private = Object.normalize(private)
|
|
|
|
unlisted = Object.normalize(unlisted)
|
|
|
|
direct = Object.normalize(direct)
|
|
|
|
list = Object.normalize(list)
|
|
|
|
|
|
|
|
# All visible to author
|
|
|
|
|
|
|
|
assert Visibility.visible_for_user?(public, user)
|
|
|
|
assert Visibility.visible_for_user?(private, user)
|
|
|
|
assert Visibility.visible_for_user?(unlisted, user)
|
|
|
|
assert Visibility.visible_for_user?(direct, user)
|
|
|
|
assert Visibility.visible_for_user?(list, user)
|
|
|
|
|
|
|
|
# All visible to a mentioned user
|
|
|
|
|
|
|
|
assert Visibility.visible_for_user?(public, mentioned)
|
|
|
|
assert Visibility.visible_for_user?(private, mentioned)
|
|
|
|
assert Visibility.visible_for_user?(unlisted, mentioned)
|
|
|
|
assert Visibility.visible_for_user?(direct, mentioned)
|
|
|
|
assert Visibility.visible_for_user?(list, mentioned)
|
|
|
|
|
|
|
|
# DM not visible for just follower
|
|
|
|
|
|
|
|
assert Visibility.visible_for_user?(public, following)
|
|
|
|
assert Visibility.visible_for_user?(private, following)
|
|
|
|
assert Visibility.visible_for_user?(unlisted, following)
|
|
|
|
refute Visibility.visible_for_user?(direct, following)
|
|
|
|
refute Visibility.visible_for_user?(list, following)
|
|
|
|
|
|
|
|
# Public and unlisted visible for unrelated user
|
|
|
|
|
|
|
|
assert Visibility.visible_for_user?(public, unrelated)
|
|
|
|
assert Visibility.visible_for_user?(unlisted, unrelated)
|
|
|
|
refute Visibility.visible_for_user?(private, unrelated)
|
|
|
|
refute Visibility.visible_for_user?(direct, unrelated)
|
|
|
|
|
|
|
|
# Public and unlisted visible for unauthenticated
|
|
|
|
|
|
|
|
assert Visibility.visible_for_user?(public, nil)
|
|
|
|
assert Visibility.visible_for_user?(unlisted, nil)
|
|
|
|
refute Visibility.visible_for_user?(private, nil)
|
|
|
|
refute Visibility.visible_for_user?(direct, nil)
|
|
|
|
|
|
|
|
# Visible for a list member
|
|
|
|
# assert Visibility.visible_for_user?(list, unrelated)
|
|
|
|
end
|
|
|
|
|
2019-05-08 10:08:50 -06:00
|
|
|
test "doesn't die when the user doesn't exist",
|
|
|
|
%{
|
|
|
|
direct: direct,
|
|
|
|
user: user
|
|
|
|
} do
|
|
|
|
Repo.delete(user)
|
2020-12-18 05:18:17 -07:00
|
|
|
Pleroma.User.invalidate_cache(user)
|
2019-05-08 10:08:50 -06:00
|
|
|
refute Visibility.is_private?(direct)
|
|
|
|
end
|
2019-05-15 09:47:29 -06:00
|
|
|
|
2019-05-15 08:30:08 -06:00
|
|
|
test "get_visibility", %{
|
|
|
|
public: public,
|
|
|
|
private: private,
|
|
|
|
direct: direct,
|
2019-07-11 06:29:24 -06:00
|
|
|
unlisted: unlisted,
|
|
|
|
list: list
|
2019-05-15 08:30:08 -06:00
|
|
|
} do
|
|
|
|
assert Visibility.get_visibility(public) == "public"
|
|
|
|
assert Visibility.get_visibility(private) == "private"
|
|
|
|
assert Visibility.get_visibility(direct) == "direct"
|
|
|
|
assert Visibility.get_visibility(unlisted) == "unlisted"
|
2019-07-11 06:29:24 -06:00
|
|
|
assert Visibility.get_visibility(list) == "list"
|
2019-05-15 08:30:08 -06:00
|
|
|
end
|
2019-05-31 21:23:21 -06:00
|
|
|
|
|
|
|
test "get_visibility with directMessage flag" do
|
|
|
|
assert Visibility.get_visibility(%{data: %{"directMessage" => true}}) == "direct"
|
|
|
|
end
|
2019-06-03 07:04:39 -06:00
|
|
|
|
2019-07-11 06:29:24 -06:00
|
|
|
test "get_visibility with listMessage flag" do
|
|
|
|
assert Visibility.get_visibility(%{data: %{"listMessage" => ""}}) == "list"
|
|
|
|
end
|
|
|
|
|
2019-06-03 07:04:39 -06:00
|
|
|
describe "entire_thread_visible_for_user?/2" do
|
|
|
|
test "returns false if not found activity", %{user: user} do
|
|
|
|
refute Visibility.entire_thread_visible_for_user?(%Activity{}, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns true if activity hasn't 'Create' type", %{user: user} do
|
|
|
|
activity = insert(:like_activity)
|
|
|
|
assert Visibility.entire_thread_visible_for_user?(activity, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns false when invalid recipients", %{user: user} do
|
|
|
|
author = insert(:user)
|
|
|
|
|
|
|
|
activity =
|
|
|
|
insert(:note_activity,
|
|
|
|
note:
|
|
|
|
insert(:note,
|
|
|
|
user: author,
|
|
|
|
data: %{"to" => ["test-user"]}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
refute Visibility.entire_thread_visible_for_user?(activity, user)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "returns true if user following to author" do
|
|
|
|
author = insert(:user)
|
2019-10-10 13:35:32 -06:00
|
|
|
user = insert(:user)
|
|
|
|
Pleroma.User.follow(user, author)
|
2019-06-03 07:04:39 -06:00
|
|
|
|
|
|
|
activity =
|
|
|
|
insert(:note_activity,
|
|
|
|
note:
|
|
|
|
insert(:note,
|
|
|
|
user: author,
|
|
|
|
data: %{"to" => [user.ap_id]}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
assert Visibility.entire_thread_visible_for_user?(activity, user)
|
|
|
|
end
|
|
|
|
end
|
2019-02-22 05:29:52 -07:00
|
|
|
end
|