add debug
[akkoma] / lib / mix / tasks / pleroma / activity.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Mix.Tasks.Pleroma.Activity do
6 alias Pleroma.Activity
7 alias Pleroma.Activity.Search
8 alias Pleroma.User
9 alias Pleroma.Web.CommonAPI
10 alias Pleroma.Pagination
11 require Logger
12 import Mix.Pleroma
13 import Ecto.Query
14 @shortdoc "A collection of activity debug tasks"
15 @moduledoc """
16 A collection of activity related tasks
17
18 mix pleroma.activity get <id>
19 """
20 def run(["get", id | _rest]) do
21 start_pleroma()
22
23 id
24 |> Activity.get_by_id()
25 |> IO.inspect()
26 end
27
28 def run(["delete_by_keyword", user, keyword | _rest]) do
29 start_pleroma()
30 u = User.get_by_nickname(user)
31 Activity
32 |> Activity.with_preloaded_object()
33 |> Activity.restrict_deactivated_users()
34 |> Activity.Queries.by_author(u)
35 |> query_with(keyword)
36 |> Pagination.fetch_paginated(
37 %{"offset" => 0, "limit" => 20, "skip_order" => false},
38 :offset
39 )
40 |> Enum.map(fn x -> CommonAPI.delete(x.id, u) end)
41 |> Enum.count
42 |> IO.puts
43 end
44
45 defp query_with(q, search_query) do
46 %{rows: [[tsc]]} =
47 Ecto.Adapters.SQL.query!(
48 Pleroma.Repo,
49 "select current_setting('default_text_search_config')::regconfig::oid;"
50 )
51
52 from([a, o] in q,
53 where:
54 fragment(
55 "to_tsvector(?::oid::regconfig, ?->>'content') @@ websearch_to_tsquery(?)",
56 ^tsc,
57 o.data,
58 ^search_query
59 )
60 )
61 end
62 end