hide opted-out instances from federation restriction lists

This commit is contained in:
Tao Bror Bojlén 2019-08-29 18:10:46 +01:00
parent f134941eb2
commit 3b28803bfa
No known key found for this signature in database
GPG key ID: C6EC7AAB905F9E6F
2 changed files with 36 additions and 12 deletions

View file

@ -1,7 +1,8 @@
defmodule BackendWeb.InstanceController do defmodule BackendWeb.InstanceController do
use BackendWeb, :controller use BackendWeb, :controller
alias Backend.Api alias Backend.{Api, Instance, Repo}
alias Graph.Cache alias Graph.Cache
import Ecto.Query
action_fallback(BackendWeb.FallbackController) action_fallback(BackendWeb.FallbackController)
@ -32,7 +33,33 @@ defmodule BackendWeb.InstanceController do
send_resp(conn, 404, "Not found") send_resp(conn, 404, "Not found")
else else
last_crawl = Cache.get_last_crawl(domain) last_crawl = Cache.get_last_crawl(domain)
render(conn, "show.json", instance: instance, crawl: last_crawl)
restricted_domains =
instance.federation_restrictions
|> Enum.map(fn %{target_domain: domain} -> domain end)
opted_out_instances =
Instance
|> select([i], i.domain)
|> where([i], i.opt_out and i.domain in ^restricted_domains)
|> Repo.all()
# convert from a list of {domain, restriction_type} to a map of %{restriction_type => list_of_domains}
federation_restrictions =
instance.federation_restrictions
|> Enum.filter(fn %{target_domain: domain} ->
not Enum.member?(opted_out_instances, domain)
end)
|> Enum.reduce(%{}, fn %{target_domain: domain, type: type}, acc ->
Map.update(acc, type, [domain], fn curr_domains -> [domain | curr_domains] end)
end)
|> Recase.Enumerable.convert_keys(&Recase.to_camel(&1))
render(conn, "show.json",
instance: instance,
crawl: last_crawl,
federation_restrictions: federation_restrictions
)
end end
end end

View file

@ -37,7 +37,11 @@ defmodule BackendWeb.InstanceView do
} }
end end
def render("show.json", %{instance: instance, crawl: crawl}) do def render("show.json", %{
instance: instance,
crawl: crawl,
federation_restrictions: federation_restrictions
}) do
user_threshold = get_config(:personal_instance_threshold) user_threshold = get_config(:personal_instance_threshold)
cond do cond do
@ -51,7 +55,7 @@ defmodule BackendWeb.InstanceView do
render_domain_and_error(instance) render_domain_and_error(instance)
true -> true ->
render_instance(instance, crawl) render_instance(instance, crawl, federation_restrictions)
end end
end end
@ -73,20 +77,13 @@ defmodule BackendWeb.InstanceView do
} }
end end
defp render_instance(instance, crawl) do defp render_instance(instance, crawl, federation_restrictions) do
last_updated = max_datetime(crawl.inserted_at, instance.updated_at) last_updated = max_datetime(crawl.inserted_at, instance.updated_at)
filtered_peers = filtered_peers =
instance.peers instance.peers
|> Enum.filter(fn peer -> not peer.opt_out end) |> Enum.filter(fn peer -> not peer.opt_out end)
federation_restrictions =
instance.federation_restrictions
|> Enum.reduce(%{}, fn %{target_domain: domain, type: type}, acc ->
Map.update(acc, type, [domain], fn curr_domains -> [domain | curr_domains] end)
end)
|> Recase.Enumerable.convert_keys(&Recase.to_camel(&1))
%{ %{
name: instance.domain, name: instance.domain,
description: instance.description, description: instance.description,