neiam
/final_track
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
Fediverse: optional artist hashtag on posts (Pink Floyd -> #pinkfloyd)
ef28fdd · gmorell · 2026-11-18 21:44
Message
{commit_body(@commit)}
Files changed
modified
README.md
+2
−1
@@ -32,7 +32,8 @@ Running at **https://finaltrack.neiam.org** (deployment:
| 32 | 32 | an account (`@name@host`) that Mastodon, Pixelfed and others can follow; |
| 33 | 33 | each non-private play is posted with its cover, followers-only or public, |
| 34 | 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 | + not see a post per play can filter the tag, and optionally the artist | |
| 36 | + (`Pink Floyd` → `#pinkfloyd`). `/@name` is the profile. | |
| 36 | 37 | - **Accounts**: invite-only registration, email/password and magic links |
| 37 | 38 | (`phx.gen.auth`), optional OpenID Connect sign-in via |
| 38 | 39 | [`neiam_auth`](https://gg.neiam.co/neiam/neiam_auth). |
modified
lib/final_track/federation/account.ex
+3
−1
@@ -29,6 +29,8 @@ defmodule FinalTrack.Federation.Account do
| 29 | 29 | field :visibility, :string, default: "unlisted" |
| 30 | 30 | # Added to every post, without the "#"; nil for none. |
| 31 | 31 | field :hashtag, :string, default: "scropple" |
| 32 | + # Also tag each post with its artist (Pink Floyd -> #pinkfloyd). | |
| 33 | + field :tag_artist, :boolean, default: false | |
| 32 | 34 | field :public_key_pem, :string |
| 33 | 35 | field :private_key_pem, :string, redact: true |
| 34 | 36 |
@@ -49,7 +51,7 @@ defmodule FinalTrack.Federation.Account do
| 49 | 51 | # No empty_values: Ecto would turn a cleared hashtag ("") into the |
| 50 | 52 | # field's default ("scropple"), so it could never be switched off. |
| 51 | 53 | # normalize_hashtag/1 makes "" nil instead. |
| 52 | − |> cast(attrs, [:username, :enabled, :visibility, :hashtag], empty_values: []) | |
| 54 | + |> cast(attrs, [:username, :enabled, :visibility, :hashtag, :tag_artist], empty_values: []) | |
| 53 | 55 | |> validate_inclusion(:visibility, Map.keys(@visibilities)) |
| 54 | 56 | |> update_change(:hashtag, &normalize_hashtag/1) |
| 55 | 57 | |> validate_length(:hashtag, max: 30) |
modified
lib/final_track/federation/objects.ex
+35
−10
@@ -112,8 +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) <> hashtag_html(account), | |
| 116 | − "tag" => hashtag_tags(account), | |
| 115 | + "content" => content(s) <> hashtag_html(hashtags(account, s)), | |
| 116 | + "tag" => hashtag_tags(hashtags(account, s)), | |
| 117 | 117 | # Its own URL, unique per post: Pixelfed treats posts sharing a url as |
| 118 | 118 | # one and drops the rest. A browser following it lands on the profile. |
| 119 | 119 | "url" => url, |
@@ -131,18 +131,43 @@ defmodule FinalTrack.Federation.Objects do
| 131 | 131 | "<p>🎵 #{track}#{album}</p>" |
| 132 | 132 | end |
| 133 | 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>) | |
| 134 | + # The post's hashtags, as Mastodon writes them: links at the end of the | |
| 135 | + # content, and Hashtags in `tag` (what servers index, filter and send to | |
| 136 | + # tag-followers). The account's own tag, then (if it asks) the artist's. | |
| 137 | + defp hashtag_html(tags) when tags == [], do: "" | |
| 138 | + | |
| 139 | + defp hashtag_html(tags) do | |
| 140 | + links = | |
| 141 | + Enum.map_join(tags, " ", fn tag -> | |
| 142 | + ~s(<a href="#{tag_url(tag)}" class="mention hashtag" rel="tag">#<span>#{escape(tag)}</span></a>) | |
| 143 | + end) | |
| 144 | + | |
| 145 | + "<p>" <> links <> "</p>" | |
| 138 | 146 | end |
| 139 | 147 | |
| 140 | − defp hashtag_html(_account), do: "" | |
| 148 | + defp hashtag_tags(tags), | |
| 149 | + do: Enum.map(tags, &%{"type" => "Hashtag", "href" => tag_url(&1), "name" => "#" <> &1}) | |
| 150 | + | |
| 151 | + defp hashtags(%Account{} = account, scrobble) do | |
| 152 | + own = if account.hashtag in [nil, ""], do: [], else: [account.hashtag] | |
| 141 | 153 | |
| 142 | − defp hashtag_tags(%Account{hashtag: tag}) when is_binary(tag) and tag != "", | |
| 143 | − do: [%{"type" => "Hashtag", "href" => tag_url(tag), "name" => "#" <> tag}] | |
| 154 | + artist = | |
| 155 | + if account.tag_artist, do: List.wrap(artist_hashtag(scrobble.track.artist.name)), else: [] | |
| 156 | + | |
| 157 | + Enum.uniq_by(own ++ artist, &String.downcase/1) | |
| 158 | + end | |
| 159 | + | |
| 160 | + @doc """ | |
| 161 | + An artist's name as a hashtag: lowercased, keeping only letters and | |
| 162 | + digits ("Pink Floyd" → "pinkfloyd", "AC/DC" → "acdc"). nil when that | |
| 163 | + leaves nothing usable: empty, or only digits, which no server takes. | |
| 164 | + """ | |
| 165 | + def artist_hashtag(name) when is_binary(name) do | |
| 166 | + tag = name |> String.downcase() |> String.replace(~r/[^\p{L}\p{N}]+/u, "") | |
| 167 | + if tag =~ ~r/\p{L}/u, do: tag, else: nil | |
| 168 | + end | |
| 144 | 169 | |
| 145 | − defp hashtag_tags(_account), do: [] | |
| 170 | + def artist_hashtag(_), do: nil | |
| 146 | 171 | |
| 147 | 172 | defp tag_url(tag), do: base() <> "/tags/" <> URI.encode(String.downcase(tag)) |
| 148 | 173 |
modified
lib/final_track_web/live/user_live/scrobbling.ex
+11
−0
@@ -336,6 +336,17 @@ defmodule FinalTrackWeb.UserLive.Scrobbling do
| 336 | 336 | instead of unfollowing. Leave it empty for none. |
| 337 | 337 | </p> |
| 338 | 338 | </div> |
| 339 | + <div> | |
| 340 | + <.input | |
| 341 | + field={@fediverse_form[:tag_artist]} | |
| 342 | + type="checkbox" | |
| 343 | + label="Also tag posts with the artist" | |
| 344 | + /> | |
| 345 | + <p class="-mt-1 text-xs text-base-content/60"> | |
| 346 | + Pink Floyd becomes <span class="font-mono">#pinkfloyd</span>: the post then shows | |
| 347 | + under that artist's tag on other servers too. | |
| 348 | + </p> | |
| 349 | + </div> | |
| 339 | 350 | <div class="flex items-center justify-between gap-3"> |
| 340 | 351 | <.input field={@fediverse_form[:enabled]} type="checkbox" label="Post my plays" /> |
| 341 | 352 | <button |
added
priv/repo/migrations/20261118214404_add_tag_artist_to_fediverse_accounts.exs
+11
−0
@@ -0,0 +1,11 @@
| 1 | +defmodule FinalTrack.Repo.Migrations.AddTagArtistToFediverseAccounts do | |
| 2 | + use Ecto.Migration | |
| 3 | + | |
| 4 | + # Whether posts also carry the artist as a hashtag (Pink Floyd -> | |
| 5 | + # #pinkfloyd). Opt-in: it puts plays into the artist's public tag stream. | |
| 6 | + def change do | |
| 7 | + alter table(:fediverse_accounts) do | |
| 8 | + add :tag_artist, :boolean, null: false, default: false | |
| 9 | + end | |
| 10 | + end | |
| 11 | +end |
modified
test/final_track/federation_test.exs
+40
−0
@@ -110,6 +110,46 @@ defmodule FinalTrack.FederationTest do
| 110 | 110 | refute content =~ "hashtag" |
| 111 | 111 | end |
| 112 | 112 | |
| 113 | + test "artist names as hashtags" do | |
| 114 | + alias FinalTrack.Federation.Objects | |
| 115 | + | |
| 116 | + assert Objects.artist_hashtag("Justice") == "justice" | |
| 117 | + assert Objects.artist_hashtag("Pink Floyd") == "pinkfloyd" | |
| 118 | + assert Objects.artist_hashtag("AC/DC") == "acdc" | |
| 119 | + assert Objects.artist_hashtag("Sigur Rós") == "sigurrós" | |
| 120 | + assert Objects.artist_hashtag("Justice & Tame Impala") == "justicetameimpala" | |
| 121 | + assert Objects.artist_hashtag("2Pac") == "2pac" | |
| 122 | + assert Objects.artist_hashtag("311") == nil | |
| 123 | + assert Objects.artist_hashtag("!!!") == nil | |
| 124 | + end | |
| 125 | + | |
| 126 | + test "the artist tag is opt-in, joins the account's, and isn't doubled", %{user: user} do | |
| 127 | + play_fixture(user) | |
| 128 | + | |
| 129 | + assert_received {:delivered, _, %{"object" => %{"tag" => [%{"name" => "#scropple"}]}}, _, _, | |
| 130 | + _} | |
| 131 | + | |
| 132 | + {:ok, _} = Federation.save_account(user, %{tag_artist: true}) | |
| 133 | + play_fixture(user, %{"track" => "Money"}) | |
| 134 | + assert_received {:delivered, _, %{"object" => note}, _, _, _} | |
| 135 | + assert Enum.map(note["tag"], & &1["name"]) == ["#scropple", "#pinkfloyd"] | |
| 136 | + assert note["content"] =~ "#<span>pinkfloyd</span>" | |
| 137 | + | |
| 138 | + # An account tag that is the artist's: once. | |
| 139 | + {:ok, _} = Federation.save_account(user, %{hashtag: "PinkFloyd"}) | |
| 140 | + play_fixture(user, %{"track" => "Breathe"}) | |
| 141 | + | |
| 142 | + assert_received {:delivered, _, %{"object" => %{"tag" => [%{"name" => "#PinkFloyd"}]}}, _, _, | |
| 143 | + _} | |
| 144 | + | |
| 145 | + # An artist with no usable tag: just the account's. | |
| 146 | + {:ok, _} = Federation.save_account(user, %{hashtag: "scropple"}) | |
| 147 | + play_fixture(user, %{"artist" => "311", "track" => "Amber", "album" => nil}) | |
| 148 | + | |
| 149 | + assert_received {:delivered, _, %{"object" => %{"tag" => [%{"name" => "#scropple"}]}}, _, _, | |
| 150 | + _} | |
| 151 | + end | |
| 152 | + | |
| 113 | 153 | test "hashtags follow Mastodon's rules", %{user: user} do |
| 114 | 154 | for bad <- ["two words", "no-dashes", "1234", String.duplicate("a", 31)] do |
| 115 | 155 | assert {:error, cs} = Federation.save_account(user, %{hashtag: bad}), bad |
modified
test/final_track_web/live/user_live/fediverse_settings_test.exs
+5
−0
@@ -33,6 +33,11 @@ defmodule FinalTrackWeb.UserLive.FediverseSettingsTest do
| 33 | 33 | lv |> form("#fediverse-form", fediverse: %{hashtag: "#nowplaying"}) |> render_submit() |
| 34 | 34 | assert %{hashtag: "nowplaying"} = Federation.get_account(user) |
| 35 | 35 | |
| 36 | + # Artist tags: off until asked for. | |
| 37 | + assert %{tag_artist: false} = Federation.get_account(user) | |
| 38 | + lv |> form("#fediverse-form", fediverse: %{tag_artist: "true"}) |> render_submit() | |
| 39 | + assert %{tag_artist: true} = Federation.get_account(user) | |
| 40 | + | |
| 36 | 41 | lv |> form("#fediverse-form", fediverse: %{enabled: "false"}) |> render_submit() |
| 37 | 42 | assert %{enabled: false} = Federation.get_account(user) |
| 38 | 43 | assert has_element?(lv, "#fediverse-status", "off") |
Parents: 2517cad