add debug
[akkoma] / lib / mix / tasks / pleroma / activity.ex
index b4ab2f7df8408ca0067e4db22db74d86365c6c66..512897a02cb27aaa8d62d58e55451ce1ce6af85b 100644 (file)
@@ -4,10 +4,13 @@
 
 defmodule Mix.Tasks.Pleroma.Activity do
   alias Pleroma.Activity
+  alias Pleroma.Activity.Search
+  alias Pleroma.User
+  alias Pleroma.Web.CommonAPI
+  alias Pleroma.Pagination
   require Logger
   import Mix.Pleroma
-  use Mix.Task
-
+  import Ecto.Query
   @shortdoc "A collection of activity debug tasks"
   @moduledoc """
    A collection of activity related tasks
@@ -21,4 +24,39 @@ defmodule Mix.Tasks.Pleroma.Activity do
     |> Activity.get_by_id()
     |> IO.inspect()
   end
+
+  def run(["delete_by_keyword", user, keyword | _rest]) do
+    start_pleroma()
+    u = User.get_by_nickname(user)
+    Activity
+      |> Activity.with_preloaded_object()
+      |> Activity.restrict_deactivated_users()
+      |> Activity.Queries.by_author(u)
+      |> query_with(keyword)
+      |> Pagination.fetch_paginated(
+        %{"offset" => 0, "limit" => 20, "skip_order" => false},
+        :offset
+      )
+      |> Enum.map(fn x -> CommonAPI.delete(x.id, u) end)
+      |> Enum.count
+      |> IO.puts
+  end
+
+  defp query_with(q, search_query) do
+    %{rows: [[tsc]]} =
+      Ecto.Adapters.SQL.query!(
+        Pleroma.Repo,
+        "select current_setting('default_text_search_config')::regconfig::oid;"
+      )
+
+    from([a, o] in q,
+      where:
+        fragment(
+          "to_tsvector(?::oid::regconfig, ?->>'content') @@ websearch_to_tsquery(?)",
+          ^tsc,
+          o.data,
+          ^search_query
+        )
+    )
+  end
 end