From 9560abea102b8cd4927c9350bbd0a1a2f1800ea6 Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Mon, 28 Jan 2019 11:03:52 +0300 Subject: [PATCH] [#534] Refactoring / tweaks per MR review. --- lib/pleroma/instances/instance.ex | 34 ++++++++----------- .../plugs/set_requester_reachable_plug.ex | 16 +++++++++ lib/pleroma/web/activity_pub/activity_pub.ex | 10 +----- .../activity_pub/activity_pub_controller.ex | 7 +--- lib/pleroma/web/controller_helper.ex | 5 --- lib/pleroma/web/ostatus/ostatus_controller.ex | 7 +--- lib/pleroma/web/salmon/salmon.ex | 3 +- lib/pleroma/web/websub/websub.ex | 3 +- lib/pleroma/web/websub/websub_controller.ex | 7 +--- .../20190123125546_create_instances.exs | 1 - 10 files changed, 36 insertions(+), 57 deletions(-) create mode 100644 lib/pleroma/plugs/set_requester_reachable_plug.ex diff --git a/lib/pleroma/instances/instance.ex b/lib/pleroma/instances/instance.ex index 60e8d0e21..e3af4a8a7 100644 --- a/lib/pleroma/instances/instance.ex +++ b/lib/pleroma/instances/instance.ex @@ -13,7 +13,6 @@ defmodule Pleroma.Instances.Instance do schema "instances" do field(:host, :string) field(:unreachable_since, :naive_datetime) - field(:reachability_checked_at, :naive_datetime) timestamps() end @@ -22,7 +21,7 @@ defmodule Pleroma.Instances.Instance do def changeset(struct, params \\ %{}) do struct - |> cast(params, [:host, :unreachable_since, :reachability_checked_at]) + |> cast(params, [:host, :unreachable_since]) |> validate_required([:host]) |> unique_constraint(:host) end @@ -66,7 +65,7 @@ defmodule Pleroma.Instances.Instance do %Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do {:ok, _instance} = existing_record - |> changeset(%{unreachable_since: nil, reachability_checked_at: DateTime.utc_now()}) + |> changeset(%{unreachable_since: nil}) |> Repo.update() end end @@ -80,27 +79,22 @@ defmodule Pleroma.Instances.Instance do host = host(url) existing_record = Repo.get_by(Instance, %{host: host}) - changes = %{ - unreachable_since: unreachable_since, - reachability_checked_at: NaiveDateTime.utc_now() - } + changes = %{unreachable_since: unreachable_since} - if existing_record do - update_changes = - if existing_record.unreachable_since && - NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt, - do: Map.delete(changes, :unreachable_since), - else: changes - - {:ok, _instance} = - existing_record - |> changeset(update_changes) - |> Repo.update() - else - {:ok, _instance} = + cond do + is_nil(existing_record) -> %Instance{} |> changeset(Map.put(changes, :host, host)) |> Repo.insert() + + existing_record.unreachable_since && + NaiveDateTime.compare(existing_record.unreachable_since, unreachable_since) != :gt -> + {:noop, existing_record} + + true -> + existing_record + |> changeset(changes) + |> Repo.update() end end diff --git a/lib/pleroma/plugs/set_requester_reachable_plug.ex b/lib/pleroma/plugs/set_requester_reachable_plug.ex new file mode 100644 index 000000000..88551be70 --- /dev/null +++ b/lib/pleroma/plugs/set_requester_reachable_plug.ex @@ -0,0 +1,16 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.Plugs.SetRequesterReachablePlug do + import Plug.Conn + + def init(_), do: [] + + def call(%Plug.Conn{} = conn, _) do + with [referer] <- get_req_header(conn, "referer"), + do: Pleroma.Instances.set_reachable(referer) + + conn + end +end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 4b34334a0..4232d6c0a 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -722,15 +722,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do end) end - def publish_one(%{inbox: inbox} = activity) do - if Instances.reachable?(inbox) do - do_publish_one(activity) - else - {:error, :noop} - end - end - - defp do_publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) do + def publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) do Logger.info("Federating #{id} to #{inbox}") host = URI.parse(inbox).host diff --git a/lib/pleroma/web/activity_pub/activity_pub_controller.ex b/lib/pleroma/web/activity_pub/activity_pub_controller.ex index dc353dff0..fadb038a2 100644 --- a/lib/pleroma/web/activity_pub/activity_pub_controller.ex +++ b/lib/pleroma/web/activity_pub/activity_pub_controller.ex @@ -18,8 +18,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do action_fallback(:errors) plug(Pleroma.Web.FederatingPlug when action in [:inbox, :relay]) + plug(Pleroma.Web.Plugs.SetRequesterReachablePlug when action in [:inbox]) plug(:relay_active? when action in [:relay]) - plug(:set_requester_reachable when action in [:inbox]) def relay_active?(conn, _) do if Keyword.get(Application.get_env(:pleroma, :instance), :allow_relay) do @@ -291,9 +291,4 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do |> put_status(500) |> json("error") end - - defp set_requester_reachable(conn, _) do - Pleroma.Web.ControllerHelper.set_requester_reachable(conn) - conn - end end diff --git a/lib/pleroma/web/controller_helper.ex b/lib/pleroma/web/controller_helper.ex index 13cf1877f..14e3d19fd 100644 --- a/lib/pleroma/web/controller_helper.ex +++ b/lib/pleroma/web/controller_helper.ex @@ -10,9 +10,4 @@ defmodule Pleroma.Web.ControllerHelper do |> put_status(status) |> json(json) end - - def set_requester_reachable(conn) do - with [referer] <- get_req_header(conn, "referer"), - do: Pleroma.Instances.set_reachable(referer) - end end diff --git a/lib/pleroma/web/ostatus/ostatus_controller.ex b/lib/pleroma/web/ostatus/ostatus_controller.ex index a89f16b94..e483447ed 100644 --- a/lib/pleroma/web/ostatus/ostatus_controller.ex +++ b/lib/pleroma/web/ostatus/ostatus_controller.ex @@ -15,7 +15,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do alias Pleroma.Web.ActivityPub.ActivityPub plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming]) - plug(:set_requester_reachable when action in [:salmon_incoming]) + plug(Pleroma.Web.Plugs.SetRequesterReachablePlug when action in [:salmon_incoming]) action_fallback(:errors) @@ -203,9 +203,4 @@ defmodule Pleroma.Web.OStatus.OStatusController do |> put_status(500) |> text("Something went wrong") end - - defp set_requester_reachable(conn, _) do - Pleroma.Web.ControllerHelper.set_requester_reachable(conn) - conn - end end diff --git a/lib/pleroma/web/salmon/salmon.ex b/lib/pleroma/web/salmon/salmon.ex index 17ca7a6e8..80023127c 100644 --- a/lib/pleroma/web/salmon/salmon.ex +++ b/lib/pleroma/web/salmon/salmon.ex @@ -168,8 +168,7 @@ defmodule Pleroma.Web.Salmon do do: send_to_user(salmon, feed, poster) def send_to_user(url, feed, poster) when is_binary(url) do - with {:reachable, true} <- {:reachable, Instances.reachable?(url)}, - {:ok, %{status: code}} when code in 200..299 <- + with {:ok, %{status: code}} when code in 200..299 <- poster.( url, feed, diff --git a/lib/pleroma/web/websub/websub.ex b/lib/pleroma/web/websub/websub.ex index cbb7a5ac7..64eba7221 100644 --- a/lib/pleroma/web/websub/websub.ex +++ b/lib/pleroma/web/websub/websub.ex @@ -272,8 +272,7 @@ defmodule Pleroma.Web.Websub do signature = sign(secret || "", xml) Logger.info(fn -> "Pushing #{topic} to #{callback}" end) - with {:reachable, true} <- {:reachable, Instances.reachable?(callback)}, - {:ok, %{status: code}} when code in 200..299 <- + with {:ok, %{status: code}} when code in 200..299 <- @httpoison.post( callback, xml, diff --git a/lib/pleroma/web/websub/websub_controller.ex b/lib/pleroma/web/websub/websub_controller.ex index 02fe075d7..9da7e70a1 100644 --- a/lib/pleroma/web/websub/websub_controller.ex +++ b/lib/pleroma/web/websub/websub_controller.ex @@ -20,7 +20,7 @@ defmodule Pleroma.Web.Websub.WebsubController do ] ) - plug(:set_requester_reachable when action in [:websub_incoming]) + plug(Pleroma.Web.Plugs.SetRequesterReachablePlug when action in [:websub_incoming]) def websub_subscription_request(conn, %{"nickname" => nickname} = params) do user = User.get_cached_by_nickname(nickname) @@ -96,9 +96,4 @@ defmodule Pleroma.Web.Websub.WebsubController do |> send_resp(500, "Error") end end - - defp set_requester_reachable(conn, _) do - Pleroma.Web.ControllerHelper.set_requester_reachable(conn) - conn - end end diff --git a/priv/repo/migrations/20190123125546_create_instances.exs b/priv/repo/migrations/20190123125546_create_instances.exs index 4f5915fba..b527ad7ec 100644 --- a/priv/repo/migrations/20190123125546_create_instances.exs +++ b/priv/repo/migrations/20190123125546_create_instances.exs @@ -5,7 +5,6 @@ defmodule Pleroma.Repo.Migrations.CreateInstances do create table(:instances) do add :host, :string add :unreachable_since, :naive_datetime - add :reachability_checked_at, :naive_datetime timestamps() end