switch to using an enum system for DM acceptance

This commit is contained in:
FloatingGhost 2023-05-23 10:29:08 +01:00
parent d310f99d6a
commit ab34680554
8 changed files with 82 additions and 34 deletions

View file

@ -158,8 +158,10 @@ defmodule Pleroma.User do
field(:last_status_at, :naive_datetime)
field(:language, :string)
field(:status_ttl_days, :integer, default: nil)
field(:accepts_direct_messages_from_followed, :boolean)
field(:accepts_direct_messages_from_not_followed, :boolean)
field(:accepts_direct_messages_from, Ecto.Enum,
values: [:everybody, :people_i_follow, :nobody]
)
embeds_one(
:notification_settings,
@ -538,7 +540,8 @@ defmodule Pleroma.User do
:is_discoverable,
:actor_type,
:disclose_client,
:status_ttl_days
:status_ttl_days,
:accepts_direct_messages_from
]
)
|> unique_constraint(:nickname)
@ -2725,16 +2728,15 @@ defmodule Pleroma.User do
not is_nil(HashtagFollow.get(user, hashtag))
end
def accepts_direct_messages?(%User{} = receiver, %User{} = sender) do
cond do
User.following?(receiver, sender) && receiver.accepts_direct_messages_from_followed == true ->
true
receiver.accepts_direct_messages_from_not_followed == true ->
true
true ->
false
end
def accepts_direct_messages?(
%User{accepts_direct_messages_from: :people_i_follow} = receiver,
%User{} = sender
) do
User.following?(receiver, sender)
end
def accepts_direct_messages?(%User{accepts_direct_messages_from: :everybody}, _), do: true
def accepts_direct_messages?(%User{accepts_direct_messages_from: :nobody}, _),
do: false
end

View file

@ -12,7 +12,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.RejectNewlyCreatedAccountNotesPolicy do
"type" => type,
"actor" => actor
} = activity
) when type in ["Note", "Create"] do
)
when type in ["Note", "Create"] do
min_age = Pleroma.Config.get([:mrf_reject_newly_created_account_notes, :age])
with %User{} = user <- Pleroma.User.get_cached_by_ap_id(actor),

View file

@ -709,16 +709,16 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do
description:
"Number of days after which statuses will be deleted. Set to -1 to disable."
},
accepts_direct_messages_from_followed: %Schema{
type: :boolean,
accepts_direct_messages_from: %Schema{
type: :string,
enum: [
"everybody",
"nobody",
"people_i_follow"
],
nullable: true,
description:
"Whether to accept DMs from people you follow (will be overridden by accepts_direct_messages_from_not_followed if true)"
},
accepts_direct_messages_from_not_followed: %Schema{
type: :boolean,
nullable: true,
description: "Whether to accept DMs from everyone"
"Who to accept DMs from"
}
},
example: %{
@ -740,7 +740,8 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do
also_known_as: ["https://foo.bar/users/foo"],
discoverable: false,
actor_type: "Person",
status_ttl_days: 30
status_ttl_days: 30,
accepts_direct_messages_from: "everybody"
}
}
end

View file

