1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.SearchController do
6 use Pleroma.Web, :controller
10 alias Pleroma.Web.MastodonAPI.AccountView
11 alias Pleroma.Web.MastodonAPI.StatusView
13 alias Pleroma.Web.ControllerHelper
17 plug(Pleroma.Plugs.RateLimiter, :search when action in [:search, :search2, :account_search])
19 def search2(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
20 accounts = with_fallback(fn -> User.search(query, search_options(params, user)) end, [])
21 statuses = with_fallback(fn -> Activity.search(user, query) end, [])
22 tags_path = Web.base_url() <> "/tag/"
28 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
29 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
30 |> Enum.map(fn tag -> %{name: tag, url: tags_path <> tag} end)
33 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
35 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
42 def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
43 accounts = with_fallback(fn -> User.search(query, search_options(params, user)) end, [])
44 statuses = with_fallback(fn -> Activity.search(user, query) end, [])
50 |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end)
51 |> Enum.map(fn tag -> String.slice(tag, 1..-1) end)
54 "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
56 StatusView.render("index.json", activities: statuses, for: user, as: :activity),
63 def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
64 accounts = User.search(query, search_options(params, user))
65 res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)
70 defp search_options(params, user) do
72 resolve: params["resolve"] == "true",
73 following: params["following"] == "true",
74 limit: ControllerHelper.fetch_integer_param(params, "limit"),
75 offset: ControllerHelper.fetch_integer_param(params, "offset"),
80 defp with_fallback(f, fallback) do
85 Logger.error("#{__MODULE__} search error: #{inspect(error)}")