P6: link previews read by the server, as the owner decided
- A public post that links to a page gets a preview card: from the post's own data first (FEP-8967 Link preview, the object's image, title and summary); otherwise the server reads the page once, 0-60 s after the post arrives, never when someone reads it. OpenGraph and Twitter tags give title, description and image; one LinkPreview per address is cached for 7 days and shared by the whole server, so a fetch never points at a persona. - Lemmy link posts, which carry no title or description, get them filled in. - The page fetch uses the guarded client (public addresses, three redirects, HTML only, first 512 KB). - Federation:FetchLinkPreviews switches page fetching off. - Local public posts get cards too. Checked live: a link to a GoToSocial profile page becomes a card with its title, description and proxied image. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012CzABvBkbcFqoHdmi8b9WB
This commit is contained in:
1 parent
fdcf5176bc
commit
001fdac3be
17 files changed
+314
-13
No files matched your search
@@ -0,0 +1,43 @@
|
||||
using PrivaPub.Domain.Content;
|
||||
using PrivaPub.Models.Post;
|
||||
|
||||
namespace PrivaPub.Tests.Domain
|
||||
{
|
||||
public class LinkPreviewTests
|
||||
{
|
||||
[Fact]
|
||||
public void Reads_open_graph_then_twitter_then_the_page_title()
|
||||
{
|
||||
var preview = LinkPreviews.Read("https://news.example/a?utm_source=x", new Uri("https://news.example/a"), """
|
||||
<html><head><title>Fallback title</title>
|
||||
<meta property="og:title" content="Rain & sun">
|
||||
<meta name="twitter:description" content="A <b>long</b> story">
|
||||
<meta property="og:image" content="/img/cover.jpg">
|
||||
<meta property="og:site_name" content="News">
|
||||
</head><body></body></html>
|
||||
""");
|
||||
|
||||
Assert.Equal("Rain & sun", preview.Title);
|
||||
Assert.Equal("A long story", preview.Description);
|
||||
Assert.Equal("https://news.example/img/cover.jpg", preview.ImageURL);
|
||||
Assert.Equal("News", preview.SiteName);
|
||||
Assert.False(preview.Failed);
|
||||
Assert.True(LinkPreviews.Read("https://x.example", new Uri("https://x.example"), "<html><body>nothing</body></html>").Failed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Picks_the_first_real_link_and_skips_mentions_tags_quotes_and_ourselves() =>
|
||||
Assert.Equal("https://blog.example/post", LinkPreviews.FirstLink(new Post
|
||||
{
|
||||
Url = "https://m.example/@a/1",
|
||||
ContentHtml = """
|
||||
<p><span class="h-card"><a href="https://m.example/@bob" class="u-url mention">@bob</a></span>
|
||||
<a href="https://m.example/tags/x" class="mention hashtag" rel="tag">#x</a>
|
||||
<span class="quote-inline">RE: <a href="https://k.example/notes/1">link</a></span>
|
||||
<a href="https://privapub.test/@me">me</a>
|
||||
<a href="https://m.example/@a/1">self</a>
|
||||
<a href="https://blog.example/post">read this</a></p>
|
||||
"""
|
||||
}, "https://privapub.test"));
|
||||
}
|
||||
}
|
||||
@@ -56,7 +56,7 @@ namespace PrivaPub.Tests.Federation
|
||||
{
|
||||
new FollowHandler(db, _local, remote, delivery),
|
||||
new UndoHandler(db, _local, new Reactions(db, delivery)),
|
||||
new CreateHandler(db, _local, remote, delivery, _blocks, new Fanout(db), new RemotePosts(db, _local, remote, _blocks, queue, new ObjectRecords(queue)), new GroupDistributor(delivery), new ObjectRecords(queue), new PollService(db, _local, delivery, queue)),
|
||||
new CreateHandler(db, _local, remote, delivery, _blocks, new Fanout(db), new RemotePosts(db, _local, remote, _blocks, queue, new ObjectRecords(queue), new NoPreviews()), new GroupDistributor(delivery), new ObjectRecords(queue), new PollService(db, _local, delivery, queue), new NoPreviews()),
|
||||
new DeleteHandler(db, _local, remote, delivery, new GroupDistributor(delivery)),
|
||||
new UpdateHandler(db, _local, remote, new GroupDistributor(delivery), new ObjectRecords(queue))
|
||||
}, NullLogger<InboxProcessor>.Instance);
|
||||
|
||||
@@ -46,7 +46,7 @@ namespace PrivaPub.Tests.Support
|
||||
Records = new ObjectRecords(Queue);
|
||||
Polls = new PollService(Db, Local, Delivery, Queue);
|
||||
Reactions = new Reactions(Db, Delivery);
|
||||
RemotePosts = new RemotePosts(Db, Local, Remote, new NoBlocks(), Queue, Records);
|
||||
RemotePosts = new RemotePosts(Db, Local, Remote, new NoBlocks(), Queue, Records, new NoPreviews());
|
||||
Receiver = new InboxReceiver(Local, Remote, Queue, new NoBlocks(), NullLogger<InboxReceiver>.Instance);
|
||||
Processor = new InboxProcessor(Remote, new IActivityHandler[]
|
||||
{
|
||||
@@ -59,7 +59,7 @@ namespace PrivaPub.Tests.Support
|
||||
new DislikeHandler(Db),
|
||||
new JoinHandler(Db, Local, Delivery),
|
||||
new AnnounceHandler(Db, Local, RemotePosts, Fanout, Remote),
|
||||
new CreateHandler(Db, Local, Remote, Delivery, new NoBlocks(), Fanout, RemotePosts, Groups, Records, Polls),
|
||||
new CreateHandler(Db, Local, Remote, Delivery, new NoBlocks(), Fanout, RemotePosts, Groups, Records, Polls, new NoPreviews()),
|
||||
new DeleteHandler(Db, Local, Remote, Delivery, Groups),
|
||||
new UpdateHandler(Db, Local, Remote, Groups, Records),
|
||||
new FlagHandler(Db, Local)
|
||||
@@ -69,7 +69,7 @@ namespace PrivaPub.Tests.Support
|
||||
Outbox = new OutboxPublisher(Db, Local, Delivery);
|
||||
Media = new MediaService(new StaticOptions<MediaOptions>(new MediaOptions { Root = Path.Combine(Path.GetTempPath(), $"privapub-media-{Guid.NewGuid():N}") }),
|
||||
Local, default, NullLogger<MediaService>.Instance);
|
||||
Statuses = new StatusService(Db, Local, Remote, Delivery, Content, Outbox, Fanout, Media, Groups, Polls);
|
||||
Statuses = new StatusService(Db, Local, Remote, Delivery, Content, Outbox, Fanout, Media, Groups, Polls, new NoPreviews());
|
||||
Posts = new PostsService(Db, Local, Statuses, new KeyLocalizer<GenericRes>(), NullLogger<PostsService>.Instance);
|
||||
Timelines = new TimelineService(Db, new KeyLocalizer<GenericRes>());
|
||||
Relationships = new RelationshipService(Db, Follows, Delivery);
|
||||
@@ -148,4 +148,9 @@ namespace PrivaPub.Tests.Support
|
||||
public LocalizedString this[string name, params object[] arguments] => new(name, string.Format(name, arguments));
|
||||
public IEnumerable<LocalizedString> GetAllStrings(bool includeParentCultures) => Enumerable.Empty<LocalizedString>();
|
||||
}
|
||||
|
||||
public sealed class NoPreviews : ILinkPreviews
|
||||
{
|
||||
public Task Wanted(PrivaPub.Models.Post.Post post, CancellationToken token) => Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user