Merge branch 'rich-media-authors' into 'develop'

Draft: Rich Media Cards: support fediverse authors

See merge request pleroma/pleroma!4169
This commit is contained in:
feld 2025-03-21 00:24:45 +00:00
commit 39c9eaa077
5 changed files with 68 additions and 7 deletions

View file

@ -451,7 +451,8 @@ config :pleroma, :rich_media,
ignore_tld: ["local", "localdomain", "lan"],
parsers: [
Pleroma.Web.RichMedia.Parsers.TwitterCard,
Pleroma.Web.RichMedia.Parsers.OEmbed
Pleroma.Web.RichMedia.Parsers.OEmbed,
Pleroma.Web.RichMedia.Parsers.Fediverse
],
timeout: 5_000,
ttl_setters: [

View file

@ -578,6 +578,37 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
audio_url = proxied_url(rich_media["audio"], page_url_data)
video_url = proxied_url(rich_media["video"], page_url_data)
# REFACTOR THIS UGLY HACK
authors =
if Map.has_key?(rich_media, "authors") do
authors = Map.get(rich_media, "authors")
Enum.map(authors, fn x ->
case Pleroma.User.get_cached_by_nickname(x) do
%Pleroma.User{} = user ->
[
%{
"name" => "",
"url" => "",
"account" => AccountView.render("show.json", %{user: user, for: nil})
}
]
_ ->
nil
end
end)
|> List.flatten()
else
[
%{
"name" => "",
"url" => "",
"account" => nil
}
]
end
%{
type: "link",
provider_name: page_url_data.host,
@ -593,7 +624,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
|> Maps.put_if_present("image", image_url)
|> Maps.put_if_present("audio", audio_url)
|> Maps.put_if_present("video", video_url)
}
},
authors: authors
}
end

View file

