Compare commits

...
Sign in to create a new pull request.

4 commits

3 changed files with 222 additions and 25 deletions

View file

@ -124,6 +124,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
target,
&User.blocks_user?(&1, &2)
),
block_expires_at: maybe_put_block_expires_at(user_relationships, target, reading_user),
blocked_by:
UserRelationship.exists?(
user_relationships,
@ -140,6 +141,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
target,
&User.mutes?(&1, &2)
),
mute_expires_at: maybe_put_mute_expires_at(user_relationships, target, reading_user),
muting_notifications:
UserRelationship.exists?(
user_relationships,
@ -343,8 +345,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
|> maybe_put_unread_conversation_count(user, opts[:for])
|> maybe_put_unread_notification_count(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_put_mute_expires_at(user, opts[:for], relationship)
|> maybe_put_block_expires_at(user, opts[:for], relationship)
|> maybe_show_birthday(user, opts[:for])
end
@ -472,22 +474,58 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
defp maybe_put_email_address(data, _, _), do: data
defp maybe_put_mute_expires_at(data, %User{} = user, target, %{mutes: true}) do
Map.put(
data,
:mute_expires_at,
UserRelationship.get_mute_expire_date(target, user)
)
defp maybe_put_mute_expires_at(user_relationships, %User{} = target, %User{} = user) do
cond do
UserRelationship.exists?(user_relationships, :mute, user, target, &User.mutes_user?(&1, &2)) ->
UserRelationship.get_mute_expire_date(user, target)
true ->
nil
end
end
defp maybe_put_mute_expires_at(data, %User{} = target, %User{} = user, relationship) do
cond do
Map.has_key?(relationship, :mute_expires_at) ->
Map.put(data, :mute_expires_at, relationship.mute_expires_at)
User.mutes_user?(user, target) ->
Map.put(data, :mute_expires_at, UserRelationship.get_mute_expire_date(user, target))
true ->
Map.put(data, :mute_expires_at, nil)
end
end
defp maybe_put_mute_expires_at(data, _, _, _), do: data
defp maybe_put_block_expires_at(data, %User{} = user, target, %{blocks: true}) do
Map.put(
data,
:block_expires_at,
UserRelationship.get_block_expire_date(target, user)
)
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)
true ->
nil
end
end
defp maybe_put_block_expires_at(data, %User{} = target, %User{} = user, relationship) do
cond do
Map.has_key?(relationship, :block_expires_at) ->
Map.put(data, :block_expires_at, relationship.block_expires_at)
User.blocks_user?(user, target) ->
Map.put(data, :block_expires_at, UserRelationship.get_block_expire_date(user, target))
true ->
Map.put(data, :block_expires_at, nil)
end
end
defp maybe_put_block_expires_at(data, _, _, _), do: data

View file

@ -1901,7 +1901,13 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
{:ok, _user_relationships} = User.mute(user, other_user1)
{:ok, _user_relationships} = User.mute(user, other_user2)
{:ok, _user_relationships} = User.mute(user, other_user3)
{:ok, _user_relationships} = User.mute(user, other_user3, %{duration: 24 * 60 * 60})
date =
DateTime.utc_now()
|> DateTime.add(24 * 60 * 60)
|> DateTime.truncate(:second)
|> DateTime.to_iso8601()
result =
conn
@ -1937,6 +1943,17 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
|> json_response_and_validate_schema(200)
assert [%{"id" => ^id3}] = result
result =
conn
|> get("/api/v1/mutes")
|> json_response_and_validate_schema(200)
assert [
%{"id" => ^id3, "mute_expires_at" => ^date},
%{"id" => ^id2, "mute_expires_at" => nil},
%{"id" => ^id1, "mute_expires_at" => nil}
] = result
end
test "list of mutes with with_relationships parameter" do
@ -1951,20 +1968,44 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
{:ok, _} = User.mute(user, other_user1)
{:ok, _} = User.mute(user, other_user2)
{:ok, _} = User.mute(user, other_user3)
{:ok, _} = User.mute(user, other_user3, %{duration: 24 * 60 * 60})
date =
DateTime.utc_now()
|> DateTime.add(24 * 60 * 60)
|> DateTime.truncate(:second)
|> DateTime.to_iso8601()
assert [
%{
"id" => ^id3,
"pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
"pleroma" => %{
"relationship" => %{
"muting" => true,
"mute_expires_at" => ^date,
"followed_by" => true
}
}
},
%{
"id" => ^id2,
"pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
"pleroma" => %{
"relationship" => %{
"muting" => true,
"mute_expires_at" => nil,
"followed_by" => true
}
}
},
%{
"id" => ^id1,
"pleroma" => %{"relationship" => %{"muting" => true, "followed_by" => true}}
"pleroma" => %{
"relationship" => %{
"muting" => true,
"mute_expires_at" => nil,
"followed_by" => true
}
}
}
] =
conn
@ -1980,7 +2021,13 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
{:ok, _user_relationship} = User.block(user, other_user1)
{:ok, _user_relationship} = User.block(user, other_user3)
{:ok, _user_relationship} = User.block(user, other_user2)
{:ok, _user_relationship} = User.block(user, other_user2, %{duration: 24 * 60 * 60})
date =
DateTime.utc_now()
|> DateTime.add(24 * 60 * 60)
|> DateTime.truncate(:second)
|> DateTime.to_iso8601()
result =
conn
@ -2045,6 +2092,18 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
|> json_response_and_validate_schema(200)
assert [%{"id" => ^id1}] = result
result =
conn
|> assign(:user, user)
|> get("api/v1/blocks")
|> json_response_and_validate_schema(200)
assert [
%{"id" => ^id3, "block_expires_at" => nil},
%{"id" => ^id2, "block_expires_at" => ^date},
%{"id" => ^id1, "block_expires_at" => nil}
] = result
end
test "list of blocks with with_relationships parameter" do
@ -2059,20 +2118,44 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
{:ok, _} = User.block(user, other_user1)
{:ok, _} = User.block(user, other_user2)
{:ok, _} = User.block(user, other_user3)
{:ok, _} = User.block(user, other_user3, %{duration: 24 * 60 * 60})
date =
DateTime.utc_now()
|> DateTime.add(24 * 60 * 60)
|> DateTime.truncate(:second)
|> DateTime.to_iso8601()
assert [
%{
"id" => ^id3,
"pleroma" => %{"relationship" => %{"blocking" => true, "followed_by" => false}}
"pleroma" => %{
"relationship" => %{
"blocking" => true,
"block_expires_at" => ^date,
"followed_by" => false
}
}
},
%{
"id" => ^id2,
"pleroma" => %{"relationship" => %{"blocking" => true, "followed_by" => false}}
"pleroma" => %{
"relationship" => %{
"blocking" => true,
"block_expires_at" => nil,
"followed_by" => false
}
}
},
%{
"id" => ^id1,
"pleroma" => %{"relationship" => %{"blocking" => true, "followed_by" => false}}
"pleroma" => %{
"relationship" => %{
"blocking" => true,
"block_expires_at" => nil,
"followed_by" => false
}
}
}
] =
conn

View file

@ -439,8 +439,10 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
following: false,
followed_by: false,
blocking: false,
block_expires_at: nil,
blocked_by: false,
muting: false,
mute_expires_at: nil,
muting_notifications: false,
subscribing: false,
notifying: false,
@ -536,6 +538,53 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
test_relationship_rendering(user, other_user, expected)
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
user = insert(:user)
other_user = insert(:user, ap_id: "https://bad.site/users/other_user")
@ -856,12 +905,39 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
User.mute(user, other_user, %{notifications: true, duration: 24 * 60 * 60})
%{
pleroma: %{
relationship: %{
mute_expires_at: mute_expires_at
}
},
mute_expires_at: mute_expires_at
} = AccountView.render("show.json", %{user: other_user, for: user, mutes: true})
} = AccountView.render("show.json", %{user: other_user, for: user, embed_relationships: true})
assert DateTime.diff(
mute_expires_at,
DateTime.utc_now() |> DateTime.add(24 * 60 * 60)
) in -3..3
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
}
},
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