Add option to restrict all users to local content
[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 defp maybe_restrict_local(q, user) do
60 limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
61
62 case {limit, user} do
63 {:all, _} -> restrict_local(q)
64 {:unauthenticated, %User{}} -> q
65 {:unauthenticated, _} -> restrict_local(q)
66 {false, _} -> q
67 end
68 end
69
70 defp restrict_local(q), do: where(q, local: true)
71
72 defp maybe_fetch(activities, user, search_query) do
73 with true <- Regex.match?(~r/https?:/, search_query),
74 {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
75 %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
76 true <- Visibility.visible_for_user?(activity, user) do
77 activities ++ [activity]
78 else
79 _ -> activities
80 end
81 end
82 end