Validate the activity is public before indexing
Additional tests added
This commit is contained in:
parent
77436451ad
commit
7c09150cdb
3 changed files with 75 additions and 26 deletions
|
|
@ -1,16 +1,19 @@
|
||||||
defmodule Pleroma.Search do
|
defmodule Pleroma.Search do
|
||||||
alias Pleroma.Activity
|
alias Pleroma.Activity
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
|
alias Pleroma.Web.ActivityPub.Visibility
|
||||||
alias Pleroma.Workers.SearchIndexingWorker
|
alias Pleroma.Workers.SearchIndexingWorker
|
||||||
|
|
||||||
@spec add_to_index(Activity.t()) :: :ok | :error
|
@spec add_to_index(Activity.t()) :: :ok | :error
|
||||||
def add_to_index(%Pleroma.Activity{id: activity_id} = activity) do
|
def add_to_index(%Pleroma.Activity{id: activity_id, object: %Object{} = object} = activity) do
|
||||||
with true <- indexable?(activity),
|
with {_, true} <- {:indexable, indexable?(activity)},
|
||||||
|
{_, "public"} <- {:visibility, Visibility.get_visibility(object)},
|
||||||
{:ok, %Oban.Job{}} <-
|
{:ok, %Oban.Job{}} <-
|
||||||
SearchIndexingWorker.enqueue("add_to_index", %{"activity" => activity_id}) do
|
SearchIndexingWorker.enqueue("add_to_index", %{"activity" => activity_id}) do
|
||||||
:ok
|
:ok
|
||||||
else
|
else
|
||||||
false -> :ok
|
{:indexable, false} -> :ok
|
||||||
|
{:visibility, _} -> :ok
|
||||||
_ -> :error
|
_ -> :error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -74,29 +74,6 @@ defmodule Pleroma.Search.MeilisearchTest do
|
||||||
assert_received("posted_to_meilisearch")
|
assert_received("posted_to_meilisearch")
|
||||||
end
|
end
|
||||||
|
|
||||||
test "doesn't index posts that are not public" do
|
|
||||||
user = insert(:user)
|
|
||||||
|
|
||||||
Enum.each(["private", "direct"], fn visibility ->
|
|
||||||
{:ok, activity} =
|
|
||||||
CommonAPI.post(user, %{
|
|
||||||
status: "guys i just don't wanna leave the swamp",
|
|
||||||
visibility: visibility
|
|
||||||
})
|
|
||||||
|
|
||||||
args = %{"op" => "add_to_index", "activity" => activity.id}
|
|
||||||
|
|
||||||
Config
|
|
||||||
|> expect(:get, fn
|
|
||||||
[Pleroma.Search, :module], nil ->
|
|
||||||
Meilisearch
|
|
||||||
end)
|
|
||||||
|
|
||||||
assert_enqueued(worker: SearchIndexingWorker, args: args)
|
|
||||||
assert :ok = perform_job(SearchIndexingWorker, args)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
test "deletes posts from index when deleted locally" do
|
test "deletes posts from index when deleted locally" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
||||||
|
|
|
||||||
69
test/pleroma/search_test.exs
Normal file
69
test/pleroma/search_test.exs
Normal file
|
|
@ -0,0 +1,69 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.SearchTest do
|
||||||
|
use Pleroma.DataCase, async: true
|
||||||
|
use Oban.Testing, repo: Pleroma.Repo
|
||||||
|
|
||||||
|
import Pleroma.Factory
|
||||||
|
|
||||||
|
alias Pleroma.Web.CommonAPI
|
||||||
|
alias Pleroma.Workers.SearchIndexingWorker
|
||||||
|
|
||||||
|
test "indexes posts that are public" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
{:ok, activity} =
|
||||||
|
CommonAPI.post(user, %{
|
||||||
|
status: "Well this is a story all about how my life got flipped turned upside down",
|
||||||
|
visibility: "public"
|
||||||
|
})
|
||||||
|
|
||||||
|
args = %{"op" => "add_to_index", "activity" => activity.id}
|
||||||
|
|
||||||
|
assert_enqueued(worker: SearchIndexingWorker, args: args)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "doesn't index posts that are not public" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
Enum.each(["private", "direct"], fn visibility ->
|
||||||
|
{:ok, activity} =
|
||||||
|
CommonAPI.post(user, %{
|
||||||
|
status: "guys i just don't wanna leave the swamp",
|
||||||
|
visibility: visibility
|
||||||
|
})
|
||||||
|
|
||||||
|
args = %{"op" => "add_to_index", "activity" => activity.id}
|
||||||
|
|
||||||
|
refute_enqueued(worker: SearchIndexingWorker, args: args)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "Indexes appropriate activity types" do
|
||||||
|
user = insert(:user)
|
||||||
|
|
||||||
|
{:ok, activity} =
|
||||||
|
CommonAPI.post(user, %{
|
||||||
|
status: "I'm my own hype man",
|
||||||
|
visibility: "public"
|
||||||
|
})
|
||||||
|
|
||||||
|
args = %{"op" => "add_to_index", "activity" => activity.id}
|
||||||
|
|
||||||
|
assert_enqueued(worker: SearchIndexingWorker, args: args)
|
||||||
|
|
||||||
|
{:ok, fav_activity} = CommonAPI.favorite(user, activity.id)
|
||||||
|
|
||||||
|
args = %{"op" => "add_to_index", "activity" => fav_activity.id}
|
||||||
|
|
||||||
|
refute_enqueued(worker: SearchIndexingWorker, args: args)
|
||||||
|
|
||||||
|
{:ok, repeat_activity} = CommonAPI.repeat(activity.id, user)
|
||||||
|
|
||||||
|
args = %{"op" => "add_to_index", "activity" => repeat_activity.id}
|
||||||
|
|
||||||
|
refute_enqueued(worker: SearchIndexingWorker, args: args)
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue