Improve group profile metadata

* Add canonical link
* Add link alternate to application/activity+json representation
* add robots noindex on remote profiles

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-08-04 10:39:38 +02:00
parent ac9587b9d1
commit 93ba408671
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
2 changed files with 46 additions and 7 deletions

View file

@ -23,11 +23,17 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Actors.Actor do
Tag.tag(:meta, property: "og:url", content: group.url),
Tag.tag(:meta, property: "og:description", content: group.summary),
Tag.tag(:meta, property: "og:type", content: "profile"),
Tag.tag(:meta, property: "profile:username", content: group.preferred_username),
Tag.tag(:meta,
property: "profile:username",
content: Actor.preferred_username_and_domain(group)
),
Tag.tag(:meta, property: "twitter:card", content: "summary")
]
|> maybe_add_avatar(group)
|> add_group_schema(group)
|> add_group_feeds(group)
|> add_canonical(group)
|> maybe_add_no_index(group)
end
def build_tags(%Actor{} = _actor, _locale), do: []
@ -42,27 +48,59 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Actors.Actor do
end
end
@spec add_group_schema(list(Tag.t()), Actor.t()) :: list(Tag.t())
defp add_group_schema(tags, %Actor{} = group) do
tags ++
[
~s{<script type="application/ld+json">#{json(group)}</script>} |> HTML.raw(),
~s{<script type="application/ld+json">#{json(group)}</script>} |> HTML.raw()
]
end
@spec add_group_feeds(list(Tag.t()), Actor.t()) :: list(Tag.t())
defp add_group_feeds(tags, %Actor{} = group) do
tags ++
[
Tag.tag(:link,
rel: "alternate",
type: "application/atom+xml",
title:
gettext("%{name}'s feed", name: group.name || group.preferred_username) |> HTML.raw(),
href: Routes.feed_url(Endpoint, :actor, group.preferred_username, :atom)
href:
Routes.feed_url(Endpoint, :actor, Actor.preferred_username_and_domain(group), :atom)
),
Tag.tag(:link,
rel: "alternate",
type: "text/calendar",
title:
gettext("%{name}'s feed", name: group.name || group.preferred_username) |> HTML.raw(),
href: Routes.feed_url(Endpoint, :actor, group.preferred_username, :ics)
href:
Routes.feed_url(
Endpoint,
:actor,
Actor.preferred_username_and_domain(group),
:ics
)
),
Tag.tag(:link,
rel: "alternate",
type: "application/activity+json",
href: group.url
)
]
end
@spec add_canonical(list(Tag.t()), Actor.t()) :: list(Tag.t())
defp add_canonical(tags, %Actor{url: group_url}) do
tags ++ [Tag.tag(:link, rel: "canonical", href: group_url)]
end
@spec maybe_add_no_index(list(Tag.t()), Actor.t()) :: list(Tag.t())
defp maybe_add_no_index(tags, %Actor{domain: nil}), do: tags
defp maybe_add_no_index(tags, %Actor{}) do
tags ++ [Tag.tag(:meta, name: "robots", content: "noindex")]
end
# Insert JSON-LD schema by hand because Tag.content_tag wants to escape it
defp json(%Actor{} = group) do
"group.json"

View file

@ -12,12 +12,13 @@ defmodule Mobilizon.Service.MetadataTest do
import Mobilizon.Factory
describe "build_tags/2 for an actor" do
# TODO : Refactor me with DOM assertions, like the event test below
test "that is a group gives tags" do
%Actor{} = group = insert(:group, name: "My group")
%Actor{} = group = insert(:group, name: "My group", domain: "remote.domain")
assert group |> Metadata.build_tags() |> Metadata.Utils.stringify_tags() ==
String.trim("""
<meta content="#{group.name} (@#{group.preferred_username})" property="og:title"><meta content="#{group.url}" property="og:url"><meta content="The event organizer didn&#39;t add any description." property="og:description"><meta content="profile" property="og:type"><meta content="#{group.preferred_username}" property="profile:username"><meta content="summary" property="twitter:card"><meta content="#{group.avatar.url}" property="og:image"><script type="application/ld+json">{"@context":"http://schema.org","@type":"Organization","address":null,"name":"#{group.name}","url":"#{group.url}"}</script><link href="#{Routes.feed_url(Endpoint, :actor, group.preferred_username, "atom")}" rel="alternate" title="#{group.name}'s feed" type="application/atom+xml"><link href="#{Routes.feed_url(Endpoint, :actor, group.preferred_username, "ics")}" rel="alternate" title="#{group.name}'s feed" type="text/calendar">
<meta content="#{group.name} (@#{group.preferred_username}@#{group.domain})" property="og:title"><meta content="#{group.url}" property="og:url"><meta content="The event organizer didn&#39;t add any description." property="og:description"><meta content="profile" property="og:type"><meta content="#{Actor.preferred_username_and_domain(group)}" property="profile:username"><meta content="summary" property="twitter:card"><meta content="#{group.avatar.url}" property="og:image"><script type="application/ld+json">{"@context":"http://schema.org","@type":"Organization","address":null,"name":"#{group.name}","url":"#{group.url}"}</script><link href="#{Routes.feed_url(Endpoint, :actor, Actor.preferred_username_and_domain(group), "atom")}" rel="alternate" title="#{group.name}'s feed" type="application/atom+xml"><link href="#{Routes.feed_url(Endpoint, :actor, Actor.preferred_username_and_domain(group), "ics")}" rel="alternate" title="#{group.name}'s feed" type="text/calendar"><link href=\"#{group.url}\" rel=\"alternate\" type=\"application/activity+json\"><link href=\"#{group.url}\" rel=\"canonical\"><meta content=\"noindex\" name=\"robots\">
""")
assert group
@ -25,7 +26,7 @@ defmodule Mobilizon.Service.MetadataTest do
|> Metadata.build_tags()
|> Metadata.Utils.stringify_tags() ==
String.trim("""
<meta content="#{group.name} (@#{group.preferred_username})" property="og:title"><meta content="#{group.url}" property="og:url"><meta content="The event organizer didn&#39;t add any description." property="og:description"><meta content="profile" property="og:type"><meta content="#{group.preferred_username}" property="profile:username"><meta content="summary" property="twitter:card"><script type="application/ld+json">{"@context":"http://schema.org","@type":"Organization","address":null,"name":"#{group.name}","url":"#{group.url}"}</script><link href="#{Routes.feed_url(Endpoint, :actor, group.preferred_username, "atom")}" rel="alternate" title="#{group.name}'s feed" type="application/atom+xml"><link href="#{Routes.feed_url(Endpoint, :actor, group.preferred_username, "ics")}" rel="alternate" title="#{group.name}'s feed" type="text/calendar">
<meta content="#{group.name} (@#{group.preferred_username}@#{group.domain})" property="og:title"><meta content="#{group.url}" property="og:url"><meta content="The event organizer didn&#39;t add any description." property="og:description"><meta content="profile" property="og:type"><meta content="#{Actor.preferred_username_and_domain(group)}" property="profile:username"><meta content="summary" property="twitter:card"><script type="application/ld+json">{"@context":"http://schema.org","@type":"Organization","address":null,"name":"#{group.name}","url":"#{group.url}"}</script><link href="#{Routes.feed_url(Endpoint, :actor, Actor.preferred_username_and_domain(group), "atom")}" rel="alternate" title="#{group.name}'s feed" type="application/atom+xml"><link href="#{Routes.feed_url(Endpoint, :actor, Actor.preferred_username_and_domain(group), "ics")}" rel="alternate" title="#{group.name}'s feed" type="text/calendar"><link href=\"#{group.url}\" rel=\"alternate\" type=\"application/activity+json\"><link href=\"#{group.url}\" rel=\"canonical\"><meta content=\"noindex\" name=\"robots\">
""")
end