1
0
mirror of https://git.pleroma.social/sjw/pleroma.git synced 2024-11-11 02:29:17 +01:00

Add list to Visibility

This commit is contained in:
Egor Kislitsyn 2019-07-11 19:29:24 +07:00
parent 958fb9aa80
commit 9e06873d58
4 changed files with 98 additions and 7 deletions

View File

@ -146,4 +146,10 @@ defmodule Pleroma.List do
end end
def memberships(_), do: [] def memberships(_), do: []
def member?(%Pleroma.List{following: following}, %User{follower_address: follower_address}) do
Enum.member?(following, follower_address)
end
def member?(_, _), do: false
end end

View File

@ -34,6 +34,19 @@ defmodule Pleroma.Web.ActivityPub.Visibility do
!is_public?(activity) && !is_private?(activity) !is_public?(activity) && !is_private?(activity)
end end
def is_list?(%{data: %{"listMessage" => _}}), do: true
def is_list?(_), do: false
def visible_for_user?(%{actor: ap_id}, %User{ap_id: ap_id}), do: true
def visible_for_user?(%{data: %{"listMessage" => list_ap_id}}, %User{} = user) do
list_ap_id
|> Pleroma.List.get_by_ap_id()
|> Pleroma.List.member?(user)
end
def visible_for_user?(%{data: %{"listMessage" => _}}, nil), do: false
def visible_for_user?(activity, nil) do def visible_for_user?(activity, nil) do
is_public?(activity) is_public?(activity)
end end
@ -73,6 +86,9 @@ defmodule Pleroma.Web.ActivityPub.Visibility do
object.data["directMessage"] == true -> object.data["directMessage"] == true ->
"direct" "direct"
is_binary(object.data["listMessage"]) ->
"list"
length(cc) > 0 -> length(cc) > 0 ->
"private" "private"

View File

@ -128,4 +128,15 @@ defmodule Pleroma.ListTest do
assert Pleroma.List.memberships(member) == [list.ap_id] assert Pleroma.List.memberships(member) == [list.ap_id]
end end
test "member?" do
user = insert(:user)
member = insert(:user)
{:ok, list} = Pleroma.List.create("foo", user)
{:ok, list} = Pleroma.List.follow(list, member)
assert Pleroma.List.member?(list, member)
refute Pleroma.List.member?(list, user)
end
end end

View File

