Add allow_following_move setting to User

This commit is contained in:
Egor Kislitsyn 2019-11-12 18:36:50 +07:00
parent 61fc739ab8
commit e6d7e27bd6
No known key found for this signature in database
GPG key ID: 1B49CB15B71E7805
10 changed files with 59 additions and 30 deletions

View file

@ -57,6 +57,7 @@ Has these additional fields under the `pleroma` object:
- `settings_store`: A generic map of settings for frontends. Opaque to the backend. Only returned in `verify_credentials` and `update_credentials` - `settings_store`: A generic map of settings for frontends. Opaque to the backend. Only returned in `verify_credentials` and `update_credentials`
- `chat_token`: The token needed for Pleroma chat. Only returned in `verify_credentials` - `chat_token`: The token needed for Pleroma chat. Only returned in `verify_credentials`
- `deactivated`: boolean, true when the user is deactivated - `deactivated`: boolean, true when the user is deactivated
- `allow_following_move`: boolean, true when the user allows automatically follow moved following accounts
- `unread_conversation_count`: The count of unread conversations. Only returned to the account owner. - `unread_conversation_count`: The count of unread conversations. Only returned to the account owner.
### Source ### Source
@ -130,6 +131,7 @@ Additional parameters can be added to the JSON body/Form data:
- `default_scope` - the scope returned under `privacy` key in Source subentity - `default_scope` - the scope returned under `privacy` key in Source subentity
- `pleroma_settings_store` - Opaque user settings to be saved on the backend. - `pleroma_settings_store` - Opaque user settings to be saved on the backend.
- `skip_thread_containment` - if true, skip filtering out broken threads - `skip_thread_containment` - if true, skip filtering out broken threads
- `allow_following_move` - if true, allows automatically follow moved following accounts
- `pleroma_background_image` - sets the background image of the user. - `pleroma_background_image` - sets the background image of the user.
### Pleroma Settings Store ### Pleroma Settings Store

View file

@ -109,26 +109,20 @@ defmodule Pleroma.FollowingRelationship do
end end
def move_following(origin, target) do def move_following(origin, target) do
following_relationships =
__MODULE__ __MODULE__
|> join(:inner, [r], f in assoc(r, :follower))
|> where(following_id: ^origin.id) |> where(following_id: ^origin.id)
|> preload([:follower]) |> where([r, f], f.allow_following_move == true)
|> limit(50) |> limit(50)
|> preload([:follower])
|> Repo.all() |> Repo.all()
|> Enum.map(fn following_relationship ->
case following_relationships do
[] ->
:ok
following_relationships ->
Enum.each(following_relationships, fn following_relationship ->
Repo.transaction(fn ->
Repo.delete(following_relationship) Repo.delete(following_relationship)
User.follow(following_relationship.follower, target) Pleroma.Web.CommonAPI.follow(following_relationship.follower, target)
end) end)
end) |> case do
[] -> :ok
move_following(origin, target) _ -> move_following(origin, target)
end end
end end
end end

View file

@ -104,6 +104,7 @@ defmodule Pleroma.User do
field(:raw_fields, {:array, :map}, default: []) field(:raw_fields, {:array, :map}, default: [])
field(:discoverable, :boolean, default: false) field(:discoverable, :boolean, default: false)
field(:invisible, :boolean, default: false) field(:invisible, :boolean, default: false)
field(:allow_following_move, :boolean, default: true)
field(:skip_thread_containment, :boolean, default: false) field(:skip_thread_containment, :boolean, default: false)
field(:also_known_as, {:array, :string}, default: []) field(:also_known_as, {:array, :string}, default: [])
@ -314,6 +315,7 @@ defmodule Pleroma.User do
:hide_followers_count, :hide_followers_count,
:hide_follows_count, :hide_follows_count,
:hide_favorites, :hide_favorites,
:allow_following_move,
:background, :background,
:show_role, :show_role,
:skip_thread_containment, :skip_thread_containment,
@ -359,6 +361,7 @@ defmodule Pleroma.User do
:hide_follows, :hide_follows,
:fields, :fields,
:hide_followers, :hide_followers,
:allow_following_move,
:discoverable, :discoverable,
:hide_followers_count, :hide_followers_count,
:hide_follows_count, :hide_follows_count,

View file

