[Pleroma.Web.MastodonAPI.MastodonAPIController].search(2)?: Remove code duplication

This commit is contained in:
Haelwenn (lanodan) Monnier 2018-09-01 03:08:54 +02:00
parent d31bbb1cfe
commit 8885d16e1b
No known key found for this signature in database
GPG Key ID: D5B7A8E43C997DEE
1 changed files with 9 additions and 29 deletions

View File

@ -654,9 +654,7 @@ def unblock_domain(%{assigns: %{user: blocker}} = conn, %{"domain" => domain}) d
json(conn, %{}) json(conn, %{})
end end
def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do def status_search(query) do
accounts = User.search(query, params["resolve"] == "true")
fetched = fetched =
if Regex.match?(~r/https?:/, query) do if Regex.match?(~r/https?:/, query) do
with {:ok, object} <- ActivityPub.fetch_object_from_id(query) do with {:ok, object} <- ActivityPub.fetch_object_from_id(query) do
@ -681,7 +679,13 @@ def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
order_by: [desc: :id] order_by: [desc: :id]
) )
statuses = Repo.all(q) ++ fetched Repo.all(q) ++ fetched
end
def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
accounts = User.search(query, params["resolve"] == "true")
statuses = status_search(query)
tags_path = Web.base_url() <> "/tag/" tags_path = Web.base_url() <> "/tag/"
@ -705,31 +709,7 @@ def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
accounts = User.search(query, params["resolve"] == "true") accounts = User.search(query, params["resolve"] == "true")
fetched = statuses = status_search(query)
if Regex.match?(~r/https?:/, query) do
with {:ok, object} <- ActivityPub.fetch_object_from_id(query) do
[Activity.get_create_activity_by_object_ap_id(object.data["id"])]
else
_e -> []
end
end || []
q =
from(
a in Activity,
where: fragment("?->>'type' = 'Create'", a.data),
where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
where:
fragment(
"to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)",
a.data,
^query
),
limit: 20,
order_by: [desc: :id]
)
statuses = Repo.all(q) ++ fetched
tags = tags =
String.split(query) String.split(query)