Use `duration` param for mute expiration duration

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-07-31 17:22:34 +02:00
parent 0814d0e0cb
commit 5d3d6a58f7
6 changed files with 58 additions and 8 deletions

View File

@ -1480,12 +1480,12 @@ def get_recipients_from_activity(%Activity{recipients: to, actor: actor}) do
{:ok, list(UserRelationship.t())} | {:error, String.t()} {:ok, list(UserRelationship.t())} | {:error, String.t()}
def mute(%User{} = muter, %User{} = mutee, params \\ %{}) do def mute(%User{} = muter, %User{} = mutee, params \\ %{}) do
notifications? = Map.get(params, :notifications, true) notifications? = Map.get(params, :notifications, true)
expires_in = Map.get(params, :expires_in, 0) duration = Map.get(params, :duration, 0)
expires_at = expires_at =
if expires_in > 0 do if duration > 0 do
DateTime.utc_now() DateTime.utc_now()
|> DateTime.add(expires_in) |> DateTime.add(duration)
else else
nil nil
end end
@ -1499,7 +1499,7 @@ def mute(%User{} = muter, %User{} = mutee, params \\ %{}) do
expires_at expires_at
)) || )) ||
{:ok, nil} do {:ok, nil} do
if expires_in > 0 do if duration > 0 do
Pleroma.Workers.MuteExpireWorker.enqueue( Pleroma.Workers.MuteExpireWorker.enqueue(
"unmute_user", "unmute_user",
%{"muter_id" => muter.id, "mutee_id" => mutee.id}, %{"muter_id" => muter.id, "mutee_id" => mutee.id},

View File

@ -278,11 +278,17 @@ def mute_operation do
%Schema{allOf: [BooleanLike], default: true}, %Schema{allOf: [BooleanLike], default: true},
"Mute notifications in addition to statuses? Defaults to `true`." "Mute notifications in addition to statuses? Defaults to `true`."
), ),
Operation.parameter(
:duration,
:query,
%Schema{type: :integer},
"Expire the mute in `duration` seconds. Default 0 for infinity"
),
Operation.parameter( Operation.parameter(
:expires_in, :expires_in,
:query, :query,
%Schema{type: :integer, default: 0}, %Schema{type: :integer, default: 0},
"Expire the mute in `expires_in` seconds. Default 0 for infinity" "Deprecated, use `duration` instead"
) )
], ],
responses: %{ responses: %{
@ -877,10 +883,15 @@ defp mute_request do
description: "Mute notifications in addition to statuses? Defaults to true.", description: "Mute notifications in addition to statuses? Defaults to true.",
default: true default: true
}, },
duration: %Schema{
type: :integer,
nullable: true,
description: "Expire the mute in `expires_in` seconds. Default 0 for infinity"
},
expires_in: %Schema{ expires_in: %Schema{
type: :integer, type: :integer,
nullable: true, nullable: true,
description: "Expire the mute in `expires_in` seconds. Default 0 for infinity", description: "Deprecated, use `duration` instead",
default: 0 default: 0
} }
}, },

View File

@ -411,6 +411,10 @@ def unfollow(%{assigns: %{user: follower, account: followed}} = conn, _params) d
@doc "POST /api/v1/accounts/:id/mute" @doc "POST /api/v1/accounts/:id/mute"
def mute(%{assigns: %{user: muter, account: muted}, body_params: params} = conn, _params) do def mute(%{assigns: %{user: muter, account: muted}, body_params: params} = conn, _params) do
params =
params
|> Map.put_new(:duration, Map.get(params, :expires_in, 0))
with {:ok, _user_relationships} <- User.mute(muter, muted, params) do with {:ok, _user_relationships} <- User.mute(muter, muted, params) do
render(conn, "relationship.json", user: muter, target: muted) render(conn, "relationship.json", user: muter, target: muted)
else else

View File

@ -1146,7 +1146,7 @@ test "expiring" do
user = insert(:user) user = insert(:user)
muted_user = insert(:user) muted_user = insert(:user)
{:ok, _user_relationships} = User.mute(user, muted_user, %{expires_in: 60}) {:ok, _user_relationships} = User.mute(user, muted_user, %{duration: 60})
assert User.mutes?(user, muted_user) assert User.mutes?(user, muted_user)
worker = Pleroma.Workers.MuteExpireWorker worker = Pleroma.Workers.MuteExpireWorker

View File

@ -9,6 +9,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.Tests.ObanHelpers alias Pleroma.Tests.ObanHelpers
alias Pleroma.User alias Pleroma.User
alias Pleroma.UserRelationship
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.InternalFetchActor alias Pleroma.Web.ActivityPub.InternalFetchActor
alias Pleroma.Web.CommonAPI alias Pleroma.Web.CommonAPI
@ -1011,6 +1012,40 @@ test "without notifications", %{conn: conn} do
assert %{"id" => _id, "muting" => false, "muting_notifications" => false} = assert %{"id" => _id, "muting" => false, "muting_notifications" => false} =
json_response_and_validate_schema(conn, 200) json_response_and_validate_schema(conn, 200)
end end
test "expiring", %{conn: conn, user: user} do
other_user = insert(:user)
conn =
conn
|> put_req_header("content-type", "multipart/form-data")
|> post("/api/v1/accounts/#{other_user.id}/mute", %{"duration" => "86400"})
assert %{"id" => _id, "muting" => true} = json_response_and_validate_schema(conn, 200)
mute_expires_at = UserRelationship.get_mute_expire_date(user, other_user)
assert DateTime.diff(
mute_expires_at,
DateTime.utc_now() |> DateTime.add(24 * 60 * 60)
) in -3..3
end
test "falls back to expires_in", %{conn: conn, user: user} do
other_user = insert(:user)
conn
|> put_req_header("content-type", "multipart/form-data")
|> post("/api/v1/accounts/#{other_user.id}/mute", %{"expires_in" => "86400"})
|> json_response_and_validate_schema(200)
mute_expires_at = UserRelationship.get_mute_expire_date(user, other_user)
assert DateTime.diff(
mute_expires_at,
DateTime.utc_now() |> DateTime.add(24 * 60 * 60)
) in -3..3
end
end end
describe "pinned statuses" do describe "pinned statuses" do

View File

@ -640,7 +640,7 @@ test "renders mute expiration date" do
other_user = insert(:user) other_user = insert(:user)
{:ok, _user_relationships} = {:ok, _user_relationships} =
User.mute(user, other_user, %{notifications: true, expires_in: 24 * 60 * 60}) User.mute(user, other_user, %{notifications: true, duration: 24 * 60 * 60})
%{ %{
mute_expires_at: mute_expires_at mute_expires_at: mute_expires_at