Merge branch 'bugfix/377-stuck-follow-request' into 'develop'
[akkoma] / lib / pleroma / activity / search.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Activity.Search do
6 alias Pleroma.Activity
7 alias Pleroma.Object.Fetcher
8 alias Pleroma.Repo
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.Visibility
11
12 import Ecto.Query
13
14 def search(user, search_query) do
15 index_type = if Pleroma.Config.get([:database, :rum_enabled]), do: :rum, else: :gin
16
17 Activity
18 |> Activity.with_preloaded_object()
19 |> Activity.restrict_deactivated_users()
20 |> restrict_public()
21 |> query_with(index_type, search_query)
22 |> maybe_restrict_local(user)
23 |> Repo.all()
24 |> maybe_fetch(user, search_query)
25 end
26
27 defp restrict_public(q) do
28 from([a, o] in q,
29 where: fragment("?->>'type' = 'Create'", a.data),
30 where: "https://www.w3.org/ns/activitystreams#Public" in a.recipients,
31 limit: 40
32 )
33 end
34
35 defp query_with(q, :gin, search_query) do
36 from([a, o] in q,
37 where:
38 fragment(
39 "to_tsvector('english', ?->>'content') @@ plainto_tsquery('english', ?)",
40 o.data,
41 ^search_query
42 ),
43 order_by: [desc: :id]
44 )
45 end
46
47 defp query_with(q, :rum, search_query) do
48 from([a, o] in q,
49 where:
50 fragment(
51 "? @@ plainto_tsquery('english', ?)",
52 o.fts_content,
53 ^search_query
54 ),
55 order_by: [fragment("? <=> now()::date", o.inserted_at)]
56 )
57 end
58
59 # users can search everything
60 defp maybe_restrict_local(q, %User{}), do: q
61
62 # unauthenticated users can only search local activities
63 defp maybe_restrict_local(q, _), do: where(q, local: true)
64
65 defp maybe_fetch(activities, user, search_query) do
66 with true <- Regex.match?(~r/https?:/, search_query),
67 {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
68 %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
69 true <- Visibility.visible_for_user?(activity, user) do
70 activities ++ [activity]
71 else
72 _ -> activities
73 end
74 end
75 end