Page MenuHomePhorge

No OneTemporary

Authored By
Unknown
Size
8 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/pleroma/search.ex b/lib/pleroma/search.ex
index b60ca3d05..2c7a60c48 100644
--- a/lib/pleroma/search.ex
+++ b/lib/pleroma/search.ex
@@ -1,38 +1,41 @@
defmodule Pleroma.Search do
alias Pleroma.Activity
alias Pleroma.Object
+ alias Pleroma.Web.ActivityPub.Visibility
alias Pleroma.Workers.SearchIndexingWorker
@spec add_to_index(Activity.t()) :: :ok | :error
- def add_to_index(%Pleroma.Activity{id: activity_id} = activity) do
- with true <- indexable?(activity),
+ def add_to_index(%Pleroma.Activity{id: activity_id, object: %Object{} = object} = activity) do
+ with {_, true} <- {:indexable, indexable?(activity)},
+ {_, "public"} <- {:visibility, Visibility.get_visibility(object)},
{:ok, %Oban.Job{}} <-
SearchIndexingWorker.enqueue("add_to_index", %{"activity" => activity_id}) do
:ok
else
- false -> :ok
+ {:indexable, false} -> :ok
+ {:visibility, _} -> :ok
_ -> :error
end
end
@spec remove_from_index(Object.t()) :: :ok | :error
def remove_from_index(%Pleroma.Object{id: object_id}) do
case SearchIndexingWorker.enqueue("remove_from_index", %{"object" => object_id}) do
{:ok, %Oban.Job{}} -> :ok
_ -> :error
end
end
def search(query, options) do
search_module = Pleroma.Config.get([Pleroma.Search, :module])
search_module.search(options[:for_user], query, options)
end
def healthcheck_endpoints do
search_module = Pleroma.Config.get([Pleroma.Search, :module])
search_module.healthcheck_endpoints
end
defp indexable?(%Activity{object: %Object{}, data: %{"type" => "Create"}}), do: true
defp indexable?(_), do: false
end
diff --git a/test/pleroma/search/meilisearch_test.exs b/test/pleroma/search/meilisearch_test.exs
index eea454323..ff32491c5 100644
--- a/test/pleroma/search/meilisearch_test.exs
+++ b/test/pleroma/search/meilisearch_test.exs
@@ -1,160 +1,137 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Search.MeilisearchTest do
require Pleroma.Constants
use Pleroma.DataCase, async: true
use Oban.Testing, repo: Pleroma.Repo
import Pleroma.Factory
import Tesla.Mock
import Mox
alias Pleroma.Search.Meilisearch
alias Pleroma.UnstubbedConfigMock, as: Config
alias Pleroma.Web.CommonAPI
alias Pleroma.Workers.SearchIndexingWorker
describe "meilisearch" do
test "indexes a local post on creation" do
user = insert(:user)
Tesla.Mock.mock(fn
%{
method: :put,
url: "http://127.0.0.1:7700/indexes/objects/documents",
body: body
} ->
assert match?(
[%{"content" => "guys i just don&#39;t wanna leave the swamp"}],
Jason.decode!(body)
)
# To make sure that the worker is called
send(self(), "posted_to_meilisearch")
%{
"enqueuedAt" => "2023-11-12T12:36:46.927517Z",
"indexUid" => "objects",
"status" => "enqueued",
"taskUid" => 6,
"type" => "documentAdditionOrUpdate"
}
|> json()
end)
Config
|> expect(:get, 3, fn
[Pleroma.Search, :module], nil ->
Meilisearch
[Pleroma.Search.Meilisearch, :url], nil ->
"http://127.0.0.1:7700"
[Pleroma.Search.Meilisearch, :private_key], nil ->
"secret"
end)
{:ok, activity} =
CommonAPI.post(user, %{
status: "guys i just don't wanna leave the swamp",
visibility: "public"
})
args = %{"op" => "add_to_index", "activity" => activity.id}
assert_enqueued(
worker: SearchIndexingWorker,
args: args
)
assert :ok = perform_job(SearchIndexingWorker, args)
assert_received("posted_to_meilisearch")
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
user = insert(:user)
Tesla.Mock.mock(fn
%{
method: :put,
url: "http://127.0.0.1:7700/indexes/objects/documents",
body: body
} ->
assert match?(
[%{"content" => "guys i just don&#39;t wanna leave the swamp"}],
Jason.decode!(body)
)
%{
"enqueuedAt" => "2023-11-12T12:36:46.927517Z",
"indexUid" => "objects",
"status" => "enqueued",
"taskUid" => 6,
"type" => "documentAdditionOrUpdate"
}
|> json()
%{method: :delete, url: "http://127.0.0.1:7700/indexes/objects/documents/" <> id} ->
send(self(), "called_delete")
assert String.length(id) > 1
json(%{})
end)
Config
|> expect(:get, 6, fn
[Pleroma.Search, :module], nil ->
Meilisearch
[Pleroma.Search.Meilisearch, :url], nil ->
"http://127.0.0.1:7700"
[Pleroma.Search.Meilisearch, :private_key], nil ->
"secret"
end)
{:ok, activity} =
CommonAPI.post(user, %{
status: "guys i just don't wanna leave the swamp",
visibility: "public"
})
args = %{"op" => "add_to_index", "activity" => activity.id}
assert_enqueued(worker: SearchIndexingWorker, args: args)
assert :ok = perform_job(SearchIndexingWorker, args)
{:ok, _} = CommonAPI.delete(activity.id, user)
delete_args = %{"op" => "remove_from_index", "object" => activity.object.id}
assert_enqueued(worker: SearchIndexingWorker, args: delete_args)
assert :ok = perform_job(SearchIndexingWorker, delete_args)
assert_received("called_delete")
end
end
end
diff --git a/test/pleroma/search_test.exs b/test/pleroma/search_test.exs
new file mode 100644
index 000000000..cdd2a72bd
--- /dev/null
+++ b/test/pleroma/search_test.exs
@@ -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

File Metadata

Mime Type
text/x-diff
Expires
Thu, Oct 29, 10:28 PM (1 d, 16 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1798766
Default Alt Text
(8 KB)

Event Timeline