Fediverse: public or followers-only posts per account; unique note urls

64c470b · gmorell · 2026-11-18 21:18

20 files +126 -32
Message
{commit_body(@commit)}

Files changed

modified lib/final_track/federation/account.ex
+16 −1
@@ -7,12 +7,26 @@ defmodule FinalTrack.Federation.Account do
7 7 use FinalTrack.Schema
8 8 import Ecto.Changeset
9 9
10 + @visibilities %{
11 + "unlisted" => "Followers' feeds only",
12 + "public" => "Public: also my profile page and public timelines"
13 + }
14 +
15 + @doc """
16 + How posts can be addressed, with labels. Unlisted keeps them off public
17 + timelines (and, on Pixelfed and angler, off the profile page as others
18 + see it); public puts them everywhere.
19 + """
20 + def visibilities, do: @visibilities
21 +
10 22 @reserved ~w(admin administrator root system support help info api www mail abuse
11 23 postmaster webmaster security noreply instance relay inbox outbox)
12 24
13 25 schema "fediverse_accounts" do
14 26 field :username, :string
15 27 field :enabled, :boolean, default: false
28 + # "unlisted" or "public": see `visibilities/0`.
29 + field :visibility, :string, default: "unlisted"
16 30 field :public_key_pem, :string
17 31 field :private_key_pem, :string, redact: true
18 32
@@ -30,7 +44,8 @@ defmodule FinalTrack.Federation.Account do
30 44 @doc "The same checks without making keys: for a form as it's typed into."
31 45 def validation_changeset(account, attrs) do
32 46 account
33 − |> cast(attrs, [:username, :enabled])
47 + |> cast(attrs, [:username, :enabled, :visibility])
48 + |> validate_inclusion(:visibility, Map.keys(@visibilities))
34 49 |> update_change(
35 50 :username,
36 51 &(&1 |> String.trim() |> String.trim_leading("@") |> String.downcase())
modified lib/final_track/federation/objects.ex
+18 −8
@@ -11,8 +11,9 @@ defmodule FinalTrack.Federation.Objects do
11 11 * `/ap/inbox` the shared inbox
12 12 * `/ap/notes/:id` a posted play
13 13
14 − Notes are unlisted (followers' timelines, not public ones): an account
15 − posts every play, which would flood local and federated timelines.
14 + Notes are unlisted by default (followers' timelines, not public ones): an
15 + account posts every play, which would flood local and federated
16 + timelines. An account can choose public instead (`Account.visibilities/0`).
16 17 """
17 18
18 19 alias FinalTrack.Accounts.User
@@ -108,12 +109,13 @@ defmodule FinalTrack.Federation.Objects do
108 109 "type" => "Note",
109 110 "attributedTo" => actor_url(account),
110 111 "published" => DateTime.to_iso8601(s.played_at),
111 − "to" => [followers_url(account)],
112 − "cc" => [@public],
112 + "to" => to(account),
113 + "cc" => cc(account),
113 114 "sensitive" => false,
114 115 "content" => content(s),
115 − # Where "open original" goes: the profile, which shows the play.
116 − "url" => profile_url(account),
116 + # Its own URL, unique per post: Pixelfed treats posts sharing a url as
117 + # one and drops the rest. A browser following it lands on the profile.
118 + "url" => url,
117 119 "attachment" => cover_attachment(s.album, s.track)
118 120 }
119 121 end
@@ -170,12 +172,20 @@ defmodule FinalTrack.Federation.Objects do
170 172 "id" => note_url(post) <> "#delete",
171 173 "type" => "Delete",
172 174 "actor" => actor_url(account),
173 − "to" => [followers_url(account)],
174 − "cc" => [@public],
175 + "to" => to(account),
176 + "cc" => cc(account),
175 177 "object" => %{"id" => note_url(post), "type" => "Tombstone"}
176 178 }
177 179 end
178 180
181 + # Public: to everyone, cc followers. Unlisted: to followers, cc everyone
182 + # (delivered alike, kept off public timelines and others' profile views).
183 + defp to(%Account{visibility: "public"}), do: [@public]
184 + defp to(account), do: [followers_url(account)]
185 +
186 + defp cc(%Account{visibility: "public"} = account), do: [followers_url(account)]
187 + defp cc(_account), do: [@public]
188 +
179 189 @doc "The `Accept` answering a `Follow`."
180 190 def accept(follow, %Account{} = account) do
181 191 %{
modified lib/final_track_web/live/user_live/scrobbling.ex
+46 −23
@@ -250,10 +250,9 @@ defmodule FinalTrackWeb.UserLive.Scrobbling do
250 250 <.icon name="hero-globe-alt-micro" class="size-4 text-accent" /> Fediverse
251 251 </h2>
252 252 <p class="mt-1 text-sm text-base-content/70">
253 − Give your plays an account that Mastodon and other fediverse users can follow. While
254 − it's on, every play you scrobble is posted (unlisted: to followers, not public
255 − timelines) with its album cover. Private plays never are, and marking something private
256 − withdraws what was already posted.
253 + Give your plays an account that Mastodon, Pixelfed and other fediverse users can
254 + follow. While it's on, every play you scrobble is posted with its album cover. Private
255 + plays never are, and marking something private withdraws what was already posted.
257 256 </p>
258 257 </div>
259 258
@@ -271,6 +270,13 @@ defmodule FinalTrackWeb.UserLive.Scrobbling do
271 270 ]}>
272 271 {if @fediverse_account.enabled, do: "posting", else: "off"}
273 272 </span>
273 + <span
274 + :if={@fediverse_account.enabled}
275 + id="fediverse-visibility"
276 + class="badge badge-sm badge-ghost"
277 + >
278 + {if @fediverse_account.visibility == "public", do: "public", else: "followers only"}
279 + </span>
274 280 <span class="text-base-content/60">
275 281 {@fediverse_followers} {if @fediverse_followers == 1, do: "follower", else: "followers"} · {@fediverse_posts} {if @fediverse_posts ==
276 282 1,
@@ -286,29 +292,46 @@ defmodule FinalTrackWeb.UserLive.Scrobbling do
286 292 id="fediverse-form"
287 293 phx-change="validate_fediverse"
288 294 phx-submit="save_fediverse"
289 − class="flex flex-col gap-3 sm:flex-row sm:items-end"
295 + class="space-y-3"
290 296 >
291 − <div class="flex-1">
292 − <.input
293 − field={@fediverse_form[:username]}
294 − type="text"
295 − label="Username"
296 − placeholder="e.g. alice"
297 − autocomplete="off"
298 − spellcheck="false"
299 − />
297 + <div class="flex flex-col gap-3 sm:flex-row sm:items-end">
298 + <div class="flex-1">
299 + <.input
300 + field={@fediverse_form[:username]}
301 + type="text"
302 + label="Username"
303 + placeholder="e.g. alice"
304 + autocomplete="off"
305 + spellcheck="false"
306 + />
307 + </div>
308 + <div class="sm:flex-1">
309 + <.input
310 + field={@fediverse_form[:visibility]}
311 + type="select"
312 + label="Who sees the posts"
313 + options={
314 + Enum.map(FinalTrack.Federation.Account.visibilities(), fn {v, l} -> {l, v} end)
315 + }
316 + />
317 + </div>
300 318 </div>
301 − <div class="sm:mb-3">
319 + <p class="-mt-1 text-xs text-base-content/60">
320 + Followers' feeds only keeps a post-per-play out of public timelines, but Pixelfed and
321 + angler then don't show the posts on your profile page. Public shows them everywhere.
322 + Changing it applies to new posts.
323 + </p>
324 + <div class="flex items-center justify-between gap-3">
302 325 <.input field={@fediverse_form[:enabled]} type="checkbox" label="Post my plays" />
326 + <button
327 + id="save-fediverse"
328 + type="submit"
329 + class="btn btn-primary"
330 + phx-disable-with="Saving…"
331 + >
332 + Save
333 + </button>
303 334 </div>
304 − <button
305 − id="save-fediverse"
306 − type="submit"
307 − class="btn btn-primary sm:mb-2"
308 − phx-disable-with="Saving…"
309 − >
310 − Save
311 − </button>
312 335 </.form>
313 336 </section>
314 337
added priv/repo/migrations/20261118211715_add_visibility_to_fediverse_accounts.exs
+12 −0
@@ -0,0 +1,12 @@
1 +defmodule FinalTrack.Repo.Migrations.AddVisibilityToFediverseAccounts do
2 + use Ecto.Migration
3 +
4 + # Who an account's posts are addressed to: "unlisted" (followers' feeds,
5 + # off public timelines and, on Pixelfed/angler, off the profile page) or
6 + # "public" (also the profile page and public timelines).
7 + def change do
8 + alter table(:fediverse_accounts) do
9 + add :visibility, :string, null: false, default: "unlisted"
10 + end
11 + end
12 +end
modified test/final_track/federation_test.exs
+27 −0
@@ -59,6 +59,33 @@ defmodule FinalTrack.FederationTest do
59 59 assert alt == "Cover of The Dark Side of the Moon by Pink Floyd"
60 60 end
61 61
62 + test "public accounts address everyone, with followers in cc", %{user: user, account: account} do
63 + {:ok, _} = Federation.save_account(user, %{visibility: "public"})
64 + play_fixture(user)
65 +
66 + assert_received {:delivered, _, %{"object" => note} = create, _, _, _}
67 + actor = FinalTrack.Federation.Objects.actor_url(account)
68 + assert note["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
69 + assert note["cc"] == [actor <> "/followers"]
70 + assert create["to"] == note["to"]
71 + end
72 +
73 + test "each post has its own url (Pixelfed drops posts that share one)", %{user: user} do
74 + play_fixture(user, %{"timestamp" => System.os_time(:second) - 120})
75 + play_fixture(user, %{"track" => "Money"})
76 +
77 + assert_received {:delivered, _, %{"object" => %{"id" => id1, "url" => url1}}, _, _, _}
78 + assert_received {:delivered, _, %{"object" => %{"id" => id2, "url" => url2}}, _, _, _}
79 + assert url1 == id1
80 + assert url2 == id2
81 + refute url1 == url2
82 + end
83 +
84 + test "visibility must be one we know", %{user: user} do
85 + assert {:error, cs} = Federation.save_account(user, %{visibility: "everyone"})
86 + assert "is invalid" in errors_on(cs).visibility
87 + end
88 +
62 89 test "no cover, no attachment", %{user: user} do
63 90 play_fixture(user)
64 91 assert_received {:delivered, _, %{"object" => %{"attachment" => []}}, _, _, _}
modified test/final_track_web/live/user_live/fediverse_settings_test.exs
+7 −0
@@ -21,6 +21,13 @@ defmodule FinalTrackWeb.UserLive.FediverseSettingsTest do
21 21 assert has_element?(lv, "#fediverse-status", "@alice@localhost")
22 22 assert has_element?(lv, "#fediverse-status", "posting")
23 23
24 + # Followers only by default; public on request.
25 + assert %{visibility: "unlisted"} = Federation.get_account(user)
26 + assert has_element?(lv, "#fediverse-visibility", "followers only")
27 + lv |> form("#fediverse-form", fediverse: %{visibility: "public"}) |> render_submit()
28 + assert %{visibility: "public"} = Federation.get_account(user)
29 + assert has_element?(lv, "#fediverse-visibility", "public")
30 +
24 31 lv |> form("#fediverse-form", fediverse: %{enabled: "false"}) |> render_submit()
25 32 assert %{enabled: false} = Federation.get_account(user)
26 33 assert has_element?(lv, "#fediverse-status", "off")

Parents: 34723be