From acbe507a2db56cb15004188a0253e8ee8a283344 Mon Sep 17 00:00:00 2001 From: sadposter Date: Fri, 15 Sep 2023 22:46:27 +0100 Subject: [PATCH] Add task to delete all user posts before their new expiry --- CHANGELOG.md | 1 + lib/mix/tasks/pleroma/user.ex | 28 ++++++++++++++++++++++++++++ lib/pleroma/activity/queries.ex | 4 ++++ lib/pleroma/user.ex | 8 ++++---- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26729a038..c13297b2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## Added - Full compatibility with Erlang OTP26 - handling of GET /api/v1/preferences +- Utility mix task to backfill user post expiry onto other posts ## Changed - OTP builds are now built on erlang OTP26 diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index 1a8e866ef..d9553ab02 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -342,6 +342,34 @@ def run(["revoke_invite", token]) do end end + def run(["backfill_expiry", nickname]) do + start_pleroma() + + with %User{ap_id: ap_id, local: true, status_ttl_days: days} = user <- + User.get_cached_by_nickname(nickname), + false <- is_nil(days) do + last_time = + Timex.now() + |> Timex.shift(days: -days) + + ap_id + |> Pleroma.Activity.Queries.by_actor() + |> Pleroma.Activity.Queries.by_type("Create") + |> Pleroma.Activity.Queries.before_time(last_time) + |> Pleroma.Repo.chunk_stream(50, :batches) + |> Stream.each(fn activities -> + Enum.each(activities, fn activity -> + IO.inspect(activity.id) + User.delete_activity(activity, user) + end) + end) + |> Stream.run() + else + _ -> + shell_error("No local user #{nickname}") + end + end + def run(["delete_activities", nickname]) do start_pleroma() diff --git a/lib/pleroma/activity/queries.ex b/lib/pleroma/activity/queries.ex index 4632651b0..469b8ac76 100644 --- a/lib/pleroma/activity/queries.ex +++ b/lib/pleroma/activity/queries.ex @@ -37,6 +37,10 @@ def by_author(query \\ Activity, %User{ap_id: ap_id}) do from(a in query, where: a.actor == ^ap_id) end + def before_time(query \\ Activity, time) do + from(a in query, where: a.inserted_at < ^time) + end + def find_by_object_ap_id(activities, object_ap_id) do Enum.find( activities, diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 1b78d0479..c4486ae9d 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1920,7 +1920,7 @@ def delete_user_activities(%User{ap_id: ap_id} = user) do |> Stream.run() end - defp delete_activity(%{data: %{"type" => "Create", "object" => object}} = activity, user) do + def delete_activity(%{data: %{"type" => "Create", "object" => object}} = activity, user) do with {_, %Object{}} <- {:find_object, Object.get_by_ap_id(object)}, {:ok, delete_data, _} <- Builder.delete(user, object) do Pipeline.common_pipeline(delete_data, local: user.local) @@ -1939,13 +1939,13 @@ defp delete_activity(%{data: %{"type" => "Create", "object" => object}} = activi end end - defp delete_activity(%{data: %{"type" => type}} = activity, user) - when type in ["Like", "Announce"] do + def delete_activity(%{data: %{"type" => type}} = activity, user) + when type in ["Like", "Announce"] do {:ok, undo, _} = Builder.undo(user, activity) Pipeline.common_pipeline(undo, local: user.local) end - defp delete_activity(_activity, _user), do: "Doing nothing" + def delete_activity(_activity, _user), do: "Doing nothing" defp delete_outgoing_pending_follow_requests(user) do user