index.community/backend/lib/backend/api.ex

228 lines
6.6 KiB
Elixir
Raw Normal View History

2019-07-14 11:47:06 +00:00
defmodule Backend.Api do
2019-08-21 12:30:47 +00:00
@moduledoc """
Functions used in the API controllers. Most of these simply return data from the database.
"""
2019-07-20 10:01:56 +00:00
alias Backend.{Edge, Instance, Repo}
2019-07-19 18:19:53 +00:00
import Backend.Util
2019-07-14 11:47:06 +00:00
import Ecto.Query
2019-08-29 20:10:37 +00:00
@type instance_sort_field :: :name | :user_count | :status_count | :insularity
@type sort_direction :: :asc | :desc
@spec get_instances(Integer.t() | nil, instance_sort_field | nil, sort_direction | nil) ::
Scrivener.Page.t()
def get_instances(page \\ nil, sort_field \\ nil, sort_direction \\ nil) do
2019-08-27 13:50:16 +00:00
Instance
2019-08-29 18:04:43 +00:00
|> where([i], not is_nil(i.type) and not i.opt_out)
2019-08-29 20:10:37 +00:00
|> maybe_order_by(sort_field, sort_direction)
2019-08-27 13:50:16 +00:00
|> Repo.paginate(page: page)
end
2019-08-29 20:10:37 +00:00
defp maybe_order_by(query, sort_field, sort_direction) do
cond do
sort_field == nil and sort_direction != nil ->
query
sort_field != nil and sort_direction == nil ->
query
|> order_by(desc: ^sort_field)
sort_direction == :asc ->
query
|> order_by(asc_nulls_last: ^sort_field)
sort_direction == :desc ->
query
|> order_by(desc_nulls_last: ^sort_field)
true ->
query
end
end
2019-08-23 13:08:05 +00:00
@spec get_instance(String.t()) :: Instance.t() | nil
def get_instance(domain) do
2019-07-14 11:47:06 +00:00
Instance
2019-08-23 13:08:05 +00:00
|> Repo.get_by(domain: domain)
2019-07-14 11:47:06 +00:00
end
2019-08-29 16:54:34 +00:00
@spec get_instance_with_relationships(String.t()) :: Instance.t() | nil
def get_instance_with_relationships(domain) do
Instance
|> preload(:peers)
2019-08-29 16:54:34 +00:00
|> preload(:federation_restrictions)
|> Repo.get_by(domain: domain)
end
2019-07-26 14:34:23 +00:00
def update_instance(instance) do
Repo.insert(
instance,
on_conflict: {:replace, [:opt_in, :opt_out]},
conflict_target: :domain
)
end
2019-07-14 11:47:06 +00:00
@doc """
Returns a list of instances that
* have a user count (required to give the instance a size on the graph)
2019-07-19 18:19:53 +00:00
* the user count is > the threshold
2019-07-18 20:05:16 +00:00
* have x and y coordinates
2019-07-23 16:32:43 +00:00
2019-08-29 20:10:37 +00:00
If `domain` is passed, then this function only returns nodes that are neighbors of that
instance.
2019-07-14 11:47:06 +00:00
"""
@spec list_nodes() :: [Instance.t()]
2019-07-23 16:32:43 +00:00
def list_nodes(domain \\ nil) do
2019-07-19 18:19:53 +00:00
user_threshold = get_config(:personal_instance_threshold)
2019-07-14 11:47:06 +00:00
Instance
2019-08-31 19:24:53 +00:00
# filter down to instances that have edges
|> join(:inner, [i], e in Edge, on: i.domain == e.source_domain or i.domain == e.target_domain)
2019-07-19 18:19:53 +00:00
|> where(
[i],
not is_nil(i.x) and not is_nil(i.y) and not is_nil(i.user_count) and
(i.user_count >= ^user_threshold or i.opt_in) and not i.opt_out
2019-07-19 18:19:53 +00:00
)
2019-07-23 16:32:43 +00:00
|> maybe_filter_nodes_to_neighborhood(domain)
2019-07-27 17:58:40 +00:00
|> select([c], [:domain, :user_count, :x, :y, :type, :statuses_per_day])
2019-07-14 11:47:06 +00:00
|> Repo.all()
end
2019-07-23 16:32:43 +00:00
# if we're getting the sub-graph around a given domain, only return neighbors.
defp maybe_filter_nodes_to_neighborhood(query, domain) do
case domain do
nil ->
query
_ ->
query
|> join(:inner, [i], outgoing_edges in Edge, on: outgoing_edges.source_domain == i.domain)
|> join(:inner, [i], incoming_edges in Edge, on: incoming_edges.target_domain == i.domain)
|> where(
[i, outgoing_edges, incoming_edges],
outgoing_edges.target_domain == ^domain or incoming_edges.source_domain == ^domain or
i.domain == ^domain
)
|> distinct(true)
end
end
2019-07-14 11:47:06 +00:00
@spec list_edges() :: [Edge.t()]
2019-08-21 12:30:47 +00:00
# credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity
2019-07-23 16:32:43 +00:00
def list_edges(domain \\ nil) do
2019-07-19 18:19:53 +00:00
user_threshold = get_config(:personal_instance_threshold)
2019-07-14 11:47:06 +00:00
Edge
|> join(:inner, [e], i1 in Instance, on: e.source_domain == i1.domain)
|> join(:inner, [e], i2 in Instance, on: e.target_domain == i2.domain)
2019-07-23 16:32:43 +00:00
|> maybe_filter_edges_to_neighborhood(domain)
2019-07-14 11:47:06 +00:00
|> select([e], [:id, :source_domain, :target_domain, :weight])
|> where(
2019-07-18 20:05:16 +00:00
[e, i1, i2],
not is_nil(i1.x) and not is_nil(i1.y) and
2019-07-19 18:19:53 +00:00
not is_nil(i2.x) and not is_nil(i2.y) and
(i1.user_count >= ^user_threshold or i1.opt_in) and
(i2.user_count >= ^user_threshold or i2.opt_in) and
2019-07-26 14:34:23 +00:00
not i1.opt_out and not i2.opt_out
2019-07-14 11:47:06 +00:00
)
|> Repo.all()
end
2019-07-23 16:32:43 +00:00
defp maybe_filter_edges_to_neighborhood(query, domain) do
case domain do
nil ->
query
_ ->
# we want all edges in the neighborhood -- not just edges connected to `domain`
query
|> join(:inner, [e], neighbor_edges in Edge,
on:
neighbor_edges.source_domain == e.target_domain or
neighbor_edges.target_domain == e.source_domain
)
|> where(
[e, i1, i2, neighbor_edges],
e.source_domain == ^domain or e.target_domain == ^domain or
neighbor_edges.source_domain == ^domain or neighbor_edges.target_domain == ^domain
)
|> distinct(true)
end
end
2019-08-04 11:39:29 +00:00
def search_instances(query, filters, from \\ 0) do
2019-07-26 22:30:11 +00:00
page_size = 50
2019-07-26 22:30:11 +00:00
search_response =
2019-08-04 11:39:29 +00:00
Elasticsearch.post(
Backend.Elasticsearch.Cluster,
"/instances/_search",
build_es_query(query, filters, page_size, from)
)
2019-07-26 22:30:11 +00:00
with {:ok, result} <- search_response do
hits =
get_in(result, ["hits", "hits"])
|> Enum.map(fn h -> h |> Map.get("_source") |> convert_keys_to_atoms() end)
next =
if length(hits) < page_size do
nil
else
from + page_size
end
%{
hits: hits,
next: next
}
end
end
2019-08-04 11:39:29 +00:00
defp build_es_query(query, filters, page_size, from) do
opt_out_filter = %{"term" => %{"opt_out" => "false"}}
filters = [opt_out_filter | filters]
%{
"sort" => "_score",
"from" => from,
"size" => page_size,
# This must be >0, otherwise all documents will be returned
"min_score" => 1,
"query" => %{
"bool" => %{
"filter" => filters,
"should" => [
%{
"multi_match" => %{
"query" => query,
"fields" => [
"description.*",
2019-08-04 11:39:29 +00:00
"domain.english"
]
}
},
%{
# If the query exactly matches a domain, that instance should always be the first result.
"wildcard" => %{
"domain.keyword" => %{
"value" => query,
"boost" => 100
}
}
},
%{
# Give substring matches in domains a large boost, too.
"wildcard" => %{
"domain.keyword" => %{
"value" => "*#{query}*",
"boost" => 10
}
}
}
]
}
}
}
end
2019-07-14 11:47:06 +00:00
end