[#534] Refactoring / tweaks per MR review.

This commit is contained in:
Ivan Tashkinov 2019-01-28 11:03:52 +03:00
parent d99650270b
commit 9560abea10
10 changed files with 36 additions and 57 deletions

View File

@ -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 @@ def set_reachable(url) when is_binary(url) 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 @@ def set_unreachable(url, unreachable_since) when is_binary(url) 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

View File

@ -0,0 +1,16 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# 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

View File

@ -722,15 +722,7 @@ def publish(actor, activity) 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

View File

@ -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 @@ def errors(conn, _e) do
|> put_status(500)
|> json("error")
end
defp set_requester_reachable(conn, _) do
Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
conn
end
end

View File

@ -10,9 +10,4 @@ def json_response(conn, status, json) 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

View File

@ -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 @@ def errors(conn, _) 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

View File

@ -168,8 +168,7 @@ def send_to_user(%{info: %{salmon: salmon}}, feed, poster),
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,

View File

@ -272,8 +272,7 @@ def publish_one(%{xml: xml, topic: topic, callback: callback, secret: secret}) d
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,

View File

@ -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 @@ def websub_incoming(conn, %{"id" => id}) do
|> send_resp(500, "Error")
end
end
defp set_requester_reachable(conn, _) do
Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
conn
end
end

View File

@ -5,7 +5,6 @@ def change do
create table(:instances) do
add :host, :string
add :unreachable_since, :naive_datetime
add :reachability_checked_at, :naive_datetime
timestamps()
end