index.community/backend/lib/backend_web/views/instance_view.ex

121 lines
3.3 KiB
Elixir
Raw Permalink Normal View History

2019-07-14 11:47:06 +00:00
defmodule BackendWeb.InstanceView do
use BackendWeb, :view
alias BackendWeb.InstanceView
2019-07-19 18:19:53 +00:00
import Backend.Util
2019-07-14 11:47:06 +00:00
2019-08-27 13:50:16 +00:00
def render("index.json", %{
instances: instances,
total_pages: total_pages,
page_number: page_number,
total_entries: total_entries,
page_size: page_size
}) do
%{
instances: render_many(instances, InstanceView, "index_instance.json"),
pageNumber: page_number,
totalPages: total_pages,
totalEntries: total_entries,
pageSize: page_size
}
end
@doc """
2019-08-29 20:10:37 +00:00
Used when rendering the index of all instances (the difference from show.json is primarily that
it does not include peers).
2019-08-27 13:50:16 +00:00
"""
def render("index_instance.json", %{instance: instance}) do
%{
name: instance.domain,
description: instance.description,
version: instance.version,
userCount: instance.user_count,
insularity: instance.insularity,
statusCount: instance.status_count,
type: instance.type,
statusesPerDay: instance.statuses_per_day,
statusesPerUserPerDay: get_statuses_per_user_per_day(instance)
}
end
def render("show.json", %{
instance: instance,
crawl: crawl,
federation_restrictions: federation_restrictions
}) do
2019-07-19 18:19:53 +00:00
user_threshold = get_config(:personal_instance_threshold)
cond do
2019-07-26 22:30:11 +00:00
instance.user_count < user_threshold and not instance.opt_in ->
2019-08-21 12:30:47 +00:00
render_personal_instance(instance)
2019-07-19 18:19:53 +00:00
2019-08-07 19:41:19 +00:00
instance.crawl_error == "robots.txt" ->
2019-08-21 12:30:47 +00:00
render_domain_and_error(instance)
2019-08-07 19:41:19 +00:00
instance.crawl_error != nil and instance.type == nil ->
2019-08-21 12:30:47 +00:00
render_domain_and_error(instance)
2019-08-07 19:41:19 +00:00
crawl == nil ->
render_domain_and_error(instance)
2019-07-19 18:19:53 +00:00
true ->
render_instance(instance, crawl, federation_restrictions)
2019-07-19 18:19:53 +00:00
end
2019-07-14 11:47:06 +00:00
end
2019-07-26 14:34:23 +00:00
2019-08-27 13:50:16 +00:00
def render("peer.json", %{instance: instance}) do
2019-07-26 14:34:23 +00:00
%{name: instance.domain}
end
2019-08-21 12:30:47 +00:00
2019-08-29 20:10:37 +00:00
def render("error.json", %{error: error}) do
%{error: error}
end
2019-08-21 12:30:47 +00:00
defp render_personal_instance(instance) do
%{
name: instance.domain,
status: "personal instance"
}
end
defp render_domain_and_error(instance) do
%{
name: instance.domain,
status: instance.crawl_error
}
end
defp render_instance(instance, crawl, federation_restrictions) do
2019-08-21 12:30:47 +00:00
last_updated = max_datetime(crawl.inserted_at, instance.updated_at)
filtered_peers =
instance.peers
|> Enum.filter(fn peer -> not peer.opt_out end)
%{
name: instance.domain,
description: instance.description,
version: instance.version,
userCount: instance.user_count,
insularity: instance.insularity,
statusCount: instance.status_count,
domainCount: length(instance.peers),
2019-08-27 13:50:16 +00:00
peers: render_many(filtered_peers, InstanceView, "peer.json"),
2019-08-29 16:54:34 +00:00
federationRestrictions: federation_restrictions,
2019-08-21 12:30:47 +00:00
lastUpdated: last_updated,
status: "success",
type: instance.type,
statusesPerDay: instance.statuses_per_day,
2019-08-27 13:50:16 +00:00
statusesPerUserPerDay: get_statuses_per_user_per_day(instance)
2019-08-21 12:30:47 +00:00
}
end
2019-08-27 13:50:16 +00:00
defp get_statuses_per_user_per_day(instance) do
if instance.statuses_per_day != nil and instance.user_count != nil and
instance.user_count > 0 do
instance.statuses_per_day / instance.user_count
else
nil
end
end
2019-07-14 11:47:06 +00:00
end