@ -152,6 +152,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
:hide_favorites, :hide_favorites,
:show_role, :show_role,
:skip_thread_containment, :skip_thread_containment,
:allow_following_move,
:discoverable :discoverable
] ]
|> Enum.reduce(%{}, fn key, acc -> |> Enum.reduce(%{}, fn key, acc ->

View file

@ -163,6 +163,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|> maybe_put_chat_token(user, opts[:for], opts) |> maybe_put_chat_token(user, opts[:for], opts)
|> maybe_put_activation_status(user, opts[:for]) |> maybe_put_activation_status(user, opts[:for])
|> maybe_put_follow_requests_count(user, opts[:for]) |> maybe_put_follow_requests_count(user, opts[:for])
|> maybe_put_allow_following_move(user, opts[:for])
|> maybe_put_unread_conversation_count(user, opts[:for]) |> maybe_put_unread_conversation_count(user, opts[:for])
end end
@ -239,6 +240,12 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
defp maybe_put_notification_settings(data, _, _), do: data defp maybe_put_notification_settings(data, _, _), do: data
defp maybe_put_allow_following_move(data, %User{id: user_id} = user, %User{id: user_id}) do
Kernel.put_in(data, [:pleroma, :allow_following_move], user.allow_following_move)
end
defp maybe_put_allow_following_move(data, _, _), do: data
defp maybe_put_activation_status(data, user, %User{is_admin: true}) do defp maybe_put_activation_status(data, user, %User{is_admin: true}) do
Kernel.put_in(data, [:pleroma, :deactivated], user.deactivated) Kernel.put_in(data, [:pleroma, :deactivated], user.deactivated)
end end

View file

@ -1,9 +0,0 @@
defmodule Pleroma.Repo.Migrations.AddAlsoKnownAsToUsers do
use Ecto.Migration
def change do
alter table(:users) do
add(:also_known_as, {:array, :string}, default: [])
end
end
end

View file

@ -0,0 +1,10 @@
defmodule Pleroma.Repo.Migrations.AddMoveSupportToUsers do
use Ecto.Migration
def change do
alter table(:users) do
add(:also_known_as, {:array, :string}, default: [], null: false)
add(:allow_following_move, :boolean, default: true, null: false)
end
end
end

View file

@ -1428,10 +1428,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
%{ap_id: old_ap_id} = old_user = insert(:user) %{ap_id: old_ap_id} = old_user = insert(:user)
%{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id]) %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
follower = insert(:user) follower = insert(:user)
follower_move_opted_out = insert(:user, allow_following_move: false)
User.follow(follower, old_user) User.follow(follower, old_user)
User.follow(follower_move_opted_out, old_user)
assert User.following?(follower, old_user) assert User.following?(follower, old_user)
assert User.following?(follower_move_opted_out, old_user)
assert {:ok, activity} = ActivityPub.move(old_user, new_user) assert {:ok, activity} = ActivityPub.move(old_user, new_user)
@ -1458,6 +1461,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
refute User.following?(follower, old_user) refute User.following?(follower, old_user)
assert User.following?(follower, new_user) assert User.following?(follower, new_user)
assert User.following?(follower_move_opted_out, old_user)
refute User.following?(follower_move_opted_out, new_user)
end end
test "old user must be in the new user's `also_known_as` list" do test "old user must be in the new user's `also_known_as` list" do

View file

@ -103,6 +103,21 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do
assert user["locked"] == true assert user["locked"] == true
end end
test "updates the user's allow_following_move", %{conn: conn} do
user = insert(:user)
assert user.allow_following_move == true
conn =
conn
|> assign(:user, user)
|> patch("/api/v1/accounts/update_credentials", %{allow_following_move: "false"})
assert refresh_record(user).allow_following_move == false
assert user = json_response(conn, 200)
assert user["pleroma"]["allow_following_move"] == false
end
test "updates the user's default scope", %{conn: conn} do test "updates the user's default scope", %{conn: conn} do
user = insert(:user) user = insert(:user)

View file

@ -102,7 +102,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
privacy = user.default_scope privacy = user.default_scope
assert %{ assert %{
pleroma: %{notification_settings: ^notification_settings}, pleroma: %{notification_settings: ^notification_settings, allow_following_move: true},
source: %{privacy: ^privacy} source: %{privacy: ^privacy}
} = AccountView.render("show.json", %{user: user, for: user}) } = AccountView.render("show.json", %{user: user, for: user})
end end