Fediverse: /@user profiles, sturdier WebFinger, host-meta

dcfbf0f · gmorell · 2026-11-18 21:00

18 files +368 -37
Message
{commit_body(@commit)}

Files changed

modified lib/final_track/federation/objects.ex
+8 −4
@@ -29,6 +29,9 @@ defmodule FinalTrack.Federation.Objects do
29 29
30 30 def instance_actor_url, do: base() <> "/ap/actor"
31 31 def actor_url(%Account{username: username}), do: base() <> "/ap/users/" <> username
32 +
33 + @doc "The account's profile page, Mastodon-style: `/@username`."
34 + def profile_url(%Account{username: username}), do: base() <> "/@" <> username
32 35 def key_id(%Account{} = account), do: actor_url(account) <> "#main-key"
33 36 def followers_url(%Account{} = account), do: actor_url(account) <> "/followers"
34 37 def note_url(%Post{id: id}), do: base() <> "/ap/notes/" <> id
@@ -72,7 +75,7 @@ defmodule FinalTrack.Federation.Objects do
72 75 "name" => name,
73 76 "summary" =>
74 77 "<p>What #{escape(name)} is listening to, scrobbled to Final Track at #{escape(domain())}.</p>",
75 − "url" => base() <> "/",
78 + "url" => profile_url(account),
76 79 "inbox" => url <> "/inbox",
77 80 "outbox" => url <> "/outbox",
78 81 "followers" => url <> "/followers",
@@ -109,7 +112,8 @@ defmodule FinalTrack.Federation.Objects do
109 112 "cc" => [@public],
110 113 "sensitive" => false,
111 114 "content" => content(s),
112 − "url" => url,
115 + # Where "open original" goes: the profile, which shows the play.
116 + "url" => profile_url(account),
113 117 "attachment" => cover_attachment(s.album, s.track)
114 118 }
115 119 end
@@ -210,13 +214,13 @@ defmodule FinalTrack.Federation.Objects do
210 214 def webfinger(%Account{} = account) do
211 215 %{
212 216 "subject" => "acct:#{account.username}@#{domain()}",
213 − "aliases" => [actor_url(account)],
217 + "aliases" => [actor_url(account), profile_url(account)],
214 218 "links" => [
215 219 %{"rel" => "self", "type" => "application/activity+json", "href" => actor_url(account)},
216 220 %{
217 221 "rel" => "http://webfinger.net/rel/profile-page",
218 222 "type" => "text/html",
219 − "href" => base() <> "/"
223 + "href" => profile_url(account)
220 224 }
221 225 ]
222 226 }
modified lib/final_track_web/controllers/federation_controller.ex
+77 −30
@@ -6,7 +6,7 @@ defmodule FinalTrackWeb.FederationController do
6 6
7 7 Actor and Note URLs are also what a person may click in their fediverse
8 8 client; a browser (anything not asking for ActivityPub JSON) is sent to
9 − the landing grid instead.
9 + the account's `/@user` profile instead (`FinalTrackWeb.ProfileController`).
10 10 """
11 11 use FinalTrackWeb, :controller
12 12
@@ -16,26 +16,14 @@ defmodule FinalTrackWeb.FederationController do
16 16
17 17 ## Discovery
18 18
19 − def webfinger(conn, %{"resource" => resource}) do
20 − domain = Objects.domain()
21 −
22 − found =
23 − case resource do
24 − "acct:" <> acct ->
25 − case String.split(acct, "@", parts: 2) do
26 − [^domain, ^domain] -> Objects.instance_webfinger()
27 − [user, ^domain] -> account_jrd(Federation.get_live_account(user))
28 − _ -> nil
29 − end
30 −
31 − url ->
32 − case String.split(url, Objects.base() <> "/ap/users/", parts: 2) do
33 − ["", user] -> account_jrd(Federation.get_live_account(user))
34 − _ -> nil
35 − end
36 − end
37 −
38 − case found do
19 + @doc """
20 + WebFinger (RFC 7033). The resource may be `acct:user@host` (with or
21 + without a leading `@` on the user, as people type handles), the account's
22 + actor URL or its `/@user` profile URL, or `acct:host@host` for the
23 + instance actor. Case-insensitive. Unknown or foreign resources are 404.
24 + """
25 + def webfinger(conn, %{"resource" => resource}) when is_binary(resource) do
26 + case resolve(String.trim(resource)) do
39 27 nil ->
40 28 send_resp(conn, 404, "")
41 29
@@ -49,9 +37,62 @@ defmodule FinalTrackWeb.FederationController do
49 37
50 38 def webfinger(conn, _params), do: send_resp(conn, 400, "")
51 39
40 + defp resolve("acct:" <> acct) do
41 + domain = String.downcase(Objects.domain())
42 +
43 + case acct |> String.trim_leading("@") |> String.downcase() |> String.split("@", parts: 2) do
44 + [^domain, ^domain] -> Objects.instance_webfinger()
45 + [user, ^domain] -> account_jrd(Federation.get_live_account(user))
46 + _ -> nil
47 + end
48 + end
49 +
50 + defp resolve(url) do
51 + base = String.downcase(Objects.base())
52 +
53 + cond do
54 + String.downcase(url) == String.downcase(Objects.instance_actor_url()) ->
55 + Objects.instance_webfinger()
56 +
57 + String.starts_with?(String.downcase(url), base <> "/ap/users/") ->
58 + url |> String.slice(String.length(base <> "/ap/users/")..-1//1) |> account_by_path()
59 +
60 + String.starts_with?(String.downcase(url), base <> "/@") ->
61 + url |> String.slice(String.length(base <> "/@")..-1//1) |> account_by_path()
62 +
63 + true ->
64 + nil
65 + end
66 + end
67 +
68 + defp account_by_path(rest) do
69 + case String.split(rest, ["/", "?", "#"], parts: 2) do
70 + [user | _] when user != "" -> account_jrd(Federation.get_live_account(user))
71 + _ -> nil
72 + end
73 + end
74 +
52 75 defp account_jrd(nil), do: nil
53 76 defp account_jrd(account), do: Objects.webfinger(account)
54 77
78 + @doc """
79 + `/.well-known/host-meta` (RFC 6415): where WebFinger lives, for clients
80 + that look here first.
81 + """
82 + def host_meta(conn, _params) do
83 + xml = """
84 + <?xml version="1.0" encoding="UTF-8"?>
85 + <XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
86 + <Link rel="lrdd" template="#{Objects.base()}/.well-known/webfinger?resource={uri}"/>
87 + </XRD>
88 + """
89 +
90 + conn
91 + |> put_resp_content_type("application/xrd+xml")
92 + |> put_resp_header("access-control-allow-origin", "*")
93 + |> send_resp(200, xml)
94 + end
95 +
55 96 def nodeinfo_links(conn, _params), do: json(conn, Objects.nodeinfo_links())
56 97
57 98 def nodeinfo(conn, _params) do
@@ -68,7 +109,7 @@ defmodule FinalTrackWeb.FederationController do
68 109 do: ap(conn, Objects.instance_actor(InstanceActor.ensure!()))
69 110
70 111 def actor(conn, %{"username" => username}) do
71 − with_account(conn, username, &ap(conn, Objects.person(&1)))
112 + with_account(conn, username, &ap(conn, Objects.person(&1), Objects.profile_url(&1)))
72 113 end
73 114
74 115 def outbox(conn, %{"username" => username}) do
@@ -86,7 +127,7 @@ defmodule FinalTrackWeb.FederationController do
86 127
87 128 def note(conn, %{"id" => id}) do
88 129 case Federation.get_post(id) do
89 − {:ok, post} -> ap(conn, Objects.note(post, post.account))
130 + {:ok, post} -> ap(conn, Objects.note(post, post.account), Objects.profile_url(post.account))
90 131 # Was posted, mayn't be shown now (made private, account off).
91 132 {:error, :gone} -> send_resp(conn, 410, "")
92 133 {:error, :not_found} -> send_resp(conn, 404, "")
@@ -100,13 +141,19 @@ defmodule FinalTrackWeb.FederationController do
100 141 end
101 142 end
102 143
103 − defp ap(conn, doc) do
104 − if wants_activity_json?(conn) do
105 − conn
106 − |> put_resp_content_type("application/activity+json")
107 − |> send_resp(200, Jason.encode_to_iodata!(doc))
108 − else
109 − redirect(conn, to: ~p"/")
144 + # A browser gets `html_to` (the account's profile, or the landing grid).
145 + defp ap(conn, doc, html_to \\ nil) do
146 + cond do
147 + wants_activity_json?(conn) ->
148 + conn
149 + |> put_resp_content_type("application/activity+json")
150 + |> send_resp(200, Jason.encode_to_iodata!(doc))
151 +
152 + html_to ->
153 + redirect(conn, external: html_to)
154 +
155 + true ->
156 + redirect(conn, to: ~p"/")
110 157 end
111 158 end
112 159
added lib/final_track_web/controllers/profile_controller.ex
+56 −0
@@ -0,0 +1,56 @@
1 +defmodule FinalTrackWeb.ProfileController do
2 + @moduledoc """
3 + `/@username`: a fediverse account's profile URL, Mastodon-style.
4 +
5 + One URL, two audiences. A server asking for ActivityPub JSON (someone
6 + pasted the URL into their search box) gets the actor document; a browser
7 + gets a public profile page with the account's recent plays — only the
8 + ones that aren't private, the same plays its followers see.
9 +
10 + Exists only while federation is on and the account is switched on.
11 + """
12 + use FinalTrackWeb, :controller
13 +
14 + alias FinalTrack.{Charts, Federation, Scrobbles}
15 + alias FinalTrack.Federation.Objects
16 +
17 + def show(conn, %{"username" => username}) do
18 + case Federation.get_live_account(username) do
19 + nil ->
20 + if activity_json?(conn) do
21 + send_resp(conn, 404, "")
22 + else
23 + # No :accepts in this pipeline, so the format is set here.
24 + conn
25 + |> put_format("html")
26 + |> put_status(:not_found)
27 + |> put_view(html: FinalTrackWeb.ErrorHTML)
28 + |> render(:"404")
29 + end
30 +
31 + account ->
32 + if activity_json?(conn) do
33 + conn
34 + |> put_resp_content_type("application/activity+json")
35 + |> send_resp(200, Jason.encode_to_iodata!(Objects.person(account)))
36 + else
37 + conn
38 + |> put_format("html")
39 + |> assign(:page_title, "@#{account.username}")
40 + |> render(:show,
41 + account: account,
42 + handle: "@#{account.username}@#{Objects.domain()}",
43 + name: FinalTrack.Accounts.User.display_name(account.user),
44 + followers: Federation.follower_count(account),
45 + plays: Scrobbles.recent(Charts.audience(nil, account.user), 24)
46 + )
47 + end
48 + end
49 + end
50 +
51 + defp activity_json?(conn) do
52 + conn
53 + |> get_req_header("accept")
54 + |> Enum.any?(&(&1 =~ "activity+json" or &1 =~ "ld+json"))
55 + end
56 +end
added lib/final_track_web/controllers/profile_html.ex
+5 −0
@@ -0,0 +1,5 @@
1 +defmodule FinalTrackWeb.ProfileHTML do
2 + use FinalTrackWeb, :html
3 +
4 + embed_templates "profile_html/*"
5 +end
added lib/final_track_web/controllers/profile_html/show.html.heex
+79 −0
@@ -0,0 +1,79 @@
1 +<Layouts.app flash={@flash} current_scope={@current_scope} wide>
2 + <div class="mx-auto max-w-5xl space-y-8 px-4 py-10 sm:px-6 lg:px-8">
3 + <header id="profile" class="flex flex-col gap-4 sm:flex-row sm:items-end sm:justify-between">
4 + <div class="flex items-center gap-4">
5 + <span class="flex size-16 shrink-0 items-center justify-center rounded-box bg-primary font-mono text-2xl font-bold text-primary-content shadow-md">
6 + {@name |> String.first() |> String.upcase()}
7 + </span>
8 + <div class="min-w-0">
9 + <h1 id="profile-name" class="truncate text-3xl font-bold tracking-tight">{@name}</h1>
10 + <p
11 + id="profile-handle"
12 + class="truncate font-mono text-sm text-base-content/70 select-all"
13 + >
14 + {@handle}
15 + </p>
16 + </div>
17 + </div>
18 + <div class="text-sm text-base-content/70 sm:text-right">
19 + <p>
20 + <span id="profile-followers" class="font-mono">{@followers}</span>
21 + {if @followers == 1, do: "follower", else: "followers"}
22 + </p>
23 + <p class="text-xs">
24 + Follow by searching <span class="font-mono">{@handle}</span> on your server.
25 + </p>
26 + </div>
27 + </header>
28 +
29 + <section>
30 + <h2 class="mb-3 flex items-center gap-2 font-semibold">
31 + <.icon name="hero-musical-note-micro" class="size-4 text-accent" /> Lately
32 + </h2>
33 + <p
34 + :if={@plays == []}
35 + id="profile-empty"
36 + class="rounded-box border border-dashed border-base-300 py-16 text-center text-base-content/60"
37 + >
38 + Nothing played yet.
39 + </p>
40 + <div
41 + :if={@plays != []}
42 + id="profile-plays"
43 + class="grid grid-cols-2 gap-3 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6"
44 + >
45 + <div
46 + :for={play <- @plays}
47 + id={"play-#{play.id}"}
48 + class="group relative aspect-square overflow-hidden rounded-box border border-base-300 bg-base-300 shadow-sm transition duration-300 hover:-translate-y-0.5 hover:shadow-xl"
49 + >
50 + <div class="absolute inset-0 transition-transform duration-500 group-hover:scale-105">
51 + <.cover album={play.album} class="size-full rounded-none! border-0!" />
52 + </div>
53 + <div class="absolute inset-0 bg-gradient-to-t from-black/85 via-black/20 to-transparent" />
54 + <span class="absolute left-2 top-2 rounded-full bg-black/50 px-2 py-0.5 font-mono text-[10px] text-white/90 backdrop-blur-sm">
55 + {ago(play.played_at)}
56 + </span>
57 + <div class="absolute inset-x-0 bottom-0 p-3 text-white">
58 + <p class="truncate text-sm font-semibold leading-tight drop-shadow">
59 + {play.track.title}
60 + </p>
61 + <p class="truncate text-xs text-white/80">{play.track.artist.name}</p>
62 + <p
63 + :if={play.album}
64 + class="max-h-0 truncate text-xs text-white/60 opacity-0 transition-all duration-300 group-hover:max-h-5 group-hover:opacity-100"
65 + >
66 + {play.album.title}
67 + </p>
68 + </div>
69 + </div>
70 + </div>
71 + </section>
72 +
73 + <footer class="text-center">
74 + <.link navigate={~p"/about"} class="text-xs text-base-content/60 hover:text-base-content">
75 + What is Final Track?
76 + </.link>
77 + </footer>
78 + </div>
79 +</Layouts.app>
modified lib/final_track_web/router.ex
+19 −0
@@ -41,10 +41,29 @@ defmodule FinalTrackWeb.Router do
41 41 plug FinalTrackWeb.Plugs.RequireFederation
42 42 end
43 43
44 + # `/@username`: what browsers and fediverse servers both ask for, so no
45 + # :accepts (an activity+json request would be a 406), but the session
46 + # and layout, for the profile page. 404 while federation is off.
47 + pipeline :profile do
48 + plug :fetch_session
49 + plug :fetch_live_flash
50 + plug :put_root_layout, html: {FinalTrackWeb.Layouts, :root}
51 + plug :put_secure_browser_headers
52 + plug :fetch_current_scope_for_user
53 + plug FinalTrackWeb.Plugs.RequireFederation
54 + end
55 +
56 + scope "/", FinalTrackWeb do
57 + pipe_through :profile
58 +
59 + get "/@:username", ProfileController, :show
60 + end
61 +
44 62 scope "/", FinalTrackWeb do
45 63 pipe_through :federation
46 64
47 65 get "/.well-known/webfinger", FederationController, :webfinger
66 + get "/.well-known/host-meta", FederationController, :host_meta
48 67 get "/.well-known/nodeinfo", FederationController, :nodeinfo_links
49 68 get "/nodeinfo/2.0", FederationController, :nodeinfo
50 69
modified test/final_track_web/controllers/federation_controller_test.exs
+54 −2
@@ -66,6 +66,7 @@ defmodule FinalTrackWeb.FederationControllerTest do
66 66 doc = conn |> ap() |> get(~p"/ap/users/alice") |> json_response(200)
67 67
68 68 assert %{"id" => ^actor_url, "type" => "Service", "preferredUsername" => "alice"} = doc
69 + assert doc["url"] == FinalTrackWeb.base_url() <> "/@alice"
69 70
70 71 assert doc["publicKey"] == %{
71 72 "id" => actor_url <> "#main-key",
@@ -77,8 +78,59 @@ defmodule FinalTrackWeb.FederationControllerTest do
77 78 refute Map.has_key?(doc, "privateKeyPem")
78 79 end
79 80
80 − test "a browser is sent to the landing grid", %{conn: conn} do
81 − assert redirected_to(get(conn, ~p"/ap/users/alice")) == ~p"/"
81 + test "a browser on the actor URL is sent to the profile", %{conn: conn} do
82 + assert redirected_to(get(conn, ~p"/ap/users/alice")) ==
83 + FinalTrackWeb.base_url() <> "/@alice"
84 + end
85 +
86 + test "webfinger takes every way people and servers write it", %{
87 + conn: conn,
88 + actor_url: actor_url
89 + } do
90 + base = FinalTrackWeb.base_url()
91 +
92 + for resource <- [
93 + "acct:alice@localhost",
94 + "acct:@alice@localhost",
95 + "acct:ALICE@LocalHost",
96 + " acct:alice@localhost ",
97 + actor_url,
98 + base <> "/@alice",
99 + base <> "/@Alice"
100 + ] do
101 + conn = get(conn, ~p"/.well-known/webfinger?#{[resource: resource]}")
102 +
103 + assert %{"subject" => "acct:alice@localhost", "aliases" => aliases, "links" => links} =
104 + json_response(conn, 200),
105 + resource
106 +
107 + assert get_resp_header(conn, "content-type") |> hd() =~ "application/jrd+json"
108 + assert (base <> "/@alice") in aliases
109 +
110 + assert %{"href" => profile} =
111 + Enum.find(links, &(&1["rel"] == "http://webfinger.net/rel/profile-page"))
112 +
113 + assert profile == base <> "/@alice"
114 + end
115 +
116 + for resource <- [
117 + "acct:alice@elsewhere.example",
118 + "https://elsewhere.example/@alice",
119 + base <> "/@",
120 + "alice",
121 + "acct:"
122 + ] do
123 + assert response(get(conn, ~p"/.well-known/webfinger?#{[resource: resource]}"), 404),
124 + resource
125 + end
126 + end
127 +
128 + test "host-meta points at webfinger", %{conn: conn} do
129 + conn = get(conn, ~p"/.well-known/host-meta")
130 + body = response(conn, 200)
131 + assert get_resp_header(conn, "content-type") |> hd() =~ "application/xrd+xml"
132 + assert body =~ ~s(rel="lrdd")
133 + assert body =~ FinalTrackWeb.base_url() <> "/.well-known/webfinger?resource={uri}"
82 134 end
83 135
84 136 test "the instance actor", %{conn: conn} do
modified test/final_track_web/controllers/federation_off_test.exs
+3 −1
@@ -32,7 +32,9 @@ defmodule FinalTrackWeb.FederationOffTest do
32 32 "/nodeinfo/2.0",
33 33 "/ap/actor",
34 34 "/ap/users/alice",
35 − "/ap/users/alice/outbox"
35 + "/ap/users/alice/outbox",
36 + "/.well-known/host-meta",
37 + "/@alice"
36 38 ] do
37 39 assert response(get(conn, path), 404), path
38 40 end
added test/final_track_web/controllers/profile_controller_test.exs
+67 −0
@@ -0,0 +1,67 @@
1 +defmodule FinalTrackWeb.ProfileControllerTest do
2 + use FinalTrackWeb.ConnCase, async: true
3 +
4 + import FinalTrack.AccountsFixtures
5 + import FinalTrack.ScrobblesFixtures
6 + import FinalTrack.FederationHelpers
7 +
8 + alias FinalTrack.{Catalog, Federation, Privacy}
9 + alias FinalTrack.Federation.Objects
10 +
11 + setup do
12 + user = user_fixture()
13 + {:ok, user} = FinalTrack.Accounts.update_user_profile(user, %{display_name: "Alice"})
14 + account = fedi_account(user)
15 + play_fixture(user)
16 + play_fixture(user, %{"artist" => "Air", "track" => "Sexy Boy", "album" => "Moon Safari"})
17 + %{user: user, account: account}
18 + end
19 +
20 + test "a browser gets the profile page, with the recent plays", %{conn: conn} do
21 + doc = conn |> get("/@alice") |> html_response(200) |> LazyHTML.from_document()
22 +
23 + assert LazyHTML.query(doc, "#profile-name") |> LazyHTML.text() =~ "Alice"
24 + assert LazyHTML.query(doc, "#profile-handle") |> LazyHTML.text() =~ "@alice@localhost"
25 + plays = LazyHTML.query(doc, "#profile-plays") |> LazyHTML.text()
26 + assert plays =~ "Time"
27 + assert plays =~ "Sexy Boy"
28 + end
29 +
30 + test "the username is case-insensitive", %{conn: conn} do
31 + assert html_response(get(conn, "/@Alice"), 200)
32 + end
33 +
34 + test "a fediverse server gets the actor document", %{conn: conn, account: account} do
35 + doc =
36 + conn
37 + |> put_req_header(
38 + "accept",
39 + ~s(application/ld+json; profile="https://www.w3.org/ns/activitystreams")
40 + )
41 + |> get("/@alice")
42 + |> json_response(200)
43 +
44 + assert doc["id"] == Objects.actor_url(account)
45 + assert doc["url"] == Objects.profile_url(account)
46 + assert doc["publicKey"]["publicKeyPem"] == account.public_key_pem
47 + end
48 +
49 + test "private plays aren't on it", %{conn: conn, user: user} do
50 + :ok = Privacy.mark(user, Catalog.ensure_artist!("Air"))
51 + html = conn |> get("/@alice") |> html_response(200)
52 + assert html =~ "Time"
53 + refute html =~ "Sexy Boy"
54 + end
55 +
56 + test "no such account, or switched off: 404", %{conn: conn, user: user} do
57 + assert html_response(get(conn, "/@nobody"), 404)
58 +
59 + assert response(
60 + conn |> put_req_header("accept", "application/activity+json") |> get("/@nobody"),
61 + 404
62 + )
63 +
64 + {:ok, _} = Federation.save_account(user, %{enabled: false})
65 + assert html_response(get(conn, "/@alice"), 404)
66 + end
67 +end

Parents: 4e3d37c