From 3b28803bfa91cfb0678aa1ca4733b57da0beb847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tao=20Bror=20Bojl=C3=A9n?= Date: Thu, 29 Aug 2019 18:10:46 +0100 Subject: [PATCH] hide opted-out instances from federation restriction lists --- .../controllers/instance_controller.ex | 31 +++++++++++++++++-- .../lib/backend_web/views/instance_view.ex | 17 +++++----- 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/backend/lib/backend_web/controllers/instance_controller.ex b/backend/lib/backend_web/controllers/instance_controller.ex index 40c35c9..7ad2690 100644 --- a/backend/lib/backend_web/controllers/instance_controller.ex +++ b/backend/lib/backend_web/controllers/instance_controller.ex @@ -1,7 +1,8 @@ defmodule BackendWeb.InstanceController do use BackendWeb, :controller - alias Backend.Api + alias Backend.{Api, Instance, Repo} alias Graph.Cache + import Ecto.Query action_fallback(BackendWeb.FallbackController) @@ -32,7 +33,33 @@ defmodule BackendWeb.InstanceController do send_resp(conn, 404, "Not found") else 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 diff --git a/backend/lib/backend_web/views/instance_view.ex b/backend/lib/backend_web/views/instance_view.ex index 6ed8468..e79d51c 100644 --- a/backend/lib/backend_web/views/instance_view.ex +++ b/backend/lib/backend_web/views/instance_view.ex @@ -37,7 +37,11 @@ defmodule BackendWeb.InstanceView do } 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) cond do @@ -51,7 +55,7 @@ defmodule BackendWeb.InstanceView do render_domain_and_error(instance) true -> - render_instance(instance, crawl) + render_instance(instance, crawl, federation_restrictions) end end @@ -73,20 +77,13 @@ defmodule BackendWeb.InstanceView do } 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) filtered_peers = instance.peers |> 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, description: instance.description,