forked from AkkomaGang/akkoma
[#534] Refactoring / tweaks per MR review.
This commit is contained in:
parent
d99650270b
commit
9560abea10
10 changed files with 36 additions and 57 deletions
|
@ -13,7 +13,6 @@ defmodule Pleroma.Instances.Instance do
|
||||||
schema "instances" do
|
schema "instances" do
|
||||||
field(:host, :string)
|
field(:host, :string)
|
||||||
field(:unreachable_since, :naive_datetime)
|
field(:unreachable_since, :naive_datetime)
|
||||||
field(:reachability_checked_at, :naive_datetime)
|
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
@ -22,7 +21,7 @@ defmodule Pleroma.Instances.Instance do
|
||||||
|
|
||||||
def changeset(struct, params \\ %{}) do
|
def changeset(struct, params \\ %{}) do
|
||||||
struct
|
struct
|
||||||
|> cast(params, [:host, :unreachable_since, :reachability_checked_at])
|
|> cast(params, [:host, :unreachable_since])
|
||||||
|> validate_required([:host])
|
|> validate_required([:host])
|
||||||
|> unique_constraint(:host)
|
|> unique_constraint(:host)
|
||||||
end
|
end
|
||||||
|
@ -66,7 +65,7 @@ def set_reachable(url) when is_binary(url) do
|
||||||
%Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do
|
%Instance{} = existing_record <- Repo.get_by(Instance, %{host: host}) do
|
||||||
{:ok, _instance} =
|
{:ok, _instance} =
|
||||||
existing_record
|
existing_record
|
||||||
|> changeset(%{unreachable_since: nil, reachability_checked_at: DateTime.utc_now()})
|
|> changeset(%{unreachable_since: nil})
|
||||||
|> Repo.update()
|
|> Repo.update()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -80,27 +79,22 @@ def set_unreachable(url, unreachable_since) when is_binary(url) do
|
||||||
host = host(url)
|
host = host(url)
|
||||||
existing_record = Repo.get_by(Instance, %{host: host})
|
existing_record = Repo.get_by(Instance, %{host: host})
|
||||||
|
|
||||||
changes = %{
|
changes = %{unreachable_since: unreachable_since}
|
||||||
unreachable_since: unreachable_since,
|
|
||||||
reachability_checked_at: NaiveDateTime.utc_now()
|
|
||||||
}
|
|
||||||
|
|
||||||
if existing_record do
|
cond do
|
||||||
update_changes =
|
is_nil(existing_record) ->
|
||||||
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} =
|
|
||||||
%Instance{}
|
%Instance{}
|
||||||
|> changeset(Map.put(changes, :host, host))
|
|> changeset(Map.put(changes, :host, host))
|
||||||
|> Repo.insert()
|
|> 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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
16
lib/pleroma/plugs/set_requester_reachable_plug.ex
Normal file
16
lib/pleroma/plugs/set_requester_reachable_plug.ex
Normal 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
|
|
@ -722,15 +722,7 @@ def publish(actor, activity) do
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def publish_one(%{inbox: inbox} = activity) do
|
def publish_one(%{inbox: inbox, json: json, actor: actor, id: id}) 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
|
|
||||||
Logger.info("Federating #{id} to #{inbox}")
|
Logger.info("Federating #{id} to #{inbox}")
|
||||||
host = URI.parse(inbox).host
|
host = URI.parse(inbox).host
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
|
||||||
action_fallback(:errors)
|
action_fallback(:errors)
|
||||||
|
|
||||||
plug(Pleroma.Web.FederatingPlug when action in [:inbox, :relay])
|
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(:relay_active? when action in [:relay])
|
||||||
plug(:set_requester_reachable when action in [:inbox])
|
|
||||||
|
|
||||||
def relay_active?(conn, _) do
|
def relay_active?(conn, _) do
|
||||||
if Keyword.get(Application.get_env(:pleroma, :instance), :allow_relay) do
|
if Keyword.get(Application.get_env(:pleroma, :instance), :allow_relay) do
|
||||||
|
@ -291,9 +291,4 @@ def errors(conn, _e) do
|
||||||
|> put_status(500)
|
|> put_status(500)
|
||||||
|> json("error")
|
|> json("error")
|
||||||
end
|
end
|
||||||
|
|
||||||
defp set_requester_reachable(conn, _) do
|
|
||||||
Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
|
|
||||||
conn
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,9 +10,4 @@ def json_response(conn, status, json) do
|
||||||
|> put_status(status)
|
|> put_status(status)
|
||||||
|> json(json)
|
|> json(json)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_requester_reachable(conn) do
|
|
||||||
with [referer] <- get_req_header(conn, "referer"),
|
|
||||||
do: Pleroma.Instances.set_reachable(referer)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,7 +15,7 @@ defmodule Pleroma.Web.OStatus.OStatusController do
|
||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
|
|
||||||
plug(Pleroma.Web.FederatingPlug when action in [:salmon_incoming])
|
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)
|
action_fallback(:errors)
|
||||||
|
|
||||||
|
@ -203,9 +203,4 @@ def errors(conn, _) do
|
||||||
|> put_status(500)
|
|> put_status(500)
|
||||||
|> text("Something went wrong")
|
|> text("Something went wrong")
|
||||||
end
|
end
|
||||||
|
|
||||||
defp set_requester_reachable(conn, _) do
|
|
||||||
Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
|
|
||||||
conn
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -168,8 +168,7 @@ def send_to_user(%{info: %{salmon: salmon}}, feed, poster),
|
||||||
do: send_to_user(salmon, feed, poster)
|
do: send_to_user(salmon, feed, poster)
|
||||||
|
|
||||||
def send_to_user(url, feed, poster) when is_binary(url) do
|
def send_to_user(url, feed, poster) when is_binary(url) do
|
||||||
with {:reachable, true} <- {:reachable, Instances.reachable?(url)},
|
with {:ok, %{status: code}} when code in 200..299 <-
|
||||||
{:ok, %{status: code}} when code in 200..299 <-
|
|
||||||
poster.(
|
poster.(
|
||||||
url,
|
url,
|
||||||
feed,
|
feed,
|
||||||
|
|
|
@ -272,8 +272,7 @@ def publish_one(%{xml: xml, topic: topic, callback: callback, secret: secret}) d
|
||||||
signature = sign(secret || "", xml)
|
signature = sign(secret || "", xml)
|
||||||
Logger.info(fn -> "Pushing #{topic} to #{callback}" end)
|
Logger.info(fn -> "Pushing #{topic} to #{callback}" end)
|
||||||
|
|
||||||
with {:reachable, true} <- {:reachable, Instances.reachable?(callback)},
|
with {:ok, %{status: code}} when code in 200..299 <-
|
||||||
{:ok, %{status: code}} when code in 200..299 <-
|
|
||||||
@httpoison.post(
|
@httpoison.post(
|
||||||
callback,
|
callback,
|
||||||
xml,
|
xml,
|
||||||
|
|
|
@ -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
|
def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
|
||||||
user = User.get_cached_by_nickname(nickname)
|
user = User.get_cached_by_nickname(nickname)
|
||||||
|
@ -96,9 +96,4 @@ def websub_incoming(conn, %{"id" => id}) do
|
||||||
|> send_resp(500, "Error")
|
|> send_resp(500, "Error")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp set_requester_reachable(conn, _) do
|
|
||||||
Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
|
|
||||||
conn
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,7 +5,6 @@ def change do
|
||||||
create table(:instances) do
|
create table(:instances) do
|
||||||
add :host, :string
|
add :host, :string
|
||||||
add :unreachable_since, :naive_datetime
|
add :unreachable_since, :naive_datetime
|
||||||
add :reachability_checked_at, :naive_datetime
|
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue