minor cleanup

This commit is contained in:
Moon Man 2024-08-24 09:19:17 +00:00
parent 5490383972
commit eb5816c2f4
3 changed files with 144 additions and 149 deletions

View File

@ -2,8 +2,8 @@ defmodule Vonbraun.InboxRouter do
use Plug.Router use Plug.Router
require Logger require Logger
plug :match plug(:match)
plug :dispatch plug(:dispatch)
get "/" do get "/" do
nickname = Application.fetch_env!(:vonbraun, :nickname) nickname = Application.fetch_env!(:vonbraun, :nickname)
@ -14,6 +14,4 @@ defmodule Vonbraun.InboxRouter do
send_resp(conn, 404, "fuck off") send_resp(conn, 404, "fuck off")
end end
end end
end end

View File

@ -1,14 +0,0 @@
defmodule Vonbraun.MyPlug do
import Plug.Conn
def init(options) do
# initialize options
options
end
def call(conn, _opts) do
conn
|> put_resp_content_type("text/plain")
|> send_resp(200, "Hello world")
end
end

View File

@ -4,12 +4,12 @@ defmodule Vonbraun.MyRouter do
alias Vonbraun.InboxRouter alias Vonbraun.InboxRouter
plug :match plug(:match)
plug :dispatch plug(:dispatch)
defp render_outbox(nickname, domain),
defp render_outbox(nickname, domain), do: """ do: """
{ {
"@context": [ "@context": [
"https://www.w3.org/ns/activitystreams", "https://www.w3.org/ns/activitystreams",
"https://#{domain}/schemas/litepub-0.1.jsonld", "https://#{domain}/schemas/litepub-0.1.jsonld",
@ -20,11 +20,12 @@ defmodule Vonbraun.MyRouter do
"first": "https://#{domain}/users/#{nickname}/outbox?page=true", "first": "https://#{domain}/users/#{nickname}/outbox?page=true",
"id": "https://#{domain}/users/#{nickname}/outbox", "id": "https://#{domain}/users/#{nickname}/outbox",
"type": "OrderedCollection" "type": "OrderedCollection"
} }
""" """
defp render_outbox_page(nickname, domain), do: """ defp render_outbox_page(nickname, domain),
{ do: """
{
"@context": [ "@context": [
"https://www.w3.org/ns/activitystreams", "https://www.w3.org/ns/activitystreams",
"https://#{domain}/schemas/litepub-0.1.jsonld", "https://#{domain}/schemas/litepub-0.1.jsonld",
@ -37,19 +38,12 @@ defmodule Vonbraun.MyRouter do
], ],
"partOf": "https://#{domain}/users/#{nickname}/outbox", "partOf": "https://#{domain}/users/#{nickname}/outbox",
"type": "OrderedCollectionPage" "type": "OrderedCollectionPage"
} }
""" """
get "/.well-known/webfinger" do defp render_webfinger(nickname, domain),
conn = Plug.Conn.fetch_query_params(conn) do: """
{
nickname = Application.fetch_env!(:vonbraun, :nickname)
domain = Application.fetch_env!(:vonbraun, :domain)
with {:resource, resource} when is_binary(resource) <- {:resource, conn.params["resource"]},
{:admin_user, true} <- {:admin_user, resource in ["acct:#{nickname}", "acct:#{nickname}@#{domain}", "https://#{domain}/users/#{nickname}"]} do
out = """
{
"subject": "acct:#{nickname}@#{domain}", "subject": "acct:#{nickname}@#{domain}",
"aliases": [ "aliases": [
"https://#{domain}/users/#{nickname}" "https://#{domain}/users/#{nickname}"
@ -66,32 +60,12 @@ defmodule Vonbraun.MyRouter do
"href": "https://#{domain}/users/#{nickname}" "href": "https://#{domain}/users/#{nickname}"
} }
] ]
} }
""" """
conn = Plug.Conn.put_resp_content_type(conn, "application/jrd+json") defp render_user(nickname, domain, name, summary, public_key),
send_resp(conn, 200, out) do: """
else {
{:resource, resource} ->
Logger.error("not a binary resource: #{resource}")
send_resp(conn, 404, "oops")
{:admin_user, false} ->
Logger.error("Bad user: #{conn.params["resource"]}")
send_resp(conn, 404, "oops")
end
end
get "/users/:user" do
nickname = Application.fetch_env!(:vonbraun, :nickname)
if conn.params["user"] == nickname do
domain = Application.fetch_env!(:vonbraun, :domain)
name = Application.fetch_env!(:vonbraun, :name)
summary = Application.fetch_env!(:vonbraun, :summary)
out = """
{
"@context": [ "@context": [
"https://www.w3.org/ns/activitystreams", "https://www.w3.org/ns/activitystreams",
"https://#{domain}/schemas/litepub-0.1.jsonld", "https://#{domain}/schemas/litepub-0.1.jsonld",
@ -123,7 +97,7 @@ defmodule Vonbraun.MyRouter do
"publicKey": { "publicKey": {
"id": "https://#{domain}/users/#{nickname}#main-key", "id": "https://#{domain}/users/#{nickname}#main-key",
"owner": "https://#{domain}/users/#{nickname}", "owner": "https://#{domain}/users/#{nickname}",
"publicKeyPem": "" "publicKeyPem": "#{public_key}"
}, },
"summary": "#{summary}", "summary": "#{summary}",
"tag": [], "tag": [],
@ -131,11 +105,70 @@ defmodule Vonbraun.MyRouter do
"url": "https://#{domain}/users/#{nickname}", "url": "https://#{domain}/users/#{nickname}",
"vcard:bday": null, "vcard:bday": null,
"webfinger": "acct:#{nickname}@#{domain}" "webfinger": "acct:#{nickname}@#{domain}"
} }
""" """
defp render_following(nickname, domain),
do: """
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://#{domain}/users/#{nickname}/following",
"type": "OrderedCollection",
"totalItems": 0,
"orderedItems": [
]
}
"""
defp render_followers(nickname, domain),
do: """
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://#{domain}/users/#{nickname}/followers",
"type": "OrderedCollection",
"totalItems": 0,
"orderedItems": [
]
}
"""
get "/.well-known/webfinger" do
conn = Plug.Conn.fetch_query_params(conn)
nickname = Application.fetch_env!(:vonbraun, :nickname)
domain = Application.fetch_env!(:vonbraun, :domain)
with {:resource, resource} when is_binary(resource) <- {:resource, conn.params["resource"]},
{:admin_user, true} <-
{:admin_user,
resource in [
"acct:#{nickname}",
"acct:#{nickname}@#{domain}",
"https://#{domain}/users/#{nickname}"
]} do
conn = Plug.Conn.put_resp_content_type(conn, "application/jrd+json")
send_resp(conn, 200, render_webfinger(nickname, domain))
else
{:resource, resource} ->
Logger.error("not a binary resource: #{resource}")
send_resp(conn, 404, "fuck off")
{:admin_user, false} ->
Logger.error("Bad user: #{conn.params["resource"]}")
send_resp(conn, 404, "fuck off")
end
end
get "/users/:user" do
nickname = Application.fetch_env!(:vonbraun, :nickname)
if conn.params["user"] == nickname do
domain = Application.fetch_env!(:vonbraun, :domain)
name = Application.fetch_env!(:vonbraun, :name)
summary = Application.fetch_env!(:vonbraun, :summary)
conn = Plug.Conn.put_resp_content_type(conn, "application/activity+json") conn = Plug.Conn.put_resp_content_type(conn, "application/activity+json")
send_resp(conn, 200, out) send_resp(conn, 200, render_user(nickname, domain, name, summary, ""))
else else
send_resp(conn, 404, "not found") send_resp(conn, 404, "not found")
end end
@ -147,43 +180,21 @@ defmodule Vonbraun.MyRouter do
if conn.params["user"] == nickname do if conn.params["user"] == nickname do
domain = Application.fetch_env!(:vonbraun, :domain) domain = Application.fetch_env!(:vonbraun, :domain)
out = """
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://#{domain}/users/#{nickname}/following",
"type": "OrderedCollection",
"totalItems": 0,
"orderedItems": [
]
}
"""
conn = Plug.Conn.put_resp_content_type(conn, "application/activity+json") conn = Plug.Conn.put_resp_content_type(conn, "application/activity+json")
send_resp(conn, 200, out) send_resp(conn, 200, render_following(nickname, domain))
else else
send_resp(conn, 404, "not found") send_resp(conn, 404, "not found")
end end
end end
get "/users/:user/followers" do get "/users/:user/followers" do
nickname = Application.fetch_env!(:vonbraun, :nickname) nickname = Application.fetch_env!(:vonbraun, :nickname)
if conn.params["user"] == nickname do if conn.params["user"] == nickname do
domain = Application.fetch_env!(:vonbraun, :domain) domain = Application.fetch_env!(:vonbraun, :domain)
out = """
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://#{domain}/users/#{nickname}/followers",
"type": "OrderedCollection",
"totalItems": 0,
"orderedItems": [
]
}
"""
conn = Plug.Conn.put_resp_content_type(conn, "application/activity+json") conn = Plug.Conn.put_resp_content_type(conn, "application/activity+json")
send_resp(conn, 200, out) send_resp(conn, 200, render_followers(nickname, domain))
else else
send_resp(conn, 404, "not found") send_resp(conn, 404, "not found")
end end
@ -210,8 +221,8 @@ nickname = Application.fetch_env!(:vonbraun, :nickname)
end end
end end
forward "/inbox", to: InboxRouter forward("/inbox", to: InboxRouter)
forward "/users/:user/inbox", to: InboxRouter forward("/users/:user/inbox", to: InboxRouter)
match _ do match _ do
send_resp(conn, 404, "oops") send_resp(conn, 404, "oops")