mirror of
https://akkoma.dev/AkkomaGang/akkoma.git
synced 2024-11-17 22:54:40 +00:00
[#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
|
||||
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
|
||||
|
||||
|
|
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 @@ 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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue