activitypub: factor out AP object fetching to it's own function and add ID-based containment
This commit is contained in:
parent
a960983815
commit
daa8ec3d62
|
@ -732,16 +732,7 @@ def fetch_object_from_id(id) do
|
||||||
else
|
else
|
||||||
Logger.info("Fetching #{id} via AP")
|
Logger.info("Fetching #{id} via AP")
|
||||||
|
|
||||||
with true <- String.starts_with?(id, "http"),
|
with {:ok, data} <- fetch_and_contain_remote_object_from_id(id),
|
||||||
{:ok, %{body: body, status_code: code}} when code in 200..299 <-
|
|
||||||
@httpoison.get(
|
|
||||||
id,
|
|
||||||
[Accept: "application/activity+json"],
|
|
||||||
follow_redirect: true,
|
|
||||||
timeout: 10000,
|
|
||||||
recv_timeout: 20000
|
|
||||||
),
|
|
||||||
{:ok, data} <- Jason.decode(body),
|
|
||||||
nil <- Object.normalize(data),
|
nil <- Object.normalize(data),
|
||||||
params <- %{
|
params <- %{
|
||||||
"type" => "Create",
|
"type" => "Create",
|
||||||
|
@ -771,6 +762,27 @@ def fetch_object_from_id(id) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fetch_and_contain_remote_object_from_id(id) do
|
||||||
|
Logger.info("Fetching #{id} via AP")
|
||||||
|
|
||||||
|
with true <- String.starts_with?(id, "http"),
|
||||||
|
{:ok, %{body: body, status_code: code}} when code in 200..299 <-
|
||||||
|
@httpoison.get(
|
||||||
|
id,
|
||||||
|
[Accept: "application/activity+json"],
|
||||||
|
follow_redirect: true,
|
||||||
|
timeout: 10000,
|
||||||
|
recv_timeout: 20000
|
||||||
|
),
|
||||||
|
{:ok, data} <- Jason.decode(body),
|
||||||
|
:ok <- Transmogrifier.contain_origin_from_id(id, data) do
|
||||||
|
{:ok, data}
|
||||||
|
else
|
||||||
|
e ->
|
||||||
|
{:error, e}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def is_public?(activity) do
|
def is_public?(activity) do
|
||||||
"https://www.w3.org/ns/activitystreams#Public" in (activity.data["to"] ++
|
"https://www.w3.org/ns/activitystreams#Public" in (activity.data["to"] ++
|
||||||
(activity.data["cc"] || []))
|
(activity.data["cc"] || []))
|
||||||
|
|
|
@ -50,6 +50,19 @@ def contain_origin(id, %{"actor" => actor} = params) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def contain_origin_from_id(id, %{"id" => nil}), do: :error
|
||||||
|
|
||||||
|
def contain_origin_from_id(id, %{"id" => other_id} = params) do
|
||||||
|
id_uri = URI.parse(id)
|
||||||
|
other_uri = URI.parse(other_id)
|
||||||
|
|
||||||
|
if id_uri.host == other_uri.host do
|
||||||
|
:ok
|
||||||
|
else
|
||||||
|
:error
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Modifies an incoming AP object (mastodon format) to our internal format.
|
Modifies an incoming AP object (mastodon format) to our internal format.
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue