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

57 lines
1.5 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
require Logger
def render("index.json", %{instances: instances}) do
render_many(instances, InstanceView, "instance.json")
end
def render("show.json", %{instance: instance, crawl: crawl}) do
render_one(instance, InstanceView, "instance_detail.json", crawl: crawl)
end
def render("instance.json", %{instance: instance}) do
%{name: instance.domain}
end
def render("instance_detail.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
instance.user_count < user_threshold ->
%{
name: instance.domain,
status: "personal instance"
}
true ->
%{
name: instance.domain,
description: instance.description,
version: instance.version,
userCount: instance.user_count,
insularity: instance.insularity,
statusCount: instance.status_count,
domainCount: length(instance.peers),
peers: render_many(instance.peers, InstanceView, "instance.json"),
lastUpdated: last_updated,
status: status
}
end
2019-07-14 11:47:06 +00:00
end
end