MastoAPI AccountView: Add mute/block expiry to the relationship key
This commit is contained in:
parent
a7a3978a20
commit
ddc1a86f40
2 changed files with 99 additions and 19 deletions
|
|
@ -124,6 +124,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|
||||||
target,
|
target,
|
||||||
&User.blocks_user?(&1, &2)
|
&User.blocks_user?(&1, &2)
|
||||||
),
|
),
|
||||||
|
block_expires_at: maybe_put_block_expires_at(user_relationships, target, reading_user),
|
||||||
blocked_by:
|
blocked_by:
|
||||||
UserRelationship.exists?(
|
UserRelationship.exists?(
|
||||||
user_relationships,
|
user_relationships,
|
||||||
|
|
@ -140,6 +141,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|
||||||
target,
|
target,
|
||||||
&User.mutes?(&1, &2)
|
&User.mutes?(&1, &2)
|
||||||
),
|
),
|
||||||
|
mute_expires_at: maybe_put_mute_expires_at(user_relationships, target, reading_user),
|
||||||
muting_notifications:
|
muting_notifications:
|
||||||
UserRelationship.exists?(
|
UserRelationship.exists?(
|
||||||
user_relationships,
|
user_relationships,
|
||||||
|
|
@ -343,8 +345,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|
||||||
|> maybe_put_unread_conversation_count(user, opts[:for])
|
|> maybe_put_unread_conversation_count(user, opts[:for])
|
||||||
|> maybe_put_unread_notification_count(user, opts[:for])
|
|> maybe_put_unread_notification_count(user, opts[:for])
|
||||||
|> maybe_put_email_address(user, opts[:for])
|
|> maybe_put_email_address(user, opts[:for])
|
||||||
|> maybe_put_mute_expires_at(user, opts[:for], opts)
|
|
||||||
|> maybe_put_block_expires_at(user, opts[:for], opts)
|
|
||||||
|> maybe_show_birthday(user, opts[:for])
|
|> maybe_show_birthday(user, opts[:for])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -472,26 +472,32 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|
||||||
|
|
||||||
defp maybe_put_email_address(data, _, _), do: data
|
defp maybe_put_email_address(data, _, _), do: data
|
||||||
|
|
||||||
defp maybe_put_mute_expires_at(data, %User{} = user, target, %{mutes: true}) do
|
defp maybe_put_mute_expires_at(user_relationships, %User{} = target, %User{} = user) do
|
||||||
Map.put(
|
cond do
|
||||||
data,
|
UserRelationship.exists?(user_relationships, :mute, user, target, &User.mutes_user?(&1, &2)) ->
|
||||||
:mute_expires_at,
|
UserRelationship.get_mute_expire_date(user, target)
|
||||||
UserRelationship.get_mute_expire_date(target, user)
|
|
||||||
)
|
true ->
|
||||||
|
nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp maybe_put_mute_expires_at(data, _, _, _), do: data
|
defp maybe_put_block_expires_at(user_relationships, %User{} = target, %User{} = user) do
|
||||||
|
cond do
|
||||||
|
UserRelationship.exists?(
|
||||||
|
user_relationships,
|
||||||
|
:block,
|
||||||
|
user,
|
||||||
|
target,
|
||||||
|
&User.blocks_user?(&1, &2)
|
||||||
|
) ->
|
||||||
|
UserRelationship.get_block_expire_date(user, target)
|
||||||
|
|
||||||
defp maybe_put_block_expires_at(data, %User{} = user, target, %{blocks: true}) do
|
true ->
|
||||||
Map.put(
|
nil
|
||||||
data,
|
end
|
||||||
:block_expires_at,
|
|
||||||
UserRelationship.get_block_expire_date(target, user)
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp maybe_put_block_expires_at(data, _, _, _), do: data
|
|
||||||
|
|
||||||
defp maybe_show_birthday(data, %User{id: user_id} = user, %User{id: user_id}) do
|
defp maybe_show_birthday(data, %User{id: user_id} = user, %User{id: user_id}) do
|
||||||
data
|
data
|
||||||
|> Kernel.put_in([:pleroma, :birthday], user.birthday)
|
|> Kernel.put_in([:pleroma, :birthday], user.birthday)
|
||||||
|
|
|
||||||
|
|
@ -439,8 +439,10 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
|
||||||
following: false,
|
following: false,
|
||||||
followed_by: false,
|
followed_by: false,
|
||||||
blocking: false,
|
blocking: false,
|
||||||
|
block_expires_at: nil,
|
||||||
blocked_by: false,
|
blocked_by: false,
|
||||||
muting: false,
|
muting: false,
|
||||||
|
mute_expires_at: nil,
|
||||||
muting_notifications: false,
|
muting_notifications: false,
|
||||||
subscribing: false,
|
subscribing: false,
|
||||||
notifying: false,
|
notifying: false,
|
||||||
|
|
@ -536,6 +538,53 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
|
||||||
test_relationship_rendering(user, other_user, expected)
|
test_relationship_rendering(user, other_user, expected)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "represent a relationship for the blocking and blocked user with expiry" do
|
||||||
|
user = insert(:user)
|
||||||
|
other_user = insert(:user)
|
||||||
|
date = DateTime.utc_now() |> DateTime.add(24 * 60 * 60) |> DateTime.truncate(:second)
|
||||||
|
|
||||||
|
{:ok, user, other_user} = User.follow(user, other_user)
|
||||||
|
{:ok, _subscription} = User.subscribe(user, other_user)
|
||||||
|
{:ok, _user_relationship} = User.block(user, other_user, %{duration: 24 * 60 * 60})
|
||||||
|
{:ok, _user_relationship} = User.block(other_user, user)
|
||||||
|
|
||||||
|
expected =
|
||||||
|
Map.merge(
|
||||||
|
@blank_response,
|
||||||
|
%{
|
||||||
|
following: false,
|
||||||
|
blocking: true,
|
||||||
|
block_expires_at: date,
|
||||||
|
blocked_by: true,
|
||||||
|
id: to_string(other_user.id)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
test_relationship_rendering(user, other_user, expected)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "represent a relationship for the muting user with expiry" do
|
||||||
|
user = insert(:user)
|
||||||
|
other_user = insert(:user)
|
||||||
|
date = DateTime.utc_now() |> DateTime.add(24 * 60 * 60) |> DateTime.truncate(:second)
|
||||||
|
|
||||||
|
{:ok, _user_relationship} =
|
||||||
|
User.mute(user, other_user, %{notifications: true, duration: 24 * 60 * 60})
|
||||||
|
|
||||||
|
expected =
|
||||||
|
Map.merge(
|
||||||
|
@blank_response,
|
||||||
|
%{
|
||||||
|
muting: true,
|
||||||
|
mute_expires_at: date,
|
||||||
|
muting_notifications: true,
|
||||||
|
id: to_string(other_user.id)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
test_relationship_rendering(user, other_user, expected)
|
||||||
|
end
|
||||||
|
|
||||||
test "represent a relationship for the user blocking a domain" do
|
test "represent a relationship for the user blocking a domain" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
|
other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
|
||||||
|
|
@ -856,12 +905,37 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
|
||||||
User.mute(user, other_user, %{notifications: true, duration: 24 * 60 * 60})
|
User.mute(user, other_user, %{notifications: true, duration: 24 * 60 * 60})
|
||||||
|
|
||||||
%{
|
%{
|
||||||
mute_expires_at: mute_expires_at
|
pleroma: %{
|
||||||
} = AccountView.render("show.json", %{user: other_user, for: user, mutes: true})
|
relationship: %{
|
||||||
|
mute_expires_at: mute_expires_at
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} = AccountView.render("show.json", %{user: other_user, for: user, embed_relationships: true})
|
||||||
|
|
||||||
assert DateTime.diff(
|
assert DateTime.diff(
|
||||||
mute_expires_at,
|
mute_expires_at,
|
||||||
DateTime.utc_now() |> DateTime.add(24 * 60 * 60)
|
DateTime.utc_now() |> DateTime.add(24 * 60 * 60)
|
||||||
) in -3..3
|
) in -3..3
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "renders block expiration date" do
|
||||||
|
user = insert(:user)
|
||||||
|
other_user = insert(:user)
|
||||||
|
|
||||||
|
{:ok, _user_relationships} =
|
||||||
|
User.block(user, other_user, %{duration: 24 * 60 * 60})
|
||||||
|
|
||||||
|
%{
|
||||||
|
pleroma: %{
|
||||||
|
relationship: %{
|
||||||
|
block_expires_at: block_expires_at
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} = AccountView.render("show.json", %{user: other_user, for: user, embed_relationships: true})
|
||||||
|
|
||||||
|
assert DateTime.diff(
|
||||||
|
block_expires_at,
|
||||||
|
DateTime.utc_now() |> DateTime.add(24 * 60 * 60)
|
||||||
|
) in -3..3
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue