neiam
/final_track
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
Fediverse: a hashtag on every post, #scropple by default
2517cad · gmorell · 2026-11-18 21:41
Message
{commit_body(@commit)}
Files changed
modified
README.md
+3
−2
@@ -30,8 +30,9 @@ Running at **https://finaltrack.neiam.org** (deployment:
| 30 | 30 | fediverse posts too. |
| 31 | 31 | - **Fediverse** (optional, off by default): a member can give their plays |
| 32 | 32 | an account (`@name@host`) that Mastodon, Pixelfed and others can follow; |
| 33 | − each non-private play is posted with its cover, followers-only or public. | |
| 34 | − `/@name` is the profile. | |
| 33 | + each non-private play is posted with its cover, followers-only or public, | |
| 34 | + tagged `#scropple` by default (changeable, or off) so people who'd rather | |
| 35 | + not see a post per play can filter the tag. `/@name` is the profile. | |
| 35 | 36 | - **Accounts**: invite-only registration, email/password and magic links |
| 36 | 37 | (`phx.gen.auth`), optional OpenID Connect sign-in via |
| 37 | 38 | [`neiam_auth`](https://gg.neiam.co/neiam/neiam_auth). |
modified
lib/final_track/federation/account.ex
+22
−1
@@ -27,6 +27,8 @@ defmodule FinalTrack.Federation.Account do
| 27 | 27 | field :enabled, :boolean, default: false |
| 28 | 28 | # "unlisted" or "public": see `visibilities/0`. |
| 29 | 29 | field :visibility, :string, default: "unlisted" |
| 30 | + # Added to every post, without the "#"; nil for none. | |
| 31 | + field :hashtag, :string, default: "scropple" | |
| 30 | 32 | field :public_key_pem, :string |
| 31 | 33 | field :private_key_pem, :string, redact: true |
| 32 | 34 |
@@ -44,8 +46,17 @@ defmodule FinalTrack.Federation.Account do
| 44 | 46 | @doc "The same checks without making keys: for a form as it's typed into." |
| 45 | 47 | def validation_changeset(account, attrs) do |
| 46 | 48 | account |
| 47 | − |> cast(attrs, [:username, :enabled, :visibility]) | |
| 49 | + # No empty_values: Ecto would turn a cleared hashtag ("") into the | |
| 50 | + # field's default ("scropple"), so it could never be switched off. | |
| 51 | + # normalize_hashtag/1 makes "" nil instead. | |
| 52 | + |> cast(attrs, [:username, :enabled, :visibility, :hashtag], empty_values: []) | |
| 48 | 53 | |> validate_inclusion(:visibility, Map.keys(@visibilities)) |
| 54 | + |> update_change(:hashtag, &normalize_hashtag/1) | |
| 55 | + |> validate_length(:hashtag, max: 30) | |
| 56 | + # Mastodon's rule: letters, digits and underscores, not only digits. | |
| 57 | + |> validate_format(:hashtag, ~r/^[\p{L}\p{N}_]*[\p{L}_][\p{L}\p{N}_]*$/u, | |
| 58 | + message: "letters, numbers and underscores, not only numbers" | |
| 59 | + ) | |
| 49 | 60 | |> update_change( |
| 50 | 61 | :username, |
| 51 | 62 | &(&1 |> String.trim() |> String.trim_leading("@") |> String.downcase()) |
@@ -60,6 +71,16 @@ defmodule FinalTrack.Federation.Account do
| 60 | 71 | |> unique_constraint(:username) |
| 61 | 72 | end |
| 62 | 73 | |
| 74 | + # "#Scropple " -> "Scropple"; "" or "#" -> nil (no hashtag). | |
| 75 | + defp normalize_hashtag(nil), do: nil | |
| 76 | + | |
| 77 | + defp normalize_hashtag(tag) do | |
| 78 | + case tag |> String.trim() |> String.trim_leading("#") do | |
| 79 | + "" -> nil | |
| 80 | + tag -> tag | |
| 81 | + end | |
| 82 | + end | |
| 83 | + | |
| 63 | 84 | defp put_keys(%{data: %{private_key_pem: pem}} = changeset) when is_binary(pem), do: changeset |
| 64 | 85 | |
| 65 | 86 | defp put_keys(changeset) do |
modified
lib/final_track/federation/objects.ex
+17
−1
@@ -112,7 +112,8 @@ defmodule FinalTrack.Federation.Objects do
| 112 | 112 | "to" => to(account), |
| 113 | 113 | "cc" => cc(account), |
| 114 | 114 | "sensitive" => false, |
| 115 | − "content" => content(s), | |
| 115 | + "content" => content(s) <> hashtag_html(account), | |
| 116 | + "tag" => hashtag_tags(account), | |
| 116 | 117 | # Its own URL, unique per post: Pixelfed treats posts sharing a url as |
| 117 | 118 | # one and drops the rest. A browser following it lands on the profile. |
| 118 | 119 | "url" => url, |
@@ -130,6 +131,21 @@ defmodule FinalTrack.Federation.Objects do
| 130 | 131 | "<p>🎵 #{track}#{album}</p>" |
| 131 | 132 | end |
| 132 | 133 | |
| 134 | + # The account's hashtag, as Mastodon writes one: a link in the content and | |
| 135 | + # a Hashtag in `tag` (what servers index and tag-followers receive). | |
| 136 | + defp hashtag_html(%Account{hashtag: tag}) when is_binary(tag) and tag != "" do | |
| 137 | + ~s(<p><a href="#{tag_url(tag)}" class="mention hashtag" rel="tag">#<span>#{escape(tag)}</span></a></p>) | |
| 138 | + end | |
| 139 | + | |
| 140 | + defp hashtag_html(_account), do: "" | |
| 141 | + | |
| 142 | + defp hashtag_tags(%Account{hashtag: tag}) when is_binary(tag) and tag != "", | |
| 143 | + do: [%{"type" => "Hashtag", "href" => tag_url(tag), "name" => "#" <> tag}] | |
| 144 | + | |
| 145 | + defp hashtag_tags(_account), do: [] | |
| 146 | + | |
| 147 | + defp tag_url(tag), do: base() <> "/tags/" <> URI.encode(String.downcase(tag)) | |
| 148 | + | |
| 133 | 149 | defp cover_attachment(nil, _track), do: [] |
| 134 | 150 | |
| 135 | 151 | defp cover_attachment(album, _track) do |
modified
lib/final_track_web/live/user_live/scrobbling.ex
+15
−0
@@ -321,6 +321,21 @@ defmodule FinalTrackWeb.UserLive.Scrobbling do
| 321 | 321 | angler then don't show the posts on your profile page. Public shows them everywhere. |
| 322 | 322 | Changing it applies to new posts. |
| 323 | 323 | </p> |
| 324 | + <div class="sm:w-1/2"> | |
| 325 | + <.input | |
| 326 | + field={@fediverse_form[:hashtag]} | |
| 327 | + type="text" | |
| 328 | + label="Hashtag" | |
| 329 | + placeholder="none" | |
| 330 | + autocomplete="off" | |
| 331 | + spellcheck="false" | |
| 332 | + /> | |
| 333 | + <p class="-mt-1 text-xs text-base-content/60"> | |
| 334 | + Added to every post<span :if={@fediverse_form[:hashtag].value not in [nil, ""]}> | |
| 335 | + as <span class="font-mono">#{String.trim_leading(to_string(@fediverse_form[:hashtag].value), "#")}</span></span>, so anyone who'd rather not see a post per play can filter or mute the tag | |
| 336 | + instead of unfollowing. Leave it empty for none. | |
| 337 | + </p> | |
| 338 | + </div> | |
| 324 | 339 | <div class="flex items-center justify-between gap-3"> |
| 325 | 340 | <.input field={@fediverse_form[:enabled]} type="checkbox" label="Post my plays" /> |
| 326 | 341 | <button |
added
priv/repo/migrations/20261118214019_add_hashtag_to_fediverse_accounts.exs
+11
−0
@@ -0,0 +1,11 @@
| 1 | +defmodule FinalTrack.Repo.Migrations.AddHashtagToFediverseAccounts do | |
| 2 | + use Ecto.Migration | |
| 3 | + | |
| 4 | + # A hashtag added to every post, without the "#". On by default | |
| 5 | + # (existing accounts included); NULL means none. | |
| 6 | + def change do | |
| 7 | + alter table(:fediverse_accounts) do | |
| 8 | + add :hashtag, :string, default: "scropple" | |
| 9 | + end | |
| 10 | + end | |
| 11 | +end |
modified
test/final_track/federation_test.exs
+34
−0
@@ -86,6 +86,40 @@ defmodule FinalTrack.FederationTest do
| 86 | 86 | assert "is invalid" in errors_on(cs).visibility |
| 87 | 87 | end |
| 88 | 88 | |
| 89 | + test "posts carry #scropple by default, in content and as a Hashtag", %{user: user} do | |
| 90 | + play_fixture(user) | |
| 91 | + assert_received {:delivered, _, %{"object" => note}, _, _, _} | |
| 92 | + | |
| 93 | + assert note["content"] =~ ~s(rel="tag">#<span>scropple</span></a>) | |
| 94 | + assert [%{"type" => "Hashtag", "name" => "#scropple", "href" => href}] = note["tag"] | |
| 95 | + assert href =~ "/tags/scropple" | |
| 96 | + end | |
| 97 | + | |
| 98 | + test "the hashtag is the account's to change or clear", %{user: user} do | |
| 99 | + {:ok, account} = Federation.save_account(user, %{hashtag: " #NowPlaying "}) | |
| 100 | + assert account.hashtag == "NowPlaying" | |
| 101 | + play_fixture(user) | |
| 102 | + | |
| 103 | + assert_received {:delivered, _, %{"object" => %{"tag" => [%{"name" => "#NowPlaying"}]}}, _, _, | |
| 104 | + _} | |
| 105 | + | |
| 106 | + {:ok, account} = Federation.save_account(user, %{hashtag: ""}) | |
| 107 | + assert account.hashtag == nil | |
| 108 | + play_fixture(user, %{"track" => "Money"}) | |
| 109 | + assert_received {:delivered, _, %{"object" => %{"tag" => [], "content" => content}}, _, _, _} | |
| 110 | + refute content =~ "hashtag" | |
| 111 | + end | |
| 112 | + | |
| 113 | + test "hashtags follow Mastodon's rules", %{user: user} do | |
| 114 | + for bad <- ["two words", "no-dashes", "1234", String.duplicate("a", 31)] do | |
| 115 | + assert {:error, cs} = Federation.save_account(user, %{hashtag: bad}), bad | |
| 116 | + assert errors_on(cs).hashtag != [] | |
| 117 | + end | |
| 118 | + | |
| 119 | + assert {:ok, %{hashtag: "música_2026"}} = | |
| 120 | + Federation.save_account(user, %{hashtag: "música_2026"}) | |
| 121 | + end | |
| 122 | + | |
| 89 | 123 | test "no cover, no attachment", %{user: user} do |
| 90 | 124 | play_fixture(user) |
| 91 | 125 | assert_received {:delivered, _, %{"object" => %{"attachment" => []}}, _, _, _} |
modified
test/final_track_web/live/user_live/fediverse_settings_test.exs
+5
−0
@@ -28,6 +28,11 @@ defmodule FinalTrackWeb.UserLive.FediverseSettingsTest do
| 28 | 28 | assert %{visibility: "public"} = Federation.get_account(user) |
| 29 | 29 | assert has_element?(lv, "#fediverse-visibility", "public") |
| 30 | 30 | |
| 31 | + # #scropple by default; changeable. | |
| 32 | + assert %{hashtag: "scropple"} = Federation.get_account(user) | |
| 33 | + lv |> form("#fediverse-form", fediverse: %{hashtag: "#nowplaying"}) |> render_submit() | |
| 34 | + assert %{hashtag: "nowplaying"} = Federation.get_account(user) | |
| 35 | + | |
| 31 | 36 | lv |> form("#fediverse-form", fediverse: %{enabled: "false"}) |> render_submit() |
| 32 | 37 | assert %{enabled: false} = Federation.get_account(user) |
| 33 | 38 | assert has_element?(lv, "#fediverse-status", "off") |
Parents: 72b0c51