schema "instances" do
field(:host, :string)
field(:unreachable_since, :naive_datetime)
- field(:reachability_checked_at, :naive_datetime)
timestamps()
end
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
%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
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
--- /dev/null
+# 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
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
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
|> put_status(500)
|> json("error")
end
-
- defp set_requester_reachable(conn, _) do
- Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
- conn
- end
end
|> 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
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)
|> put_status(500)
|> text("Something went wrong")
end
-
- defp set_requester_reachable(conn, _) do
- Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
- conn
- end
end
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,
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,
]
)
- 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)
|> send_resp(500, "Error")
end
end
-
- defp set_requester_reachable(conn, _) do
- Pleroma.Web.ControllerHelper.set_requester_reachable(conn)
- conn
- end
end
create table(:instances) do
add :host, :string
add :unreachable_since, :naive_datetime
- add :reachability_checked_at, :naive_datetime
timestamps()
end