Merge pull request 'api/statuses: allow expires_in to override user-level status_ttl_default' (#899) from Oneric/akkoma:expires_in_overriding_default_status_ttl into develop
Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/899
This commit is contained in:
commit
6a6d4254d5
2 changed files with 58 additions and 6 deletions
|
@ -172,14 +172,15 @@ def create(%{assigns: %{user: user}, body_params: %{status: _} = params} = conn,
|
||||||
|> put_application(conn)
|
|> put_application(conn)
|
||||||
|
|
||||||
expires_in_seconds =
|
expires_in_seconds =
|
||||||
if is_nil(user.status_ttl_days),
|
Map.get(params, :expires_in) ||
|
||||||
do: nil,
|
(user.status_ttl_days && 60 * 60 * 24 * user.status_ttl_days)
|
||||||
else: 60 * 60 * 24 * user.status_ttl_days
|
|
||||||
|
|
||||||
params =
|
params =
|
||||||
if is_nil(expires_in_seconds),
|
case expires_in_seconds do
|
||||||
do: params,
|
nil -> params
|
||||||
else: Map.put(params, :expires_in, expires_in_seconds)
|
0 -> Map.delete(params, :expires_in)
|
||||||
|
_ -> Map.put(params, :expires_in, expires_in_seconds)
|
||||||
|
end
|
||||||
|
|
||||||
with {:ok, activity} <- CommonAPI.post(user, params) do
|
with {:ok, activity} <- CommonAPI.post(user, params) do
|
||||||
try_render(conn, "show.json",
|
try_render(conn, "show.json",
|
||||||
|
|
|
@ -162,6 +162,57 @@ test "automatically setting a post expiry if status_ttl_days is set" do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "API paramater overrides user status_ttl_days default" do
|
||||||
|
user = insert(:user, status_ttl_days: 1)
|
||||||
|
%{user: _user, token: _token, conn: conn} = oauth_access(["write:statuses"], user: user)
|
||||||
|
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> put_req_header("content-type", "application/json")
|
||||||
|
|> post("/api/v1/statuses", %{
|
||||||
|
"status" => "aa chikichiki banban",
|
||||||
|
"expires_in" => 2 * 60 * 60
|
||||||
|
})
|
||||||
|
|
||||||
|
assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
|
||||||
|
|
||||||
|
activity = Activity.get_by_id_with_object(id)
|
||||||
|
{:ok, expires_at, _} = DateTime.from_iso8601(activity.data["expires_at"])
|
||||||
|
|
||||||
|
expiry_delay = Timex.diff(expires_at, DateTime.utc_now(), :minutes)
|
||||||
|
assert(expiry_delay in [120, 119])
|
||||||
|
|
||||||
|
assert_enqueued(
|
||||||
|
worker: Pleroma.Workers.PurgeExpiredActivity,
|
||||||
|
args: %{activity_id: id},
|
||||||
|
scheduled_at: expires_at
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "API paramater can disable expiry from user-level status_ttl_default" do
|
||||||
|
user = insert(:user, status_ttl_days: 1)
|
||||||
|
%{user: _user, token: _token, conn: conn} = oauth_access(["write:statuses"], user: user)
|
||||||
|
|
||||||
|
conn =
|
||||||
|
conn
|
||||||
|
|> put_req_header("content-type", "application/json")
|
||||||
|
|> post("/api/v1/statuses", %{
|
||||||
|
"status" => "aa chikichiki banban",
|
||||||
|
"expires_in" => 0
|
||||||
|
})
|
||||||
|
|
||||||
|
assert %{"id" => id} = json_response_and_validate_schema(conn, 200)
|
||||||
|
|
||||||
|
activity = Activity.get_by_id_with_object(id)
|
||||||
|
|
||||||
|
refute activity.data["expires_at"]
|
||||||
|
|
||||||
|
refute_enqueued(
|
||||||
|
worker: Pleroma.Workers.PurgeExpiredActivity,
|
||||||
|
args: %{activity_id: id}
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
test "it fails to create a status if `expires_in` is less or equal than an hour", %{
|
test "it fails to create a status if `expires_in` is less or equal than an hour", %{
|
||||||
conn: conn
|
conn: conn
|
||||||
} do
|
} do
|
||||||
|
|
Loading…
Reference in a new issue