Compare commits

...

6 Commits

Author SHA1 Message Date
Sadposter
8d717c4be7 remove vim swap file 2019-11-26 19:07:00 +00:00
Sadposter
ae0e2446f7 obligatory format commit 2019-11-26 18:33:15 +00:00
Sadposter
0c79869932 fix relationship view tests 2019-11-26 18:32:31 +00:00
Sadposter
c5a7b8ad98 add whitelist route tests 2019-11-26 18:26:37 +00:00
Sadposter
686174897b add API routes to whitelist/unwhitelist 2019-11-26 18:26:17 +00:00
Sadposter
f741f79256 add user whitelist relationship 2019-11-26 17:56:37 +00:00
8 changed files with 194 additions and 2 deletions

View File

@ -79,6 +79,7 @@ defmodule Pleroma.User do
field(:muted_reblogs, {:array, :string}, default: [])
field(:muted_notifications, {:array, :string}, default: [])
field(:subscribers, {:array, :string}, default: [])
field(:whitelist, {:array, :string}, default: [])
field(:deactivated, :boolean, default: false)
field(:no_rich_text, :boolean, default: false)
field(:ap_enabled, :boolean, default: false)
@ -1019,6 +1020,14 @@ defmodule Pleroma.User do
blocker
end
blocker =
if whitelists?(blocker, blocked) do
{:ok, blocker} = unwhitelist(blocker, blocked)
blocker
else
blocker
end
if following?(blocked, blocker), do: unfollow(blocked, blocker)
{:ok, blocker} = update_follower_count(blocker)
@ -1035,6 +1044,22 @@ defmodule Pleroma.User do
remove_from_block(blocker, ap_id)
end
def whitelist(whitelister, %User{ap_id: ap_id} = whitelisted) do
whitelister =
if blocks?(whitelister, whitelisted) do
{:ok, whitelister} = unblock(whitelister, whitelisted)
whitelister
else
whitelister
end
add_to_whitelist(whitelister, ap_id)
end
def unwhitelist(whitelister, %{ap_id: ap_id}) do
remove_from_whitelist(whitelister, ap_id)
end
def mutes?(nil, _), do: false
def mutes?(user, %{ap_id: ap_id}), do: Enum.member?(user.mutes, ap_id)
@ -1045,7 +1070,8 @@ defmodule Pleroma.User do
do: Enum.member?(user.muted_notifications, ap_id)
def blocks?(%User{} = user, %User{} = target) do
blocks_ap_id?(user, target) || blocks_domain?(user, target)
blocks_ap_id?(user, target) ||
(blocks_domain?(user, target) && !whitelists?(user, target))
end
def blocks?(nil, _), do: false
@ -1064,6 +1090,12 @@ defmodule Pleroma.User do
def blocks_domain?(_, _), do: false
def whitelists?(%User{} = user, %User{} = target) do
Enum.member?(user.whitelist, target.ap_id)
end
def whitelists?(_, _), do: false
def subscribed_to?(user, %{ap_id: ap_id}) do
with %User{} = target <- get_cached_by_ap_id(ap_id) do
Enum.member?(target.subscribers, user.ap_id)
@ -1846,6 +1878,14 @@ defmodule Pleroma.User do
set_subscribers(user, List.delete(user.subscribers, subscribed))
end
def add_to_whitelist(user, whitelisted) do
set_whitelist(user, Enum.uniq([whitelisted | user.whitelist]))
end
def remove_from_whitelist(user, whitelisted) do
set_whitelist(user, List.delete(user.whitelist, whitelisted))
end
defp set_domain_blocks(user, domain_blocks) do
params = %{domain_blocks: domain_blocks}
@ -1872,6 +1912,15 @@ defmodule Pleroma.User do
|> update_and_set_cache()
end
defp set_whitelist(user, whitelist) do
params = %{whitelist: whitelist}
user
|> cast(params, [:whitelist])
|> validate_required([:whitelist])
|> update_and_set_cache()
end
def add_to_block(user, blocked) do
set_blocks(user, Enum.uniq([blocked | user.blocks]))
end

View File

@ -55,6 +55,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
muting: User.mutes?(user, target),
muting_notifications: User.muted_notifications?(user, target),
subscribing: User.subscribed_to?(user, target),
whitelisting: User.whitelists?(user, target),
requested: requested,
domain_blocking: User.blocks_domain?(user, target),
showing_reblogs: User.showing_reblogs?(user, target),

View File

@ -43,7 +43,12 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do
)
plug(RateLimiter, [name: :account_confirmation_resend] when action == :confirmation_resend)
plug(:assign_account_by_id when action in [:favourites, :subscribe, :unsubscribe])
plug(
:assign_account_by_id
when action in [:favourites, :subscribe, :unsubscribe, :whitelist, :unwhitelist]
)
plug(:put_view, Pleroma.Web.MastodonAPI.AccountView)
@doc "POST /api/v1/pleroma/accounts/confirmation_resend"
@ -159,4 +164,22 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do
{:error, message} -> json_response(conn, :forbidden, %{error: message})
end
end
@doc "POST /api/v1/pleroma/accounts/:id/whitelist"
def whitelist(%{assigns: %{user: user, account: whitelist_target}} = conn, _params) do
with {:ok, user} <- User.whitelist(user, whitelist_target) do
render(conn, "relationship.json", user: user, target: whitelist_target)
else
{:error, message} -> json_response(conn, :forbidden, %{error: message})
end
end
@doc "POST /api/v1/pleroma/accounts/:id/unwhitelist"
def unwhitelist(%{assigns: %{user: user, account: whitelist_target}} = conn, _params) do
with {:ok, user} <- User.unwhitelist(user, whitelist_target) do
render(conn, "relationship.json", user: user, target: whitelist_target)
else
{:error, message} -> json_response(conn, :forbidden, %{error: message})
end
end
end

