Merge branch 'improve-move-notificaions-api' into 'develop'

Add `with_move` query param to the notifications API

See merge request pleroma/pleroma!2032
This commit is contained in:
rinpatch 2019-12-08 13:07:05 +00:00
commit 8404f8c8fb
7 changed files with 60 additions and 16 deletions

View file

@ -121,8 +121,12 @@ def move_following(origin, target) do
Pleroma.Web.CommonAPI.follow(following_relationship.follower, target) Pleroma.Web.CommonAPI.follow(following_relationship.follower, target)
end) end)
|> case do |> case do
[] -> :ok [] ->
_ -> move_following(origin, target) User.update_follower_count(origin)
:ok
_ ->
move_following(origin, target)
end end
end end
end end

View file

@ -77,6 +77,7 @@ def for_user_query(user, opts \\ %{}) do
|> exclude_notification_muted(user, exclude_notification_muted_opts) |> exclude_notification_muted(user, exclude_notification_muted_opts)
|> exclude_blocked(user, exclude_blocked_opts) |> exclude_blocked(user, exclude_blocked_opts)
|> exclude_visibility(opts) |> exclude_visibility(opts)
|> exclude_move(opts)
end end
defp exclude_blocked(query, user, opts) do defp exclude_blocked(query, user, opts) do
@ -106,6 +107,14 @@ defp exclude_notification_muted(query, user, opts) do
|> where([n, a, o, tm], is_nil(tm.user_id)) |> where([n, a, o, tm], is_nil(tm.user_id))
end end
defp exclude_move(query, %{with_move: true}) do
query
end
defp exclude_move(query, _opts) do
where(query, [n, a], fragment("?->>'type' != 'Move'", a.data))
end
@valid_visibilities ~w[direct unlisted public private] @valid_visibilities ~w[direct unlisted public private]
defp exclude_visibility(query, %{exclude_visibilities: visibility}) defp exclude_visibility(query, %{exclude_visibilities: visibility})

View file

@ -70,7 +70,8 @@ defp cast_params(params) do
exclude_types: {:array, :string}, exclude_types: {:array, :string},
exclude_visibilities: {:array, :string}, exclude_visibilities: {:array, :string},
reblogs: :boolean, reblogs: :boolean,
with_muted: :boolean with_muted: :boolean,
with_move: :boolean
} }
changeset = cast({%{}, param_types}, params, Map.keys(param_types)) changeset = cast({%{}, param_types}, params, Map.keys(param_types))

View file

@ -643,13 +643,7 @@ test "move activity generates a notification" do
Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user) Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
ObanHelpers.perform_all() ObanHelpers.perform_all()
assert [ assert [] = Notification.for_user(follower)
%{
activity: %{
data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
}
}
] = Notification.for_user(follower)
assert [ assert [
%{ %{
@ -657,7 +651,17 @@ test "move activity generates a notification" do
data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id} data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
} }
} }
] = Notification.for_user(other_follower) ] = Notification.for_user(follower, %{with_move: true})
assert [] = Notification.for_user(other_follower)
assert [
%{
activity: %{
data: %{"type" => "Move", "actor" => ^old_ap_id, "target" => ^new_ap_id}
}
}
] = Notification.for_user(other_follower, %{with_move: true})
end end
end end

View file

@ -1637,10 +1637,10 @@ test "create" do
activity = %Activity{activity | object: nil} activity = %Activity{activity | object: nil}
assert [%Notification{activity: ^activity}] = assert [%Notification{activity: ^activity}] =
Notification.for_user_since(follower, ~N[2019-04-13 11:22:33]) Notification.for_user(follower, %{with_move: true})
assert [%Notification{activity: ^activity}] = assert [%Notification{activity: ^activity}] =
Notification.for_user_since(follower_move_opted_out, ~N[2019-04-13 11:22:33]) Notification.for_user(follower_move_opted_out, %{with_move: true})
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

@ -341,6 +341,32 @@ test "see notifications after muting user with notifications and with_muted para
assert length(json_response(conn, 200)) == 1 assert length(json_response(conn, 200)) == 1
end end
test "see move notifications with `with_move` parameter", %{
conn: conn
} do
old_user = insert(:user)
new_user = insert(:user, also_known_as: [old_user.ap_id])
follower = insert(:user)
User.follow(follower, old_user)
Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)
Pleroma.Tests.ObanHelpers.perform_all()
conn =
conn
|> assign(:user, follower)
|> get("/api/v1/notifications")
assert json_response(conn, 200) == []
conn =
build_conn()
|> assign(:user, follower)
|> get("/api/v1/notifications", %{"with_move" => "true"})
assert length(json_response(conn, 200)) == 1
end
defp get_notification_id_by_activity(%{id: id}) do defp get_notification_id_by_activity(%{id: id}) do
Notification Notification
|> Repo.get_by(activity_id: id) |> Repo.get_by(activity_id: id)

View file

@ -109,8 +109,8 @@ test "Follow notification" do
end end
test "Move notification" do test "Move notification" do
%{ap_id: old_ap_id} = old_user = insert(:user) old_user = insert(:user)
%{ap_id: _new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id]) new_user = insert(:user, also_known_as: [old_user.ap_id])
follower = insert(:user) follower = insert(:user)
User.follow(follower, old_user) User.follow(follower, old_user)
@ -120,7 +120,7 @@ test "Move notification" do
old_user = refresh_record(old_user) old_user = refresh_record(old_user)
new_user = refresh_record(new_user) new_user = refresh_record(new_user)
[notification] = Notification.for_user(follower) [notification] = Notification.for_user(follower, %{with_move: true})
expected = %{ expected = %{
id: to_string(notification.id), id: to_string(notification.id),