2019-06-14 05:39:57 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.MastodonAPI.SearchController do
|
|
|
|
use Pleroma.Web, :controller
|
2019-07-10 02:28:03 -06:00
|
|
|
|
2019-06-14 05:39:57 -06:00
|
|
|
alias Pleroma.Activity
|
2019-07-10 02:28:03 -06:00
|
|
|
alias Pleroma.Plugs.RateLimiter
|
2019-06-14 05:39:57 -06:00
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web
|
2019-07-10 02:28:03 -06:00
|
|
|
alias Pleroma.Web.ControllerHelper
|
2019-06-14 05:39:57 -06:00
|
|
|
alias Pleroma.Web.MastodonAPI.AccountView
|
|
|
|
alias Pleroma.Web.MastodonAPI.StatusView
|
|
|
|
|
|
|
|
require Logger
|
2019-07-10 02:28:03 -06:00
|
|
|
plug(RateLimiter, :search when action in [:search, :search2, :account_search])
|
2019-06-14 05:39:57 -06:00
|
|
|
|
|
|
|
def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
|
2019-07-03 04:19:51 -06:00
|
|
|
accounts = with_fallback(fn -> User.search(query, search_options(params, user)) end, [])
|
|
|
|
statuses = with_fallback(fn -> Activity.search(user, query) end, [])
|
2019-07-10 02:28:03 -06:00
|
|
|
|
2019-06-14 05:39:57 -06:00
|
|
|
tags_path = Web.base_url() <> "/tag/"
|
|
|
|
|
|
|
|
tags =
|
|
|
|
query
|
2019-07-10 02:28:03 -06:00
|
|
|
|> prepare_tags
|
2019-06-14 05:39:57 -06:00
|
|
|
|> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
|
|
|
|
|
|
|
|
res = %{
|
|
|
|
"accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
|
|
|
|
"statuses" =>
|
|
|
|
StatusView.render("index.json", activities: statuses, for: user, as: :activity),
|
|
|
|
"hashtags" => tags
|
|
|
|
}
|
|
|
|
|
|
|
|
json(conn, res)
|
|
|
|
end
|
|
|
|
|
|
|
|
def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
|
2019-07-10 02:28:03 -06:00
|
|
|
accounts = with_fallback(fn -> User.search(query, search_options(params, user)) end)
|
|
|
|
statuses = with_fallback(fn -> Activity.search(user, query) end)
|
2019-06-14 05:39:57 -06:00
|
|
|
|
2019-07-10 02:28:03 -06:00
|
|
|
tags = prepare_tags(query)
|
2019-06-14 05:39:57 -06:00
|
|
|
|
|
|
|
res = %{
|
|
|
|
"accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
|
|
|
|
"statuses" =>
|
|
|
|
StatusView.render("index.json", activities: statuses, for: user, as: :activity),
|
|
|
|
"hashtags" => tags
|
|
|
|
}
|
|
|
|
|
|
|
|
json(conn, res)
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
|
|
|
|
accounts = User.search(query, search_options(params, user))
|
|
|
|
res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
|
|
|
|
|
|
|
|
json(conn, res)
|
|
|
|
end
|
|
|
|
|
2019-07-10 02:28:03 -06:00
|
|
|
defp prepare_tags(query) do
|
|
|
|
query
|
|
|
|
|> String.split()
|
|
|
|
|> Enum.uniq()
|
|
|
|
|> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
|
|
|
|
|> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
|
|
|
|
end
|
|
|
|
|
2019-06-14 05:39:57 -06:00
|
|
|
defp search_options(params, user) do
|
|
|
|
[
|
|
|
|
resolve: params["resolve"] == "true",
|
|
|
|
following: params["following"] == "true",
|
|
|
|
limit: ControllerHelper.fetch_integer_param(params, "limit"),
|
|
|
|
offset: ControllerHelper.fetch_integer_param(params, "offset"),
|
|
|
|
for_user: user
|
|
|
|
]
|
|
|
|
end
|
2019-07-03 04:19:51 -06:00
|
|
|
|
2019-07-10 02:28:03 -06:00
|
|
|
defp with_fallback(f, fallback \\ []) do
|
2019-07-03 04:19:51 -06:00
|
|
|
try do
|
|
|
|
f.()
|
|
|
|
rescue
|
|
|
|
error ->
|
|
|
|
Logger.error("#{__MODULE__} search error: #{inspect(error)}")
|
|
|
|
fallback
|
|
|
|
end
|
|
|
|
end
|
2019-06-14 05:39:57 -06:00
|
|
|
end
|