@ -191,7 +191,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
:show_role,
:skip_thread_containment,
:allow_following_move,
:also_known_as
:also_known_as,
]
|> Enum.reduce(%{}, fn key, acc ->
Maps.put_if_present(acc, key, params[key], &{:ok, Params.truthy_param?(&1)})
@ -221,6 +221,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
|> Maps.put_if_present(:is_discoverable, params[:discoverable])
|> Maps.put_if_present(:language, Pleroma.Web.Gettext.normalize_locale(params[:language]))
|> Maps.put_if_present(:status_ttl_days, params[:status_ttl_days], status_ttl_days_value)
|> Maps.put_if_present(:accepts_direct_messages_from, params[:accepts_direct_messages_from])
# What happens here:
#

View file

@ -354,6 +354,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|> Kernel.put_in([:source, :privacy], user.default_scope)
|> Kernel.put_in([:source, :pleroma, :show_role], user.show_role)
|> Kernel.put_in([:source, :pleroma, :no_rich_text], user.no_rich_text)
|> Kernel.put_in([:accepts_direct_messages_from], user.accepts_direct_messages_from)
end
defp maybe_put_settings(data, _, _, _), do: data

View file

@ -3,8 +3,7 @@ defmodule Pleroma.Repo.Migrations.AddUnfollowedDmRestrictions do
def change do
alter table(:users) do
add(:accepts_direct_messages_from_followed, :boolean, default: true)
add(:accepts_direct_messages_from_not_followed, :boolean, default: true)
add(:accepts_direct_messages_from, :string, default: "everybody")
end
end
end

View file

@ -2758,11 +2758,10 @@ defmodule Pleroma.UserTest do
end
describe "accepts_direct_messages?/2" do
test "should return true if the recipient follows the sender and has turned on 'accept from follows'" do
test "should return true if the recipient follows the sender and has set accept to :people_i_follow" do
recipient =
insert(:user, %{
accepts_direct_messages_from_followed: true,
accepts_direct_messages_from_not_followed: false
accepts_direct_messages_from: :people_i_follow
})
sender = insert(:user)
@ -2774,15 +2773,15 @@ defmodule Pleroma.UserTest do
assert User.accepts_direct_messages?(recipient, sender)
end
test "should return true if the recipient has 'accept from everyone' on" do
recipient = insert(:user, %{accepts_direct_messages_from_not_followed: true})
test "should return true if the recipient has set accept to :everyone" do
recipient = insert(:user, %{accepts_direct_messages_from: :everybody})
sender = insert(:user)
assert User.accepts_direct_messages?(recipient, sender)
end
test "should return false if the receipient has 'accept from everyone' off" do
recipient = insert(:user, %{accepts_direct_messages_from_not_followed: false})
test "should return false if the receipient set accept to :nobody" do
recipient = insert(:user, %{accepts_direct_messages_from: :nobody})
sender = insert(:user)
refute User.accepts_direct_messages?(recipient, sender)

View file

@ -727,4 +727,48 @@ defmodule Pleroma.Web.MastodonAPI.UpdateCredentialsTest do
assert account["source"]["pleroma"]["actor_type"] == "Person"
end
end
describe "Updating direct message settings" do
setup do: oauth_access(["write:accounts"])
setup :request_content_type
test "changing to :everybody", %{conn: conn} do
account =
conn
|> patch("/api/v1/accounts/update_credentials", %{accepts_direct_messages_from: "everybody"})
|> json_response_and_validate_schema(200)
assert account["accepts_direct_messages_from"]
assert account["accepts_direct_messages_from"] == "everybody"
assert Pleroma.User.get_by_ap_id(account["url"]).accepts_direct_messages_from == :everybody
end
test "changing to :nobody", %{conn: conn} do
account =
conn
|> patch("/api/v1/accounts/update_credentials", %{accepts_direct_messages_from: "nobody"})
|> json_response_and_validate_schema(200)
assert account["accepts_direct_messages_from"]
assert account["accepts_direct_messages_from"] == "nobody"
assert Pleroma.User.get_by_ap_id(account["url"]).accepts_direct_messages_from == :nobody
end
test "changing to :people_i_follow", %{conn: conn} do
account =
conn
|> patch("/api/v1/accounts/update_credentials", %{accepts_direct_messages_from: "people_i_follow"})
|> json_response_and_validate_schema(200)
assert account["accepts_direct_messages_from"]
assert account["accepts_direct_messages_from"] == "people_i_follow"
assert Pleroma.User.get_by_ap_id(account["url"]).accepts_direct_messages_from == :people_i_follow
end
test "changing to an unsupported value", %{conn: conn} do
conn
|> patch("/api/v1/accounts/update_credentials", %{accepts_direct_messages_from: "unsupported"})
|> json_response(400)
end
end
end