@ -41,11 +41,10 @@ defmodule Pleroma.Web.RichMedia.Parser do
end
defp maybe_parse(html) do
Enum.reduce_while(parsers(), %{}, fn parser, acc ->
case parser.parse(html, acc) do
data when data != %{} -> {:halt, data}
_ -> {:cont, acc}
end
Enum.reduce(parsers(), %{}, fn parser, acc ->
result = parser.parse(html, acc)
Map.merge(acc, result)
end)
end

View file

@ -0,0 +1,18 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.RichMedia.Parsers.Fediverse do
def parse(html, data) do
case get_authors(html) do
[] -> data
authors when is_list(authors) -> Map.put(data, "authors", authors)
_ -> data
end
end
defp get_authors(html) do
Floki.find(html, "meta[name^='fediverse:']")
|> Enum.map(fn {_, x, _} -> List.last(x) |> elem(1) end)
end
end

11
test/fixtures/mastodon-journalism.html vendored Normal file
View file

@ -0,0 +1,11 @@
<!doctype html><html lang><head><meta charset=utf-8><meta name=viewport content='width=device-width,initial-scale=1'><meta name=generator content="Hugo 0.124.1"><title>Highlighting journalism on Mastodon - Mastodon Blog</title>
<link rel="shortcut icon" type=image/png href=https://blog.joinmastodon.org/favicon.ico><link rel=canonical href=https://blog.joinmastodon.org/2024/07/highlighting-journalism-on-mastodon/><link rel=stylesheet href="https://blog.joinmastodon.org/css/styles.min.0ff7db6851f0326dde45ec59418290e69bcc519b34c9063e48b11e912473c286.css" integrity="sha256-D/fbaFHwMm3eRexZQYKQ5pvMUZs0yQY+SLEekSRzwoY="><meta property="og:locale" content><meta property="og:type" content="article"><meta property="og:title" content="Highlighting journalism on Mastodon"><meta property="og:description" content="Today we're launching a new feature that will highlight writers and journalists that are active on the fediverse when their their articles are being shared."><meta property="og:url" content="https://blog.joinmastodon.org/2024/07/highlighting-journalism-on-mastodon/"><meta property="og:site_name" content="Mastodon Blog"><meta name=og:image content="https://blog.joinmastodon.org/2024/07/highlighting-journalism-on-mastodon/hero_hu4a7f6b428bb6667126ec1448b58cea40_1079820_1200x0_resize_box_3.png"><meta name=article:author content="Eugen Rochko"><meta name=fediverse:creator content="Gargron@mastodon.social"></head><body><div class="bg-white py-12 sm:py-32"><div class="mx-auto max-w-7xl px-6 lg:px-8"><article class=h-entry><header class="max-w-prose mx-auto relative"><div class="flex flex-wrap items-center gap-x-4 text-xs mb-8"><a href=/ class="inline-flex items-center font-bold text-blurple-600 hover:underline hover:text-blurple-500"><svg class="icon arrow-small-left-icon w-5 h-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentcolor" class="w-5 h-5"><path fill-rule="evenodd" d="M15 10a.75.75.0 01-.75.75H7.612l2.158 1.96a.75.75.0 11-1.04 1.08l-3.5-3.25a.75.75.0 010-1.08l3.5-3.25a.75.75.0 111.04 1.08L7.612 9.25h6.638A.75.75.0 0115 10z" clip-rule="evenodd"/></svg> Back</a>
<time class="text-gray-500 dt-published" datetime=2024-07-02T00:00:00Z>Jul 2, 2024</time><div class="flex flex-wrap items-center gap-x-4"><a class="relative z-10 rounded-full bg-nightshade-50 text-nightshade-900 px-3 py-1.5 font-medium hover:text-nightshade-600 whitespace-nowrap" href=/categories/new-features>New Features</a></div></div><h1 class="p-name text-3xl font-bold tracking-tight text-gray-900 sm:text-4xl">Highlighting journalism on Mastodon</h1><h2 class="text-xl leading-8 text-gray-700 mt-2">A new feature for media organizations, journalists and bloggers</h2><div class="relative mt-8 flex items-center gap-x-4"><div class="h-10 w-10 rounded-full bg-blurple-gradient relative overflow-hidden"><img src=https://blog.joinmastodon.org/authors/eugen-rochko.jpg alt class="absolute w-full h-full object-cover"></div><div class="text-sm leading-6"><p class="font-semibold text-gray-900"><span class="absolute inset-0"></span>
Eugen Rochko</p><p class=text-gray-600>CEO / Founder</p></div></div></header><div class="rounded-lg overflow-hidden max-w-3xl my-8 mx-auto shadow-lg"><img src=/2024/07/highlighting-journalism-on-mastodon/hero.png alt class="w-full object-contain"></div><main class="e-content max-w-prose mx-auto prose mt-8"><p>To reinforce and encourage Mastodon as the go-to place for journalism, were launching a new feature today. You will notice that underneath some links shared on Mastodon, the author byline can be clicked to open the authors associated fediverse account, right in the app. This highlights writers and journalists that are active on the fediverse, and makes it easier than ever to follow them and keep up with their future work—potentially across different publications. Writers often work with different publishers over the span of their careers, but Mastodon is the perfect platform to amass a loyal audience that you, as the author, truly own, and can take with you across the fediverse.</p><p>Some of the first websites adopting this feature are The Verge, MacStories, and MacRumors. If you have an account on <a href=http://mastodon.social>mastodon.social</a> and use the web version or one of our official mobile apps, you will see associated fediverse accounts underneath their articles, no matter who posts them. Of course, its also part of Mastodons API, so we expect to see support among the vast array of third party apps for Mastodon as well. All Mastodon features are always available to all third-party apps since the API that powers the web version and the official mobile apps is the exact same for everyone.</p><figure><img src=screenshot-web-story.png class="rounded-lg shadow-lg"></figure><div class="grid grid-cols-2 gap-4 not-prose my-8"><div class=col-span-1><figure><img src=screenshot-web-link1.png class="rounded-lg shadow-lg"></figure></div><div class=col-span-1><figure><img src=screenshot-web-link2.png class="rounded-lg shadow-lg"></figure></div></div><h3 id=the-technical>The technical</h3><p>Weve decided to create a new kind of OpenGraph tag—the same kind of tags you have on your website to determine which thumbnail image will appear on the preview for the page when shared on Discord, iMessage, or Mastodon. It looks like this: <code>&lt;meta name="fediverse:creator" content="@Gargron@mastodon.social" /></code>.</p><p>The handle can be any fediverse account, not just Mastodon. That includes Flipboard, Threads, WordPress (with the ActivityPub plugin installed), PeerTube, Pixelfed, and many others. It will work with and without the leading at-symbol for the handle. If multiple tags are present on the page, the first one will be displayed, but we may add support for showing multiple authors in the future. We intend to propose a specification draft for other ActivityPub platforms in the coming weeks.</p><p>The support for this tag is currently rolled out on <a href=http://mastodon.social>mastodon.social</a> and any other server that uses a recent Mastodon nightly release, but the feature will only show up for links to moderator-approved websites. In the future we would like to make the feature available to all without a manual review process. <strong>For now, if youre part of a news organization, please reach out to us at <a href=mailto:hello@joinmastodon.org>hello@joinmastodon.org</a> so we can enable it for your website.</strong></p></main></article><div class="grid grid-cols-1 gap-x-8 gap-y-16 border-t border-gray-200 pt-10 sm:mt-16 sm:pt-16 md:grid-cols-2 h-feed"><a href=/2023/09/mastodon-4.2/ class="group flex lg:max-w-xl flex-col items-start justify-between h-entry u-url" rel=bookmark><div class=w-full><div class="flex items-center gap-x-4 text-xs"><time class="text-gray-500 dt-published" datetime=2023-09-21T00:00:00Z>Sep 21, 2023
</time><span class="relative z-10 rounded-full bg-nightshade-50 text-nightshade-900 px-3 py-1.5 font-medium">New Features</span></div><div class="w-full relative aspect-[3/2] w-full overflow-hidden rounded-md my-4 bg-blurple-gradient shadow-lg ring-blurple-500 group-hover:ring-2"><img src=/2023/09/mastodon-4.2/hero.png alt class="absolute w-full h-full object-cover"></div><div class=relative><h3 class="mt-3 text-lg font-semibold leading-6 text-gray-900 group-hover:text-blurple-500 p-name"><span class="absolute inset-0"></span>
Mastodon 4.2</h3><p class="mt-5 line-clamp-3 text-sm leading-6 text-gray-600 p-summary">In this massive update we've added search and removed friction. What's not to love?</p></div></div><div class="relative mt-8 flex items-center gap-x-4"><div class="h-10 w-10 rounded-full bg-blurple-gradient relative overflow-hidden"><img src=https://blog.joinmastodon.org/authors/eugen-rochko.jpg alt class="absolute w-full h-full object-cover"></div><div class="text-sm leading-6"><p class="font-semibold text-gray-900"><span class="absolute inset-0"></span>
Eugen Rochko</p><p class=text-gray-600>CEO / Founder</p></div></div></a><a href=/2022/03/mastodon-3.5/ class="group flex lg:max-w-xl flex-col items-start justify-between h-entry u-url" rel=bookmark><div class=w-full><div class="flex items-center gap-x-4 text-xs"><time class="text-gray-500 dt-published" datetime=2022-03-30T00:00:00Z>Mar 30, 2022
</time><span class="relative z-10 rounded-full bg-nightshade-50 text-nightshade-900 px-3 py-1.5 font-medium">New Features</span></div><div class="w-full relative aspect-[3/2] w-full overflow-hidden rounded-md my-4 bg-blurple-gradient shadow-lg ring-blurple-500 group-hover:ring-2"><img src=/2022/03/mastodon-3.5/hero.png alt class="absolute w-full h-full object-cover"></div><div class=relative><h3 class="mt-3 text-lg font-semibold leading-6 text-gray-900 group-hover:text-blurple-500 p-name"><span class="absolute inset-0"></span>
Mastodon 3.5</h3><p class="mt-5 line-clamp-3 text-sm leading-6 text-gray-600 p-summary">A brand new explore page, post editing, and new moderation features are just some of the additions in this new release of Mastodon!</p></div></div><div class="relative mt-8 flex items-center gap-x-4"><div class="h-10 w-10 rounded-full bg-blurple-gradient relative overflow-hidden"><img src=https://blog.joinmastodon.org/authors/eugen-rochko.jpg alt class="absolute w-full h-full object-cover"></div><div class="text-sm leading-6"><p class="font-semibold text-gray-900"><span class="absolute inset-0"></span>
Eugen Rochko</p><p class=text-gray-600>CEO / Founder</p></div></div></a></div><footer class="border-t border-gray-200 pt-10 mt-10 text-gray-600"><div class="flex justify-between"><p class=flex-0><a class=hover:underline href=https://joinmastodon.org>Mastodon</a> · <a class=hover:underline href=https://github.com/mastodon/blog/blob/master/content//posts/2024-07-02_highlighting-journalism-on-mastodon>View source</a> · <a class=hover:underline href=https://creativecommons.org/licenses/by-sa/4.0/>CC BY-SA 4.0</a> · <a class=hover:underline href=https://joinmastodon.org/imprint>Imprint</a></p><p class="flex-0 justify-end flex gap-x-4 items-center"><a class=hover:text-blurple-500 href=https://mastodon.social/@Mastodon target=_blank><svg class="icon mastodon-icon w-5 h-5" width="74" height="79" viewBox="0 0 74 79" fill="currentcolor" xmlns="http://www.w3.org/2000/svg"><path d="M73.7014 17.9592C72.5616 9.62034 65.1774 3.04876 56.424 1.77536 54.9472 1.56019 49.3517.7771 36.3901.7771H36.2933c-12.9652.0-15.7468.78309-17.2236.99826C10.56 3.01348 2.78877 8.91838.903306 17.356-.00357857 21.5113-.100361 26.1181.068112 30.3439.308275 36.404.354874 42.4535.91406 48.489c.38658 4.009 1.06096 7.9861 2.01809 11.9015 1.79226 7.2312 9.04735 13.249 16.15545 15.704C26.6979 78.6548 34.8821 79.0799 42.724 77.3221 43.5866 77.1245 44.4398 76.8953 45.2833 76.6342 47.1867 76.0381 49.4199 75.3714 51.0616 74.2003 51.0841 74.1839 51.1026 74.1627 51.1156 74.1382 51.1286 74.1138 51.1359 74.0868 51.1368 74.0592V68.2108C51.1364 68.185 51.1302 68.1596 51.1185 68.1365 51.1069 68.1134 51.0902 68.0932 51.0695 68.0773 51.0489 68.0614 51.0249 68.0503 50.9994 68.0447 50.9738 68.0391 50.9473 68.0392 50.9218 68.045c-5.0242 1.181-10.1727 1.773-15.3382 1.7637C26.694 69.8087 24.3031 65.6569 23.6184 63.9285 23.0681 62.4347 22.7186 60.8764 22.5789 59.2934 22.5775 59.2669 22.5825 59.2403 22.5934 59.216 22.6043 59.1916 22.621 59.1702 22.6419 59.1533 22.6629 59.1365 22.6876 59.1248 22.714 59.1191 22.7404 59.1134 22.7678 59.1139 22.794 59.1206 27.7345 60.2936 32.799 60.8856 37.8813 60.8843c1.2223.0 2.441.0 3.6634-.0317000000000007C46.6562 60.7115 52.0437 60.454 57.0728 59.4874 57.1983 59.4628 57.3237 59.4416 57.4313 59.4098c7.9325-1.4991 15.4815-6.2047 16.2486-18.1203C73.7086 40.8204 73.7803 36.3758 73.7803 35.889 73.7839 34.2347 74.3216 24.1533 73.7014 17.9592zM61.4925 47.6918H53.1514V27.5855c0-4.2329-1.7923-6.3917-5.4378-6.3917-4.0075.0-6.0148 2.5538-6.0148 7.5981V39.7974h-8.291V28.7919c0-5.0443-2.0109-7.5981-6.0184-7.5981-3.624.0-5.4342 2.1588-5.4378 6.3917V47.6918h-8.334V26.9752c0-4.2329 1.0981-7.5957 3.2942-10.0884 2.2654-2.4868 5.237-3.7637 8.9255-3.7637 4.2691.0 7.4952 1.6155 9.6459 4.8431L37.5587 21.3949l2.079-3.4287c2.1507-3.2276 5.3768-4.8431 9.6388-4.8431 3.6849.0 6.6564 1.2769 8.929 3.7637 2.1962 2.4904 3.2942 5.8532 3.2942 10.0884L61.4925 47.6918z" fill="inherit"/></svg></a>
<a class=hover:text-blurple-500 href=/index.xml target=_blank><svg class="icon rss-icon w-5 h-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentcolor"><path d="M3.75 3A.75.75.0 003 3.75v.5c0 .414.336.75.75.75H4c6.075.0 11 4.925 11 11v.25c0 .414.336.75.75.75h.5a.75.75.0 00.75-.75V16C17 8.82 11.18 3 4 3h-.25z"/><path d="M3 8.75A.75.75.0 013.75 8H4a8 8 0 018 8v.25a.75.75.0 01-.75.75h-.5a.75.75.0 01-.75-.75V16a6 6 0 00-6-6h-.25A.75.75.0 013 9.25v-.5zM7 15a2 2 0 11-4 0 2 2 0 014 0z"/></svg></a></p></div></footer></div></div></body></html>