postland/lib/activity_pub.ex
2024-11-26 18:04:08 -06:00

51 lines
1 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
body = Jason.encode!(activity)
headers =
Headers.signing_headers(
"POST",
inbox,
body,
sender_id,
private_key
)
Req.post(inbox, headers: headers, body: body)
end
def fetch_actor(actor_id) do
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