Merge branch 'develop' into feature/report-notes
[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.Pagination
9 alias Pleroma.User
10 alias Pleroma.Web.ActivityPub.Visibility
11
12 require Pleroma.Constants
13
14 import Ecto.Query
15
16 def search(user, search_query, options \\ []) do
17 index_type = if Pleroma.Config.get([:database, :rum_enabled]), do: :rum, else: :gin
18 limit = Enum.min([Keyword.get(options, :limit), 40])
19 offset = Keyword.get(options, :offset, 0)
20 author = Keyword.get(options, :author)
21
22 Activity
23 |> Activity.with_preloaded_object()
24 |> Activity.restrict_deactivated_users()
25 |> restrict_public()
26 |> query_with(index_type, search_query)
27 |> maybe_restrict_local(user)
28 |> maybe_restrict_author(author)
29 |> Pagination.fetch_paginated(%{"offset" => offset, "limit" => limit}, :offset)
30 |> maybe_fetch(user, search_query)
31 end
32
33 def maybe_restrict_author(query, %User{} = author) do
34 from([a, o] in query,
35 where: a.actor == ^author.ap_id
36 )
37 end
38
39 def maybe_restrict_author(query, _), do: query
40
41 defp restrict_public(q) do
42 from([a, o] in q,
43 where: fragment("?->>'type' = 'Create'", a.data),
44 where: ^Pleroma.Constants.as_public() in a.recipients
45 )
46 end
47
48 defp query_with(q, :gin, search_query) do
49 from([a, o] in q,
50 where:
51 fragment(
52 "to_tsvector('english', ?->>'content') @@ plainto_tsquery('english', ?)",
53 o.data,
54 ^search_query
55 )
56 )
57 end
58
59 defp query_with(q, :rum, search_query) do
60 from([a, o] in q,
61 where:
62 fragment(
63 "? @@ plainto_tsquery('english', ?)",
64 o.fts_content,
65 ^search_query
66 ),
67 order_by: [fragment("? <=> now()::date", o.inserted_at)]
68 )
69 end
70
71 defp maybe_restrict_local(q, user) do
72 limit = Pleroma.Config.get([:instance, :limit_to_local_content], :unauthenticated)
73
74 case {limit, user} do
75 {:all, _} -> restrict_local(q)
76 {:unauthenticated, %User{}} -> q
77 {:unauthenticated, _} -> restrict_local(q)
78 {false, _} -> q
79 end
80 end
81
82 defp restrict_local(q), do: where(q, local: true)
83
84 defp maybe_fetch(activities, user, search_query) do
85 with true <- Regex.match?(~r/https?:/, search_query),
86 {:ok, object} <- Fetcher.fetch_object_from_id(search_query),
87 %Activity{} = activity <- Activity.get_create_by_object_ap_id(object.data["id"]),
88 true <- Visibility.visible_for_user?(activity, user) do
89 [activity | activities]
90 else
91 _ -> activities
92 end
93 end
94 end