diff --git a/lib/vonbraun/inbox_router.ex b/lib/vonbraun/inbox_router.ex index c443e16..50cda23 100644 --- a/lib/vonbraun/inbox_router.ex +++ b/lib/vonbraun/inbox_router.ex @@ -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 - diff --git a/lib/vonbraun/my_plug.ex b/lib/vonbraun/my_plug.ex deleted file mode 100644 index 92a185a..0000000 --- a/lib/vonbraun/my_plug.ex +++ /dev/null @@ -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 diff --git a/lib/vonbraun/router.ex b/lib/vonbraun/router.ex index 0987450..a82ed53 100644 --- a/lib/vonbraun/router.ex +++ b/lib/vonbraun/router.ex @@ -4,41 +4,133 @@ defmodule Vonbraun.MyRouter do alias Vonbraun.InboxRouter - plug :match - plug :dispatch + plug(:match) + 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: """ -{ - "@context": [ - "https://www.w3.org/ns/activitystreams", - "https://#{domain}/schemas/litepub-0.1.jsonld", + defp render_outbox_page(nickname, domain), + do: """ + { + "@context": [ + "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" - } - ], - "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", + "rel": "http://webfinger.net/rel/profile-page", + "type": "text/html", + "href": "https://#{domain}/users/#{nickname}" + }, { - "@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", - "type": "OrderedCollectionPage" -} -""" + ] + } + """ + + defp render_user(nickname, domain, name, summary, public_key), + 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 conn = Plug.Conn.fetch_query_params(conn) @@ -47,41 +139,26 @@ defmodule Vonbraun.MyRouter do 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}", - "aliases": [ - "https://#{domain}/users/#{nickname}" - ], - "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}" - } - ] -} -""" - + {: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, out) + send_resp(conn, 200, render_webfinger(nickname, domain)) else {:resource, resource} -> Logger.error("not a binary resource: #{resource}") - send_resp(conn, 404, "oops") + send_resp(conn, 404, "fuck off") + {:admin_user, false} -> Logger.error("Bad user: #{conn.params["resource"]}") - send_resp(conn, 404, "oops") + send_resp(conn, 404, "fuck off") end end - get "/users/:user" do nickname = Application.fetch_env!(:vonbraun, :nickname) @@ -90,52 +167,8 @@ defmodule Vonbraun.MyRouter do name = Application.fetch_env!(:vonbraun, :name) 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") - send_resp(conn, 200, out) + send_resp(conn, 200, render_user(nickname, domain, name, summary, "")) else send_resp(conn, 404, "not found") end @@ -147,43 +180,21 @@ 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 end get "/users/:user/followers" do -nickname = Application.fetch_env!(:vonbraun, :nickname) + 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 @@ -197,9 +208,9 @@ nickname = Application.fetch_env!(:vonbraun, :nickname) conn = Plug.Conn.fetch_query_params(conn) with {:user, ^nickname} <- {:user, user}, - {:page, "true"} <- {:page, conn.params["page"]} do - conn = Plug.Conn.put_resp_content_type(conn, "application/activity+json") - send_resp(conn, 200, render_outbox_page(nickname, domain)) + {:page, "true"} <- {:page, conn.params["page"]} do + conn = Plug.Conn.put_resp_content_type(conn, "application/activity+json") + send_resp(conn, 200, render_outbox_page(nickname, domain)) else {:user, _} -> send_resp(conn, 404, "fuck off") @@ -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")