Fediverse: /tags/:tag, where post hashtags link to

870f945 · gmorell · 2026-11-18 21:46

25 files +236 -15
Message
{commit_body(@commit)}

Files changed

modified README.md
+2 −1
@@ -33,7 +33,8 @@ Running at **https://finaltrack.neiam.org** (deployment:
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 35 not see a post per play can filter the tag, and optionally the artist
36 − (`Pink Floyd` → `#pinkfloyd`). `/@name` is the profile.
36 + (`Pink Floyd` → `#pinkfloyd`). `/@name` is the profile; `/tags/<tag>`
37 + lists what public accounts here posted under a tag.
37 38 - **Accounts**: invite-only registration, email/password and magic links
38 39 (`phx.gen.auth`), optional OpenID Connect sign-in via
39 40 [`neiam_auth`](https://gg.neiam.co/neiam/neiam_auth).
modified lib/final_track/catalog.ex
+13 −1
@@ -25,9 +25,21 @@ defmodule FinalTrack.Catalog do
25 25
26 26 @doc "Finds or creates the artist named `name`."
27 27 def ensure_artist!(name) do
28 − upsert!(%Artist{name: name, key: normalize(name)}, [:key])
28 + upsert!(%Artist{name: name, key: normalize(name), tag: hashtag(name)}, [:key])
29 29 end
30 30
31 + @doc """
32 + A name as a hashtag: lowercased, keeping only letters and digits
33 + ("Pink Floyd" → "pinkfloyd", "AC/DC" → "acdc"). nil when that leaves
34 + nothing usable: empty, or only digits, which no server takes as a tag.
35 + """
36 + def hashtag(name) when is_binary(name) do
37 + tag = name |> String.downcase() |> String.replace(~r/[^\p{L}\p{N}]+/u, "")
38 + if tag =~ ~r/\p{L}/u, do: tag, else: nil
39 + end
40 +
41 + def hashtag(_), do: nil
42 +
31 43 @doc "Finds or creates the album `title` under `artist`."
32 44 def ensure_album!(%Artist{id: artist_id}, title) do
33 45 upsert!(%Album{artist_id: artist_id, title: title, key: normalize(title)}, [:artist_id, :key])
modified lib/final_track/catalog/artist.ex
+2 −0
@@ -5,6 +5,8 @@ defmodule FinalTrack.Catalog.Artist do
5 5 schema "artists" do
6 6 field :name, :string
7 7 field :key, :string
8 + # The name as a hashtag (`FinalTrack.Catalog.hashtag/1`); nil if none.
9 + field :tag, :string
8 10
9 11 has_many :albums, FinalTrack.Catalog.Album
10 12
modified lib/final_track/federation.ex
+31 −0
@@ -242,6 +242,37 @@ defmodule FinalTrack.Federation do
242 242 |> Enum.map(&%{&1 | account: account})
243 243 end
244 244
245 + @doc """
246 + What this server has published under `tag` (no "#", any case), newest
247 + first: posts of public accounts that use it as their hashtag, or that tag
248 + posts with their artist and played an artist whose tag it is. Unlisted
249 + accounts are left out, as Mastodon keeps unlisted posts off hashtag
250 + timelines; private plays never show. Posts come with scrobble (track,
251 + album, artist) and account (user) loaded.
252 + """
253 + def tagged_posts(tag, limit \\ 48) when is_binary(tag) do
254 + tag = tag |> String.trim_leading("#") |> String.downcase()
255 +
256 + if enabled?() and tag != "" do
257 + from(p in Post,
258 + join: a in assoc(p, :account),
259 + # Through the association, so the preload below can use it.
260 + join: s in assoc(p, :scrobble),
261 + as: :scrobble,
262 + join: ar in assoc(s, :artist),
263 + where: a.enabled and a.visibility == "public",
264 + where: fragment("lower(?)", a.hashtag) == ^tag or (a.tag_artist and ar.tag == ^tag),
265 + order_by: [desc: s.played_at],
266 + limit: ^limit,
267 + preload: [scrobble: {s, [track: :artist, album: :artist]}, account: {a, :user}]
268 + )
269 + |> Privacy.visible_to(nil)
270 + |> Repo.all()
271 + else
272 + []
273 + end
274 + end
275 +
245 276 @doc """
246 277 Withdraws `user`'s posts that are private now (after a `Privacy.mark/2`):
247 278 a `Delete` to followers, and the post is forgotten.
modified lib/final_track/federation/objects.ex
+2 −11
@@ -157,17 +157,8 @@ defmodule FinalTrack.Federation.Objects do
157 157 Enum.uniq_by(own ++ artist, &String.downcase/1)
158 158 end
159 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
169 −
170 − def artist_hashtag(_), do: nil
160 + @doc "An artist's name as a hashtag; see `FinalTrack.Catalog.hashtag/1`."
161 + def artist_hashtag(name), do: FinalTrack.Catalog.hashtag(name)
171 162
172 163 defp tag_url(tag), do: base() <> "/tags/" <> URI.encode(String.downcase(tag))
173 164
added lib/final_track_web/controllers/tag_controller.ex
+22 −0

Click to load diff…

added lib/final_track_web/controllers/tag_html.ex
+5 −0

Click to load diff…

added lib/final_track_web/controllers/tag_html/show.html.heex
+56 −0

Click to load diff…

modified lib/final_track_web/router.ex
+4 −1

Click to load diff…

added priv/repo/migrations/20261118214529_add_tag_to_artists.exs
+32 −0

Click to load diff…

modified test/final_track_web/controllers/federation_off_test.exs
+2 −1

Click to load diff…

added test/final_track_web/controllers/tag_controller_test.exs
+65 −0

Click to load diff…

Parents: ef28fdd