View File

@ -310,6 +310,8 @@ defmodule Pleroma.Web.Router do
post("/accounts/:id/subscribe", AccountController, :subscribe)
post("/accounts/:id/unsubscribe", AccountController, :unsubscribe)
post("/accounts/:id/whitelist", AccountController, :whitelist)
post("/accounts/:id/unwhitelist", AccountController, :unwhitelist)
end
post("/accounts/confirmation_resend", AccountController, :confirmation_resend)

View File

@ -0,0 +1,9 @@
defmodule Pleroma.Repo.Migrations.AddUserWhitelistedColumn do
use Ecto.Migration
def change do
alter table(:users) do
add_if_not_exists(:whitelist, {:array, :text}, default: [])
end
end
end

View File

@ -816,6 +816,19 @@ defmodule Pleroma.UserTest do
refute User.subscribed_to?(blocker, blocked)
refute User.subscribed_to?(blocked, blocker)
end
test "blocks tear down whitelister->whitelisted relationship" do
blocker = insert(:user)
blocked = insert(:user)
{:ok, blocker} = User.whitelist(blocker, blocked)
assert User.whitelists?(blocker, blocked)
refute User.whitelists?(blocked, blocker)
{:ok, blocker} = User.block(blocker, blocked)
assert User.blocks?(blocker, blocked)
refute User.whitelists?(blocker, blocked)
end
end
describe "domain blocking" do
@ -881,6 +894,49 @@ defmodule Pleroma.UserTest do
end
end
describe "whitelists" do
test "whitelists user" do
user = insert(:user)
good_eggo = insert(:user)
{:ok, user} = User.whitelist(user, good_eggo)
assert User.whitelists?(user, good_eggo)
end
test "unwhitelists user" do
user = insert(:user)
good_eggo = insert(:user)
{:ok, user} = User.whitelist(user, good_eggo)
{:ok, user} = User.unwhitelist(user, good_eggo)
refute User.whitelists?(user, good_eggo)
end
test "undoes any block" do
user = insert(:user)
good_eggo = insert(:user)
{:ok, user} = User.block(user, good_eggo)
{:ok, user} = User.whitelist(user, good_eggo)
assert User.whitelists?(user, good_eggo)
refute User.blocks?(user, good_eggo)
end
test "takes precedence over domain blocks" do
user = insert(:user)
good_eggo = insert(:user, %{ap_id: "https://awful-and-rude-instance.com/user/cuteposter"})
{:ok, user} = User.block_domain(user, "awful-and-rude-instance.com")
{:ok, user} = User.whitelist(user, good_eggo)
assert User.whitelists?(user, good_eggo)
refute User.blocks?(user, good_eggo)
end
end
describe "blocks_import" do
test "it imports user blocks from list" do
[user1, user2, user3] = insert_list(3, :user)

View File

@ -193,6 +193,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
{:ok, other_user} = User.subscribe(user, other_user)
{:ok, user} = User.mute(user, other_user, true)
{:ok, user} = CommonAPI.hide_reblogs(user, other_user)
{:ok, user} = User.whitelist(user, other_user)
expected = %{
id: to_string(other_user.id),
@ -206,6 +207,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
requested: false,
domain_blocking: false,
showing_reblogs: false,
whitelisting: true,
endorsed: false
}
@ -234,6 +236,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
requested: false,
domain_blocking: false,
showing_reblogs: true,
whitelisting: false,
endorsed: false
}
@ -271,6 +274,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
requested: true,
domain_blocking: false,
showing_reblogs: true,
whitelisting: false,
endorsed: false
}
@ -344,6 +348,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
requested: false,
domain_blocking: false,
showing_reblogs: true,
whitelisting: false,
endorsed: false
},
skip_thread_containment: false

View File

@ -391,4 +391,51 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest do
assert %{"error" => "Record not found"} = json_response(conn, 404)
end
end
describe "whitelisting / unwhitelisting" do
test "whitelisting / unwhitelisting to a user", %{conn: conn} do
user = insert(:user)
whitelist_target = insert(:user)
conn =
conn
|> assign(:user, user)
|> post("/api/v1/pleroma/accounts/#{whitelist_target.id}/whitelist")
assert %{"id" => _id, "whitelisting" => true} = json_response(conn, 200)
conn =
build_conn()
|> assign(:user, user)
|> post("/api/v1/pleroma/accounts/#{whitelist_target.id}/unwhitelist")
assert %{"id" => _id, "whitelisting" => false} = json_response(conn, 200)
end
end
describe "whitelisting" do
test "returns 404 when whitelist_target not found", %{conn: conn} do
user = insert(:user)
conn =
conn
|> assign(:user, user)
|> post("/api/v1/pleroma/accounts/target_id/whitelist")
assert %{"error" => "Record not found"} = json_response(conn, 404)
end
end
describe "unwhitelisting" do
test "returns 404 when whitelist_target not found", %{conn: conn} do
user = insert(:user)
conn =
conn
|> assign(:user, user)
|> post("/api/v1/pleroma/accounts/target_id/unwhitelist")
assert %{"error" => "Record not found"} = json_response(conn, 404)
end
end
end