Ensure only indexing public posts and implement clearing and delete

This commit is contained in:
Ekaterina Vaartis 2021-08-16 22:24:31 +03:00
parent 0318e9a599
commit 365024abec
4 changed files with 38 additions and 2 deletions

View File

@ -4,6 +4,7 @@
defmodule Mix.Tasks.Pleroma.Search.Meilisearch do defmodule Mix.Tasks.Pleroma.Search.Meilisearch do
require Logger require Logger
require Pleroma.Constants
import Mix.Pleroma import Mix.Pleroma
import Ecto.Query import Ecto.Query
@ -29,7 +30,11 @@ def run(["index"]) do
Pleroma.Repo.chunk_stream( Pleroma.Repo.chunk_stream(
from(Pleroma.Object, from(Pleroma.Object,
where: fragment("data->>'type' = 'Note'") and fragment("LENGTH(data->>'source') > 0") # Only index public posts which are notes and have some text
where:
fragment("data->>'type' = 'Note'") and
fragment("LENGTH(data->>'source') > 0") and
fragment("data->'to' \\? ?", ^Pleroma.Constants.as_public())
), ),
200, 200,
:batches :batches
@ -51,4 +56,12 @@ def run(["index"]) do
end) end)
|> Stream.run() |> Stream.run()
end end
def run(["clear"]) do
start_pleroma()
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
{:ok, _} = Pleroma.HTTP.request(:delete, "#{endpoint}/indexes/objects/documents", "", [], [])
end
end end

View File

@ -368,6 +368,7 @@ def restrict_deactivated_users(query) do
defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search
def add_to_index(_activity), do: nil def add_to_index(_activity), do: nil
def remove_from_index(_object), do: nil
def direct_conversation_id(activity, for_user) do def direct_conversation_id(activity, for_user) do
alias Pleroma.Conversation.Participation alias Pleroma.Conversation.Participation

View File

@ -1,5 +1,6 @@
defmodule Pleroma.Search.Meilisearch do defmodule Pleroma.Search.Meilisearch do
require Logger require Logger
require Pleroma.Constants
alias Pleroma.Activity alias Pleroma.Activity
@ -41,7 +42,8 @@ def search(user, query, options \\ []) do
def add_to_index(activity) do def add_to_index(activity) do
object = activity.object object = activity.object
if activity.data["type"] == "Create" and not is_nil(object) and object.data["type"] == "Note" do if activity.data["type"] == "Create" and not is_nil(object) and object.data["type"] == "Note" and
Pleroma.Constants.as_public() in object.data["to"] do
data = object.data data = object.data
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url]) endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
@ -57,4 +59,17 @@ def add_to_index(activity) do
end end
end end
end end
def remove_from_index(object) do
endpoint = Pleroma.Config.get([Pleroma.Search.Meilisearch, :url])
{:ok, _} =
Pleroma.HTTP.request(
:delete,
"#{endpoint}/indexes/objects/documents/#{object.id}",
"",
[],
[]
)
end
end end

View File

@ -147,6 +147,13 @@ def delete(activity_id, user) do
true <- User.superuser?(user) || user.ap_id == object.data["actor"], true <- User.superuser?(user) || user.ap_id == object.data["actor"],
{:ok, delete_data, _} <- Builder.delete(user, object.data["id"]), {:ok, delete_data, _} <- Builder.delete(user, object.data["id"]),
{:ok, delete, _} <- Pipeline.common_pipeline(delete_data, local: true) do {:ok, delete, _} <- Pipeline.common_pipeline(delete_data, local: true) do
# Also delete from search index
search_module = Pleroma.Config.get([Pleroma.Search, :module])
ConcurrentLimiter.limit(Pleroma.Search, fn ->
Task.start(fn -> search_module.remove_from_index(object) end)
end)
{:ok, delete} {:ok, delete}
else else
{:find_activity, _} -> {:find_activity, _} ->