70 lines
1.6 KiB
Elixir
70 lines
1.6 KiB
Elixir
defmodule ActivityPub do
|
|
alias ActivityPub.Headers
|
|
|
|
def get(url, actor_url, private_key) do
|
|
headers = Headers.signing_headers("GET", url, "", actor_url, private_key)
|
|
|
|
Req.new(url: url)
|
|
|> Req.Request.put_header("accept", "application/activity+json")
|
|
|> Req.Request.put_headers(headers)
|
|
|> Req.get()
|
|
|> case do
|
|
{:ok, response} ->
|
|
{:ok, response.body}
|
|
|
|
other ->
|
|
other
|
|
end
|
|
end
|
|
|
|
def post_activity(sender_id, private_key, inbox, activity) do
|
|
if String.contains?(inbox, "example.com") do
|
|
{:ok, %{status: 200, body: "ok"}}
|
|
else
|
|
body = Jason.encode!(activity)
|
|
|
|
headers =
|
|
Headers.signing_headers(
|
|
"POST",
|
|
inbox,
|
|
body,
|
|
sender_id,
|
|
private_key
|
|
)
|
|
|
|
Req.post(inbox, headers: headers, body: body)
|
|
end
|
|
end
|
|
|
|
def fetch_actor(actor_id) do
|
|
if String.contains?(actor_id, "example.com") do
|
|
{:ok,
|
|
%{
|
|
"@context" => [
|
|
"https://www.w3.org/ns/activitystreams",
|
|
"https://w3id.org/security/v1"
|
|
],
|
|
"id" => actor_id,
|
|
"type" => "Person",
|
|
"preferredUsername" => "example",
|
|
"name" => "Example User",
|
|
"inbox" => "#{actor_id}/inbox"
|
|
}}
|
|
else
|
|
request =
|
|
Req.new(url: actor_id)
|
|
|> Req.Request.put_header("accept", "application/json")
|
|
|
|
case Req.get(request) do
|
|
{:ok, %{status: 200} = result} ->
|
|
{:ok, result.body}
|
|
|
|
{:ok, %{status: 404}} ->
|
|
nil
|
|
|
|
error ->
|
|
error
|
|
end
|
|
end
|
|
end
|
|
end
|