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

63 lines
1.7 KiB
Elixir
Raw 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
def render("show.json", %{instance: instance, crawl: crawl}) do
2019-07-19 18:19:53 +00:00
user_threshold = get_config(:personal_instance_threshold)
2019-07-14 11:47:06 +00:00
[status, last_updated] =
case crawl do
nil ->
["not crawled", nil]
_ ->
case crawl.error do
nil -> ["success", crawl.inserted_at]
err -> [err, crawl.inserted_at]
end
end
2019-07-19 18:19:53 +00:00
cond do
2019-07-26 22:30:11 +00:00
instance.user_count < user_threshold and not instance.opt_in ->
2019-07-19 18:19:53 +00:00
%{
name: instance.domain,
status: "personal instance"
}
true ->
2019-07-26 14:34:23 +00:00
filtered_peers =
instance.peers
|> Enum.filter(fn peer -> not peer.opt_out end)
statuses_per_user_per_day =
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
2019-07-19 18:19:53 +00:00
%{
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-07-26 14:34:23 +00:00
peers: render_many(filtered_peers, InstanceView, "instance.json"),
2019-07-19 18:19:53 +00:00
lastUpdated: last_updated,
2019-07-24 16:25:20 +00:00
status: status,
2019-07-27 17:58:40 +00:00
type: instance.type,
statusesPerDay: instance.statuses_per_day,
statusesPerUserPerDay: statuses_per_user_per_day
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
def render("instance.json", %{instance: instance}) do
%{name: instance.domain}
end
2019-07-14 11:47:06 +00:00
end