Merge 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 )
44 end
45
46 defp query_with(q, :rum, search_query) do
47 from([a, o] in q,
48 where:
49 fragment(
50 "? @@ plainto_tsquery('english', ?)",
51 o.fts_content,
52 ^search_query
53 ),
54 order_by: [fragment("? <=> now()::date", o.inserted_at)]
55 )
56 end
57
58 defp maybe_restrict_local(q, user) do
59 limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
60
61 case {limit, user} do
62 {:all, _} -> restrict_local(q)
63 {:unauthenticated, %User{}} -> q
64 {:unauthenticated, _} -> restrict_local(q)
65 {false, _} -> q
66 end
67 end
68
69 defp restrict_local(q), do: where(q, local: true)
70
71 defp maybe_fetch(activities, user, search_query) do
72 with true <- Regex.match?(~r/https?:/, search_query),
73 {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
74 %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
75 true <- Visibility.visible_for_user?(activity, user) do
76 activities ++ [activity]
77 else
78 _ -> activities
79 end
80 end
81 end