@ -16,6 +16,9 @@ defmodule Pleroma.Web.ActivityPub.VisibilityTest do
following = insert(:user) following = insert(:user)
unrelated = insert(:user) unrelated = insert(:user)
{:ok, following} = Pleroma.User.follow(following, user) {:ok, following} = Pleroma.User.follow(following, user)
{:ok, list} = Pleroma.List.create("foo", user)
Pleroma.List.follow(list, unrelated)
{:ok, public} = {:ok, public} =
CommonAPI.post(user, %{"status" => "@#{mentioned.nickname}", "visibility" => "public"}) CommonAPI.post(user, %{"status" => "@#{mentioned.nickname}", "visibility" => "public"})
@ -29,6 +32,12 @@ defmodule Pleroma.Web.ActivityPub.VisibilityTest do
{:ok, unlisted} = {:ok, unlisted} =
CommonAPI.post(user, %{"status" => "@#{mentioned.nickname}", "visibility" => "unlisted"}) CommonAPI.post(user, %{"status" => "@#{mentioned.nickname}", "visibility" => "unlisted"})
{:ok, list} =
CommonAPI.post(user, %{
"status" => "@#{mentioned.nickname}",
"visibility" => "list:#{list.id}"
})
%{ %{
public: public, public: public,
private: private, private: private,
@ -37,29 +46,65 @@ defmodule Pleroma.Web.ActivityPub.VisibilityTest do
user: user, user: user,
mentioned: mentioned, mentioned: mentioned,
following: following, following: following,
unrelated: unrelated unrelated: unrelated,
list: list
} }
end end
test "is_direct?", %{public: public, private: private, direct: direct, unlisted: unlisted} do test "is_direct?", %{
public: public,
private: private,
direct: direct,
unlisted: unlisted,
list: list
} do
assert Visibility.is_direct?(direct) assert Visibility.is_direct?(direct)
refute Visibility.is_direct?(public) refute Visibility.is_direct?(public)
refute Visibility.is_direct?(private) refute Visibility.is_direct?(private)
refute Visibility.is_direct?(unlisted) refute Visibility.is_direct?(unlisted)
assert Visibility.is_direct?(list)
end end
test "is_public?", %{public: public, private: private, direct: direct, unlisted: unlisted} do test "is_public?", %{
public: public,
private: private,
direct: direct,
unlisted: unlisted,
list: list
} do
refute Visibility.is_public?(direct) refute Visibility.is_public?(direct)
assert Visibility.is_public?(public) assert Visibility.is_public?(public)
refute Visibility.is_public?(private) refute Visibility.is_public?(private)
assert Visibility.is_public?(unlisted) assert Visibility.is_public?(unlisted)
refute Visibility.is_public?(list)
end end
test "is_private?", %{public: public, private: private, direct: direct, unlisted: unlisted} do test "is_private?", %{
public: public,
private: private,
direct: direct,
unlisted: unlisted,
list: list
} do
refute Visibility.is_private?(direct) refute Visibility.is_private?(direct)
refute Visibility.is_private?(public) refute Visibility.is_private?(public)
assert Visibility.is_private?(private) assert Visibility.is_private?(private)
refute Visibility.is_private?(unlisted) refute Visibility.is_private?(unlisted)
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)
end end
test "visible_for_user?", %{ test "visible_for_user?", %{
@ -70,7 +115,8 @@ defmodule Pleroma.Web.ActivityPub.VisibilityTest do
user: user, user: user,
mentioned: mentioned, mentioned: mentioned,
following: following, following: following,
unrelated: unrelated unrelated: unrelated,
list: list
} do } do
# All visible to author # All visible to author
@ -78,13 +124,15 @@ defmodule Pleroma.Web.ActivityPub.VisibilityTest do
assert Visibility.visible_for_user?(private, user) assert Visibility.visible_for_user?(private, user)
assert Visibility.visible_for_user?(unlisted, user) assert Visibility.visible_for_user?(unlisted, user)
assert Visibility.visible_for_user?(direct, user) assert Visibility.visible_for_user?(direct, user)
assert Visibility.visible_for_user?(list, user)
# All visible to a mentioned user # All visible to a mentioned user, except when it's a list activity
assert Visibility.visible_for_user?(public, mentioned) assert Visibility.visible_for_user?(public, mentioned)
assert Visibility.visible_for_user?(private, mentioned) assert Visibility.visible_for_user?(private, mentioned)
assert Visibility.visible_for_user?(unlisted, mentioned) assert Visibility.visible_for_user?(unlisted, mentioned)
assert Visibility.visible_for_user?(direct, mentioned) assert Visibility.visible_for_user?(direct, mentioned)
refute(Visibility.visible_for_user?(list, mentioned))
# DM not visible for just follower # DM not visible for just follower
@ -92,6 +140,7 @@ defmodule Pleroma.Web.ActivityPub.VisibilityTest do
assert Visibility.visible_for_user?(private, following) assert Visibility.visible_for_user?(private, following)
assert Visibility.visible_for_user?(unlisted, following) assert Visibility.visible_for_user?(unlisted, following)
refute Visibility.visible_for_user?(direct, following) refute Visibility.visible_for_user?(direct, following)
refute Visibility.visible_for_user?(list, following)
# Public and unlisted visible for unrelated user # Public and unlisted visible for unrelated user
@ -99,6 +148,9 @@ defmodule Pleroma.Web.ActivityPub.VisibilityTest do
assert Visibility.visible_for_user?(unlisted, unrelated) assert Visibility.visible_for_user?(unlisted, unrelated)
refute Visibility.visible_for_user?(private, unrelated) refute Visibility.visible_for_user?(private, unrelated)
refute Visibility.visible_for_user?(direct, unrelated) refute Visibility.visible_for_user?(direct, unrelated)
# Visible for a list member
assert Visibility.visible_for_user?(list, unrelated)
end end
test "doesn't die when the user doesn't exist", test "doesn't die when the user doesn't exist",
@ -115,18 +167,24 @@ defmodule Pleroma.Web.ActivityPub.VisibilityTest do
public: public, public: public,
private: private, private: private,
direct: direct, direct: direct,
unlisted: unlisted unlisted: unlisted,
list: list
} do } do
assert Visibility.get_visibility(public) == "public" assert Visibility.get_visibility(public) == "public"
assert Visibility.get_visibility(private) == "private" assert Visibility.get_visibility(private) == "private"
assert Visibility.get_visibility(direct) == "direct" assert Visibility.get_visibility(direct) == "direct"
assert Visibility.get_visibility(unlisted) == "unlisted" assert Visibility.get_visibility(unlisted) == "unlisted"
assert Visibility.get_visibility(list) == "list"
end end
test "get_visibility with directMessage flag" do test "get_visibility with directMessage flag" do
assert Visibility.get_visibility(%{data: %{"directMessage" => true}}) == "direct" assert Visibility.get_visibility(%{data: %{"directMessage" => true}}) == "direct"
end end
test "get_visibility with listMessage flag" do
assert Visibility.get_visibility(%{data: %{"listMessage" => ""}}) == "list"
end
describe "entire_thread_visible_for_user?/2" do describe "entire_thread_visible_for_user?/2" do
test "returns false if not found activity", %{user: user} do test "returns false if not found activity", %{user: user} do
refute Visibility.entire_thread_visible_for_user?(%Activity{}, user) refute Visibility.entire_thread_visible_for_user?(%Activity{}, user)