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
require Logger
plug :match
plug :dispatch
plug(:match)
plug(:dispatch)
get "/" do
nickname = Application.fetch_env!(:vonbraun, :nickname)
@ -14,6 +14,4 @@ defmodule Vonbraun.InboxRouter do
send_resp(conn, 404, "fuck off")
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,11 +4,11 @@ defmodule Vonbraun.MyRouter do
alias Vonbraun.InboxRouter
plug :match
plug :dispatch
plug(:match)
plug(:dispatch)
defp render_outbox(nickname, domain), do: """
defp render_outbox(nickname, domain),
do: """
{
"@context": [
"https://www.w3.org/ns/activitystreams",
@ -23,7 +23,8 @@ defmodule Vonbraun.MyRouter do
}
"""
defp render_outbox_page(nickname, domain), do: """
defp render_outbox_page(nickname, domain),
do: """
{
"@context": [
"https://www.w3.org/ns/activitystreams",
@ -40,15 +41,8 @@ defmodule Vonbraun.MyRouter do
}
"""
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
out = """
defp render_webfinger(nickname, domain),
do: """
{
"subject": "acct:#{nickname}@#{domain}",
"aliases": [
@ -69,28 +63,8 @@ defmodule Vonbraun.MyRouter do
}
"""
conn = Plug.Conn.put_resp_content_type(conn, "application/jrd+json")
send_resp(conn, 200, out)
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 = """
defp render_user(nickname, domain, name, summary, public_key),
do: """
{
"@context": [
"https://www.w3.org/ns/activitystreams",
@ -123,7 +97,7 @@ defmodule Vonbraun.MyRouter do
"publicKey": {
"id": "https://#{domain}/users/#{nickname}#main-key",
"owner": "https://#{domain}/users/#{nickname}",
"publicKeyPem": ""
"publicKeyPem": "#{public_key}"
},
"summary": "#{summary}",
"tag": [],
@ -134,8 +108,67 @@ defmodule Vonbraun.MyRouter do
}
"""
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")
send_resp(conn, 200, out)
send_resp(conn, 200, render_user(nickname, domain, name, summary, ""))
else
send_resp(conn, 404, "not found")
end
@ -147,19 +180,8 @@ defmodule Vonbraun.MyRouter do
if conn.params["user"] == nickname do
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")
send_resp(conn, 200, out)
send_resp(conn, 200, render_following(nickname, domain))
else
send_resp(conn, 404, "not found")
end
@ -171,19 +193,8 @@ nickname = Application.fetch_env!(:vonbraun, :nickname)
if conn.params["user"] == nickname do
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")
send_resp(conn, 200, out)
send_resp(conn, 200, render_followers(nickname, domain))
else
send_resp(conn, 404, "not found")
end
@ -210,8 +221,8 @@ nickname = Application.fetch_env!(:vonbraun, :nickname)
end
end
forward "/inbox", to: InboxRouter
forward "/users/:user/inbox", to: InboxRouter
forward("/inbox", to: InboxRouter)
forward("/users/:user/inbox", to: InboxRouter)
match _ do
send_resp(conn, 404, "oops")