Deduplicate query_with
This commit is contained in:
parent
aba4e6ea60
commit
af349b073a
2 changed files with 21 additions and 24 deletions
|
@ -8,6 +8,7 @@ defmodule Mix.Tasks.Pleroma.Activity do
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
alias Pleroma.Web.CommonAPI
|
alias Pleroma.Web.CommonAPI
|
||||||
alias Pleroma.Pagination
|
alias Pleroma.Pagination
|
||||||
|
alias Pleroma.Search.DatabaseSearch
|
||||||
require Logger
|
require Logger
|
||||||
import Mix.Pleroma
|
import Mix.Pleroma
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
@ -24,11 +25,25 @@ def run(["delete_by_keyword", user, keyword | _rest]) do
|
||||||
start_pleroma()
|
start_pleroma()
|
||||||
u = User.get_by_nickname(user)
|
u = User.get_by_nickname(user)
|
||||||
|
|
||||||
|
index_type =
|
||||||
|
cond do
|
||||||
|
Pleroma.Config.get([:database, :rum_enabled]) -> :rum
|
||||||
|
Pleroma.Config.get([:database, :pgroonga_enabled]) -> :pgroonga
|
||||||
|
true -> :gin
|
||||||
|
end
|
||||||
|
|
||||||
|
search_function =
|
||||||
|
if :persistent_term.get({Pleroma.Repo, :postgres_version}) >= 11 do
|
||||||
|
:websearch
|
||||||
|
else
|
||||||
|
:plain
|
||||||
|
end
|
||||||
|
|
||||||
Activity
|
Activity
|
||||||
|> Activity.with_preloaded_object()
|
|> Activity.with_preloaded_object()
|
||||||
|> Activity.restrict_deactivated_users()
|
|> Activity.restrict_deactivated_users()
|
||||||
|> Activity.Queries.by_author(u)
|
|> Activity.Queries.by_author(u)
|
||||||
|> query_with(keyword)
|
|> DatabaseSearch.query_with(index_type, keyword, search_function)
|
||||||
|> Pagination.fetch_paginated(
|
|> Pagination.fetch_paginated(
|
||||||
%{"offset" => 0, "limit" => 20, "skip_order" => false},
|
%{"offset" => 0, "limit" => 20, "skip_order" => false},
|
||||||
:offset
|
:offset
|
||||||
|
@ -37,22 +52,4 @@ def run(["delete_by_keyword", user, keyword | _rest]) do
|
||||||
|> Enum.count()
|
|> Enum.count()
|
||||||
|> IO.puts()
|
|> IO.puts()
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -78,7 +78,7 @@ def restrict_public(q) do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp query_with(q, :gin, search_query, :plain) do
|
def query_with(q, :gin, search_query, :plain) do
|
||||||
%{rows: [[tsc]]} =
|
%{rows: [[tsc]]} =
|
||||||
Ecto.Adapters.SQL.query!(
|
Ecto.Adapters.SQL.query!(
|
||||||
Pleroma.Repo,
|
Pleroma.Repo,
|
||||||
|
@ -96,7 +96,7 @@ defp query_with(q, :gin, search_query, :plain) do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp query_with(q, :gin, search_query, :websearch) do
|
def query_with(q, :gin, search_query, :websearch) do
|
||||||
%{rows: [[tsc]]} =
|
%{rows: [[tsc]]} =
|
||||||
Ecto.Adapters.SQL.query!(
|
Ecto.Adapters.SQL.query!(
|
||||||
Pleroma.Repo,
|
Pleroma.Repo,
|
||||||
|
@ -114,7 +114,7 @@ defp query_with(q, :gin, search_query, :websearch) do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp query_with(q, :rum, search_query, :plain) do
|
def query_with(q, :rum, search_query, :plain) do
|
||||||
from([a, o] in q,
|
from([a, o] in q,
|
||||||
where:
|
where:
|
||||||
fragment(
|
fragment(
|
||||||
|
@ -126,7 +126,7 @@ defp query_with(q, :rum, search_query, :plain) do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp query_with(q, :rum, search_query, :websearch) do
|
def query_with(q, :rum, search_query, :websearch) do
|
||||||
from([a, o] in q,
|
from([a, o] in q,
|
||||||
where:
|
where:
|
||||||
fragment(
|
fragment(
|
||||||
|
@ -138,7 +138,7 @@ defp query_with(q, :rum, search_query, :websearch) do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp query_with(q, :pgroonga, search_query, _) do
|
def query_with(q, :pgroonga, search_query, _) do
|
||||||
# PGroonga only supports PostgreSQL version 11 or newer
|
# PGroonga only supports PostgreSQL version 11 or newer
|
||||||
from([a, o] in q,
|
from([a, o] in q,
|
||||||
where:
|
where:
|
||||||
|
|
Loading…
Reference in a new issue