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,41 +4,133 @@ defmodule Vonbraun.MyRouter do
alias Vonbraun.InboxRouter alias Vonbraun.InboxRouter
plug :match plug(:match)
plug :dispatch plug(:dispatch)
defp render_outbox(nickname, domain),
do: """
{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://#{domain}/schemas/litepub-0.1.jsonld",
{
"@language": "und"
}
],
"first": "https://#{domain}/users/#{nickname}/outbox?page=true",
"id": "https://#{domain}/users/#{nickname}/outbox",
"type": "OrderedCollection"
}
"""
defp render_outbox(nickname, domain), do: """ defp render_outbox_page(nickname, domain),
{ do: """
"@context": [ {
"https://www.w3.org/ns/activitystreams", "@context": [
"https://#{domain}/schemas/litepub-0.1.jsonld", "https://www.w3.org/ns/activitystreams",
"https://#{domain}/schemas/litepub-0.1.jsonld",
{
"@language": "und"
}
],
"id": "https://#{domain}/users/#{nickname}/outbox?page=true",
"orderedItems": [
],
"partOf": "https://#{domain}/users/#{nickname}/outbox",
"type": "OrderedCollectionPage"
}
"""
defp render_webfinger(nickname, domain),
do: """
{
"subject": "acct:#{nickname}@#{domain}",
"aliases": [
"https://#{domain}/users/#{nickname}"
],
"links": [
{ {
"@language": "und" "rel": "http://webfinger.net/rel/profile-page",
} "type": "text/html",
], "href": "https://#{domain}/users/#{nickname}"
"first": "https://#{domain}/users/#{nickname}/outbox?page=true", },
"id": "https://#{domain}/users/#{nickname}/outbox",
"type": "OrderedCollection"
}
"""
defp render_outbox_page(nickname, domain), do: """
{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://#{domain}/schemas/litepub-0.1.jsonld",
{ {
"@language": "und" "rel": "self",
"type": "application/activity+json",
"href": "https://#{domain}/users/#{nickname}"
} }
], ]
"id": "https://#{domain}/users/#{nickname}/outbox?page=true", }
"orderedItems": [ """
],
"partOf": "https://#{domain}/users/#{nickname}/outbox", defp render_user(nickname, domain, name, summary, public_key),
"type": "OrderedCollectionPage" do: """
} {
""" "@context": [
"https://www.w3.org/ns/activitystreams",
"https://#{domain}/schemas/litepub-0.1.jsonld",
{
"@language": "und"
}
],
"alsoKnownAs": [],
"attachment": [],
"capabilities": {
"acceptsChatMessages": true
},
"discoverable": false,
"endpoints": {
"sharedInbox": "https://#{domain}/inbox"
},
"followers": "https://#{domain}/users/#{nickname}/followers",
"following": "https://#{domain}/users/#{nickname}/following",
"icon": {
"type": "Image",
"url": "https://#{domain}/icon.png"
},
"id": "https://#{domain}/users/#{nickname}",
"inbox": "https://#{domain}/users/#{nickname}/inbox",
"manuallyApprovesFollowers": false,
"name": "#{name}",
"outbox": "https://#{domain}/users/#{nickname}/outbox",
"preferredUsername": "#{nickname}",
"publicKey": {
"id": "https://#{domain}/users/#{nickname}#main-key",
"owner": "https://#{domain}/users/#{nickname}",
"publicKeyPem": "#{public_key}"
},
"summary": "#{summary}",
"tag": [],
"type": "Person",
"url": "https://#{domain}/users/#{nickname}",
"vcard:bday": null,
"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 get "/.well-known/webfinger" do
conn = Plug.Conn.fetch_query_params(conn) conn = Plug.Conn.fetch_query_params(conn)
@ -47,41 +139,26 @@ defmodule Vonbraun.MyRouter do
domain = Application.fetch_env!(:vonbraun, :domain) domain = Application.fetch_env!(:vonbraun, :domain)
with {:resource, resource} when is_binary(resource) <- {:resource, conn.params["resource"]}, 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 {:admin_user, true} <-
out = """ {:admin_user,
{ resource in [
"subject": "acct:#{nickname}@#{domain}", "acct:#{nickname}",
"aliases": [ "acct:#{nickname}@#{domain}",
"https://#{domain}/users/#{nickname}" "https://#{domain}/users/#{nickname}"
], ]} do
"links": [
{
"rel": "http://webfinger.net/rel/profile-page",
"type": "text/html",
"href": "https://#{domain}/users/#{nickname}"
},
{
"rel": "self",
"type": "application/activity+json",
"href": "https://#{domain}/users/#{nickname}"
}
]
}
"""
conn = Plug.Conn.put_resp_content_type(conn, "application/jrd+json") conn = Plug.Conn.put_resp_content_type(conn, "application/jrd+json")
send_resp(conn, 200, out) send_resp(conn, 200, render_webfinger(nickname, domain))
else else
{:resource, resource} -> {:resource, resource} ->
Logger.error("not a binary resource: #{resource}") Logger.error("not a binary resource: #{resource}")
send_resp(conn, 404, "oops") send_resp(conn, 404, "fuck off")
{:admin_user, false} -> {:admin_user, false} ->
Logger.error("Bad user: #{conn.params["resource"]}") Logger.error("Bad user: #{conn.params["resource"]}")
send_resp(conn, 404, "oops") send_resp(conn, 404, "fuck off")
end end
end end
get "/users/:user" do get "/users/:user" do
nickname = Application.fetch_env!(:vonbraun, :nickname) nickname = Application.fetch_env!(:vonbraun, :nickname)
@ -90,52 +167,8 @@ defmodule Vonbraun.MyRouter do
name = Application.fetch_env!(:vonbraun, :name) name = Application.fetch_env!(:vonbraun, :name)
summary = Application.fetch_env!(:vonbraun, :summary) summary = Application.fetch_env!(:vonbraun, :summary)
out = """
{
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://#{domain}/schemas/litepub-0.1.jsonld",
{
"@language": "und"
}
],
"alsoKnownAs": [],
"attachment": [],
"capabilities": {
"acceptsChatMessages": true
},
"discoverable": false,
"endpoints": {
"sharedInbox": "https://#{domain}/inbox"
},
"followers": "https://#{domain}/users/#{nickname}/followers",
"following": "https://#{domain}/users/#{nickname}/following",
"icon": {
"type": "Image",
"url": "https://#{domain}/icon.png"
},
"id": "https://#{domain}/users/#{nickname}",
"inbox": "https://#{domain}/users/#{nickname}/inbox",
"manuallyApprovesFollowers": false,
"name": "#{name}",
"outbox": "https://#{domain}/users/#{nickname}/outbox",
"preferredUsername": "#{nickname}",
"publicKey": {
"id": "https://#{domain}/users/#{nickname}#main-key",
"owner": "https://#{domain}/users/#{nickname}",
"publicKeyPem": ""
},
"summary": "#{summary}",
"tag": [],
"type": "Person",
"url": "https://#{domain}/users/#{nickname}",
"vcard:bday": null,
"webfinger": "acct:#{nickname}@#{domain}"
}
"""
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
@ -197,9 +208,9 @@ nickname = Application.fetch_env!(:vonbraun, :nickname)
conn = Plug.Conn.fetch_query_params(conn) conn = Plug.Conn.fetch_query_params(conn)
with {:user, ^nickname} <- {:user, user}, with {:user, ^nickname} <- {:user, user},
{:page, "true"} <- {:page, conn.params["page"]} do {:page, "true"} <- {:page, conn.params["page"]} do
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, render_outbox_page(nickname, domain)) send_resp(conn, 200, render_outbox_page(nickname, domain))
else else
{:user, _} -> {:user, _} ->
send_resp(conn, 404, "fuck off") send_resp(conn, 404, "fuck off")
@ -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")